Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should a server adhere to the HTTP Connection: close header sent from a client?

I have a HTTP client that sets the Connection header to the following value when I make a request:

   Connection: close

However when the server sends a response, it is setting the header to Keep-Alive:

   Connection: Keep-Alive

This seems intuitively wrong to me, and I am wondering how the client should handle such a response from the server? Also why would a server respond with Keep-Alive, when the client has asked for the connection to be closed, is this valid?

According to the HTTP RFC:

"HTTP/1.1 defines the "close" connection option for the sender to signal that the connection will be closed after completion of the response. For example,

   Connection: close

in either the request or the response header fields indicates that the connection SHOULD NOT be considered `persistent' (section 8.1) after the current request/response is complete."

like image 340
alphadevx Avatar asked Aug 06 '10 14:08

alphadevx


People also ask

When should I close http connection?

In HTTP 1.0, the server always closes the connection after sending the response UNLESS the client sent a Connection: keep-alive request header and the server sent a Connection: keep-alive response header. If no such response header exists, the client must close its end of the connection after receiving the response.

Which HTTP header will prevent a connection from being closed after a server sends its response?

Therefore in HTTP 1.0 the client can tell the server that it will keep the connection open by using the connection: keep-alive header.

What is connection close in HTTP header?

close. Indicates that either the client or the server would like to close the connection. This is the default on HTTP/1.0 requests. any comma-separated list of HTTP headers [Usually keep-alive only] Indicates that the client would like to keep the connection open.

Which header tells that connection is persistent?

The Connection: Keep-Alive header must be sent with all messages that want to continue the persistence. If the client does not send a Connection: Keep-Alive header, the server will close the connection after that request.


2 Answers

That's fine. You are telling the server you don't support persistent connections and it's telling you it does. Either party is completely valid in closing the connection - it's more of a message about what both supports rather then a YOU MUST CLOSE THIS CONNECTION command.

like image 151
Gandalf Avatar answered Oct 19 '22 19:10

Gandalf


The client says I will close the connection when the current request/response is finished,or in other words , said you don't support persisten connections. That is, it doesn't tell the server to close the connection. The server replies that it supports persistent connections(keep-alive).

As you've told the server that you don't support persistent connection, you should close the connection when you've read the response.

like image 24
nos Avatar answered Oct 19 '22 20:10

nos