Skip to content
Hamza Haddani edited this page Jun 8, 2023 · 1 revision

Introduction

The Hypertext Transfer Protocol (HTTP) is an application layer protocol that facilitates the communication and exchange of hypertext, such as HTML documents, images, and videos, between web servers and clients over the Internet.

HTTP is a widely-used application layer protocol for communication between web servers and clients. It enables the exchange of hypertext, which includes HTML documents, images, videos, and other resources, over the Internet. This documentation provides an overview of the HTTP protocol and its key features.

Key Concepts

Client-Server Model

The HTTP protocol follows a client-server model, where the communication between web clients and web servers is based on a request-response paradigm. The client, typically a web browser or an application, initiates an HTTP request to the server. The server, which hosts the requested resources, processes the request and sends back an HTTP response containing the requested data or an appropriate status code. This model enables the separation of concerns, where the server is responsible for storing and serving the resources, while the client handles the presentation and interaction with those resources. The client-server model in HTTP forms the foundation of modern web applications and facilitates efficient and scalable communication between clients and servers.

The client-server model used by the HTTP protocol has a few advantages. First, it helps to divide tasks clearly. The server stores and serves resources, while the client takes care of how things look and work for the user. This split makes it easier to handle big projects and keep them running smoothly. Second, the client-server model allows many users to use the same server at the same time, which makes things more efficient. Third, it improves security by keeping important information on the server, so it's less exposed to potential risks. In summary, the client-server model is the basis of modern web applications, making them fast, safe, and easy to manage.

Client-Server Model

Uniform Resource Identifier (URI)

A Uniform Resource Identifier (URI) is a string of characters that identifies a resource on the web. It consists of a scheme (e.g., http, https), followed by the authority (e.g., hostname and port), path, query parameters, and fragment identifier. URIs are used in HTTP requests to specify the location of the requested resource.

Here's an example of a URI:

https://www.example.com:8080/path/to/resource?key1=value1&key2=value2#fragment

HTTP Request

An HTTP request is a message sent by a client to a server, requesting a specific action to be performed. It consists of a request line, headers, and an optional message body. The request line contains the HTTP method (e.g., GET, POST), the target URI, and the HTTP version.

Request Methods HTTP defines several request methods, including:

  • GET: Retrieves a resource identified by the URI.
  • POST: Submits data to be processed by the server.
  • PUT: Replaces the target resource with the provided content. (In Webserv, this method is used to upload files)
  • DELETE: Removes the specified resource.

Here's are some examples of an HTTP request:

GET /about.html HTTP/1.1
Host: www.example.com

This example request uses the GET method to retrieve the /about.html page

POST /login HTTP/1.1
Host: www.example.com
Content-Type: application/json
Content-Length: 51

{"username": "john", "password": "mysecretpassword"}

This example request uses the POST method to submit a JSON object to the /login endpoint.

HTTP Response

An HTTP response is a message sent by a server to a client in response to an HTTP request. It consists of a status line, headers, and an optional message body. The status line includes the HTTP version, a status code indicating the outcome of the request, and a status message.

Status Codes

HTTP status codes indicate the result of an HTTP request. Some commonly used status codes include:

  • 200 OK: The request succeeded, and the response contains the requested resource.
  • 404 Not Found: The requested resource could not be found on the server.
  • 500 Internal Server Error: The server encountered an unexpected condition.

For a complete list of HTTP status codes, see HTTP Status Codes.

Here's an example of an HTTP response:

HTTP/1.1 200 OK
Server: webserv/1.0.0 (Unix) (MacOS/Intel)
Content-Type: text/html
Content-Length: 21

<h1>Hello World!</h1>

HTTP Headers

HTTP headers provide additional information about the request or response. They consist of a header name followed by a value, separated by a colon. Headers can convey details such as content type, caching directives, authentication credentials, and more.

Those are the headers commonly used in Webserv:

  • Host: The domain name of the server (mandatory in HTTP/1.1).
  • Content-Type: The MIME type of the response body.
  • Content-Length: The length of the response body in bytes.
  • Transfer-Encoding: The type of encoding used to transfer the response body (The only value supported is chunked, for more detail see MDN's Transfer Encoding documentation).
  • Location: The URI of the new location that the client should redirect to.
  • Connection: The type of connection used to transfer the response body (The only value supported in Webserv is close).
  • Server: The name of the server software.
  • Set-Cookie: A cookie sent by the server to be stored by the client.
  • Cookie: A cookie previously sent by the server with Set-Cookie.

Cookies

Cookies are small pieces of data sent by the server to the client and then returned by the client with subsequent requests. They are used to maintain state and track user interactions. Cookies are sent as part of the HTTP headers and can store information such as session identifiers, user preferences, and shopping cart contents.

Conclusion

The HTTP protocol is the foundation of modern web communication. Understanding its key concepts, request methods, response codes, headers and cookies is crucial for developing and maintaining web servers and clients. By following the specifications and guidelines outlined in the HTTP protocol, developers can ensure efficient and secure communication between web applications.

Please note that this documentation provides a high-level overview of the HTTP protocol. For more detailed information and specifications, refer to the HTTP/1.1 RFC 7230 and RFC 7231

Additional Resources

Clone this wiki locally