1 17 18 package org.apache.coyote.http11; 19 20 import java.net.InetAddress ; 21 import java.net.Socket ; 22 import java.net.URLEncoder ; 23 import java.util.HashMap ; 24 import java.util.Iterator ; 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.JIoEndpoint; 39 import org.apache.tomcat.util.net.SSLImplementation; 40 import org.apache.tomcat.util.net.ServerSocketFactory; 41 import org.apache.tomcat.util.net.JIoEndpoint.Handler; 42 import org.apache.tomcat.util.res.StringManager; 43 44 45 54 public class Http11Protocol 55 implements ProtocolHandler, MBeanRegistration { 56 57 58 protected static org.apache.commons.logging.Log log 59 = org.apache.commons.logging.LogFactory.getLog(Http11Protocol.class); 60 61 64 protected static StringManager sm = 65 StringManager.getManager(Constants.Package); 66 67 68 70 71 public Http11Protocol() { 72 setSoLinger(Constants.DEFAULT_CONNECTION_LINGER); 73 setSoTimeout(Constants.DEFAULT_CONNECTION_TIMEOUT); 74 setTcpNoDelay(Constants.DEFAULT_TCP_NO_DELAY); 76 } 77 78 79 81 82 protected Http11ConnectionHandler cHandler = new Http11ConnectionHandler(this); 83 protected JIoEndpoint endpoint = new JIoEndpoint(); 84 85 86 protected ObjectName tpOname = null; 88 protected ObjectName rgOname = null; 90 91 92 protected ServerSocketFactory socketFactory = null; 93 protected SSLImplementation sslImplementation = null; 94 95 96 99 100 protected HashMap <String , Object > attributes = new HashMap <String , Object >(); 101 102 103 106 public void setProperty(String name, String value) { 107 setAttribute(name, value); 108 } 109 110 113 public String getProperty(String name) { 114 return (String )getAttribute(name); 115 } 116 117 120 public void setAttribute(String name, Object value) { 121 if (log.isTraceEnabled()) { 122 log.trace(sm.getString("http11protocol.setattribute", name, value)); 123 } 124 attributes.put(name, value); 125 } 126 127 public Object getAttribute(String key) { 128 return attributes.get(key); 129 } 130 131 public Iterator getAttributeNames() { 132 return attributes.keySet().iterator(); 133 } 134 135 136 139 protected Adapter adapter; 140 public void setAdapter(Adapter adapter) { this.adapter = adapter; } 141 public Adapter getAdapter() { return adapter; } 142 143 144 public void init() throws Exception { 145 endpoint.setName(getName()); 146 endpoint.setHandler(cHandler); 147 148 try { 150 if (isSSLEnabled()) { 151 sslImplementation = 152 SSLImplementation.getInstance(sslImplementationName); 153 socketFactory = sslImplementation.getServerSocketFactory(); 154 endpoint.setServerSocketFactory(socketFactory); 155 } else if (socketFactoryName != null) { 156 socketFactory = (ServerSocketFactory) Class.forName(socketFactoryName).newInstance(); 157 endpoint.setServerSocketFactory(socketFactory); 158 } 159 } catch (Exception ex) { 160 log.error(sm.getString("http11protocol.socketfactory.initerror"), 161 ex); 162 throw ex; 163 } 164 165 if (socketFactory!=null) { 166 Iterator <String > attE = attributes.keySet().iterator(); 167 while( attE.hasNext() ) { 168 String key = attE.next(); 169 Object v=attributes.get(key); 170 socketFactory.setAttribute(key, v); 171 } 172 } 173 174 try { 175 endpoint.init(); 176 } catch (Exception ex) { 177 log.error(sm.getString("http11protocol.endpoint.initerror"), ex); 178 throw ex; 179 } 180 if (log.isInfoEnabled()) 181 log.info(sm.getString("http11protocol.init", getName())); 182 183 } 184 185 public void start() throws Exception { 186 if (this.domain != null) { 187 try { 188 tpOname = new ObjectName 189 (domain + ":" + "type=ThreadPool,name=" + getName()); 190 Registry.getRegistry(null, null) 191 .registerComponent(endpoint, tpOname, null ); 192 } catch (Exception e) { 193 log.error("Can't register endpoint"); 194 } 195 rgOname=new ObjectName 196 (domain + ":type=GlobalRequestProcessor,name=" + getName()); 197 Registry.getRegistry(null, null).registerComponent 198 ( cHandler.global, rgOname, null ); 199 } 200 201 try { 202 endpoint.start(); 203 } catch (Exception ex) { 204 log.error(sm.getString("http11protocol.endpoint.starterror"), ex); 205 throw ex; 206 } 207 if (log.isInfoEnabled()) 208 log.info(sm.getString("http11protocol.start", getName())); 209 } 210 211 public void pause() throws Exception { 212 try { 213 endpoint.pause(); 214 } catch (Exception ex) { 215 log.error(sm.getString("http11protocol.endpoint.pauseerror"), ex); 216 throw ex; 217 } 218 if (log.isInfoEnabled()) 219 log.info(sm.getString("http11protocol.pause", getName())); 220 } 221 222 public void resume() throws Exception { 223 try { 224 endpoint.resume(); 225 } catch (Exception ex) { 226 log.error(sm.getString("http11protocol.endpoint.resumeerror"), ex); 227 throw ex; 228 } 229 if (log.isInfoEnabled()) 230 log.info(sm.getString("http11protocol.resume", getName())); 231 } 232 233 public void destroy() throws Exception { 234 if (log.isInfoEnabled()) 235 log.info(sm.getString("http11protocol.stop", getName())); 236 endpoint.destroy(); 237 if (tpOname!=null) 238 Registry.getRegistry(null, null).unregisterComponent(tpOname); 239 if (rgOname != null) 240 Registry.getRegistry(null, null).unregisterComponent(rgOname); 241 } 242 243 244 246 247 252 protected boolean secure; 253 public boolean getSecure() { return secure; } 254 public void setSecure(boolean b) { secure = b; } 255 256 protected boolean SSLEnabled = false; 257 public boolean isSSLEnabled() { return SSLEnabled;} 258 public void setSSLEnabled(boolean SSLEnabled) {this.SSLEnabled = SSLEnabled;} 259 260 263 protected String socketFactoryName = null; 264 public String getSocketFactory() { return socketFactoryName; } 265 public void setSocketFactory(String valueS) { socketFactoryName = valueS; } 266 267 268 271 protected String sslImplementationName=null; 272 public String getSSLImplementation() { return sslImplementationName; } 273 public void setSSLImplementation( String valueS) { 274 sslImplementationName = valueS; 275 setSecure(true); 276 } 277 278 279 284 protected int maxKeepAliveRequests = 100; 285 public int getMaxKeepAliveRequests() { return maxKeepAliveRequests; } 286 public void setMaxKeepAliveRequests(int mkar) { maxKeepAliveRequests = mkar; } 287 288 289 296 protected int timeout = 300000; 297 public int getTimeout() { return timeout; } 298 public void setTimeout(int timeout) { this.timeout = timeout; } 299 300 301 306 protected int maxSavePostSize = 4 * 1024; 307 public int getMaxSavePostSize() { return maxSavePostSize; } 308 public void setMaxSavePostSize(int valueI) { maxSavePostSize = valueI; } 309 310 311 315 protected int maxHttpHeaderSize = 8 * 1024; 316 public int getMaxHttpHeaderSize() { return maxHttpHeaderSize; } 317 public void setMaxHttpHeaderSize(int valueI) { maxHttpHeaderSize = valueI; } 318 319 320 325 protected boolean disableUploadTimeout = true; 326 public boolean getDisableUploadTimeout() { return disableUploadTimeout; } 327 public void setDisableUploadTimeout(boolean isDisabled) { disableUploadTimeout = isDisabled; } 328 329 330 334 protected String compression = "off"; 335 public String getCompression() { return compression; } 336 public void setCompression(String valueS) { compression = valueS; } 337 338 339 protected String noCompressionUserAgents = null; 341 public String getNoCompressionUserAgents() { return noCompressionUserAgents; } 342 public void setNoCompressionUserAgents(String valueS) { noCompressionUserAgents = valueS; } 343 344 345 protected String compressableMimeTypes = "text/html,text/xml,text/plain"; 347 public String getCompressableMimeType() { return compressableMimeTypes; } 348 public void setCompressableMimeType(String valueS) { compressableMimeTypes = valueS; } 349 350 351 protected int compressionMinSize = 2048; 353 public int getCompressionMinSize() { return compressionMinSize; } 354 public void setCompressionMinSize(int valueI) { compressionMinSize = valueI; } 355 356 357 361 protected String restrictedUserAgents = null; 362 public String getRestrictedUserAgents() { return restrictedUserAgents; } 363 public void setRestrictedUserAgents(String valueS) { restrictedUserAgents = valueS; } 364 365 366 370 protected String server; 371 public void setServer( String server ) { this.server = server; } 372 public String getServer() { return server; } 373 374 375 377 public Executor getExecutor() { 379 return endpoint.getExecutor(); 380 } 381 382 public void setExecutor(Executor executor) { 384 endpoint.setExecutor(executor); 385 } 386 387 public int getMaxThreads() { 389 return endpoint.getMaxThreads(); 390 } 391 392 public void setMaxThreads( int maxThreads ) { 394 endpoint.setMaxThreads(maxThreads); 395 } 396 397 public void setThreadPriority(int threadPriority) { 399 endpoint.setThreadPriority(threadPriority); 400 } 401 402 public int getThreadPriority() { 404 return endpoint.getThreadPriority(); 405 } 406 407 public int getBacklog() { 409 return endpoint.getBacklog(); 410 } 411 412 public void setBacklog( int i ) { 414 endpoint.setBacklog(i); 415 } 416 417 public int getPort() { 419 return endpoint.getPort(); 420 } 421 422 public void setPort( int port ) { 424 endpoint.setPort(port); 425 } 426 427 public InetAddress getAddress() { 429 return endpoint.getAddress(); 430 } 431 432 public void setAddress(InetAddress ia) { 434 endpoint.setAddress( ia ); 435 } 436 437 public String getName() { 439 String encodedAddr = ""; 440 if (getAddress() != null) { 441 encodedAddr = "" + getAddress(); 442 if (encodedAddr.startsWith("/")) 443 encodedAddr = encodedAddr.substring(1); 444 encodedAddr = URLEncoder.encode(encodedAddr) + "-"; 445 } 446 return ("http-" + encodedAddr + endpoint.getPort()); 447 } 448 449 public boolean getTcpNoDelay() { 451 return endpoint.getTcpNoDelay(); 452 } 453 454 public void setTcpNoDelay( boolean b ) { 456 endpoint.setTcpNoDelay( b ); 457 } 458 459 public int getSoLinger() { 461 return endpoint.getSoLinger(); 462 } 463 464 public void setSoLinger( int i ) { 466 endpoint.setSoLinger( i ); 467 } 468 469 public int getSoTimeout() { 471 return endpoint.getSoTimeout(); 472 } 473 474 public void setSoTimeout( int i ) { 476 endpoint.setSoTimeout(i); 477 } 478 479 483 public boolean getKeepAlive() { 484 return ((maxKeepAliveRequests != 0) && (maxKeepAliveRequests != 1)); 485 } 486 487 491 public void setKeepAlive(boolean keepAlive) { 492 if (!keepAlive) { 493 setMaxKeepAliveRequests(1); 494 } 495 } 496 497 500 501 public String getKeystore() { 502 return (String ) getAttribute("keystore"); 503 } 504 505 public void setKeystore( String k ) { 506 setAttribute("keystore", k); 507 } 508 509 public String getKeypass() { 510 return (String ) getAttribute("keypass"); 511 } 512 513 public void setKeypass( String k ) { 514 attributes.put("keypass", k); 515 } 517 518 public String getKeytype() { 519 return (String ) getAttribute("keystoreType"); 520 } 521 522 public void setKeytype( String k ) { 523 setAttribute("keystoreType", k); 524 } 525 526 public String getClientauth() { 527 return (String ) getAttribute("clientauth"); 528 } 529 530 public void setClientauth( String k ) { 531 setAttribute("clientauth", k); 532 } 533 534 public String getProtocols() { 535 return (String ) getAttribute("protocols"); 536 } 537 538 public void setProtocols(String k) { 539 setAttribute("protocols", k); 540 } 541 542 public String getAlgorithm() { 543 return (String ) getAttribute("algorithm"); 544 } 545 546 public void setAlgorithm( String k ) { 547 setAttribute("algorithm", k); 548 } 549 550 public String getCiphers() { 551 return (String ) getAttribute("ciphers"); 552 } 553 554 public void setCiphers(String ciphers) { 555 setAttribute("ciphers", ciphers); 556 } 557 558 public String getKeyAlias() { 559 return (String ) getAttribute("keyAlias"); 560 } 561 562 public void setKeyAlias(String keyAlias) { 563 setAttribute("keyAlias", keyAlias); 564 } 565 566 568 protected static class Http11ConnectionHandler implements Handler { 569 protected Http11Protocol protocol; 570 protected static int count = 0; 571 protected RequestGroupInfo global = new RequestGroupInfo(); 572 protected ThreadLocal <Http11Processor> localProcessor = new ThreadLocal <Http11Processor>(); 573 574 Http11ConnectionHandler(Http11Protocol proto) { 575 this.protocol = proto; 576 } 577 578 public boolean process(Socket socket) { 579 Http11Processor processor = null; 580 try { 581 processor = localProcessor.get(); 582 if (processor == null) { 583 processor = 584 new Http11Processor(protocol.maxHttpHeaderSize, protocol.endpoint); 585 processor.setAdapter(protocol.adapter); 586 processor.setMaxKeepAliveRequests(protocol.maxKeepAliveRequests); 587 processor.setTimeout(protocol.timeout); 588 processor.setDisableUploadTimeout(protocol.disableUploadTimeout); 589 processor.setCompression(protocol.compression); 590 processor.setCompressionMinSize(protocol.compressionMinSize); 591 processor.setNoCompressionUserAgents(protocol.noCompressionUserAgents); 592 processor.setCompressableMimeTypes(protocol.compressableMimeTypes); 593 processor.setRestrictedUserAgents(protocol.restrictedUserAgents); 594 processor.setMaxSavePostSize(protocol.maxSavePostSize); 595 processor.setServer(protocol.server); 596 localProcessor.set(processor); 597 if (protocol.getDomain() != null) { 598 synchronized (this) { 599 try { 600 RequestInfo rp = processor.getRequest().getRequestProcessor(); 601 rp.setGlobalProcessor(global); 602 ObjectName rpName = new ObjectName 603 (protocol.getDomain() + ":type=RequestProcessor,worker=" 604 + protocol.getName() + ",name=HttpRequest" + count++); 605 Registry.getRegistry(null, null).registerComponent(rp, rpName, null); 606 } catch (Exception e) { 607 log.warn("Error registering request"); 608 } 609 } 610 } 611 } 612 613 if (processor instanceof ActionHook) { 614 ((ActionHook) processor).action(ActionCode.ACTION_START, null); 615 } 616 617 if (protocol.secure && (protocol.sslImplementation != null)) { 618 processor.setSSLSupport 619 (protocol.sslImplementation.getSSLSupport(socket)); 620 } else { 621 processor.setSSLSupport(null); 622 } 623 624 processor.process(socket); 625 return false; 626 627 } catch(java.net.SocketException e) { 628 Http11Protocol.log.debug 630 (sm.getString 631 ("http11protocol.proto.socketexception.debug"), e); 632 } catch (java.io.IOException e) { 633 Http11Protocol.log.debug 635 (sm.getString 636 ("http11protocol.proto.ioexception.debug"), e); 637 } 638 catch (Throwable e) { 642 Http11Protocol.log.error 646 (sm.getString("http11protocol.proto.error"), e); 647 } finally { 648 651 if (processor instanceof ActionHook) { 652 ((ActionHook) processor).action(ActionCode.ACTION_STOP, null); 653 } 654 } 655 return false; 656 } 657 } 658 659 660 662 protected String domain; 664 protected ObjectName oname; 665 protected MBeanServer mserver; 666 667 public ObjectName getObjectName() { 668 return oname; 669 } 670 671 public String getDomain() { 672 return domain; 673 } 674 675 public ObjectName preRegister(MBeanServer server, 676 ObjectName name) throws Exception { 677 oname=name; 678 mserver=server; 679 domain=name.getDomain(); 680 return name; 681 } 682 683 public void postRegister(Boolean registrationDone) { 684 } 685 686 public void preDeregister() throws Exception { 687 } 688 689 public void postDeregister() { 690 } 691 } 692 | Popular Tags |