Thttpd is a small HTTP server.
Http://www.acme.com/software/thttpd/
Download the source code of version 2.25b, decompress it, and switch to the source code directory. The compilation steps are as follows:
./Configure
Make
Thttpd has a total of 16 source files:
Config. h fdwatch. h libhttpd. h match. h MMC. h tdate_parse.h timers. h version. h
Fdwatch. c libhttpd. C match. c MMC. c strerror. c tdate_parse.c thttpd. c timers. c
Fdwatch is the socket management module.
MMC is the memory management module.
Timers is the timer management module.
How to Implement the HTTP server using libhttpd
Thttpd implements the main function
The general flowchart of the main module is as follows:
MMC module Analysis
Map the file to the memory. If have_mmap is defined, use MMAP. Otherwise, use malloc.
The mmc_map function is as follows:
Search for a hash table. If it already exists in a hash table, retrieve it from the table and return it;
Otherwise, find free_maps. If free_maps is available, use it directly. If free_maps is empty, use malloc memory;
Next, determine the file size. If the size is smaller than 0, the MMAP memory ing file is used because have_mmap is defined;
Add the map struct to the hash table, and add the map to the head of the linked list of the global variables maps.
Steps for the mmc_unmap function:
First, search for the map structure in the hash table. If no map structure is found, traverse the maps linked list of the global variable;
If none of them are found or the reference count is found <= 0, the error log is printed and the function ends;
If it is found, the reference count of this map will be reduced.
Steps for the mmc_cleanup function:
Traverse the global variables maps. When the reference count on this map is 0 or the time expires, call really_unmap.
If the total number of idle resources is greater than desired_free_count, the elements in the head of the global variable free_maps linked list will be deleted cyclically
Free_count <= desired_free_count
Steps for the really_unmap function:
Munmap cancels memory ing and adds the map structure to the header of the free_maps linked list to reduce the total number of mappings and increase the total number of idle connections.
Steps for the mmc_destroy function:
Traverse the maps linked list, call really_unmap, and traverse the free_maps linked list to release the memory, reducing the total number of idle resources and the total number of allocated resources.
Steps for the check_hash_size function:
When hash_table is empty, initialize hash_size, hash_mask,
If hash_size> = the total number of 3 ing times, return directly,
Otherwise, release the original hash table and increase the hash_size,
Malloc (hash_size) memory, traverse the maps linked list, and call add_hash
Add to the hash table.
Fdwatch module Analysis
This source file determines which model to use based on the macro definition in makefile.
Select, kqueue -- BSD system
Poll -- sysv System
Functions of the fdwatch_get_nfiles function:
Obtains the total number of operable file descriptors;
Which of the following macros is defined according to have_select, have_poll, have_devpoll, and have_kqueue,
Macro init is called. Because have_select macro is defined, init is defined as select_init.
Fdwatch_add_fd
Macro add_fd is defined as select_add_fd
Fdwatch_del_fd
Macro del_fd is defined as select_del_fd
Fdwatch
Macro watch is defined as select_watch
Fdwatch_check_fd
Macro check_fd is defined as select_check_fd
Fdwatch_get_next_client_data
Macro get_fd is defined as select_get_fd
Libhttpd module Analysis
Functions of the httpd_initialize function:
Malloc (httpd_server), apply for a piece of memory to save the structure of httpd_server, and then set the members in the structure;
Create a socket based on the sa6p and sa4p parameters, bind the socket, and then listen to the socket;
Initialize the mime media type.
Functions of the httpd_unlisten function:
Call close to close the listening socket
Functions of the httpd_get_conn function:
Httpd_realloc_str allocates memory space;
Call accept to receive client connections and assign values to fields in the httpd_conn struct.
Functions of the httpd_parse_request function:
Call bufgets to obtain the HTTP fields of the remote connection, such as the HTTP Version and MIME type.
Functions of the httpd_close_conn function:
Release file ing and disable this socket connection
Functions of LS functions:
Scan subdirectories and files in a specified directory to list file names, attributes, number of links, size, and modification time.
Analysis of thttpd main module
The handle_term function is as follows:
Functions for processing sigterm and SIGINT Signals
The steps of the parse_args function are as follows:
Process the parameters passed when the process starts and set the corresponding global variables.
The steps for the read_config function are as follows:
Read the configuration file and set the corresponding global variables.
The steps for the read_throttlefile function are as follows:
Read the 'traffic control configuration file' and set the throttletab.
The shut_down function is as follows:
Close all connections in connecttab and destroy these connections;
Disable the listening socket and destroy the HTTP server;
Destroys memory management objects and timer objects.
The steps for the handle_newconnect function are as follows:
Malloc stores httpd_conn in one memory and calls httpd_get_conn to obtain the receiving connection,
Call httpd_set_ndelay to set it to non-blocking mode;
Then call fdwatch_add_fd to join the select set.
The handle_read function is as follows:
Call read to read the data sent from this socket and call httpd_got_request to obtain the HTTP request header,
Call httpd_parse_request to analyze the request header and call check_throttles to check whether the traffic control policy is met,
Call httpd_start_request to send it back to the HTTP response header,
Call fdwatch_del_fd to remove the read socket and call fdwatch_add_fd to add the write socket.
The finish_connection function is as follows:
Call httpd_write_response to send the data in the buffer;
Call clear_connection to clear the connection.
The steps for the really_clear_connection function are:
Call fdwatch_del_fd to remove this socket from the select set;
Call httpd_close_conn to close the connection;
Call clear_throttles to clear the traffic control;
Set the status of the connection table to idle. Set the idle connection index to available to reduce the total number of connections;
The idle function is as follows:
Traverse the connects array to obtain the status and process it according to the current status;
This function is used to call the idle timer set in the main function.
The show_stats function is as follows:
Call the logstats function. The logstats function calls thttpd_logstats, httpd_logstats, mmc_logstats,
Fdwatch_logstats and tmr_logstats record logs.
The main function is as follows:
Call openlog to open system logs;
Call parse_args to process the parameters passed in by the main function;
Call read_throttlefile to read the traffic control configuration file;
Call the system function getcwd to obtain the current working directory;
Because the MAKEFILE file defines have_daemon, you can call daemon to start the background service, fork to create a sub-process, and the parent process to exit;
The system function setsid is called because the macro have_setsid is defined;
Call fdwatch_get_nfiles to initialize fdwatch and obtain resource information;
Because the macro have_sigset is defined, call the sigset to set the processing function for the system signal;
Call tmr_init to initialize the timer object;
Call httpd_initialize to initialize the HTTP server object;
Install four system timers: occasional, idle, update_throttles, and show_stats;
Set UID;
Call malloc to allocate the connects connection table array;
Call fdwatch_add_fd to add IPv4 and IPv6 listening sockets to the select set;
Enter the main cycle;
Call fdwatch to obtain information in the socket set;
When an event occurs on the socket, check the socket and call handle_newconnect to obtain a new connection;
Call fdwatch_get_next_client_data to obtain data and determine the connection status of the socket,
Call the handle_read, handle_send, and handle_linger functions based on the status;
Call tmr_run to run the timer;
Determine the global variable got_usr1. When the SIGUSR1 signal is received, the handle_usr1 function is called and got_usr1 is set to 1.
The terminate variable is set to 1, the main loop ends, the listening socket is removed, and the httpd_unlisten function is called;
Call shut_down to destroy the HTTP server object;
Shut Down System logs and the daemon process has ended.