http protocols Easy --- reprint

Started http protocol

##(一)、 HTTP/0.9

HTTP application layer protocol is based on TCP / IP protocol. It does not relate to data packet (Packet) transmissions, the main communication format predetermined between the client and the server, using the default port 80.

The earliest version of the 1991 release of version 0.9. This version is extremely simple, only one command GET.

GET /index.html

The above command, said after the TCP connection (connection) is established, the client request to a server (request) page index.html.

Agreement, the server can only respond HTML-formatted string, can not respond to other formats.

<html>
  <body>Hello World</body>
</html>

The server sends finished, it closes the TCP connection.

(二)、http/1.0

2.1 Introduction

In May 1996, HTTP / 1.0 release, content greatly increased.

First, the content in any format can be sent. This makes the Internet can not only transfer the text, but also to transfer images, video, binary files. This laid the foundation for the development of the Internet.

Secondly, in addition to the GET command, also introduced HEAD POST commands and command, rich interactive means of the browser and the server.

Again, HTTP request and response format has changed. In addition to the data portion, each communication must include header information (HTTP header), is used to describe some metadata.

Other new features include a status code (status code), multi-character set support, multi-part send (multi-part type), permission (authorization), cache (cache), content encoding (content encoding) and so on.

2.2 Request format

The following is an example of a version 1.0 HTTP request.

GET / HTTP/1.0
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5)
Accept: */*

We can see, the format and version 0.9 has greatly changed.

The first row is a request command, you must add the protocol version (HTTP / 1.0) at the tail. Is behind the multi-line headers, describe the situation of the client.

2.3 response format

To respond to the server as follows.

HTTP/1.0 200 OK 
Content-Type: text/plain
Content-Length: 137582
Expires: Thu, 05 Dec 1997 16:00:00 GMT
Last-Modified: Wed, 5 August 1996 15:55:28 GMT
Server: Apache 0.84

<html>
  <body>Hello World</body>
</html>

The response format is "header + a blank line (\ r \ n) + data." Wherein the first line is "+ protocol version status code (status code) + State Description."

2.4 Content-Type field

About character encoding, version 1.0 provides header information must be ASCII code, the following data can be in any format. Therefore, the server response time, you must tell the client what format the data is, this is the role Content-Type field.

The following are some of the common value of Content-Type field.

text/plain
text/html
text/css
image/jpeg
image/png
image/svg+xml
audio/mp4
video/mp4
application/javascript
application/pdf
application/zip
application/atom+xml

These data types are collectively referred to MIME type, and each value comprises two types of a type, separated by a slash between.

In addition to the predefined types, manufacturers can also custom type.

application/vnd.debian.binary-package

The above shows that the type of transmission is Debianbinary data packet system.

MIME typeYou can also use a semicolon at the end, add parameters.

Content-Type: text/html; charset=utf-8

The above type show that the page transmission, and coding is UTF-8.

When a client requests, you can use Accepta field declaration which data formats they can accept.

Accept: */*

The above code, the client can accept data declared themselves in any format.

MIME typeNot only in HTTPagreement, it can also be used in other places, such as HTMLweb pages.

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<!-- 等同于 -->
<meta charset="utf-8" /> 

2.5 Content-Encoding field

Because the data transmitted may be in any format, data can be compressed before transmission. Content-Encoding field specifies the data compression method.

Content-Encoding: gzip
Content-Encoding: compress
Content-Encoding: deflate

When the client request, with Accept-Encodingfield specifies the compression method which can accept its own.

Accept-Encoding: gzip, deflate

2.6 shortcomings

The main disadvantage of HTTP / 1.0 version, each TCP connection can send a request. Sending data is completed, the connection is closed, even if the request for additional resources, it is necessary to re-create a new connection.

New TCP connection cost is high because it requires the client and server three-way handshake, and at the beginning of the transmission rate slow (slow start). So, HTTP 1.0 version of the relatively poor performance. As more and more external resources page load, this problem is increasingly prominent.

To solve this problem, some browsers when requested, with a non-standard Connection field.

Connection: keep-alive

This field requires the server not close the TCP connection multiplexing for other requests. The same server respond to this field.

Connection: keep-alive

A TCP connection multiplexing can be established on until the client or the server closes a connection. However, this is not a standard field, to achieve different behavior may be inconsistent, and therefore not a fundamental solution.

(三) http/1.1

In January 1997, HTTP / 1.1 release, only six months later than version 1.0. It further improved the HTTP protocol, has been used today, 20 years later, it is now the most popular version.

3.1 persistent connection

The biggest change in version 1.1 is the introduction of persistent connections (persistent connection), that is not the default TCP connection is closed, multiple requests can be multiplexed without declarationConnection: keep-alive。

Client and server activity not find each other for some time, you can take the initiative to close the connection. However, the standard practice is that the client at the time of the last request, send Connection: close, explicitly requires the server closes the TCP connection.

Connection: close

At present, for the same domain name, most browsers allow simultaneous establishment of six persistent connections.

3.2 pipeline mechanism

1.1 also introduces pipeline mechanism (PIPELINING), i.e. a TCP connection with which, the client may send multiple requests at the same time. This will further improve the efficiency of the HTTP protocol.

For example, the client requires two resource requests. Previous practice, in which a TCP connection with the first A request to send, and then wait for the server to respond, and then received a request issued B. Pipeline mechanism is to allow the browser requests A and B also issued a request, but the server is still in the order, to respond to requests A, B after the completion of the request response.

3.3 Content-Length field

A TCP connection can now transmit multiple responses, we should certainly have a mechanism to distinguish between packet which is a response belongs. This is the role of Content-length data length field, declare this response.

