Simple Web Server
Introduction
A web server is basically a program which listen on a port (80) and send the content of HTML pages to the client which has requested it.
HTTP Protocol
The HTTP protocol is quite simple if you consider simple requests. The most common requests are:
- GET
- POST
Both methods are used to request HTML pages. They are different in the way of passing arguments (if any). For our purpose, we only manage the GET method and do not handle parameters. The format of a GET request is as follows:
GET URI PROTOCOL
The URI can be an absolute URL (e.g.: http://tuxigloo.org/index.html) or a relative URL (e.g. /index.html).
The protocol is "HTTP/" followed by the version of HTTP. The version 1.0 is usually used. This field is optional.
So for example, a GET request can be as follows:
GET http://tuxigloo.org/index.html HTTP/1.0
The server then replies by sending a header plus the content of the requested file. The header is something like:
HTTP/1.0 200 OK Content-Length: 234 Content-Type: text/html
- The first line contains the protocol used and a code which says whether an error occured or if the request was accepted. There are about thirty codes in the HTTP protocol. But the most important ones are:
- 200: OK
- 400: bad request
- 404: the page was not found
The content-length is the size of the requested file. The content-type is the type of the requested file. It is determined either by the file extension or by its magic cookie (first two bytes of the file).
Design
The web server has to manage several connections at the same time. So in the implementation, you can use the fork or thread function to handle them. You will understand more by looking at the source. You can download the full archive here


