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 27 import javax.management.MBeanRegistration ; 28 import javax.management.MBeanServer ; 29 import javax.management.ObjectName ; 30 31 import org.apache.coyote.ActionCode; 32 import org.apache.coyote.ActionHook; 33 import org.apache.coyote.Adapter; 34 import org.apache.coyote.ProtocolHandler; 35 import org.apache.coyote.RequestGroupInfo; 36 import org.apache.coyote.RequestInfo; 37 import org.apache.tomcat.util.modeler.Registry; 38 import org.apache.tomcat.util.net.AprEndpoint; 39 import org.apache.tomcat.util.net.SocketStatus; 40 import org.apache.tomcat.util.net.AprEndpoint.Handler; 41 import org.apache.tomcat.util.res.StringManager; 42 43 44 52 public class Http11AprProtocol implements ProtocolHandler, MBeanRegistration 53 { 54 public Http11AprProtocol() { 55 cHandler = new Http11ConnectionHandler( this ); 56 setSoLinger(Constants.DEFAULT_CONNECTION_LINGER); 57 setSoTimeout(Constants.DEFAULT_CONNECTION_TIMEOUT); 58 setTcpNoDelay(Constants.DEFAULT_TCP_NO_DELAY); 60 } 61 62 65 protected static StringManager sm = 66 StringManager.getManager(Constants.Package); 67 68 70 public void setAttribute( String name, Object value ) { 71 if( log.isTraceEnabled()) 72 log.trace(sm.getString("http11protocol.setattribute", name, value)); 73 74 attributes.put(name, value); 75 } 76 77 public Object getAttribute( String key ) { 78 if( log.isTraceEnabled()) 79 log.trace(sm.getString("http11protocol.getattribute", key)); 80 return attributes.get(key); 81 } 82 83 public Iterator getAttributeNames() { 84 return attributes.keySet().iterator(); 85 } 86 87 90 public void setProperty(String name, String value) { 91 setAttribute(name, value); 92 } 93 94 97 public String getProperty(String name) { 98 return (String )getAttribute(name); 99 } 100 101 103 public void setAdapter(Adapter adapter) { 104 this.adapter=adapter; 105 } 106 107 public Adapter getAdapter() { 108 return adapter; 109 } 110 111 112 114 public void init() throws Exception { 115 ep.setName(getName()); 116 ep.setHandler(cHandler); 117 118 try { 119 ep.init(); 120 } catch (Exception ex) { 121 log.error(sm.getString("http11protocol.endpoint.initerror"), ex); 122 throw ex; 123 } 124 if(log.isInfoEnabled()) 125 log.info(sm.getString("http11protocol.init", getName())); 126 127 } 128 129 ObjectName tpOname; 130 ObjectName rgOname; 131 132 public void start() throws Exception { 133 if( this.domain != null ) { 134 try { 135 tpOname=new ObjectName 136 (domain + ":" + "type=ThreadPool,name=" + getName()); 137 Registry.getRegistry(null, null) 138 .registerComponent(ep, tpOname, null ); 139 } catch (Exception e) { 140 log.error("Can't register threadpool" ); 141 } 142 rgOname=new ObjectName 143 (domain + ":type=GlobalRequestProcessor,name=" + getName()); 144 Registry.getRegistry(null, null).registerComponent 145 ( cHandler.global, rgOname, null ); 146 } 147 148 try { 149 ep.start(); 150 } catch (Exception ex) { 151 log.error(sm.getString("http11protocol.endpoint.starterror"), ex); 152 throw ex; 153 } 154 if(log.isInfoEnabled()) 155 log.info(sm.getString("http11protocol.start", getName())); 156 } 157 158 public void pause() throws Exception { 159 try { 160 ep.pause(); 161 } catch (Exception ex) { 162 log.error(sm.getString("http11protocol.endpoint.pauseerror"), ex); 163 throw ex; 164 } 165 if(log.isInfoEnabled()) 166 log.info(sm.getString("http11protocol.pause", getName())); 167 } 168 169 public void resume() throws Exception { 170 try { 171 ep.resume(); 172 } catch (Exception ex) { 173 log.error(sm.getString("http11protocol.endpoint.resumeerror"), ex); 174 throw ex; 175 } 176 if(log.isInfoEnabled()) 177 log.info(sm.getString("http11protocol.resume", getName())); 178 } 179 180 public void destroy() throws Exception { 181 if(log.isInfoEnabled()) 182 log.info(sm.getString("http11protocol.stop", getName())); 183 ep.destroy(); 184 if( tpOname!=null ) 185 Registry.getRegistry(null, null).unregisterComponent(tpOname); 186 if( rgOname != null ) 187 Registry.getRegistry(null, null).unregisterComponent(rgOname); 188 } 189 190 protected AprEndpoint ep=new AprEndpoint(); 192 protected boolean secure; 193 194 protected Hashtable attributes = new Hashtable (); 195 196 private int maxKeepAliveRequests=100; private int timeout = 300000; private int maxSavePostSize = 4 * 1024; 199 private int maxHttpHeaderSize = 8 * 1024; 200 private int socketCloseDelay=-1; 201 private boolean disableUploadTimeout = true; 202 private int socketBuffer = 9000; 203 private Adapter adapter; 204 private Http11ConnectionHandler cHandler; 205 206 209 private String compression = "off"; 210 private String noCompressionUserAgents = null; 211 private String restrictedUserAgents = null; 212 private String compressableMimeTypes = "text/html,text/xml,text/plain"; 213 private int compressionMinSize = 2048; 214 215 private String server; 216 217 219 public Executor getExecutor() { 221 return ep.getExecutor(); 222 } 223 224 public void setExecutor(Executor executor) { 226 ep.setExecutor(executor); 227 } 228 229 public int getMaxThreads() { 230 return ep.getMaxThreads(); 231 } 232 233 public void setMaxThreads( int maxThreads ) { 234 ep.setMaxThreads(maxThreads); 235 setAttribute("maxThreads", "" + maxThreads); 236 } 237 238 public void setThreadPriority(int threadPriority) { 239 ep.setThreadPriority(threadPriority); 240 setAttribute("threadPriority", "" + threadPriority); 241 } 242 243 public int getThreadPriority() { 244 return ep.getThreadPriority(); 245 } 246 247 249 public int getBacklog() { 250 return ep.getBacklog(); 251 } 252 253 public void setBacklog( int i ) { 254 ep.setBacklog(i); 255 setAttribute("backlog", "" + i); 256 } 257 258 public int getPort() { 259 return ep.getPort(); 260 } 261 262 public void setPort( int port ) { 263 ep.setPort(port); 264 setAttribute("port", "" + port); 265 } 266 267 public int getFirstReadTimeout() { 268 return ep.getFirstReadTimeout(); 269 } 270 271 public void setFirstReadTimeout( int i ) { 272 ep.setFirstReadTimeout(i); 273 setAttribute("firstReadTimeout", "" + i); 274 } 275 276 public int getPollTime() { 277 return ep.getPollTime(); 278 } 279 280 public void setPollTime( int i ) { 281 ep.setPollTime(i); 282 setAttribute("pollTime", "" + i); 283 } 284 285 public void setPollerSize(int i) { 286 ep.setPollerSize(i); 287 setAttribute("pollerSize", "" + i); 288 } 289 290 public int getPollerSize() { 291 return ep.getPollerSize(); 292 } 293 294 public void setSendfileSize(int i) { 295 ep.setSendfileSize(i); 296 setAttribute("sendfileSize", "" + i); 297 } 298 299 public int getSendfileSize() { 300 return ep.getSendfileSize(); 301 } 302 303 public boolean getUseSendfile() { 304 return ep.getUseSendfile(); 305 } 306 307 public void setUseSendfile(boolean useSendfile) { 308 ep.setUseSendfile(useSendfile); 309 } 310 311 public InetAddress getAddress() { 312 return ep.getAddress(); 313 } 314 315 public void setAddress(InetAddress ia) { 316 ep.setAddress( ia ); 317 setAttribute("address", "" + ia); 318 } 319 320 public String getName() { 321 String encodedAddr = ""; 322 if (getAddress() != null) { 323 encodedAddr = "" + getAddress(); 324 if (encodedAddr.startsWith("/")) 325 encodedAddr = encodedAddr.substring(1); 326 encodedAddr = URLEncoder.encode(encodedAddr) + "-"; 327 } 328 return ("http-" + encodedAddr + ep.getPort()); 329 } 330 331 public boolean getTcpNoDelay() { 332 return ep.getTcpNoDelay(); 333 } 334 335 public void setTcpNoDelay( boolean b ) { 336 ep.setTcpNoDelay( b ); 337 setAttribute("tcpNoDelay", "" + b); 338 } 339 340 public boolean getDisableUploadTimeout() { 341 return disableUploadTimeout; 342 } 343 344 public void setDisableUploadTimeout(boolean isDisabled) { 345 disableUploadTimeout = isDisabled; 346 } 347 348 public int getSocketBuffer() { 349 return socketBuffer; 350 } 351 352 public void setSocketBuffer(int valueI) { 353 socketBuffer = valueI; 354 } 355 356 public String getCompression() { 357 return compression; 358 } 359 360 public void setCompression(String valueS) { 361 compression = valueS; 362 setAttribute("compression", valueS); 363 } 364 365 public int getMaxSavePostSize() { 366 return maxSavePostSize; 367 } 368 369 public void setMaxSavePostSize(int valueI) { 370 maxSavePostSize = valueI; 371 setAttribute("maxSavePostSize", "" + valueI); 372 } 373 374 public int getMaxHttpHeaderSize() { 375 return maxHttpHeaderSize; 376 } 377 378 public void setMaxHttpHeaderSize(int valueI) { 379 maxHttpHeaderSize = valueI; 380 setAttribute("maxHttpHeaderSize", "" + valueI); 381 } 382 383 public String getRestrictedUserAgents() { 384 return restrictedUserAgents; 385 } 386 387 public void setRestrictedUserAgents(String valueS) { 388 restrictedUserAgents = valueS; 389 setAttribute("restrictedUserAgents", valueS); 390 } 391 392 public String getNoCompressionUserAgents() { 393 return noCompressionUserAgents; 394 } 395 396 public void setNoCompressionUserAgents(String valueS) { 397 noCompressionUserAgents = valueS; 398 setAttribute("noCompressionUserAgents", valueS); 399 } 400 401 public String getCompressableMimeType() { 402 return compressableMimeTypes; 403 } 404 405 public void setCompressableMimeType(String valueS) { 406 compressableMimeTypes = valueS; 407 setAttribute("compressableMimeTypes", valueS); 408 } 409 410 public int getCompressionMinSize() { 411 return compressionMinSize; 412 } 413 414 public void setCompressionMinSize(int valueI) { 415 compressionMinSize = valueI; 416 setAttribute("compressionMinSize", "" + valueI); 417 } 418 419 public int getSoLinger() { 420 return ep.getSoLinger(); 421 } 422 423 public void setSoLinger( int i ) { 424 ep.setSoLinger( i ); 425 setAttribute("soLinger", "" + i); 426 } 427 428 public int getSoTimeout() { 429 return ep.getSoTimeout(); 430 } 431 432 public void setSoTimeout( int i ) { 433 ep.setSoTimeout(i); 434 setAttribute("soTimeout", "" + i); 435 } 436 437 public String getProtocol() { 438 return getProperty("protocol"); 439 } 440 441 public void setProtocol( String k ) { 442 setSecure(true); 443 setAttribute("protocol", k); 444 } 445 446 public boolean getSecure() { 447 return secure; 448 } 449 450 public void setSecure( boolean b ) { 451 secure=b; 452 setAttribute("secure", "" + b); 453 } 454 455 public int getMaxKeepAliveRequests() { 456 return maxKeepAliveRequests; 457 } 458 459 461 public void setMaxKeepAliveRequests(int mkar) { 462 maxKeepAliveRequests = mkar; 463 setAttribute("maxKeepAliveRequests", "" + mkar); 464 } 465 466 469 public boolean getKeepAlive() { 470 return ((maxKeepAliveRequests != 0) && (maxKeepAliveRequests != 1)); 471 } 472 473 476 public void setKeepAlive(boolean keepAlive) { 477 if (!keepAlive) { 478 setMaxKeepAliveRequests(1); 479 } 480 } 481 482 public int getSocketCloseDelay() { 483 return socketCloseDelay; 484 } 485 486 public void setSocketCloseDelay( int d ) { 487 socketCloseDelay=d; 488 setAttribute("socketCloseDelay", "" + d); 489 } 490 491 public void setServer( String server ) { 492 this.server = server; 493 } 494 495 public String getServer() { 496 return server; 497 } 498 499 public int getTimeout() { 500 return timeout; 501 } 502 503 public void setTimeout( int timeouts ) { 504 timeout = timeouts; 505 setAttribute("timeout", "" + timeouts); 506 } 507 508 510 513 public boolean isSSLEnabled() { return ep.isSSLEnabled(); } 514 public void setSSLEnabled(boolean SSLEnabled) { ep.setSSLEnabled(SSLEnabled); } 515 516 517 520 public String getSSLProtocol() { return ep.getSSLProtocol(); } 521 public void setSSLProtocol(String SSLProtocol) { ep.setSSLProtocol(SSLProtocol); } 522 523 524 528 public String getSSLPassword() { return ep.getSSLPassword(); } 529 public void setSSLPassword(String SSLPassword) { ep.setSSLPassword(SSLPassword); } 530 531 532 535 public String getSSLCipherSuite() { return ep.getSSLCipherSuite(); } 536 public void setSSLCipherSuite(String SSLCipherSuite) { ep.setSSLCipherSuite(SSLCipherSuite); } 537 538 539 542 public String getSSLCertificateFile() { return ep.getSSLCertificateFile(); } 543 public void setSSLCertificateFile(String SSLCertificateFile) { ep.setSSLCertificateFile(SSLCertificateFile); } 544 545 546 549 public String getSSLCertificateKeyFile() { return ep.getSSLCertificateKeyFile(); } 550 public void setSSLCertificateKeyFile(String SSLCertificateKeyFile) { ep.setSSLCertificateKeyFile(SSLCertificateKeyFile); } 551 552 553 556 public String getSSLCertificateChainFile() { return ep.getSSLCertificateChainFile(); } 557 public void setSSLCertificateChainFile(String SSLCertificateChainFile) { ep.setSSLCertificateChainFile(SSLCertificateChainFile); } 558 559 560 563 public String getSSLCACertificatePath() { return ep.getSSLCACertificatePath(); } 564 public void setSSLCACertificatePath(String SSLCACertificatePath) { ep.setSSLCACertificatePath(SSLCACertificatePath); } 565 566 567 570 public String getSSLCACertificateFile() { return ep.getSSLCACertificateFile(); } 571 public void setSSLCACertificateFile(String SSLCACertificateFile) { ep.setSSLCACertificateFile(SSLCACertificateFile); } 572 573 574 577 public String getSSLCARevocationPath() { return ep.getSSLCARevocationPath(); } 578 public void setSSLCARevocationPath(String SSLCARevocationPath) { ep.setSSLCARevocationPath(SSLCARevocationPath); } 579 580 581 584 public String getSSLCARevocationFile() { return ep.getSSLCARevocationFile(); } 585 public void setSSLCARevocationFile(String SSLCARevocationFile) { ep.setSSLCARevocationFile(SSLCARevocationFile); } 586 587 588 591 public String getSSLVerifyClient() { return ep.getSSLVerifyClient(); } 592 public void setSSLVerifyClient(String SSLVerifyClient) { ep.setSSLVerifyClient(SSLVerifyClient); } 593 594 595 598 public int getSSLVerifyDepth() { return ep.getSSLVerifyDepth(); } 599 public void setSSLVerifyDepth(int SSLVerifyDepth) { ep.setSSLVerifyDepth(SSLVerifyDepth); } 600 601 603 static class Http11ConnectionHandler implements Handler { 604 605 protected Http11AprProtocol proto; 606 protected static int count = 0; 607 protected RequestGroupInfo global = new RequestGroupInfo(); 608 609 protected ThreadLocal <Http11AprProcessor> localProcessor = 610 new ThreadLocal <Http11AprProcessor>(); 611 protected ConcurrentHashMap <Long , Http11AprProcessor> connections = 612 new ConcurrentHashMap <Long , Http11AprProcessor>(); 613 protected java.util.Stack <Http11AprProcessor> recycledProcessors = 614 new java.util.Stack <Http11AprProcessor>(); 615 616 Http11ConnectionHandler(Http11AprProtocol proto) { 617 this.proto = proto; 618 } 619 620 public SocketState event(long socket, SocketStatus status) { 621 Http11AprProcessor result = connections.get(socket); 622 623 SocketState state = SocketState.CLOSED; 624 if (result != null) { 625 try { 627 state = result.event(status); 628 } catch (java.net.SocketException e) { 629 Http11AprProtocol.log.debug 631 (sm.getString 632 ("http11protocol.proto.socketexception.debug"), e); 633 } catch (java.io.IOException e) { 634 Http11AprProtocol.log.debug 636 (sm.getString 637 ("http11protocol.proto.ioexception.debug"), e); 638 } 639 catch (Throwable e) { 643 Http11AprProtocol.log.error 647 (sm.getString("http11protocol.proto.error"), e); 648 } finally { 649 if (state != SocketState.LONG) { 650 connections.remove(socket); 651 recycledProcessors.push(result); 652 } 653 } 654 } 655 return state; 656 } 657 658 public SocketState process(long socket) { 659 Http11AprProcessor processor = null; 660 try { 661 processor = (Http11AprProcessor) localProcessor.get(); 662 if (processor == null) { 663 synchronized (recycledProcessors) { 664 if (!recycledProcessors.isEmpty()) { 665 processor = recycledProcessors.pop(); 666 localProcessor.set(processor); 667 } 668 } 669 } 670 if (processor == null) { 671 processor = 672 new Http11AprProcessor(proto.maxHttpHeaderSize, proto.ep); 673 processor.setAdapter(proto.adapter); 674 processor.setMaxKeepAliveRequests(proto.maxKeepAliveRequests); 675 processor.setTimeout(proto.timeout); 676 processor.setDisableUploadTimeout(proto.disableUploadTimeout); 677 processor.setCompression(proto.compression); 678 processor.setCompressionMinSize(proto.compressionMinSize); 679 processor.setNoCompressionUserAgents(proto.noCompressionUserAgents); 680 processor.setCompressableMimeTypes(proto.compressableMimeTypes); 681 processor.setRestrictedUserAgents(proto.restrictedUserAgents); 682 processor.setSocketBuffer(proto.socketBuffer); 683 processor.setMaxSavePostSize(proto.maxSavePostSize); 684 processor.setServer(proto.server); 685 localProcessor.set(processor); 686 if (proto.getDomain() != null) { 687 synchronized (this) { 688 try { 689 RequestInfo rp = processor.getRequest().getRequestProcessor(); 690 rp.setGlobalProcessor(global); 691 ObjectName rpName = new ObjectName 692 (proto.getDomain() + ":type=RequestProcessor,worker=" 693 + proto.getName() + ",name=HttpRequest" + count++); 694 Registry.getRegistry(null, null).registerComponent(rp, rpName, null); 695 } catch (Exception e) { 696 log.warn("Error registering request"); 697 } 698 } 699 } 700 } 701 702 if (processor instanceof ActionHook) { 703 ((ActionHook) processor).action(ActionCode.ACTION_START, null); 704 } 705 706 SocketState state = processor.process(socket); 707 if (state == SocketState.LONG) { 708 connections.put(socket, processor); 712 localProcessor.set(null); 713 proto.ep.getCometPoller().add(socket); 714 } 715 return state; 716 717 } catch (java.net.SocketException e) { 718 Http11AprProtocol.log.debug 720 (sm.getString 721 ("http11protocol.proto.socketexception.debug"), e); 722 } catch (java.io.IOException e) { 723 Http11AprProtocol.log.debug 725 (sm.getString 726 ("http11protocol.proto.ioexception.debug"), e); 727 } 728 catch (Throwable e) { 732 Http11AprProtocol.log.error 736 (sm.getString("http11protocol.proto.error"), e); 737 } 738 return SocketState.CLOSED; 739 } 740 } 741 742 protected static org.apache.commons.logging.Log log 743 = org.apache.commons.logging.LogFactory.getLog(Http11AprProtocol.class); 744 745 747 protected String domain; 748 protected ObjectName oname; 749 protected MBeanServer mserver; 750 751 public ObjectName getObjectName() { 752 return oname; 753 } 754 755 public String getDomain() { 756 return domain; 757 } 758 759 public ObjectName preRegister(MBeanServer server, 760 ObjectName name) throws Exception { 761 oname=name; 762 mserver=server; 763 domain=name.getDomain(); 764 return name; 765 } 766 767 public void postRegister(Boolean registrationDone) { 768 } 769 770 public void preDeregister() throws Exception { 771 } 772 773 public void postDeregister() { 774 } 775 } 776 | Popular Tags |