1 17 18 package org.apache.coyote.http11; 19 20 import java.net.InetAddress ; 21 import java.net.URLEncoder ; 22 import java.util.Hashtable ; 23 import java.util.Iterator ; 24 import java.util.concurrent.ConcurrentHashMap ; 25 import java.util.concurrent.Executor ; 26 import javax.management.MBeanRegistration ; 27 import javax.management.MBeanServer ; 28 import javax.management.ObjectName ; 29 30 import org.apache.coyote.ActionCode; 31 import org.apache.coyote.ActionHook; 32 import org.apache.coyote.Adapter; 33 import org.apache.coyote.ProtocolHandler; 34 import org.apache.coyote.RequestGroupInfo; 35 import org.apache.coyote.RequestInfo; 36 import org.apache.tomcat.util.modeler.Registry; 37 import org.apache.tomcat.util.net.NioEndpoint; 38 import org.apache.tomcat.util.net.NioEndpoint.Handler; 39 import org.apache.tomcat.util.res.StringManager; 40 import org.apache.tomcat.util.net.NioChannel; 41 import org.apache.tomcat.util.net.SSLImplementation; 42 import org.apache.tomcat.util.net.SecureNioChannel; 43 import org.apache.tomcat.util.net.SocketStatus; 44 45 46 55 public class Http11NioProtocol implements ProtocolHandler, MBeanRegistration 56 { 57 protected SSLImplementation sslImplementation = null; 58 59 public Http11NioProtocol() { 60 cHandler = new Http11ConnectionHandler( this ); 61 setSoLinger(Constants.DEFAULT_CONNECTION_LINGER); 62 setSoTimeout(Constants.DEFAULT_CONNECTION_TIMEOUT); 63 setTcpNoDelay(Constants.DEFAULT_TCP_NO_DELAY); 65 } 66 67 70 protected static StringManager sm = 71 StringManager.getManager(Constants.Package); 72 73 75 public void setAttribute( String name, Object value ) { 76 if( log.isTraceEnabled()) 77 log.trace(sm.getString("http11protocol.setattribute", name, value)); 78 79 attributes.put(name, value); 80 } 81 82 public Object getAttribute( String key ) { 83 if( log.isTraceEnabled()) 84 log.trace(sm.getString("http11protocol.getattribute", key)); 85 return attributes.get(key); 86 } 87 88 public Iterator getAttributeNames() { 89 return attributes.keySet().iterator(); 90 } 91 92 95 public void setProperty(String name, String value) { 96 if ( name!=null && (name.startsWith("socket.") ||name.startsWith("selectorPool.")) ){ 97 ep.setProperty(name, value); 98 } 99 setAttribute(name, value); 100 } 101 102 105 public String getProperty(String name) { 106 return (String )getAttribute(name); 107 } 108 109 111 public void setAdapter(Adapter adapter) { 112 this.adapter=adapter; 113 } 114 115 public Adapter getAdapter() { 116 return adapter; 117 } 118 119 120 122 public void init() throws Exception { 123 ep.setName(getName()); 124 ep.setHandler(cHandler); 125 126 ep.getSocketProperties().setRxBufSize(Math.max(ep.getSocketProperties().getRxBufSize(),getMaxHttpHeaderSize())); 128 ep.getSocketProperties().setTxBufSize(Math.max(ep.getSocketProperties().getTxBufSize(),getMaxHttpHeaderSize())); 129 130 try { 131 ep.init(); 132 sslImplementation = SSLImplementation.getInstance("org.apache.tomcat.util.net.jsse.JSSEImplementation"); 133 } catch (Exception ex) { 134 log.error(sm.getString("http11protocol.endpoint.initerror"), ex); 135 throw ex; 136 } 137 if(log.isInfoEnabled()) 138 log.info(sm.getString("http11protocol.init", getName())); 139 140 } 141 142 ObjectName tpOname; 143 ObjectName rgOname; 144 145 public void start() throws Exception { 146 if( this.domain != null ) { 147 try { 148 tpOname=new ObjectName 149 (domain + ":" + "type=ThreadPool,name=" + getName()); 150 Registry.getRegistry(null, null) 151 .registerComponent(ep, tpOname, null ); 152 } catch (Exception e) { 153 log.error("Can't register threadpool" ); 154 } 155 rgOname=new ObjectName 156 (domain + ":type=GlobalRequestProcessor,name=" + getName()); 157 Registry.getRegistry(null, null).registerComponent 158 ( cHandler.global, rgOname, null ); 159 } 160 161 try { 162 ep.start(); 163 } catch (Exception ex) { 164 log.error(sm.getString("http11protocol.endpoint.starterror"), ex); 165 throw ex; 166 } 167 if(log.isInfoEnabled()) 168 log.info(sm.getString("http11protocol.start", getName())); 169 } 170 171 public void pause() throws Exception { 172 try { 173 ep.pause(); 174 } catch (Exception ex) { 175 log.error(sm.getString("http11protocol.endpoint.pauseerror"), ex); 176 throw ex; 177 } 178 if(log.isInfoEnabled()) 179 log.info(sm.getString("http11protocol.pause", getName())); 180 } 181 182 public void resume() throws Exception { 183 try { 184 ep.resume(); 185 } catch (Exception ex) { 186 log.error(sm.getString("http11protocol.endpoint.resumeerror"), ex); 187 throw ex; 188 } 189 if(log.isInfoEnabled()) 190 log.info(sm.getString("http11protocol.resume", getName())); 191 } 192 193 public void destroy() throws Exception { 194 if(log.isInfoEnabled()) 195 log.info(sm.getString("http11protocol.stop", getName())); 196 ep.destroy(); 197 if( tpOname!=null ) 198 Registry.getRegistry(null, null).unregisterComponent(tpOname); 199 if( rgOname != null ) 200 Registry.getRegistry(null, null).unregisterComponent(rgOname); 201 } 202 203 protected NioEndpoint ep=new NioEndpoint(); 205 protected boolean secure = false; 206 207 protected Hashtable attributes = new Hashtable (); 208 209 private int maxKeepAliveRequests=100; private int timeout = 300000; private int maxSavePostSize = 4 * 1024; 212 private int maxHttpHeaderSize = 8 * 1024; 213 private int socketCloseDelay=-1; 214 private boolean disableUploadTimeout = true; 215 private int socketBuffer = 9000; 216 217 private Adapter adapter; 218 private Http11ConnectionHandler cHandler; 219 220 223 private String compression = "off"; 224 private String noCompressionUserAgents = null; 225 private String restrictedUserAgents = null; 226 private String compressableMimeTypes = "text/html,text/xml,text/plain"; 227 private int compressionMinSize = 2048; 228 229 private String server; 230 231 233 public void setPollerThreadCount(int count) { 234 ep.setPollerThreadCount(count); 235 } 236 237 public int getPollerThreadCount() { 238 return ep.getPollerThreadCount(); 239 } 240 241 public void setSelectorTimeout(long timeout) { 242 ep.setSelectorTimeout(timeout); 243 } 244 245 public long getSelectorTimeout() { 246 return ep.getSelectorTimeout(); 247 } 248 public Executor getExecutor() { 250 return ep.getExecutor(); 251 } 252 253 public void setExecutor(Executor executor) { 255 ep.setExecutor(executor); 256 } 257 258 public int getMaxThreads() { 259 return ep.getMaxThreads(); 260 } 261 262 public void setMaxThreads( int maxThreads ) { 263 ep.setMaxThreads(maxThreads); 264 setAttribute("maxThreads", "" + maxThreads); 265 } 266 267 public void setThreadPriority(int threadPriority) { 268 ep.setThreadPriority(threadPriority); 269 setAttribute("threadPriority", "" + threadPriority); 270 } 271 272 public int getThreadPriority() { 273 return ep.getThreadPriority(); 274 } 275 276 278 public int getBacklog() { 279 return ep.getBacklog(); 280 } 281 282 public void setBacklog( int i ) { 283 ep.setBacklog(i); 284 setAttribute("backlog", "" + i); 285 } 286 287 public int getPort() { 288 return ep.getPort(); 289 } 290 291 public void setPort( int port ) { 292 ep.setPort(port); 293 setAttribute("port", "" + port); 294 } 295 296 public int getFirstReadTimeout() { 297 return ep.getFirstReadTimeout(); 298 } 299 300 public void setFirstReadTimeout( int i ) { 301 ep.setFirstReadTimeout(i); 302 setAttribute("firstReadTimeout", "" + i); 303 } 304 305 public InetAddress getAddress() { 306 return ep.getAddress(); 307 } 308 309 public void setAddress(InetAddress ia) { 310 ep.setAddress( ia ); 311 setAttribute("address", "" + ia); 312 } 313 314 public String getName() { 315 String encodedAddr = ""; 316 if (getAddress() != null) { 317 encodedAddr = "" + getAddress(); 318 if (encodedAddr.startsWith("/")) 319 encodedAddr = encodedAddr.substring(1); 320 encodedAddr = URLEncoder.encode(encodedAddr) + "-"; 321 } 322 return ("http-" + encodedAddr + ep.getPort()); 323 } 324 325 public boolean getTcpNoDelay() { 326 return ep.getTcpNoDelay(); 327 } 328 329 public void setTcpNoDelay( boolean b ) { 330 ep.setTcpNoDelay( b ); 331 setAttribute("tcpNoDelay", "" + b); 332 } 333 334 public boolean getDisableUploadTimeout() { 335 return disableUploadTimeout; 336 } 337 338 public void setDisableUploadTimeout(boolean isDisabled) { 339 disableUploadTimeout = isDisabled; 340 } 341 342 public int getSocketBuffer() { 343 return socketBuffer; 344 } 345 346 public void setSocketBuffer(int valueI) { 347 socketBuffer = valueI; 348 } 349 350 public String getCompression() { 351 return compression; 352 } 353 354 public void setCompression(String valueS) { 355 compression = valueS; 356 setAttribute("compression", valueS); 357 } 358 359 public int getMaxSavePostSize() { 360 return maxSavePostSize; 361 } 362 363 public void setMaxSavePostSize(int valueI) { 364 maxSavePostSize = valueI; 365 setAttribute("maxSavePostSize", "" + valueI); 366 } 367 368 public int getMaxHttpHeaderSize() { 369 return maxHttpHeaderSize; 370 } 371 372 public void setMaxHttpHeaderSize(int valueI) { 373 maxHttpHeaderSize = valueI; 374 setAttribute("maxHttpHeaderSize", "" + valueI); 375 } 376 377 public String getRestrictedUserAgents() { 378 return restrictedUserAgents; 379 } 380 381 public void setRestrictedUserAgents(String valueS) { 382 restrictedUserAgents = valueS; 383 setAttribute("restrictedUserAgents", valueS); 384 } 385 386 public String getNoCompressionUserAgents() { 387 return noCompressionUserAgents; 388 } 389 390 public void setNoCompressionUserAgents(String valueS) { 391 noCompressionUserAgents = valueS; 392 setAttribute("noCompressionUserAgents", valueS); 393 } 394 395 public String getCompressableMimeType() { 396 return compressableMimeTypes; 397 } 398 399 public void setCompressableMimeType(String valueS) { 400 compressableMimeTypes = valueS; 401 setAttribute("compressableMimeTypes", valueS); 402 } 403 404 public int getCompressionMinSize() { 405 return compressionMinSize; 406 } 407 408 public void setCompressionMinSize(int valueI) { 409 compressionMinSize = valueI; 410 setAttribute("compressionMinSize", "" + valueI); 411 } 412 413 public int getSoLinger() { 414 return ep.getSoLinger(); 415 } 416 417 public void setSoLinger( int i ) { 418 ep.setSoLinger( i ); 419 setAttribute("soLinger", "" + i); 420 } 421 422 public int getSoTimeout() { 423 return ep.getSoTimeout(); 424 } 425 426 public void setSoTimeout( int i ) { 427 ep.setSoTimeout(i); 428 setAttribute("soTimeout", "" + i); 429 } 430 431 public String getProtocol() { 432 return getProperty("protocol"); 433 } 434 435 public void setProtocol( String k ) { 436 setSecure(true); 437 setAttribute("protocol", k); 438 } 439 440 public boolean getSecure() { 441 return secure; 442 } 443 444 public void setSecure( boolean b ) { 445 ep.setSecure(b); 446 secure=b; 447 setAttribute("secure", "" + b); 448 } 449 450 public int getMaxKeepAliveRequests() { 451 return maxKeepAliveRequests; 452 } 453 454 456 public void setMaxKeepAliveRequests(int mkar) { 457 maxKeepAliveRequests = mkar; 458 setAttribute("maxKeepAliveRequests", "" + mkar); 459 } 460 461 464 public boolean getKeepAlive() { 465 return ((maxKeepAliveRequests != 0) && (maxKeepAliveRequests != 1)); 466 } 467 468 471 public void setKeepAlive(boolean keepAlive) { 472 if (!keepAlive) { 473 setMaxKeepAliveRequests(1); 474 } 475 } 476 477 public int getSocketCloseDelay() { 478 return socketCloseDelay; 479 } 480 481 public void setSocketCloseDelay( int d ) { 482 socketCloseDelay=d; 483 setAttribute("socketCloseDelay", "" + d); 484 } 485 486 public void setServer( String server ) { 487 this.server = server; 488 } 489 490 public String getServer() { 491 return server; 492 } 493 494 public int getTimeout() { 495 return timeout; 496 } 497 498 public void setTimeout( int timeouts ) { 499 timeout = timeouts; 500 setAttribute("timeout", "" + timeouts); 501 } 502 503 505 public String getKeystoreFile() { return ep.getKeystoreFile();} 506 public void setKeystoreFile(String s ) { ep.setKeystoreFile(s);} 507 public void setKeystore(String s) { setKeystoreFile(s);} 508 public String getKeystore(){ return getKeystoreFile();} 509 510 public String getAlgorithm() { return ep.getAlgorithm();} 511 public void setAlgorithm(String s ) { ep.setAlgorithm(s);} 512 513 public boolean getClientAuth() { return ep.getClientAuth();} 514 public void setClientAuth(boolean b ) { ep.setClientAuth(b);} 515 516 public String getKeystorePass() { return ep.getKeystorePass();} 517 public void setKeystorePass(String s ) { ep.setKeystorePass(s);} 518 519 public String getKeystoreType() { return ep.getKeystoreType();} 520 public void setKeystoreType(String s ) { ep.setKeystoreType(s);} 521 522 public String getSslProtocol() { return ep.getSslProtocol();} 523 public void setSslProtocol(String s) { ep.setSslProtocol(s);} 524 525 public String getCiphers() { return ep.getCiphers();} 526 public void setCiphers(String s) { ep.setCiphers(s);} 527 528 public boolean getSSLEnabled() { return ep.isSSLEnabled(); } 529 public void setSSLEnabled(boolean SSLEnabled) { ep.setSSLEnabled(SSLEnabled); } 530 531 532 533 535 static class Http11ConnectionHandler implements Handler { 536 537 protected Http11NioProtocol proto; 538 protected static int count = 0; 539 protected RequestGroupInfo global = new RequestGroupInfo(); 540 541 protected ThreadLocal <Http11NioProcessor> localProcessor = 542 new ThreadLocal <Http11NioProcessor>(); 543 protected ConcurrentHashMap <NioChannel, Http11NioProcessor> connections = 544 new ConcurrentHashMap <NioChannel, Http11NioProcessor>(); 545 protected java.util.Stack <Http11NioProcessor> recycledProcessors = 546 new java.util.Stack <Http11NioProcessor>(); 547 548 Http11ConnectionHandler(Http11NioProtocol proto) { 549 this.proto = proto; 550 } 551 552 public SocketState event(NioChannel socket, SocketStatus status) { 553 Http11NioProcessor result = connections.get(socket); 554 555 SocketState state = SocketState.CLOSED; 556 if (result != null) { 557 try { 559 state = result.event(status); 560 } catch (java.net.SocketException e) { 561 Http11NioProtocol.log.debug 563 (sm.getString 564 ("http11protocol.proto.socketexception.debug"), e); 565 } catch (java.io.IOException e) { 566 Http11NioProtocol.log.debug 568 (sm.getString 569 ("http11protocol.proto.ioexception.debug"), e); 570 } 571 catch (Throwable e) { 575 Http11NioProtocol.log.error 579 (sm.getString("http11protocol.proto.error"), e); 580 } finally { 581 if (state != SocketState.LONG) { 582 connections.remove(socket); 583 recycledProcessors.push(result); 584 } 585 } 586 } 587 return state; 588 } 589 590 public SocketState process(NioChannel socket) { 591 Http11NioProcessor processor = null; 592 try { 593 processor = (Http11NioProcessor) localProcessor.get(); 594 if (processor == null) { 595 synchronized (recycledProcessors) { 596 if (!recycledProcessors.isEmpty()) { 597 processor = recycledProcessors.pop(); 598 localProcessor.set(processor); 599 } 600 } 601 } 602 if (processor == null) { 603 processor = createProcessor(); 604 } 605 606 if (processor instanceof ActionHook) { 607 ((ActionHook) processor).action(ActionCode.ACTION_START, null); 608 } 609 610 611 if (proto.ep.getSecure() && (proto.sslImplementation != null)) { 612 if (socket instanceof SecureNioChannel) { 613 SecureNioChannel ch = (SecureNioChannel)socket; 614 processor.setSslSupport(proto.sslImplementation.getSSLSupport(ch.getSslEngine().getSession())); 615 }else processor.setSslSupport(null); 616 } else { 617 processor.setSslSupport(null); 618 } 619 620 621 SocketState state = processor.process(socket); 622 if (state == SocketState.LONG) { 623 connections.put(socket, processor); 627 localProcessor.set(null); 628 socket.getPoller().add(socket); 629 } 630 return state; 631 632 } catch (java.net.SocketException e) { 633 Http11NioProtocol.log.debug 635 (sm.getString 636 ("http11protocol.proto.socketexception.debug"), e); 637 } catch (java.io.IOException e) { 638 Http11NioProtocol.log.debug 640 (sm.getString 641 ("http11protocol.proto.ioexception.debug"), e); 642 } 643 catch (Throwable e) { 647 Http11NioProtocol.log.error 651 (sm.getString("http11protocol.proto.error"), e); 652 } 653 return SocketState.CLOSED; 654 } 655 656 public Http11NioProcessor createProcessor() { 657 Http11NioProcessor processor = new Http11NioProcessor( 658 proto.ep.getSocketProperties().getRxBufSize(), 659 proto.ep.getSocketProperties().getTxBufSize(), 660 proto.maxHttpHeaderSize, 661 proto.ep); 662 processor.setAdapter(proto.adapter); 663 processor.setMaxKeepAliveRequests(proto.maxKeepAliveRequests); 664 processor.setTimeout(proto.timeout); 665 processor.setDisableUploadTimeout(proto.disableUploadTimeout); 666 processor.setCompression(proto.compression); 667 processor.setCompressionMinSize(proto.compressionMinSize); 668 processor.setNoCompressionUserAgents(proto.noCompressionUserAgents); 669 processor.setCompressableMimeTypes(proto.compressableMimeTypes); 670 processor.setRestrictedUserAgents(proto.restrictedUserAgents); 671 processor.setSocketBuffer(proto.socketBuffer); 672 processor.setMaxSavePostSize(proto.maxSavePostSize); 673 processor.setServer(proto.server); 674 localProcessor.set(processor); 675 if (proto.getDomain() != null) { 676 synchronized (this) { 677 try { 678 RequestInfo rp = processor.getRequest().getRequestProcessor(); 679 rp.setGlobalProcessor(global); 680 ObjectName rpName = new ObjectName 681 (proto.getDomain() + ":type=RequestProcessor,worker=" 682 + proto.getName() + ",name=HttpRequest" + count++); 683 Registry.getRegistry(null, null).registerComponent(rp, rpName, null); 684 } catch (Exception e) { 685 log.warn("Error registering request"); 686 } 687 } 688 } 689 return processor; 690 } 691 } 692 693 protected static org.apache.commons.logging.Log log 694 = org.apache.commons.logging.LogFactory.getLog(Http11NioProtocol.class); 695 696 698 protected String domain; 699 protected ObjectName oname; 700 protected MBeanServer mserver; 701 702 public ObjectName getObjectName() { 703 return oname; 704 } 705 706 public String getDomain() { 707 return domain; 708 } 709 710 public ObjectName preRegister(MBeanServer server, 711 ObjectName name) throws Exception { 712 oname=name; 713 mserver=server; 714 domain=name.getDomain(); 715 return name; 716 } 717 718 public void postRegister(Boolean registrationDone) { 719 } 720 721 public void preDeregister() throws Exception { 722 } 723 724 public void postDeregister() { 725 } 726 } 727 | Popular Tags |