Will be the front end must know the information in the HTTP request http series (three) HTTP packets

http packets

Information for the HTTP protocol interaction is known as HTTP packets. The requester is called http request message packet, the responder is called the response packet, HTTP message itself multiple lines of text data consisting of a string.

http packets can be divided into message header and message body is two, two main messages. Both from the initial rental. Blank lines to divide normally do not necessarily have a message body.

Request packet and response packet structure

We look at the request and response message packet structure. Request packet and response packet header contents by the following data. Now all kinds of header fields and a status code that appears will explain later.

Example request Xinlang

Request header
Response header

Request line

The method comprises the request for the request URL and HTTP request

State line

The results show that the response contains a status code, reason phrase and HTTP version

Header field

Comprising said conditions and properties of various types of request and response header
typically has four first portions are generic header, the request header, the response header, entity daemon

other

It can contain the HTTP RFC, which is not defined in the header

Coding to enhance the transmission efficiency

It can be transmitted directly in accordance with the original HTTP data during data transmission, but may be. In the transmission process, improve the transmission efficiency by encoding. Is coded by the transmission, to effectively handle a large number of access requests, the encoding operation requires the computer to complete, and therefore consumes more CPU resources

Difference message subject and body of the entity

  • Packet
    is the basic unit of HTTP communication, by throttling octet. Composition transmitted via HTTP communication
  • Entity
    as a request or response payload data is transmitted, the contents of the topic entities daemon bulk composition and
    body of an HTTP entity body for a transmission request or response.
    The message body is generally equal to the entity body. Only transmission operation to encode the same day, the contents of the entity subject to change, and that led to his message body is a difference in
    the packet and entities after two terms often appear in advance understand the difference between the two

Compressed content coded transmission

Like the message to be sent is increased attachment, in order to make smaller the capacity of the message, we will first add a zip file attachments after sending
the HTTP protocol is called a functional encoded contents, a similar operation can be performed, content encoding application of the encoding format specified in the content of an entity, and entity information as to maintain the compression, the entity receiving the content encoded by a client and is responsible for decoding

Commonly used are the following content encoding

  • gzip
  • comperss (compression standard UNIX systems)
  • deflate(zlib)
  • identity (not coded)

Dividing chunked transfer encoding transmitted

In HTTP traffic, the encoder Not all physical resource requested transfer is complete before the browser requests a page can not be displayed, when transferring large amounts of data through the data is divided into a plurality of blocks can be gradual so that the browser displays the page
that the entity function block called chunked transfer encoding

Chunked transfer encoding will entity body into a plurality of sections, each one will be labeled with a hexadecimal block size, and the last one will use the entity body "0 (CR + LF)" marked
entities relating chunked transfer encoding , the client will be received, responsible for decoding, to recover before the coding entity body
HTTP1.1 present transmission mechanism called coding (transfer coding), he may be transmitted by some coding scheme at the time of communication, but specifies for more than one in the chunked transfer encoding

Transmitting a plurality of data object set multipart

When sending a message, we can write the text in the mail and add multiple attachments. This is because the use of MIME (Multipurpose Internet Mail Extensions, Multipurpose Internet Mail Extensions) mechanism. It allows many different types of mail processing text, images, video and other data. For example, binary data such as images to an ASCII string specified coding method, is the use of MIME types described tag data. In the MIME extensions use a method called a multi-part set of objects (Multipart), and to accommodate multiple different types of data.
Corresponding HTTP protocol is also adopted a multi-part set of objects, a transmission packet body may contain many types of entities. It is typically used when a picture or text files to upload.
Multi-target part of the object included in the following set.

  • multipart / form-data
    is used when uploading a web form.
  • multipart / byteranges
    status code response message 206 contains the contents of a plurality of ranges used.
    Using multi-part objects in HTTP packets need to add Content-type field in the header inside the set time. This is reluctant to know about, we explain later
    use boundary string to divide the multi-part collection of objects of various types of instruction entities, inserted before the start of each entity boundary line specified string "-" mark (eg: - AaB03x , - THIS_STRING_SEPARATES) in a multi-part set of objects corresponding to the last character string, into the "-" mark as the end
    of each part of a multi-part type collection of objects may contain header fields, may additionally embedded in a portion sets, multi-car part of the object.

Range request content acquisition section

Previously, users can now use this high-speed bandwidth access to the Internet, then download a size slightly larger pictures or documents had been very hard for. If a download encountered during the network interruption. It must re-start, in order to solve the above problem, a need for a mechanism to restore the so-called recovery is the recovery interrupted downloads from the download before.
Realize this function need to specify the download range entity. Thus, the specified range of the transmission request is called range request (Range Request).
A resource size of 10000 bytes, if requested scope, 5001 to 10000 bytes of the requested resources.
The implementation of a range request header field will be used to specify byte range Rang resources. a byte range specified as follows:

  • 5001-10000 bytes
Range: bytes=5001-10000
复制代码
  • After all 5001 bytes from
Range: bytes=5001-
复制代码
  • From a beginning to Multiple Range 5000-7000 3000 bytes and bytes
Range: bytes=0-3000, 5000-700
复制代码

For a range request, the response will return a status code of 206 Partial Content response packet. Further, the range for the range of multiple requests, responses will return a response indicating the multipart / byteranges in the Content-Type header field of the packet.
If the server can not respond to a range request, a status code 200 ok complete and substantial contents is returned.

Content negotiation returns the most appropriate content

With a web site that there may be multiple copies of the same content page. Such as English and Chinese versions of web pages, their contents although the same, but the language used is different.
Time when the default browser language is English or Chinese, the RUI when accessing the same web page, the corresponding English or Chinese version of the web page is displayed. Such a mechanism is called content negotiation.
Content negotiation mechanism refers to resources on the client and server to negotiate a response, and then provided to the client the most appropriate resource. Content negotiation in the language, character set encoding as the benchmark to determine the response of resources.
Contains some header fields in the request packet is the determination reference. Detailed descriptions of these header fields, please refer to the next section

  • Accept
  • Accept-Charset
  • Accept-Encoding
  • Accept-Language
  • Content-Language

Content negotiation technology has the following three types.
Server negotiation (Server-driven Negotiation)
content negotiation by the server. In request header field as a reference, at the end of the automatic processing server. But for users, the information sent by the browser as the basis for decision, does not necessarily filter out the best content.
Client-driven negotiation (Agent-driven Negotiation)
have client content negotiation mode. The user manually select from a list of available browser real options. Open the selection can be made automatically on the web page can use JavaScript. For example, by OS type have to type or browser, switch to the PC version of the page itself or Mobile version.
Transparent negotiation (Transparent Negotiation)
is the server and the client-driven drive combination, by the server and client are each a method for content negotiation.

Front-end technology writer brother, reproduced declare

The new front-end technology exchange group of people called front-end technology, there Node.js / Vue.js / React.js / React-Native.js / micro-channel technology applet communication problems. Welcome! Group number: 426 334 209

Click on the link to join a group chat [front-end technology exchange group] qm.qq.com/cgi-bin/qm/...

Guess you like

Origin juejin.im/post/5ce94e5ff265da1b6d4000e1