HTTP messages in HTTP messages
The HTTP communication process includes a request from the client to the server and a response from the server to return the client. This article will specifically understand how the request and response work.
3.1 HTTP Messages
The information used for HTTP protocol interaction is called an HTTP message. The request side (client) HTTP message is called the request message, the response side (server side) is called the response message. The HTTP message itself is a string literal consisting of multirow (line break with CR+LF) data.
The HTTP message can be divided into two parts: message header and message subject. The two are separated by a blank line (CR+LF) that initially appears. Usually, there is no need to have a message body.
650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M00/89/64/wKioL1gRxFCh9dcYAABu0TEIBu4783.png-wh_500x0-wm_3 -wmp_4-s_2565922699.png "title=" 03-01.png "alt=" Wkiol1grxfch9dcyaabu0teibu4783.png-wh_50 "/>
3.2 Structure of request message and response message
Structure of request message and response message:
650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M02/89/64/wKioL1gRxiPjMB45AAFaON7rx2Q395.png-wh_500x0-wm_3 -wmp_4-s_2704182927.png "title=" 03-02.png "alt=" Wkiol1grxipjmb45aafaon7rx2q395.png-wh_50 "/>
Examples of request messages and response messages are as follows:
GET / HTTP/1.1 Request Line Host:hackr.jp various header fields user-agent:mozilla/5.0 (windows nt 6.1;xxx) accept:text/html, application/xhtml+xml,application/xml;q=0.9,*/*,q=0.8 accept-langeuage:ja,en-us,q=0.7, en;q=03 accept-encoding:gzip,default dnt:1 connection:keep-alive pragma:no-cache cache-control: No-cache http/1.1 200 ok Status Line Date:Fri ,13 Jul 2012 02:45:26 GMT Server:Apache Last-Modified:Fri ,31 Aug 2007 02:02:20 gmt etag: "45bael-16a-46d776ac" accept-ranges :bytes content-length:362 connection:close Content-Type:text/html various header fields blank lines (CR+LF)
The header content of the request message and the response message is composed of the following data. The various header fields and status codes that appear are now explained in detail.
Request Line: Contains the method used for the request, the request URI, and the HTTP version.
Status line: Contains status codes, reason phrases, and HTTP versions that indicate the structure of the response.
Header field: Contains various headers that represent the various conditions and attributes of a request or response. The general first part is General header, request header, response header and entity header four kinds.
Other: Undefined headers (cookies, etc.) that may contain an HTTP RFC
3.3 Encoding increases transfer rate
HTTP can transmit data in a direct way, but it can also be encoded to increase the transfer rate during transmission. By encoding during transmission, a large number of access requests can be handled efficiently. However, the encoded operation requires the computer to complete, so it consumes more resources such as CPU.
3.3.1 The difference between the message body and the entity body
• Message (message)
is the basic unit of HTTP communication, consisting of 8-bit byte streams (Octet sequence, where Octet is 8 bits), transmitted over HTTP traffic.
• Entities (Entity)
Payload data (supplementary items) that are requested or responded to are transmitted, with the contents of the entity header and the entity body.
The body of an HTTP message is used to transfer the entity body of the request or response.
Typically, the message body equals the entity body . Only when the transfer process is encoded, the entity body content changes, which causes it to differ from the message body.
3.3.2 content encoding for compressed transmissions
When adding attachments to outgoing messages, to make the message less capacity, we will first zip the file and then add the attachment to send. There is a feature called content encoding in the HTTP protocol that can perform similar operations. The content encoding indicates the encoding format applied to the entity content and keeps the entity information compressed as it is. The content-encoded entity is received by the client and is responsible for decoding.
Common content encodings include the following:
· GZIP (GUN Zip)
compress (standard compression of UNIX systems)
deflate (zlib)
identity (not coded)
3.3.3 Partition-transmitted block transfer encoding
In the HTTP communication process, the requested encoding entity resource has not been fully transferred until the browser cannot display the request page. When transferring large volumes of data, by separating the data into chunks, the browser is able to display the page progressively. This function, which blocks the entity body, is called chunked transmission encoding (Chunked Transfer Coding).
chunked transfer encoding divides the entity body into multiple parts (blocks). Each block is marked with hexadecimal, and the last piece of the entity body is marked with "0 (CR+LF)". Entity principals that use chunked transfer encoding are decoded by the receiving client and revert to the entity body before the encoding. There is a mechanism called transfer encoding in http/1.1, which can be transmitted in a coded way during communication, but only defined in the chunked transfer encoding.
650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M02/89/67/wKiom1gR0FfCPSBNAACtEY4wdo4403.png-wh_500x0-wm_3 -wmp_4-s_498878906.png "title=" 03-03.png "alt=" Wkiom1gr0ffcpsbnaactey4wdo4403.png-wh_50 "/>
3.4 Multi-part object collection for sending multiple data
When sending a message, we can write text and add multiple attachments to the message. This is due to the use of the MIME (Multipurpose Internet Mail Extensions, Multipurpose Internet Mail Extension) mechanism, which allows messages to handle many different types of data, such as text, pictures, and video. For example, binary data such as images are indicated by ASCII string encoding, which is the use of mime to describe the tag data type. In the MIME extension, a method called a multipart object collection (Multipart) is used to accommodate multiple copies of different types of data.
Accordingly, the HTTP protocol also adopted a multi-part object collection, sent a message body internal medicine contains a multi-type entity. This is usually used when uploading images or text files.
The multi-part object collection contains the following objects:
Multipart/form-data is used when uploading Web Forms files.
Multipart/byteranges Status Code 206 (partial content, partial contents) The response message contains multiple ranges of content.
multipart/form-data content-type: Multipart/form-data;boundary=aab03x-- content-disposition:form-data;name= "Field1" Joe Blow --AaB03x content-disposition:form-data;name= "Pics"; filename= "File1.txt" content-type:text/ PLAIN       &NBSP, ..... (File1.txt's data) ... --aab03x-- multipart/byteranges HTTP/1.1 206 partial content date:fri ,13 jul 2012 02:45:26 gmt Last-Modified:Fri ,31 Aug 2007 02:02:20 GMT content-type:multipart/byteranges;boundary=this_string_separates --this_string_separates content-type:application/pdf Content-Range:bytes 500-999/8000 ... (range-specified data) ... --this_string_separates content-type:application/ pdf content-range:bytes 7000-7999/8000 &NBSP, ..... (range-specified data) ... --this_string_separates--
When using a multipart object collection in an HTTP message, you need to add Content-type to the header field. The first field of Content-type will be described in detail later.
Use the boundary string to divide the various entities specified by the multipart object collection. Inserts the "---" tag (for example,--aab03x 、--this_string_separates) before the starting line of the individual entities specified by the boundary string, and the last insert "--" tag in the multi-part object audit corresponding string (for example,--aab03x--、 --this_string_separates--) as the end.
Each part type of a multipart object collection can contain a header field. In addition, you can nest a collection of multi-part objects in a section.
3.5 Getting range requests for partial content
Previously, users were unable to access the Internet with this high-speed bandwidth, and it was difficult to download a slightly larger picture or file. If you encounter a network outage during the download process, you must start over, and in order to solve the above problem, you need a recoverable mechanism. The so-called recovery is downloaded only from the previous download outage.
For the range request, the response status code is the 206 Partial content response message. In addition, for multiple ranges of range requests, the response is Content-type in the header field indicating the multipart/bytesranges response message. If the server is unable to respond to a range request, a status code of OK and full entity content is returned.
To implement this feature, you specify the range of entities to download. In this case, the request sent by the specified range is called a range request. When a range request is executed, the header field range is used to specify the byte range of the resource. The specified form of the byte range is as follows:
3.5.1 5001-10 000 bytes
range:bytes=5001-10000
3.5.2 from 5001 bytes after all the
Range:bytes=5001-
3.5.3 from one start to 3000 bytes and 5000-7000 bytes of multiple ranges
range:bytes=-3000,5000-7000
Examples of requests for specific portions of the content are as follows:
650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M01/89/65/wKioL1gR2iHRNb-lAADtZbwbtSc661.png-wh_500x0-wm_3 -wmp_4-s_1011690427.png "title=" 03-04.png "alt=" Wkiol1gr2ihrnb-laadtzbwbtsc661.png-wh_50 "/>
3.6 Content Negotiation Returns the most appropriate content
The same Web site may have multiple pages of the same content, such as English class and Chinese version of the Web page, they are the same content, but the language used is different. When the browser's default language is English or Chinese, a Web page with the same URI will be displayed, with the corresponding English or Chinese version. Such a mechanism is called content negotiation (negotiation).
The content negotiation mechanism refers to the client and server to negotiate the content of the response , and then provides the most appropriate resources for the client . Content negotiation responds to the resource's language, character set, encoding, etc. as a benchmark.
Some of the header fields included in the request message (below) are the benchmarks of the judgment. The meaning of these first fields will be carefully explained in the next chapter:
· Accept
· Accept-charset
· Accept-encoding
· Accept-language
· Content-language
Content negotiation technology is divided into server-driven negotiation, client-driven negotiation and transparent negotiation. Three kinds
3.6.1 Server-driven negotiation (server-driven negotiation)
Content negotiation is performed by the server. The requested header field is automatically processed on the server side as a reference. But for users, the information sent by the browser as the basis for decision, and not necessarily be able to sift out the best content.
3.6.2 client-side driver negotiation (agent-driven negotiation)
Content negotiation is done by the client. The user selects manually from the list of selectable options displayed in the browser. You can also use JavaScript scripts to automatically make these choices on a Web page. For example, depending on the type of OS or browser type, switch to the PC version or mobile page.
3.6.3 Transparent Negotiation (Transparent negotiation)
is a combination of server-driven and client-driven, which is a method of content negotiation by the server and client.
Read the summary of schematic HTTP-Chapter III