How do I know WHEN to close HTTP 1.1 Keep-Alive Connection?

I am writing a web server in Java and I want it to support HTTP 1.1 Keep-Alive. But how can I know when the client will send requests for this connection? (e.g. double end of string or something else).

Let's see how stackoverflow handles this very obscure question - the answers to which, at Google, are mired in technical specifications and an obscure language. I want plain English for a programmer other than C :)


I see. which confirms my suspicion of having to rely on a SocketTimeoutException. But I was not sure if there was anything I could rely on from a client who indicated that this was done through a connection - which would allow me to close the connections earlier in most cases - instead of waiting for a timeout. Thanks

+7
java network-protocols
source share
4 answers

If you are building your server to comply with the standard, then you have a lot of information that will help you here already.

Simply put, it should be based on the time since the connection was used and not so much on the request data level.

In a longer order, the practical considerations section of the HTTP / 1.1 document contains some guidelines for you:

"Servers usually have some time for which they will no longer support inactive connections. Proxies may have a higher value, because the client will make more connections through the same server. Using persistent connections does not impose length requirements (or existence) of this timeout for either the client or the server. "

or

β€œWhen a client or server SHOULD issue an elegant close transport message. Clients and servers SHOULD constantly monitor the other side of the transport and respond to it if necessary. If the client or server does not detect the other side of the shutdown, this may lead to an unnecessary resource leak in network. "

+8
source

Let's see how stackoverflow handles this very obscure question - the answers to which, at Google, are mired in technical specifications and an obscure language.

I just put When do I need to close the HTTP 1.1 connection? on Google, and the third hit was HTTP Made Really Easy. The table of contents contains a link to a section called Persistent Connections and the heading "Connection: close" . This section is three paragraphs long, uses a very simple language and tells you exactly what you want to know.

I need plain English for a programmer other than C :)

With all due respect, programming is a technical task in which details are important. Reading technical documentation is an essential skill. Relying on β€œplain English” third-party interpretations of the specifications will only lead to poor performance.

+3
source

You close it whenever you want. The header indicates that the client would prefer that you leave the connection open, but this does not require the server to match. Most servers leave it open for about 5-10 seconds, some do not pay attention to it at all.

+1
source

You should read the RFC regarding Keep-Alive. Otherwise, you may end up on a server that is not working properly.

As @ [Stephen] already pointed out, the server can close the connection at any time it wants (normally, but not in the middle of a request / response pair). The same goes for the customer. Any other solution will allow the server or client to perform DoS on the other side.

EDIT: look at the Connection header. The client (and server) can request a graceful closing of the connection using the header. For example, Connection: close inside a request is a request to the server to close the connection after sending the response.

+1
source

All Articles