Coyote for http11: org. Apache. Coyote. http11

Source: Internet
Author: User
Document directory
  • Overview
  • Http11protocol
  • Http11processor
  • Internalinputbuffer
  • Internaloutputbuffer
Overview

This package supports the http1.1 Protocol and is divided into three types: ARP, NiO, and common HTTP. Here we only make a simple research on the basic HTTP (using Java Io streams instead of NiO streams ).

According to the coyote interface mentioned in the previous article, this package mainly has the following classes:

  • Http11protocol implements the protocolhandler Interface
  • Http11processor, implementing the actionhook Interface
  • Internalinputbuffer implements the inputbuffer Interface
  • Internaloutputbuffer, which implements the outputbuffer Interface
  • Inputfilter and outputfilter interfaces. The specific implementation classes are in org. Apache. Coyote. http11.filters.

The following is the relationship between these classes. I drew a picture randomly and joined it to see ^_^.

The general process is as follows:

  1. Jioendpoint acts as a connection pool and can start multiple socket listeners. Once the browser receives a request, it passes the corresponding socket object to http11connectionhandler through the process method, and then delivers it to http11processor.
  2. Http11processor has an internal internalinputbuffer (not shown in the figure). internalinputbuffer actually processes the byte stream contained in the socket and converts the byte to request.
  3. Request flows through the filter filters, and finally reaches the container that implements the adapter interface. Coyote's work ends here, and continues to process the next socket.

The following describes the functions of several main classes.

Http11protocol

Protocolhandler Implementation of http1.1 Protocol

Mainly include
Http11connectionhandler (internal class)
Jioendpoint
Serversocketfactory (j2se)

The general process is as follows:

In the init method, pass serversocketfactory and http11connectionhandler to jioendpoint for initialization.

Then, in methods such as start and pause, the jioendpoint's start and pause

Jioendpoint can set attributes such as the maximum number of threads, priority, and port. Based on these attributes, jioendpoint generates the corresponding number of serversocketfactory, which is used to listen to the corresponding port. Once an HTTP request is received, jioendpoint transmits the corresponding socket instance to http11connectionhandler. while http11connectionhandler has a processor instance, which actually processes the socket and converts the data into a request object.

Therefore, protocolhandler encapsulates all these connected components, sets their attributes in a unified manner, and controls their lifecycles.

Http11processor

This class is used to generate a request (of course internalinputbuffer in essence) and deliver it to the container that implements the adapter interface.

This class includes several key fields, such as adapter, request, response, inputbuffer, and outputbuffer. The rest are fields related to the HTTP protocol, and there are many methods related to the HTTP protocol, I can't understand the limited level. It is estimated that I have to learn the HTTP protocol in detail before I can understand it. Here I will skip it and look at the most critical process (Socket socket) method.

This method performs the following tasks in sequence:

  1. Associate inputstream and outputstream of socket with inputbuffer and outputbuffer respectively.
  2. The inputbuffer. parserequestline () and inputbuffer. parseheaders () methods are used to parse header fields in the socket byte stream and write them to the request.
  3. Filters are assembled using the preparerequest method to process HTTP message bodies.
  4. Adapter. Service (request, response) sends the generated request and response to the container for processing.
  5. If everything goes smoothly, start to process the next request in the socket (because http1.1 supports persistent connection, so a socket may contain multiple requests), loop back to the first step
  6. If an error occurs, set the response code of response and terminate the loop.

The preparerequest method is used to prepare the inputbuffer filter. Here, we will write it briefly. For more information about the filter mechanism, see:

  1. Based on the previous parsing of the HTTP header fields, check protocol, method, CT, User-Agent, and mimeheaders, And the URI format (whether it complies with: Protocol: // host: port/format)
  2. Prepare to load filter
  3. If the header field transfer-encoding appears to be in the encoding format, multiple fields can be separated by commas.
  4. Check the Content-Length header field
Internalinputbuffer

To study this class, we can start with the process method of http11processor.

The main function of this class is to get byte streams from the socket, read the byte into a buffer Buf, and then parse the HTTP request headers and content one by one from the buffer.

Main fields:

  • Request: The request object. The information parsed from the buffer is written into the request.
  • Buf: buffer zone. The Bytes read from the inputstream of the socket are placed in this buffer zone.
  • Headers: mimeheaders, which stores the headers that appear as key-value pairs, that is, all headers after the first line of the Request Message

For specific HTTP Request Header specifications, refer to W3C or
Http://www.yuanma.org/data/2008/0827/article_3143.htm

Parserequestline ()

Parse 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), and Uri. After resolution, put it in the request

Parseheader ()

Parse the header after parserequestline (). Because the headers after requestline are all key-value pairs separated by ":", this method is executed every time, add a key-value pair to headers. If the format is incorrect, false is returned.

Endrequest ()

End the processing of a request and clear the extra bytes.

Nextrequest ()

Prepare the processing of the next request. This method is mainly used to reset all the mark bits and pointers.

Fill ()

Reading a certain number of bytes from the inputstream of the socket and filling the Buf are useful in many methods. For example, when parsing the header and finding that the Buf has been read, call fill to re-fill the Buf. If inputstream has been read, fill returns false

Internaloutputbuffer

According to the understanding of inputbuffer, We can roughly guess that this class is used to read information from response, and then write it into the outputstream of socket, and return it to the client

Many methods in the class are the same as those in inputbuffer, But It is puzzling that there are actually nextrequest () and endrequest () methods, what is done inside is for response (outputbuffer only has response), and nothing is related to the request. Did the author forget to change the method name when copying the code?

Finally, I think there are too many similarities between this class and internalinputbuffer. Why not abstract a parent class?

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.