🔗 HTTP
In client-server protocols, like HTTP, sessions consist of three phases:
- The client establishes a TCP connection (or the appropriate connection if the transport layer is not TCP).
- The client sends its request, and waits for the answer.
- The server processes the request, sending back its answer, providing a status code and appropriate data.
As of HTTP/1.1, the connection is no longer closed after completing the third phase, and the client is now granted a further request: this means the second and third phases can now be performed any number of times.
Establishing a connection
In client-server protocols, it is the client which establishes the connection. Opening a connection in HTTP means initiating a connection in the underlying transport layer, usually this is TCP. See Connection management in HTTP 1.x for details.
Sending a client request
Once the connection is established, the user-agent can send the request (a user-agent is typically a web browser, but can be anything else, a crawler, for example). A client request consists of text directives, separated by CRLF (carriage return, followed by line feed), divided into three blocks: see HTTP Requests for details.
Example requests
Fetching the root page of developer.mozilla.org, (https://developer.mozilla.org/
), and telling the server that the user-agent would prefer the page in French, if possible:
Observe that final empty line, this separates the data block from the header block. As there is no Content-Length
provided in an HTTP header, this data block is presented empty, marking the end of the headers, allowing the server to process the request the moment it receives this empty line.
For example, sending the result of a form:
Request methods
HTTP defines a set of request methods indicating the desired action to be performed upon a resource. Although they can also be nouns, these requests methods are sometimes referred as HTTP verbs. The most common requests are GET
and POST
:
- The
GET
method requests a data representation of the specified resource. Requests usingGET
should only retrieve data. - The
POST
method sends data to a server so it may change its state. This is the method often used for HTML Forms.
Structure of a server response
After the connected agent has sent its request, the web server processes it, and ultimately returns a response. Similar to a client request, a server response is formed of text directives, separated by CRLF, though divided into three blocks: see HTTP Responses for details.
Example responses
Successful web page response:
Notification that the requested resource has permanently moved:
Notification that the requested resource doesn’t exist:
Response status codes
HTTP Response Status Codes indicate if a specific HTTP request has been successfully completed. Responses are grouped into five classes: informational responses, successful responses, redirects, client errors, and server errors.
200
: OK. The request has succeeded.301
: Moved Permanently. This response code means that the URI of requested resource has been changed.404
: Not Found. The server cannot find the requested resource.
📂 HTTP | Последнее изменение: 20.02.2024 15:44