Internalinputbuffer processes HTTP Request lines-Tomcat source code-How far can we go series (11) Tomcat stringmanager read and learn

Source: Internet
Author: User
How far can we go series (11)

Nonsense:

Is the recent market not good? Less job hopping, haha. Some people have been resisting job hopping and think there are many drawbacks: What business accumulation, jobs, and others think you are unreliable. I thought: I have a limited time in my life. Why should I give up the opportunity to pass through the forest for a tree that can take advantage of the coolness? Wish you a good friend on the job-hopping journey! (PS: I like the excitement of the interview)

The coolest thing about lying down, the most beautiful sunset. The sunset in autumn is the most gorgeous in a year. Don't miss it.

Subject:

In tomcat, an HTTP request is sent to the http11processor class, and the incoming socket is processed by the process (socket thesocket) of this class. The socket contains the HTTP message.

Tomcat is how to call the process method to http11processor, you can refer to: http://blog.csdn.net/woorh/article/details/8017323

Http11processor is in the org. Apache. Coyote. http11 package.

In the rocess method of http11processor, inputbuffer. parserequestline (); is used to call the request line for parsing the HTTP message. Inputbuffer is the tomcat-defined internalinputbuffer.

You need to know:

1. org. Apache. Coyote. Request is a data structure used internally by Tomcat to store request messages.

2. org. Apache. tomcat. util. Buf. messagebytes is used to store messages. A large number of parsed byte characters are stored in org. Apache. Coyote. Request.

3, org. apache. tomcat. util. buf. bytechunk is the data structure used to store data. It stores byte [], org. apache. tomcat. util. buf. messagebytes uses it.

Big process:

The HTTP message is parsed through inputbuffer and put into the request. The request puts it in the corresponding messagebytes, and finally stores it in bytechunk.

You can call the preceding methods to complete the process.

The main concern is the resolution.Source code, View the sourceCodeBefore you need to understand the structure of the HTTP request line: can refer to: http://www.cnblogs.com/killbug/archive/2012/10/10/2719142.html

Prerequisites:

1. When all exceptions in the method are found, the getstring method is called.StringmanagerThis is a unified log management method in Tomcat. The detailed explanation has elapsed since the previous article:Tomcat stringmanager

2,

Escape characters Meaning ASCII value (decimal)
\ Bell (BEL) 007
\ B Return to BS to move the current position to the previous column. 008
\ F Change page (ff), move the current position to the beginning of the next page 012
\ N Line feed (LF), move the current position to the beginning of the next line 010
\ R Press enter (CR) to move the current position to the beginning of the line 013
\ T Horizontal tabulation (HT) (jump to the next tab) 009
\ V Vertical tabulation (VT) 011
\\ Represents a backslash character ''\' 092