Content-Length: 3495

The above code tells the browser, the response of this length is 3495 bytes, the following byte belongs to the next response.

In version 1.0, the Content-Lengthfield is not necessary, because the browser discovery server closes TCP connection, it indicates that the received data packet has it all.

3.4 chunked transfer encoding

Use Content-Length field is a prerequisite, before the server sends a response, you must know the data length of the response.

For some dynamic operation is very time-consuming, this means that the server until all operations complete before sending data, it is clear this is not efficient. A better approach is to generate a data, a transmission, a "stream mode" (Stream) substituted "cache mode" (buffer).

Therefore, version 1.1 without using a predetermined Content-Lengthfield, the use of "chunked transfer encoding" ( chunked transfer encoding). As long as the request or response header has Transfer-Encodingfields to indicate the number of pending responses by the data blocks.

Transfer-Encoding: chunked

Non-empty before each block of data, there will be a hexadecimal value representing the length of this block. Finally, a block size of 0, it represents a response to this data transmission over. Below is an example.

HTTP/1.1 200 OK
Content-Type: text/plain
Transfer-Encoding: chunked

25
This is the data in the first chunk

1C
and this is the second one

3
con

8
sequence

0

3.5 Other features

Version 1.1 also adds a number of verbs method:PUT、PATCH、HEAD、 OPTIONS、DELETE。

Further, the header information of the new client requests the Host field, for the domain name of the server.

Host: www.example.com

With the Host field, you can request to be sent to different sites on the same server, laid the foundation for the emergence of virtual hosts.

3.6 shortcomings

Although multiplex 1.1 allows connection TCP, but which is connected with a TCP, all data communications are carried out in sequence. Server only processed a response before the next response. If the response is especially slow in front, behind there will be many requests waiting in line. This is known as "head of line blockage" (Head-of-line blocking).

To avoid this problem, only two ways: one is to reduce the number of requests, while the second is more open persistent connection. This led to a lot of web optimization techniques, such as merging scripts and stylesheets, the picture is embedded CSS code, domain fragment (domain sharding) and so on. If the HTTP protocol designed to be better, this extra work can be avoided.

(Four), SPDY protocol

In 2009, Google disclosed a self-developed SPDY protocol, mainly to solve the HTTP / 1.1 efficiency is not high.

After this agreement proves feasible Chrome browser, it was used as the basis of HTTP / 2, the main features are among the HTTP / 2 to be inherited.

五、HTTP/2

2015, HTTP / 2 release. It is not called HTTP / 2.0, because the Commission does not intend to publish the standard version of the child, the next version will be a new HTTP / 3.

5.1 binary protocol

HTTP / 1.1 version of the header information must be text (ASCII encoding), the data may be a text, it can be binary. HTTP / 2 is a complete binary protocol header information and body data are binary, and collectively referred to as "frame" (frame): frame header information and a data frame.

One benefit of binary protocol is that you can define additional frames. HTTP / 2 defines about a dozen frame, lay the foundation for future advanced applications. If you use text to achieve this functionality, parsing the data will become very troublesome, binary parsing is much more convenient.

More than 5.2 workers

HTTP / 2 TCP connection multiplexing, in a connection, the client and the browser can simultaneously send multiple requests or respond to, and do not follow the order of one to one, thus avoiding the "team head clogging."

By way of example, in which a TCP connection, the server request while A and B received a request, so the first request response A, and found that the process is very time consuming, so it transmits part A request has been handled well, and then respond to the request B, after completion, a retransmission request rest.

Such two-way, real-time communication, is called multiplexing ( Multiplexing).

5.3 Data Flow

Because the packet HTTP / 2 is not transmitted in order, which is connected with a consecutive packets, it may belong to different responses. Therefore, it is necessary to mark packets, it pointed out that it belongs to respond.

HTTP / 2 all packets each request or response, referred to as a data stream (stream). Each data stream has a unique number. When the packet transmission, the data stream must be marked ID, used to differentiate the data streams to which it belongs. It also established, the client sends the data stream, ID, always an odd number, the server issues, ID, an even number.

Transmitting the data stream to the half of the time, the client and the server can send a signal (RST_STREAM frame), to cancel the data stream. The only way to cancel the data stream version 1.1, is to close the TCP connection. That is, HTTP / 2 can be canceled once a request, the TCP connection while still open, can be used by other requests.

The client may also specify the priority of the data stream. The higher the priority, the server will respond sooner.

5.4 Information Compression

HTTP protocol with no state, each request must include all of the information. So, many of the requested fields are repeated, such as Cookie and User Agent, exactly the same content, each request must be accompanied by, this will waste a lot of bandwidth, but also affect the speed.

HTTP / 2 on this point is optimized, the introduction of a header compression mechanism (header compression). On the one hand, header compression using gzip or compress before sending; on the other hand, the client and the server at the same time maintaining a table header, all fields will be stored in this table, to generate an index number, the future does not send the same field only to send the index number, thus improving the speed.

5.5 server push

HTTP / 2 server allows unsolicited, take the initiative to send resources to the client, which is called server push ( server push).

Common scenario is a client requests a Web page, the page which contains a lot of static resources. Under normal circumstances, the client must receive pages, parse the HTML source code and found that static resource, and then issued a static resource requests. In fact, you can expect the server to the client requests after the page is likely to again request a static resource, so we take the initiative to put these pages together with static resources to the client a.

Address reprint ------- http://www.ruanyifeng.com/blog/2016/08/http.html

Guess you like

Origin www.cnblogs.com/chenyameng/p/12192368.html