1. HTTP (Hypertext Transfer Protocol)

  1. Closing client connections
    1. Lost data after a connection close
    2. Closing an HTTP/1.1 stream
    3. Receiving a closed HTTP/1.1 response
    4. See also

Closing client connections

In HTTP, connections are typically closed by the client, when they have no further requests to make and all the responses have been processed.

HTTP servers may occasionally wish to close the connection on the client instead. Care should be taken when doing this, as incorrectly closing the connection may result in the client not receiving all the information written by the server.

Lost data after a connection close

If, while a client is uploading data, the server prematurely writes a response then closes the connection, the client may not ever see that written data, due to how TCP implemented by operating systems: If the server closes the connection, then receives additional data from the client, the server kernel will reply with a TCP RST packet. Servers normally send RST when they receive data on a closed connection to indicate that the data was lost. However, this has the effect of wiping out any data buffered by the client kernel.

Closing an HTTP/1.1 stream

Closing an HTTP/1.1 stream (request and associated response) is the same as closing the underlying reliable/TCP stream, and is performed in several stages:

  1. Send a complete response with a Connection: close header
  2. Call shutdown() on the socket, e.g. shutdown(socket, SHUT_WR);
  3. Call read() on the socket, in a loop until the buffer is emptied (until an empty string is read)
  4. Call close() on the socket

Receiving a closed HTTP/1.1 response

Closing an HTTP/1.1 stream (request and associated response) is the same as closing the underlying reliable/TCP stream, and is performed in several stages:

  1. Stop sending data
  2. Close the socket

See also