1. Introduction
TINYHTTPD is an open-source, ultra-lightweight HTTP server that reads its source code and can learn more about HTTP protocols and micro-servers.
Source link;
Reference Blog: tinyhttpd Source Analysis
2. Notes
------------------------------------------------------May 7, 2016 21:31:35--------------------------------------- -------------
A function prototype catalog that lists all the function prototypes needed for the entire project
1 voidAccept_request (void*);2 voidBad_request (int);3 voidCatint, FILE *);4 voidCannot_execute (int);5 voidError_die (Const Char*);6 voidEXECUTE_CGI (int,Const Char*,Const Char*,Const Char*);7 intGet_line (int,Char*,int);8 voidHeadersint,Const Char*);9 voidNot_found (int);Ten voidServe_file (int,Const Char*); One intStartup (U_short *); A voidUnimplemented (int);
The role of each function in the project's Readme file has a simple description:
| Function |
Describe |
| Accept_request |
Handles an HTTP request that is heard from a socket, where a large part of the server processing request process can be represented |
| Bad_request |
Return to client this is an error request, HTTP status? |
| Cat |
Read a file on the server write to the socket socket |
| Cannot_execute |
Primary handling of errors that occur when executing CGI programs |
| Error_die |
Write the error message to Perror and exit |
| execute_cgi |
The process of running a CGI program is also a major function |
| Get_line |
Read a line of the socket, the carriage return line, and so on are unified for the end of the line break |
| Headers |
Writes the header of the HTTP response to the socket |
| Not_found |
Case where the requested file was not found in the main process |
| Sever_file |
Call cat to return the server file to the browser |
| Startup |
Initialize the httpd service, including setting up sockets, binding ports, listening, etc. |
| unimplemented |
Returned to the browser indicates that the method used to receive the HTTP request is not supported |
Suggested Source reading order:
Main--->startup--->accept_request--->execute_cgi
In this order, the entire server workflow is cleared.
Main function
1 intMainvoid)2 {3 intServer_sock =-1;4U_short port =4000;5 intClient_sock =-1;6 structsockaddr_in client_name;7socklen_t Client_name_len =sizeof(client_name);8 pthread_t Newthread;9 TenServer_sock = Startup (&port); Oneprintf"httpd running on port%d\n", port); A - while(1) - { theClient_sock =Accept (Server_sock, -(structSOCKADDR *) &Client_name, -&Client_name_len); - if(Client_sock = =-1) +Error_die ("Accept"); - /*accept_request (client_sock);*/ + if(Pthread_create (&newthread, NULL, (void*) Accept_request, (void*) &client_sock)! =0) APerror ("pthread_create"); at } - - Close (server_sock); - - return(0); -}
3–7 line is the definition of socket related variables, the detailed introduction of the socket can be online access to relevant information or read the book "UNIX Network Programming";
The 10th line calls the startup function, and the contents of the function follow a brief explanation;
第13-23 line, listen to the client to the service side of the request connection, when a connection is received, create a thread to execute the corresponding request, the thread entry function is Accept_request, but also about the use of Linux pthread_create need to pay attention to some of the details of the problem.
Line 25th closes the socket on the server side.
startup function
The function parameter is a port number that is used to create a TCP socket for the corresponding port number
1 intStartup (U_short *Port)2 {3 inthttpd =0;4 structsockaddr_in name;5 6httpd = socket (pf_inet, Sock_stream,0);7 if(httpd = =-1)8Error_die ("Socket");9memset (&name,0,sizeof(name));Tenname.sin_family =af_inet; OneName.sin_port = htons (*port); ANAME.SIN_ADDR.S_ADDR =htonl (inaddr_any); - if(Bind (httpd, (structSOCKADDR *) &name,sizeof(name)) <0) -Error_die ("Bind"); the if(*port = =0)/*if dynamically allocating a port*/ - { -socklen_t Namelen =sizeof(name); - if(GetSockName (httpd, (structSOCKADDR *) &name, &namelen) = =-1) +Error_die ("getsockname"); -*port =Ntohs (name.sin_port); + } A if(Listen (httpd,5) <0) atError_die ("Listen"); - return(httpd); -}
Line 6th creates a socket socket, because its parameters are sock_stream, so the TCP socket is created;
7–8 line, if the socket creation is unsuccessful, then call the encapsulated error handler Error_die, which is simpler, is to print the error message and end the entire program.
第13-23 is the basic process of creating socket server, first bind operation, then listen operation;
TINYHTTPD Reading notes