Source code:

 Public   Void Parserequestline ()  Throws  Ioexception {  Int Start = 0 ;  //          //  Skipping blank lines  //  Ignore empty rows  //          Byte CHR = 0 ;  Do  { //  Read new bytes if needed              If (Pos> = Lastvalid ){  If (! Fill ())  Throw   New Eofexception (Sm. getstring ("IIB. EOF. error" );} Chr = Buf [POS ++ ];}  While (CHR = constants. cr) | (CHR = Constants. Lf); POS --;  //  Mark the Current Buffer position Start = Pos;  //          //  Reading the method name  //  Method Name is always US-ASCII  //          //  Space is similar to the switch. If it is set to false, check the content. If it is set to true, remove the blank line time.          Boolean Space = False ;  While (! Space ){  //  Read new bytes if needed              If (Pos> = Lastvalid ){  If (! Fill ())  Throw   New Eofexception (Sm. getstring ("IIB. EOF. error" ));}  // Spec says no Cr or lf in method name              If (BUF [POS] = constants. Cr | Buf [POS] = Constants. Lf ){  Throw   New  Illegalargumentexception (Sm. getstring ( "IIB. invalidmethod" ));}  //  Spec says single SP but it also says be tolerant of HT  //  When the first space is found, the tab is allowed.              If (BUF [POS] = constants. SP | Buf [POS] = Constants. HT) {Space = True ; //  Skip Loop  //  Directory The following mark. The method () here gets a requast messagebytes: methodmb Request. Method (). setbytes (BUF, start, pos- Start);} POS ++ ;}  //  Spec says single SP but also says be tolerant of multiple and/or HT  // Ignore spaces or tabs after spaces. Start is not required because the content is ignored.          While  (Space ){  //  Read new bytes if needed              If (Pos> = Lastvalid ){  If (! Fill ())  Throw   New Eofexception (Sm. getstring ("IIB. EOF. error" ));}  If (BUF [POS] = constants. SP | Buf [POS] = Constants. HT) {pos ++; //  The ignore method is to move the subscript. } Else  {Space = False  ;}}  //  Mark the Current Buffer position Start = Pos; //  Start appears. The subscript must be recorded later.          Int End = 0;  Int Questionpos =-1 ;  //          //  Reading the URI  //  The above is the source code comment. What is the Uri? You know  //          Boolean EOL = False  ;  While (! Space ){  // Read new bytes if needed              If (Pos> = Lastvalid ){  If (! Fill ())  Throw   New Eofexception (Sm. getstring ("IIB. EOF. error" ));}  //  Spec says single SP but it also says be tolerant of HT  //  Look for the second space. The first space and the second space are the legendary URI.              If (BUF [POS] = constants. SP | Buf [POS] = Constants. HT) {Space = True  ; End = Pos ;}  Else   If (BUF [POS] = Constants. cr) | (BUF [POS] = Constants. Lf )){  //  HTTP/0.9 style request  //  To be compatible with HTTP/0.9 format EOL = True  ; Space = True  ; End = Pos ;}  Else   If (BUF [POS] = constants. Question) //  '? ' & (Questionpos =-1 )){  //  Record the question mark position first Questionpos =Pos;} POS ++ ;}  //  Record the start and expires bits of Uris that may contain question marks. Request. unparseduri (). setbytes (BUF, start, end- Start );  If (Questionpos> = 0 ){ //  Question mark  //  Question mark position record Request. querystring (). setbytes (BUF, questionpos + 1 , End -Questionpos-1);  //  Record the URI Request. requesturi (). setbytes (BUF, start, questionpos- Start );}  Else  {Request. requesturi (). setbytes (BUF, start, end - Start );}  //  Spec says single SP but also says be tolerant of multiple and/or HT  //  This is a duplicate code, that is, the space and tab are ignored.          While (Space ){  //  Read new bytes if needed              If (Pos> = Lastvalid ){  If (! Fill ())  Throw   New Eofexception (Sm. getstring ("IIB. EOF. error" ));}  If (BUF [POS] = constants. SP | Buf [POS] = Constants. HT) {pos ++;}  Else  {Space = False  ;}}  //  Mark the Current Buffer position Start = Pos; End = 0 ;  //          //  Reading the Protocol  //  Protocol is always US-ASCII //          //  The EOL flag is used to identify whether the request is an HTTP/0.9 style request.  //  Last: Protocol (HTTP/1.1 or 1.0)          While (! EOL ){  //  Read new bytes if needed              If (Pos> = Lastvalid ){  If (! Fill ())  Throw  New Eofexception (Sm. getstring ("IIB. EOF. error" ));}  //  Locate/R/N (CRLF)              If (BUF [POS] = Constants. cr) {end = Pos ;}  Else   If (BUF [POS] = Constants. Lf ){  If (END = 0 ) End =Pos; EOL = True  ;} POS ++ ;}  //  At this point, the head is divided into three parts and put in the messagebytes defined by the request.          If (End-Start)> 0 ) {Request. Protocol (). setbytes (BUF, start, end - Start );}  Else  {Request. Protocol (). setstring ( "" );}} 

Summary:

1. The expression bit is used to control the function of each while expression. The above Code usesSpace

2. You do not need to truncate the required content, but the subscript of the record's start and end.

 

 

Let's move on

 

----------------------------------------------------------------------

 

Hard work may fail, but not hard work will certainly fail.
Sharing

 

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.