Introduction to Sockets and Network Connections
HTTP is a standardized, text-based protocol that defines how web clients and servers communicate using a simple request-response model.
From Connection to Web Page
When a web client communicates with a web server, the exchange follows a predictable pattern. The client connects, sends a request, the server processes that request, and the server sends a response. HTTP, the Hypertext Transfer Protocol, defines this communication as a standardized, text-based request-response model. This cycle is the foundation of web communication.
HTTP as the Communication Protocol
HTTP provides the rules for communication between a web client and a web server. It is standardized, which means the participants follow an agreed structure, and text-based, which means the request and response are organized as readable textual parts. The model is intentionally simple: the client sends a request, and the server returns a response.
HTTP describes what the client asks for and how the server reports the result. The network connection is the setting in which this request-response exchange takes place.
Reading a GET Request
An HTTP GET request identifies the resource the client wants. Its required structure includes the HTTP method, the resource path, and the protocol version. Header lines can provide additional request information. The request ends with a mandatory blank line, which separates the request headers from what follows.
A Request for a Resource
Identify the parts of a generated HTTP GET request.
Method: GET identifies the HTTP method used by the client.
Resource path: /articles identifies the requested resource path.
Protocol version: HTTP/1.1 identifies the protocol version in this generated example.
Header: Host: example.test is a header line carrying request metadata in this generated example.
Blank line: The empty line marks the required end of the request headers.
The request communicates that the client wants the resource at /articles and then terminates its headers with a blank line.
The exact names and values of headers can vary, but their structural role is the same in this model: they are header lines that carry metadata. The essential GET pattern is the request line followed by header lines and a mandatory blank line.
Interpreting the Server Response
After processing the request, the server sends an HTTP response. The response has four structural parts: a status line, header lines containing metadata, a blank line separator, and a response body. The body contains the requested resource.
Reading a Generated Response
Identify the structural parts of a generated HTTP server response.
Status line: HTTP/1.1 200 OK is the status line in this generated example.
Header lines: Content-Type: text/plain is a header line containing response metadata in this generated example.
Blank line: The empty line separates the response headers from the response body.
Response body: Welcome to the articles resource is the body containing the requested resource in this generated example.
The client can distinguish the response status, its metadata, and the requested resource by reading the response in order.
The blank line has a structural purpose in both directions of the exchange. In a GET request, it marks the end of the request headers. In a server response, it separates response headers from the response body.
Tracing the Complete Exchange
The complete cycle can be traced as four connected stages. First, the client establishes a connection. Second, it sends an HTTP request such as a GET request. Third, the server processes that request. Fourth, the server sends an HTTP response, which the client receives and processes. Leaving out any of these stages gives an incomplete picture of web communication.
What do you think happens?
A client has connected to a server and sent a GET request. What must happen before the client can process the requested resource?
Reveal answer
Answer: The server processes the request and sends an HTTP response.
HTTP follows a request-response model. The connection provides the setting for communication, but the server must process the request and return a response containing a status line, headers, a separator, and the response body.
- The client connects to the server.
- The client sends a structured HTTP request.
- The server processes the request.
- The server sends a structured HTTP response.
- The client processes the response and its requested resource.
Mistakes in Message Structure
Treating a GET request as only a resource path
An HTTP GET request specifies the method, resource path, and protocol version as part of its request structure.
Fix:
Read the request line as a complete structure: method, resource path, and protocol version.Omitting the mandatory blank line at the end of a GET request
The blank line marks the end of the request headers.
Fix:
Include the blank line as the final structural part of the request.Confusing response headers with the response body
The response headers contain metadata, while the response body contains the requested resource.
Fix:
Use the blank line to distinguish the metadata section from the response body.Stopping the cycle after the connection
The complete cycle also requires a request, server processing, and a response.
Fix:
Trace all stages from connection through response processing.
Practice the Trace
A client wants a resource from a web server. Describe the exchange in order, beginning with the connection and ending when the client processes the server's response. Then list the four structural parts of the response.
Hints
- Use the sequence: connection, request, server processing, response.
- For the response structure, begin with the status line.
- Remember the blank line separator before the response body.
| Message | Required structural parts |
|---|---|
| HTTP GET request | Method, resource path, protocol version, header lines, mandatory blank line |
| HTTP server response | Status line, header lines with metadata, blank line separator, response body |
A comparison of the two main HTTP messages introduced in this article.
Key Takeaways
- HTTP is a standardized, text-based protocol built around a request-response model.
- A GET request identifies a method, resource path, and protocol version, includes header lines, and ends with a mandatory blank line.
- An HTTP server response contains a status line, metadata headers, a blank line separator, and a body containing the requested resource.
- The complete web communication cycle is connection, request, server processing, response, and client processing.
- A network connection is only one stage; HTTP gives the exchange its request and response structure.
Key Takeaways
- HTTP is a standardized, text-based protocol for communication between web clients and servers.
- A GET request contains a method, resource path, protocol version, header lines, and a mandatory blank line.
- A server response contains a status line, metadata headers, a blank-line separator, and the requested resource in its body.
- The complete HTTP cycle moves from connection to request, server processing, response, and client processing.