@TOC

Reading source code won’t immediately improve your coding skills. But it gives you a better understanding of programming.

Since Tomcat mostly handles requests in NIO form, this series focuses on the components that handle NIO type requests.

It is crucial to understand the concepts before explaining the process.

participants

The Connector in Tomcat handles requests.

1. Handle the ProtocolHandler

Connector uses a ProtocolHandler handler to process requests. Different ProtocolHandlers represent different connection types.

A ProtocolHandler can be used as a protocol processing coordinator to handle requests by managing other work components. The ProtocolHandler contains three important components:

  • Endpoint: Accepts and processes socket network connections

  • Processor: encapsulates the socket connection received from the Endpoint as a request based on the protocol type

  • Adapter: Sends the encapsulated Request to the Container for processing.

2. The Socket of the encapsulation

  • NioChannel: Base wrapper class for SocketChannel, used in EndPonit.

  • SocketWrapper: Wrapper class for sockets that carry (NioChannel) sockets for transmission.

Difference :NioChannel is the basic wrapper,SocketWrapper is the further wrapper of NioChannel.

3. A variety of buffer

  • Socket InputStream InputStream: an IO package belonging to the JDK. Acts as a channel for the operating system to read socket bytes from underneath
  • SokcetBuffer ByteBuffer. Nio layer buffer under the java.nio package. Tomcat’s default size for creating this type of Buffer is 8 x 1024, or 8K
  • InputBuffer interface (internal buffer) : Under the Coyote package, used for Tomcat internal Request buffer. Socket input buffering device that provides a buffered mode to read byte streams from the socket. Through viewing the source code I feel it is also a tool interface, more inclined to a tool. Examples include the implementation class Http11InputBuffer, which provides parsing headers and escape functions.
  • InputBuffer class: under the Connector package. Buffer inside the Request for the Servlet specification
  • MessageBytes: Tomcat does not convert the incoming bytes from the socket immediately after it receives them. Instead, it holds the byte[] array and converts them where they are needed. MessageBytes is an abstraction of byte[]
  • Subblock manipulation tool ByteChunk: First of all it is a tool, a buffer manipulation tool. Second, there are two internal interfaces: ByteInputChannel and ByteOutputChannel, which read and write data. There is also a Chartset object for easy coding. So it’s a tool that provides encoding and caching.

4. Two of the request, the response

Tomcat has two groups of Request, respone

  • Org.apache. coyote: is an internal Tomcat definition of a request. Classes of final type are not open to developers. The main function is to encapsulate the socket and parse HTTP headers.
  • Org. Apache. Catalina. : under the connector packet belongs to the implementation of the servlet specification. This is a common request object that we develop.

Therefore, Tomcat will have a request to request conversion process, which will be discussed in the future. This article will only explain the concept of components.

Tomcat internally uses the Apache Coyote library to handle network I/O. Connector packages are mostly used for Servlet specifications.

5. Multiple processors

A lot of things inside Tomcat are called processors. Here are some of the differences:

  • ProtocolHandler handler: Processor at the coordination or management level. The Connector passes request processing to a ProtocolHandler. The rest is the ProtocolHandler orchestration. This shows that he is a handler of the management domain

  • ConnectionHandler: Connects the Processor. The internal maintenance Processor’s map is used to reuse the SocketProcessor. Leave the creation of the Processor to the ProtocolHandler.

    SocketProcessor Processor called ConnectionHandler. The process () socket request content into the Processor. The process () for processing.

    ConnectionHandler acts as a link between the preceding and the following.

  • SocketBufferHandler: socketBuffer processor. Contains two SocketBuffers used to buffer socket reads and writes.

  • SocketProcessor: Indicates the socket processor. It is a worker and sends the socket content to the Processor

  • HTTP 1.1 is the most used VERSION of the HTTP protocol. From the name of the processor, we can see that it deals with this protocol. This processor is the main component of the socket into the request, he will parse the HTTP protocol content from the socket, but in Tomcat he is only responsible for the HTTP request line, request header parsing; The resolution of the request body is deferred to the servlet. Http11Processor is created by ConnectionHandler. Tomcat reuses key classes to reduce the cost of frequent creation and destruction. It will pop up in recycledProcessors

conclusion

Once you understand the concepts of these related components. The next chapter explains how a socket becomes a request.