2.1 Execution Process

The figure above shows the general execution flow of Tiny HTTPD. The main() function starts a TCP server port by calling the startup() function. When the port receives a request, the main() function creates a thread that executes accept_request() to process the request. The accept_request() function first parses the request line and, if the request is a non-GET, POST request, returns a 501 (Not Implemented) response to the client. If the request is a GET or POST request, the accept_request() function continues. If the requested file does Not exist, a 404 (Not Found) response is returned. If the requested file exists, the server determines whether it is an HTML static file or a CGI program. If it is an HTML static file, it is returned directly. If it is a CGI program, the Request header is parsed first. If parsing fails, a 400 (Bad Request) response is returned. The CGI program is executed and the result of the program execution is returned.

2.2 Analysis of main functions

Before looking at each function in detail, let’s take a look at what they do.

2.2.1 startup ()

Startup () starts a TCP server. As we all know, HTTP protocol is based on TCP protocol. There is an important network programming concept called socket, which we will spend a lot of time on later, which is the foundation of the whole network programming.

2.2.2 accept_request ()

Accept_request () handles HTTP requests and is the core of the entire server. Tiny HTTPD creates a thread to execute the accept_request() function every time it receives a request. The following major functions are called from accept_request().

2.2.3 unimplemented ()

HTTP is a fairly loose protocol, and the specification only requires that we implement the GET and HEAD methods; the rest are optional. So when the server receives a method that it does Not implement, the HTTP protocol dictates that the server return a 501 (Not Implemented) response to tell the client that the method is Not currently supported by the server. Tiny HTTPD was written by J.David Blackstone in 1999 for learning purposes, so it supports GET and POST methods. When a client requests a method that is not a GET, POST method, Tiny HTTPD calls UnImplemented () and returns a 501 response.

2.2.4 not_found ()

Not Found is the most common error we make when browsing the web. It means that the server cannot find the resource we requested. Of course, the HTTP protocol does Not mandate the use of a Not Found response only in this case, so if the server does Not want to return a resource to the client without giving a specific reason, it can also return a 404 response to “fudge” the client. The not_found() function in Tiny HTTPD is used to return a 404 response.

2.2.5 serve_file ()

Serve_file () is responsible for returning static resources on the server to the client. Common static resources are HTML documents, CSS files, and so on.

2.2.6 execute_cgi ()

Tiny HTTPD calls execute_cgi() when the client requests a CGI program. Here we’ll touch on another very important concept, CGI, which stands for Common Gateway Interface. In fact, CGI is pretty much obsolete today, and it’s important because today’s protocols like WSGI and Servlets were designed with CGI in mind. The main purpose of CGI is to dynamically generate responses based on the context of the request. Details will be explored in a future blog post.

2.2.7 bad_request ()

The HTTP protocol has certain requirements on the format of the Request. If the server receives a Request in the wrong format, the HTTP protocol requires the server to return a 400 (Bad Request) response. Bad_request () plays this role in Tiny HTTPD.

2.3 summarize

Those of you familiar with HTTP and Web development should know what I’m talking about, but those of you unfamiliar may already be in this state: