Introduction to HTTP and the Concept of Upgrade
HTTP is a protocol for transferring extensive data, and it is one of the fundamental web protocols utilized for transmitting information between the client and the server. When we talk about HTTP, it means we are accessing websites, downloading files, or sending data to the server. Now, in RFC 9110, section 7.8, the concept of Upgrade is mentioned, which is a specific feature in HTTP.
The meaning of Upgrade, in fact, grants clients the ability to request a protocol or communication format from the server to transition to a newer version or type. This is especially true in cases where a new request needs more extensive features or a better functionality. For example, when a browser wants to switch from HTTP/1.1 to WebSocket, it can use this capability.
By using the Upgrade header, the client can inform the server whether it is capable of switching to a different protocol or not. If the server forwards this request, it can transfer the connection to the new protocol. This means that after the protocol change, the new connection can feature different and better operations.
As an example, you might want a user connection to be more interactive, requiring a persistent connection with the server. In such cases, you could use HTTP to establish the initial connection and then request an Upgrade to WebSocket for a persistent connection. In this state, HTTP would only be a starting point, and once the connection is established, WebSocket will be used.
Code Example for Using Upgrade
GET /chat HTTP/1.1
Host: example.com
Connection: Upgrade
Upgrade: websocket
Code Explanation
Here, you can observe a HTTP request to upgrade to WebSocket:
GET /chat HTTP/1.1
This line indicates that a request can be made to the /chat route on the connected server, using the HTTP protocol version 1.1.
Host: example.com
This line specifies the server's hostname that the request will be sent to.
Connection: Upgrade
This line informs the server that we want to establish an upgraded connection.
Upgrade: websocket
We are informing the server that we want to switch to the WebSocket protocol.