"Illustrated HTTP"-Book Sharing (3)

Posted Jun 5, 20204 min read


Chapter 3:HTTP information in HTTP messages

3.1 HTTP message

  • The HTTP communication process includes the request sent from the client to the server, and the response returned from the server to the client
  • The information exchanged by the user HTTP protocol is called HTTP message, which is essentially a string text composed of multiple lines of data
  • The HTTP message of the requesting end(client) is called request message
  • The HTTP message on the response side(server side) is called Response Message
  • HTTP messages are mainly divided into message header, message body

3.2 The structure of request message and response message

  • The header content of the request message and response message has the following structure:
  • Request line:contains the method used for the request, the request URI and the HTTP version
  • Status line:Contains the status code, reason phrase and HTTP version indicating the result of the response
  • Header field:all kinds of headers containing various conditions and attributes of representation and response, generally there are 4 kinds of headers, general header, request header, response header, entity header
  • Other:may contain headers(cookies, etc.) that are not defined in the HTTP RFC

3.3 Encoding to increase transmission rate

3.3.1 Differences between message body and entity body
  • Message:It is the basic unit of HTTP communication protocol, which is composed of 8-byte byte stream, and is transmitted through HTTP communication
  • Entity:The payload data is transmitted as request and response, and its content is composed of entity header and entity body
  • Generally, the message body is equal to the entity body, and the HTTP message body is used to transmit the request or response entity body. Only when the encoding operation is carried out in the transmission and the content of the entity body changes, will it cause a difference between him and the message body
3.3.2 Content encoding for compressed transmission
  • Function:HTTP content encoding, similar to the function of text zip compression
  • Content encoding indicates the encoding format applied to the physical content and keeps the entity information compressed as it is. The encoded entity is received by the client and decoded
  • Common encoding formats:
  • gzip(Gun zip)
  • compress(Unix system standard format)
  • deflate(zlib)
  • identity(no encoding)
3.3.3 Block transmission coding for split transmission
  • Scenario:In HTTP communication, the browser cannot display the requested page before the requested encoding entity resources have been completely transmitted
  • Block transmission coding:the function of blocking the entity body

3.4 Multipart object collection for sending multiple data

  • Scenario:Similar to when sending emails, various data such as text, video, audio, photos, etc. can be added to the attachment
  • The HTTP protocol also adopts a multi-part object collection, and the body of a message sent can contain multiple types of entities, usually used when uploading pictures and text.
  • The multi-part object collection contains the following objects:
  • multipart/form-data user web form upload
  • multipart/byterange status code 206, used when the response message contains multiple ranges of content
  • HTTP message uses multi-part object combination, you need to add Content-type in the first field

3.5 Scope request to get some content

  • Scenario:When the speed of the network is not good or the picture is too large, the network is interrupted when downloading the picture. In order to avoid downloading from the beginning again, it is necessary to be able to resume the download function from the previous download interruption
  • Scope request:request sent in the specified range
  • Example:For example, a resource with a size of 10000 bytes, using range request, you can only request resources of 5001-10000 subsections
  • When performing range lookup, the header field Range is used to specify the byte range of the resource
  • For return search, the response will return a response message with status code 206 Partial Content

3.6 Content negotiation Return the most suitable content

  • Example:For example, the same website may have multiple copies of the same static page(Chinese version, English version). When the default language of the browser is Chinese, the Chinese page will be returned; if it is English, the English page will be returned.
  • Concept:The content negotiation mechanism means that the client and the server negotiate on the content of the response resource, and then provide the most suitable resource to the client.
  • The content negotiation will use the response resource language, character set, encoding method, etc. as the judgment criteria
  • Some header fields included in the request message can be used as judgment criteria
  • Accept
  • Accept-Charset
  • Accept-Encoding
  • Accept-Language
  • Content-Language
  • Content negotiation technology is divided into three types
  • Server-driven negotiation:automatic processing on the server side
  • Client driver negotiation:for example, js automatically switches the PC or mobile version
  • Transparent negotiation:a combination of the two