The interviewer asked me: How many a TCP connection can send HTTP requests? I did not answer

There was once a classic face questions: from the URL in the browser is input to the page to show what happened in the process?

I believe most of the students can answer ready for it, but if you continue to ask: HTML received if the label contains dozens of pictures, these pictures are what way, in what order, how many connections established, what protocols are downloaded it?

To get to know the problem, we need to solve the following five questions:

  1. Modern browser and server to establish whether the disconnection will be completed in a HTTP request after a TCP connection? Will be disconnected under what circumstances?

  2. A TCP connection can correspond to several HTTP requests?

  3. A TCP connection transmitting an HTTP request can be sent with it (such as a request made with three, then three responses received together)?

  4. Why sometimes refresh the page do not need to re-establish an SSL connection?

  5. Host of the same browser establishes a TCP connection there is no limit to the number?

first question

Modern browser and server to establish whether the disconnection will be completed in a HTTP request after a TCP connection? Will be disconnected under what circumstances?

In HTTP / 1.0 in a server After sending a HTTP response, disconnects the TCP connection. But this every request to re-establish a TCP connection and disconnection, the cost is too large. Therefore, although there is no set standard, some server Connection: keep-alive were the Header support. This means that, after the completion of the HTTP request, do not disconnect the HTTP request using TCP connections. The advantage is that the connection can be reused time after sending HTTP requests do not need to re-establish a TCP connection, and if we maintain the connection, also avoid the overhead of SSL, two pictures of my short two visits https: / /www.github.com time statistics:

The first time access, and SSL connection initialization overhead

 

SSL connection initialization and overhead goes away, using the same TCP connection

Persistent connections: Since so many benefits to maintain TCP connections, HTTP / 1.1 Connection head put written standards, and persistent connections enabled by default, unless stated in the request Connection: close, then between the browser and the server will continue for some time TCP connection, not a request to cut off the end.

So the answer to the first question is: to establish default TCP connection is not disconnected, only the request header declaration Connection: close will close the connection after the request is complete.

second question

A TCP connection can correspond to several HTTP requests?

Knowing the first question, in fact, this question has been answered, if we maintain the connection, a TCP connection can send multiple HTTP requests.

The third question

A TCP connection transmitting an HTTP request can be sent with it (such as a request made with three, then three responses received together)?

HTTP / 1.1 there is a problem, a single TCP connection at the same time can handle only one request, meaning that: the life cycle of the two requests can not overlap, any two HTTP requests to the end of time can not start from the same TCP connection in overlapping.

While HTTP / 1.1 specification defines Pipelining to try to solve this problem, but this feature is off by default in the browser.

Pipelining first look at what is specified in the RFC 2616:

A client that supports persistent connections MAY "pipeline" its requests (ie, send multiple requests without waiting for each response). A server MUST send its responses to those requests in the same order that the requests were received. A support persistent connections of customers end may send multiple requests on a connection (need not wait for a response to any request). Server receives a request must be sent in response to the request in the order received.

As for why such a standard is set, we can probably guess one reason: because the HTTP / 1.1 protocol is text, and returns the contents also can not distinguish corresponds to which to send the request, the order must remain the same. For example, you two requests to the server  and the server returns the two results, the browser based on the response result is no way to determine which of a response corresponding to the request.GET/query?q=AGET/query?q=B

Pipelining this idea looks nice, but there will be many problems in practice:

  • Some proxy servers can not correctly handle HTTP Pipelining.

  • Correct pipeline implementation is complex.

  • Head-of-line Blocking blocking connector: after establishing a TCP connection, assuming continuous transmission of several client request to the server in this connection. As standard, the server should return the results in the order of receipt of the request, assuming that the server spend a lot of time when processing the first request, all subsequent requests need to wait for the end of the first request to respond.

So modern browser default is not open HTTP Pipelining is.

However, provided Multiplexing HTTP2 multiplex transmission characteristic, a plurality of HTTP requests can be completed simultaneously in a TCP connection. As for how to achieve specific Multiplexing is another question. We can look at the use of HTTP2 effect.

 

Green is a request to initiate a request for the return of the waiting time, download time response is blue, you can see all the same in a Connection, done in parallel

So the answer to this question has also been: the presence in HTTP / 1.1 Pipelining technology can simultaneously send multiple requests to complete this, but because the browser is off by default, so you can think that this is not feasible. In HTTP2 Multiplexing characteristics due to the presence, a plurality of HTTP requests may be performed in parallel on the same TCP connection.

So in HTTP / 1.1 era, the browser is how to improve page load efficiency of it? There are the following two points:

  1. And maintain the server TCP connection has been established, the same connection sequence processing a plurality of requests.

  2. And server to establish multiple TCP connections.

The fourth question

Why sometimes refresh the page do not need to re-establish an SSL connection?

In discussing the first question already have the answer, TCP connections can sometimes be maintained for some time the browser and the server. Do not need to re-establish TCP, SSL will naturally use before.

The fifth question

Host of the same browser establishes a TCP connection there is no limit to the number?

Suppose we are still in HTTP / 1.1 era, when there were no multiplexes, when the browser to get a few pictures of the web page how to do it? Certainly can not just open a TCP connection to download the order, as users would like very hard to accept, but if each picture will open a TCP connection to send HTTP requests that computer or server could not stand, and if there are 1000 pictures of words can not open 1000 TCP connection, and your computer will not necessarily agree with NAT agree.

So the answer is: Yes. Chrome allows for up to six TCP connection to the same Host. Different browsers there are some differences.

https://developers.google.com/web/tools/chrome-devtools/network/issues#queued-or-stalled-requestsdevelopers.google.com

So back to the question of the beginning, HTML received if the label contains dozens of pictures, these pictures are what way, in what order, how many connections established, what protocols are downloaded it?

If the picture is the same HTTPS connection and a domain name, the browser then after the SSL handshake will discuss server and can not be used HTTP2, if so Multiplexing functions on the use of multiplexing in this connection. But it may not be all hanging in this domain resources will use a TCP connection to obtain, but it is certain that Multiplexing is likely to be used.

If you find that it does not take HTTP2? Or does not take HTTPS (reality HTTP2 are implemented on HTTPS, so it is only using the HTTP / 1.1). That the browser will establish multiple TCP connections, limit the maximum number of connections depends on the browser settings, the browser connection will be used to send a new request in his spare time on a HOST, if all the connections are being sent request it? That other requests can only so the.

Guess you like

Origin www.cnblogs.com/geass-jango/p/11458549.html