The Connection
HTTP header field plays a key role in defining how a client (or a proxy) and a server manage network connections. By providing instructions to either close the connection or keep it open for further requests, the Connection
header is integral to optimizing network resources and managing connection persistence.
Header Format
Before diving into details, let’s understand the basic syntax of the Connection
HTTP header. In its simplest form, it is as follows:
Connection: <directive>
Here, <directive>
can be either keep-alive
or close
, each having a distinct impact on the lifecycle of the connection.
Directives
Keep-Alive
In HTTP/1.1, the default connection behavior is “persistent” or “keep-alive”, meaning the connection remains open for multiple requests/responses. It results in reduced latency for subsequent requests, improved performance, and optimal use of resources, as opening and closing TCP connections can be expensive.
The following example shows the use of the Connection: keep-alive
header:
Request:
GET /index.html HTTP/1.1
Host: www.example.com
Connection: keep-alive
Response:
HTTP/1.1 200 OK
Content-Type: text/html
Connection: keep-alive
Content-Length: 137
Though it’s not necessary to explicitly mention keep-alive
in HTTP/1.1 as it’s the default, this is helpful when communicating with older HTTP/1.0 servers that default to close
.
Close
The Connection: close
directive tells the server that the client (or a proxy) wishes to end the connection after the current request/response is complete. This behavior was default in HTTP/1.0 and is mostly useful for single, isolated requests where maintaining a connection isn’t beneficial.
Request:
GET /index.html HTTP/1.1
Host: www.example.com
Connection: close
Response:
HTTP/1.1 200 OK
Content-Type: text/html
Connection: close
Content-Length: 137
Other Usages
The Connection
header also has another key role: it can convey hop-by-hop headers, indicating that the mentioned headers should not be forwarded by proxies or other intermediaries, but are meant for the immediate connection.
For instance, consider the following request:
GET /index.html HTTP/1.1
Host: www.example.com
Connection: keep-alive, Upgrade
Upgrade: h2c
In the above example, Upgrade
is a hop-by-hop header. By including it in the Connection
header, we specify that it’s only relevant for the immediate, or “hop-by-hop,” connection.
Compatibility
Remember, while Connection: keep-alive
provides performance benefits, not all servers, clients, or proxies support persistent connections. Moreover, the HTTP/2 and HTTP/3 protocols handle connection management differently, making the Connection
header obsolete in these contexts. Always consider the entire communication path when determining whether to use keep-alive
or close
.
Summary
The Connection
HTTP header plays a key role in managing the lifecycle of network connections. With its two primary directives, keep-alive
and close
, it allows optimal utilization of network resources. However, be mindful of the environment and protocol in use to ensure compatibility and expected behavior. The Connection
header’s secondary role in handling hop-by-hop headers provides granular control over the connection’s specifics, further expanding its functionality.