Overview
This package supports the HTTP1.1 protocol, which is divided into three categories: ARP, NIO, Normal HTTP, and only a simple study of the most basic common http (IO flow using Java, rather than the NIO stream)
According to the Coyote interface mentioned in the previous article, this package mainly has the following classes: Http11protocol, implements the Protocolhandler interface Http11processor, implements the Actionhook interface Internalinputbuffer, realizes the InputBuffer interface Internaloutputbuffer, realizes the OutputBuffer interface Inputfilter and the Outputfilter interface, the concrete realization class in In Org.apache.coyote.http11.filters
Here are the relationships between these classes, and casually draw a picture, make sure to see ^_^
The general process is as follows: Jioendpoint play a role in a connection pool, you can start multiple socket monitoring, once received a browser request, the corresponding socket object through the process method, passed to Http11connectionhandler, And then to Http11processor http11processor inside there is a internalinputbuffer (the picture is not drawn), Internalinputbuffer is really to the socket contained in the word throttling processing, It converts bytes to request request, flows through the filter filters, and finally arrives at the container that implements the adapter interface, and the coyote's work is over, and I'll go back to the next socket.
Here are a few of the main classes of functional introduction Http11protocol
Protocolhandler implementation of http1.1 protocol
Mainly contains
Http11connectionhandler (inner Class)
Jioendpoint
The general process of Serversocketfactory (J2SE) is as follows:
In the Init method, the Serversocketfactory, Http11connectionhandler are passed to the Jioendpoint for initialization.
Then, in the methods of start, pause, and so on, the jioendpoint start, pause
Jioendpoint can set the maximum number of threads, priority, port and other properties, according to these properties, Jioendpoint generate a corresponding number of serversocketfactory, for listening to the corresponding port, once you receive the HTTP request, Jioendpoint the corresponding socket instance is passed to the Http11connectionhandler.process method for processing; and Http11connectionhandler has a processor instance that really handles the socket and convert the data to the Request object
Therefore, the role of Protocolhandler is to wrap all these connection-related components, set their properties uniformly, and control their lifecycle http11processor
The function of this class is to generate the request (which is, of course, internalinputbuffer done), to the container that implements the adapter interface.
This class has adapter, request, response, InputBuffer, OutputBuffer, and several key fields, the rest is the HTTP protocol-related fields, there are many methods about HTTP protocol, the level of limited can not understand, It is estimated that the HTTP protocol must be studied in detail before it can be read, and this is skipped, direct look at the most critical process (socket socket) method
This method is followed by the following: The InputStream and outputstream of the socket are associated with InputBuffer and OutputBuffer respectively through Inputbuffer.parserequestline () and The Inputbuffer.parseheaders () method, which resolves the header field in the socket byte stream, is written to the request to assemble the filter through the Preparerequest method for processing the HTTP message body Adapter.service ( Request, response) deliver the generated request and response to the container. If all goes well, Start processing the next request in the socket (because http1.1 is supported for persistent connections, so one socket may contain multiple requests), loop back to the first step if there is an error, set the response response code and terminate the loop
Preparerequest method, used to prepare InputBuffer filter, here is a simple write. For the mechanism of filter, see: Check protocol, method, expect, user-agent, and Mimeheaders, based on previous parsing of HTTP header fields, and also check the format of the URI (whether: protocol:// host:port/format) Ready to load filter if there is transfer-encoding this header field (looks like the encoding format, can have multiple, comma split), then set the different coded filter checksum content-length header field Internalinputbuffer
To study this class, we can start with the Http11processor process method.
The main function of this class is to get the byte stream from the socket, read the bytes into a buffer buf, and then parse the HTTP request headers and the contents from the buffer individually
Primary field: The Request:request object in which the information parsed from the buffer is written to the request buf: buffer, and the bytes read from the InputStream of the socket are placed in this buffer headers:mimeheaders, Holds the header that appears with a key value pair, that is, removing all headers after the first line of the request message
The specification of the specific HTTP request header can refer to the WWW, or
Http://www.yuanma.org/data/2008/0827/article_3143.htm parserequestline ()
Resolves the first line of the request header, such as: Get Http://class/download.microtool.de:80/somedata.exe, including the request method (get or POST), Protocol (HTTP), URI. After parsing, put in Request Parseheader ()
Resolves the header after the Parserequestline (), because the header after Requestline is a key-value pair separated by ":" So every time this method is executed, a key-value pair is added to the headers and false if the format is wrong EndRequest ()
End the processing of a request, emptying the extra bytes nextrequest ()
Prepares the next request processing, which is used primarily to reset all tag bits and pointers to fill ()
Reading a certain number of bytes from the InputStream of the socket, filling the buf, is useful in many ways. For example, when parsing the header, when it is found that BUF has been read, call fill to repopulate buf, if InputStream has read, fill returns false Internaloutputbuffer some questions
According to InputBuffer's understanding, it can be roughly guessed that this class is used to read information from the response, and then write to the socket in the OutputStream, returned to the client's
Many of the methods inside the class are the same as InputBuffer, but it is surprising that there are nextrequest (), endrequest () methods, And what is done inside is directed at response (OutputBuffer originally had only response), do not see anything related to request. Is the author of the code over the time forgot to change the name of the method.
Finally, think that this class and internalinputbuffer really have so much in common, why not abstract a parent class.