Tomcat processing HTTP request source code analysis (I)

Source: Internet
Author: User
Document directory
  • 1.1 Connector types
  • 1.2 connector configuration
  • 2.1 Tomcat Architecture
  • 2.2 Tomcat running process
  • 3.1 Tomcat startup analysis and integration ideas
  • 3.2 connector class diagram and sequence diagram
  • 3.3 connector Workflow

Reproduced http://www.infoq.com/cn/articles/zh-tomcat-http-request-1

Many open-source application servers integrate Tomcat as Web Container, And the servlet container code of Tomcat is rarely changed. In this way, the performance of these application servers depends on the performance of the connector module that Tomcat processes HTTP requests. This article first analyzes all types and usage of connector in Tomcat at the application level, and then analyzes the position of the connector module in the entire tomcat, at last, the source code of connector is analyzed in detail. In addition, we take http11nioprotocol as an example to illustrate how Tomcat constructs connector by implementing the protocolhandler interface.

By studying this article, you can easily integrate Tomcat as a web iner into a third-party system and customize any high-performance HTTP connector you want.

1 connector introduces 1.1 Connector types

The connector-related classes in Tomcat source code are located in the org. Apache. Coyote package. connector is divided into the following categories:

  • HTTP ctor Ctor is responsible for establishing HTTP connections based on the HTTP protocol. It can be divided into bio HTTP ctor AND NiO HTTP connector. The latter provides support for non-blocking Io and persistent connection comet.
  • Based on the AJP protocol, AJP is a specially designed protocol for communication between Tomcat and HTTP servers, providing high communication speed and efficiency. For example, this protocol is used when it is integrated with the Apache server.
  • Apr http connector, which is implemented in C and called through JNI. It mainly improves the access performance of static resources (such as HTML, images, CSS, and JS. The library is now available independently in any project. Tomcat performs very well after configuring Apr.
1.2 connector configuration

The connector configuration is in the conf/server. xml file.

1.2.1 bio HTTP/1.1 Connector Configuration

A typical configuration is as follows:

<Connector port=”8080” protocol=”HTTP/1.1” maxThreads=”150”  connectionTimeout=”20000” redirectPort=”8443”

Other important attributes are as follows:

  • Acceptcount: Maximum number of connections that receive requests. The default value is 10.
  • Address: bind an IP address. If it is not bound, it is bound to any IP Address by default.
  • Allowtrace: if it is true, the trace HTTP method is allowed.
  • Compressiblemimetypes: Various mimetype, separated by commas, such as text/html, text/XML
  • Compression: gzip compression can be used if the bandwidth is limited.
  • Connectiontimeout: timeout time. The default value is 60000 ms (60 s)
  • Maxkeepaliverequest: 100 by default
  • Maxthreads: Number of connector threads that process requests. The default value is 200.

For SSL configuration, see the following:

<Connector port="8181" protocol="HTTP/1.1" SSLEnabled="true"      maxThreads="150" scheme="https" secure="true"      clientAuth="false" sslProtocol = "TLS"      address="0.0.0.0"      keystoreFile="E:/java/jonas-full-5.1.0-RC3/conf/keystore.jks"      keystorePass="changeit" /> 

The keystorefile is the certificate location, and the keystorepass is the certificate password.

1.2.2 NiO HTTP/1.1 Connector Configuration
<Connector port=”8080” protocol=”org.apache.coyote.http11.Http11NioProtocol”      maxThreads=”150” connectionTimeout=”20000” redirectPort=”8443” 
1.2.3 native APR ctor Configuration
  1. ARP is written in C/C ++ to optimize static resources (such as HTML and images. So download the local library

    Tcnative-1.dlland openssl.exe are stored in the % Tomcat % \ bin directory.

    Yes: http://tomcat.heanet.ie/native/1.1.10/binaries/win32/

  2. Configure a listener in server. XML, for example. Tomcat is configured by default.
    <!--APR library loader. Documentation at /docs/apr.html -->  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" /> 
  3. Configure to use APR connector
    <Connector port=”8080” protocol=”org.apache.coyote.http11.Http11AprProtocol

    maxThreads=”150” connectionTimeout=”20000” redirectPort=”8443”

  4. If the configuration is successful, start Tomcat and you will see the following information:
    org.apache.coyote.http11.Http11AprProtocol init 
2 connector position in Tomcat 2.1 Tomcat Architecture

Figure 2-1 Tomcat Architecture

  • Server is a top-level component of Tomcat. Everything is included in the server. The server implementation class standardserver can contain one or more services;
  • The implementation class of the sub-level element service calls the container (container) interface for standardservice, which actually calls the Servlet Engine (ENGINE) and also specifies the server to which the service belongs;
  • The following sub-components are containers. The host, context, and engine are inherited from the container interface, so they are all containers. However, they have parent-child relationships. Among the three types of containers, host, context, and engine, the engine is a top-level container and directly contains a host container, the host container contains the context container, so the engine, host, and context constitute a parent-child relationship in terms of size, although they all inherit from the container interface.
  • The connector connects the service and the container. First, it needs to register a service. Its function is to forward requests from the client to the container ), this is why it is called a connector.

Therefore, we divide the Tomcat source code into five sub-modules from the perspective of functions:

  1. Jsper submodule: This submodule is responsible for parsing JSP pages and verifying JSP attributes. It is also responsible for dynamically converting JSP pages into Java code and compiling them into class files. In the Tomcat source code, all the source codes in the org. Apache. Jasper package and its sub-packages belong to this sub-module;
  2. Servlet and JSP specification implementation module: the source code of this submodule belongs to javax. servlet package and its sub-packages, such as javax, which we are very familiar. servlet. servlet interface, javax. servet. HTTP. httpservlet class and javax. servlet. JSP. httpjsppage is located in this submodule;
  3. Catalina submodule: This submodule contains all Java source code starting with org. Apache. Catalina. The task of this sub-module standardizes the overall architecture of Tomcat, defines key components such as server, service, host, connector, context, session, and cluster, and the implementation of these components, this submodule uses the composite design mode in a large amount. At the same time, it standardizes the execution process of events such as startup and stop of Catalina. From the perspective of code reading, this submodule should be the focus of our reading and learning.
  4. Connectors sub-module: if the above three sub-modules implement the Tomcat application server, then this sub-module is the implementation of the Web server. The so-called connector is a bridge connecting customers and application servers. It receives user requests and packs user requests into standard HTTP requests (including the protocol name, request header head, the request method is get, post, and so on ). At the same time, this sub-module is also responsible for sending the response page to the client according to the standard HTTP protocol. For example, when the request page is not found, connector sends a standard HTTP 404 error response page to the client browser.
  5. Resource submodule: This submodule contains some resource files, such as server. XML and Web. xml configuration files. Strictly speaking, this sub-module does not contain Java source code, but it is required for Tomcat compilation and running.
2.2 Tomcat running process

Figure 2-2 Tomcat running process

Assume that the request from the client is http: // localhost: 8080/test/index. jsp.

  1. The request is sent to the local port 8080, which is obtained by coyote HTTP/1.1 Connector
  2. Connector sends the request to the engine of the Service to process the request, and waits for the engine to respond.
  3. The engine obtains the request localhost: 8080/test/index. jsp, matching all its VM hosts.
  4. The engine matches the host named localhost (the request is handed over to the host even if the match fails, because the host is defined as the default host of the engine)
  5. Localhost obtains the request/test/index. jsp and matches all the context
  6. The host matches the context in the/test path. (if no match is found, the request is sent to the context with the path name "" for processing)
  7. Obtain the request/index. jsp from the context of Path = "/test" and find the corresponding servlet in its mapping table.
  8. The context matches the servlet whose URL pattern is *. jsp and corresponds to the jspservlet class.
  9. Construct the httpservletrequest object and httpservletresponse object, and call the doget or dopost methods of jspservlet as parameters.
  10. Context returns the httpservletresponse object after execution to the host
  11. Host returns the httpservletresponse object to the engine
  12. Engine returns the httpservletresponse object to Connector
  13. Connector returns the httpservletresponse object to the client browser.
3 connector Source Code Analysis 3.1 Tomcat startup analysis and integration ideas

We know that there are two ways to start Tomcat:

  • Double-click bin/startup. bat
  • Run bin/Catalina. BAT Run

They correspond to bootstrap and Catalina classes. Now we only care about the Catalina class. This class uses Apache digester to parse CONF/server. the XML file generates the Tomcat component, and then calls the start method of the embedded class to start Tomcat.

Therefore, there are two ways to integrate Tomcat:

  • Use Tomcat's server. xml
  • Define an XML format to configure the parameters of tocmat, write and parse the XML, and use the API provided by Tomcat to generate Tomcat components based on the XML, call the start method of the embedded class to start Tomcat.

I personally think that the first method should be superior and give developers a better user experience. If this method is used, the integration can be achieved directly by imitating the Catalina class method.

Currently, Jonas uses this integration method. JBoss and glassfish use the second custom XML method.

3.2 connector class diagram and sequence diagram

Figure 3-1 ctor class diagram

Figure 3-2 connector workflow Sequence

We can get the following information from the figure above:

  1. Tomcat has four types of containers (context, engine, host, and wrapper). The first three are common. The fourth is uncommon, but it is also a container that implements the container interface.
  2. If you wantTo customize a ctor, you only need to implement the protocolhander interface.The interface is defined as follows:

Figure 3-3 protocolhandler interface to be implemented for custom ctor

Tomcat implements this interface using HTTP (including bio and NiO), AJP, APR, and memory protocols (they are: ajpaprprotocol, ajpprotocol, http11aprprotocol, http11nioprotocol, http11protocal, jkcoyotehandler, and memoryprotocolhandler. configured in XML, the constructor will instantiate the configured implementation class through reflection:

<Connector port="8181"     protocol="org.apache.coyote.http11.Http11AprProtocol " /> 
3.3 connector Workflow

The following uses http11aprprotocol as an example to describe the workflow of ctor.

  1. It delegates the work to the nioendpoint class. Build a socketserver In the init method of the nioendpoint class (of course, there will be slight changes in different implementation classes. For example, if NiO is used, it will build socketserverchannel)
  2. In the nioendpoint. acceptor class, a new client connection request is received, for example:

  3. In the nioendpoint class, there is an internal interface handle, which is defined as follows:

  4. The handle internal interface is implemented in the http11nioprotocol class, And the http11nioprocessor class is called (this class implements the actionhook callback interface ). The response class calls the actionhook implementation method of the class. The action method of the response class is as follows:

  5. In the process Implementation Method of http11nioprocessor, the adapter is used to call the servler container to generate a response result.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.