The objective of Webserv is to write an HTTP server in C++ 98.
Here you will learn more about :
HTTP is a [client-server] protocol for fetching resources such as HTML documents.
It is the foundation of any data exchange on the Web and it is a client-server protocol,
which means requests are initiated by the recipient, usually the Web browser.
Requests will be sent by a client (the web browser) to a server which will gives an answer called response. The HTTP protocol should be reliable and connexion-oriented. Therefore our webserv will be based on the TCP standard (=/= UCP standard).
| Version | Behavior |
|---|---|
| HTTP/1.0 | opens a separate TCP connection for each HTTP request/response pair |
| HTTP/1.1 | introduces pipelining and persistent connections. HTTP messages are human-readable. |
| HTTP/2 | multiplexed messages over a single connection. Connection is kept warm and efficient. HTTP messages are not human-readable because of its binary structure. |
1) The client asks the server to open a connexion to send one or several requests and receive answers. It can be:
- an new connexion
- an existing one that has been previously opened
The request contains:
- The HTTP method. For our project, only the GET, POST and DELETE methods will be coded.
| Method | Behavior |
|---|---|
| GET | Requests data from a specified resource (for instance if you want to get srcs/file.html ressource) |
| POST | Sends data to a server to create/update a resource |
| DELETE | Deletes the specified resource |
-
The path of the ressource
-
The version of the HTTP protocol (it will be HTTP/1.1 for our project)
-
Optional headers : languages, host, cache-control...
-
A body that will contain the ressource sent
The response contains:
-
The version of the HTTP protocol
-
The status code. It will indicate whether the request was successful or not
-
The status message. It is a short description of the status code's meaning
-
The HTTP headers
-
A body (optional) containing the fetched resource
I found these examples here.
| Function | Description & OS |
|---|---|
| select | there are as many waiting lists as there are sockets. Works on Linux & MacOS |
| poll | the socket itself has its own waiting list. The socket has to be added and deleted only once (=/= select). Works on MacOS |
| epoll | the socket itself has its own waiting list. The socket has to be added and deleted only once (=/= select). Works on Linux |
| kqueue | we didn't find a lot of documentation about kqueue. Works on FreeBSD |
For our project, we have chosen to work on MacOS and to use poll because it seemed:
- more optimised and quicker
- easier to use
- provided with documentation
You really should read this if you want to better understand our choice.
Socket programming is a way of connecting two nodes on a network to communicate with each other.
One socket(node) listens on a particular port at an IP, while other socket reaches out
to the other to form a connection.
Here, you will learn how to create:
- your server socket
- your client socket
- how to make them communicate
First of all, you have to include :
#include <sys/types.h>
#include <sys/socket.h>
Then, you have to complete the following steps:
int sockfd = socket(int domain, int type, int protocol);
It will create a socket descriptor (sockfd) that will be a communication point.
| Variable | Type | Description |
|---|---|---|
| domain | int | Communication domain |
| type | int | Communication type |
| protocol | int | Protocol value (for IP -> 0) |
Return value:
- a descriptor referencing the socket
- -1 if an error occurs
int setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen);
Helps in reuse of address and port. Prevents error such as: “address already in use”.
| Variable | Type | Description |
|---|---|---|
| sockfd | int | The socket descriptor created |
| level | int | Protocol level |
| optname | int | Option to set |
| optval | const void * | Identifies a buffer in which the value for the requested option is to be returned |
| optlen | socklen_t | The size of the optval buffer |
Return value:
- 0 if it's a success
- -1 if an error occurs
int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
It will give a name to the socket previously created.
| Variable | Type | Description |
|---|---|---|
| sockfd | int | The socket descriptor created |
| addr | const struct sockaddr * | Cast the structure pointer passed in addr in order to avoid compiler warnings |
| addrlen | socklen_t | The size in bytes of the address structure addr |
Return value:
- 0 if it's a success
- -1 if an error occurs
int listen(int sockfd, int backlog);
Waiting for a connexion on the socket.
| Variable | Type | Description |
|---|---|---|
| sockfd | int | The socket descriptor created |
| backlog | int | The maximum number of outstanding connections in the socket's listen queue |
Return value:
- 0 if it's a success
- -1 if an error occurs
int new_socket = accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
Accept a connexion on a socket.
| Variable | Type | Description |
|---|---|---|
| sockfd | int | The socket descriptor created |
| addr | struct sockaddr * | Cast the structure pointer passed in addr in order to avoid compiler warnings |
| addrlen | socklen_t | The size in bytes of the address structure addr |
Return value:
- a file descriptor for the accepted socket (= non-negative integer)
- -1 if an error occurs
- An overview of HTTP
- Socket Programming in C/C++
- HTTP Server: Everything you need to know to Build a simple HTTP server from scratch
- How nginx processes a request
- Documentation Webserv
- Building a Web Server in C++ | Part 1
- Building a Web Server in C++ | Part 2
- NGINX vs. Apache : comparaison des architectures et des possibilités de configuration et d’extension
- Simple C example of doing an HTTP POST and consuming the response
- How to build a web client? (sockets)
- Program your own web server in C. (sockets)
- Why nginx is faster than Apache, and why you needn't necessarily care — Django deployment

