Mini Web Server Design

Source: Internet
Author: User
Tags format definition local time small web server

MINI Web Server Design

Here is a simple Miniweb server that was designed when Watchmen a friend to learn network programming. The following is its work plan: 1. See http/1.0 Agreement see: RFC1945, <> 9th
2. configuration file format Definition < see MINIM.CONF&GT;OPTION=VAL1, val2,... # comment Text # option set start option=val #选项值定义 # option Set End
3, define the function to be implemented main function: 3.1 Logging Server program log: The global variables (islog, logfile, loglevel) of the profile minim.conf determine the log format: ID:TIME:IP:PORT:DOMAIN:LEVEL: MSG Client Access log: defined by the specific host (Islog, logfile, loglevel) log format: client_ip:client_port:time:request_msg:response_msg3.2 supports multiple real hosts: I P and Port only determine ip=port=*3.3 support virtual host: IP and port also add domain name to determine the difference between the virtual host and the real host ip=port=domain=3.4 error message reply 400--404--3.5 Home directory with default home page home=index=3.6 request_methodget3.7 host thread settings default=max=increase=min_idle=max_idle= Accessibility 3.8 Server running users and groups: minim-www:minim-www3.9 can be started as a daemon, see Chapter <> 13th
Other *3.10 server status queries, such as Apache's server-status feature * This feature is not available in version 1th
4, the function realizes the method 4.1 log record (1). The server program information is sent to stderr (2). The client access log is sent to stdout (3). Before entering Deamon mode, ensure that all information is displayed on the terminal, and after entering Deamon mode, redirect stderr to the global log file and redirect stdout to the client access log file
4.2 Multi-Host (1). The sub-process of fork () should first shut down the access handle (2) for the other Web server. Scan the configuration file first, find out all, each server is a Web server, and deal with the conflict problem (3) according to the unique principle. Define a struct Web_server entity for each server and generate the corresponding process
*4.3 Virtual Host (1). A virtual host problem is more complex than a real host because it requires multiple hosts to be bound on a set of Ip:port, each with domain distinction, which uses the SO_REUSEADDR and So_reuseport options (2). Rational use of surprise group effect (process)
4.4. Error message reply (1). Define a global Error message reply page (2). Define the corresponding struct Err_response structure (3) for each Web server (including with). Handle the mapping relationship between global and individual Web servers
4.5 Home Directory with default homepage (1). Home= focus on examining the following possible issues a. The directory name and the Access file name cannot have ".." Prevent clients from exceeding their access to B. Consider Chroot ()--see man-s2 chroot--to the appropriate directory (2). index=
4.6 Request_method (1). Using enum to define the method to be implemented and the corresponding processing function, in order to facilitate the expansion
4.7 Threads (1). Each process completes listen () before entering the thread and calls accept (), (2) in the threads. The code that calls accept () is defined as a critical code, which is synchronized with the semaphore of sem_* () or pthread_mutex_* () (3). Define an idle line threads to ensure that the number of idle threads is between Min_idle and Max_idle (4). When the number of idle threads is less than min_idle, increase threads (5) are created at a time by the main thread. The number of bus threads cannot be greater than max number, which can effectively set the size of a Web server (6). The exit of the idle thread is handled by the thread itself, and in order to prevent oscillation, the idle thread's decision and exit operations are defined as the critical Code (7). Recv blocking time per thread should not be greater than timeout time (Unit:second)
4.8 Server running users and groups: (1). Define the appropriate users and groups at installation: Minim-www (2). When the program starts, set its own Sid,sgid,seid, Segid
4.9 can be started as daemon, see Chapter <> 13th (1). Before you fork () more than once, handle the configuration file.
*4.10 Server status queries, such as Apache's Server-status feature (1). Each process writes state information (2) to a unified file by the specified protocol (custom). The main process establishes a thread to listen for requests for Server status (2). A listening thread reads a state file, generates a Web page file, and sends a message to the client
5, program start sequence (see Flow-chart.pdf)

6, Precautions (1). All file directories in the program cannot be written directly in the code, should be defined by the macro, the specific value should be determined at configure configuration, and then generated in a header file (PATH.H), the program contains this file. For ease of debugging, you can manually edit the PATH.H (2) beforehand. Some of the values in profile doc/minim.h should also be determined by configure when configured (3). Files determined by autoconf when you need to configure path.h.in---path.hminim.conf.in---minim.confconfig.h.in Config.h
FYI: (automake system variables)  --bindir=dir           user executables [eprefix/bin]&nbsp ; --sbindir=dir          system admin executables [eprefix/sbin] --libexecdir=dir   &NB Sp   Program Executables [eprefix/libexec] --sysconfdir=dir       read-only single-machine data [P refix/etc] --sharedstatedir=dir   Modifiable architecture-independent data [prefix/com] -- Localstatedir=dir    modifiable single-machine data [prefix/var] --libdir=dir           Object code libraries [eprefix/lib] --includedir=dir       C header files [prefix/inclu de] --oldincludedir=dir    c header files for non-gcc [/usr/include] --datarootdir=dir   &NBSP ;  read-only arch.-independent Data root [prefix/share] --datadir=dir           Read-only architecture-independent data [DATAROOTDir] --infodir=dir          info documentation [datarootdir/info] --localedir=dir & nbsp      locale-dependent data [datarootdir/locale] --mandir=dir           man D Ocumentation [datarootdir/man] --docdir=dir           documentation Root [Datarootdir/doc /charpter3] --htmldir=dir          html documentation [docdir] --dvidir=dir   & nbsp       DVI documentation [docdir] --pdfdir=dir           PDF documentation [D ocdir] --psdir=dir           &NBSP;PS documentation [DOCDIR]

7. program File Structure Minim | |--doc |--minim.conf |--plan.text |--request.txt |--include |--path.h |--minim.conf.h  |--err_pages |--400.html |--404.html |--500.html |--conf |--minim.conf | |--lib |--|--src |-- |--readme |--todo.txt



all documentation and code download

minim-0.0.1.tar.bz2


HTTP protocol Documentation

HTTP protocol. rar



*******************************************************************for embedded development, it is more obvious that an open source embedded Web server is used. The common embedded Web server is as follows:

Lighttpd
Shttpd
Thttpd
Boa
Mathopd
Minihttpd
Appweb
Goahead

The porting method of the embedded Web server boa (i)

Boais a very small Web server, executable code only about 60KB. It is a single-task Web server that can only be completed in sequence by the user's request,instead of fork out a new process to handle concurrent connection requests. But Boa supports CGI, which is able to fork out a process for CGI programs to execute. Boa's design goalsis speed and security, and in its site-published performance tests, Boa performs better than the Apache server.
(1) fromwww.boa.orgDownload the BOA source code, unzip it and enter the source directorysrc Sub-directory
# tar Xzf boa-0.94.13.tar.gz
# CD BOA-0.94.13/SRC
Generate Makefile File
#./configure
Modify the Makefile file, locate the CC=GCC, change it to CC = ARM-LINUX-GCC, and then find CPP = Gcc–e,change it to CPP = Arm-linux-gcc–e and save the exit.
then run make to compile, get the executable program for Boa, the debugging information stripped, get the final program only about 60KB size.
# Make
# Arm-linux-strip Boa

(2) Complete the configuration of the boa so that it can support the execution of CGI programs. Boa needs to create a boa directory in the/etc directory, which is put into Boa's main configuration file, boa.conf. In the BOA source directory there is already an example boa.conf, can be modified on its basis, the following explanation of the meaning of the file:
# Listening port number, the default is 80, generally do not need to modify
Port 80


# The IP address of the bind call, which is generally commented out, indicates that all IP addresses that are bound to the server are Inaddr_any
#Listen 192.68.0.5


# as which user runs, that is, it has the permissions of the user, is generally nobody, need to/etc/passwd
#nobody User
User Nobody
# as which user group runs, that is, it has the permissions of the user group, is generally nogroup, need to have Nogroup group in the/etc/group text
Group Nogroup


# When the server has a problem, send an alarm email address, currently unused, commented out
#ServerAdmin [email protected]


# error log file. If it does not start with/, it means starting from the root path of the server. If the error log is not required, use #/dev/null. When setting up below, be sure to set up the/var/log/boa directory
Errorlog/var/log/boa/error_log


# access log files. If it does not start with/, it means starting from the root path of the server. If the error log is not required, It is commented out with #/dev/null or directly. When setting up below, be sure to set up the/var/log/boa directory
#AccessLog/var/log/boa/access_log


# whether to use local time. If it is not commented out, the local time is used. Use UTC when commenting out
#UseLocaltime


# whether to log CGI run information, if not commented out, then record, comment out then do not record
#VerboseCGILogs


# server name
ServerName hostname


# Whether or not to start the virtual host function, that is, the device can have multiple network interfaces, each interface can have a virtual Web server . Generally commented out, i.e. no need to start
#VirtualHost


# Very important, the home directory of the HTML document. If it does not start with/, it means starting from the root path of the server.
Documentroot/var/www


# If you receive a user request, add the directory name after the user's home directory
Userdir public_html


#HTML The file name of the directory index, and no user indicates only the file name that is returned when the directory is accessed
DirectoryIndex index.html


# When the HTML directory does not have an index file, the user only indicates that when the directory is accessed, Boa calls the program to generate the index file and then returns it to the user, because the process is slower and better not to execute, can be commented out or added to each HTML directory # Documents specified by DirectoryIndex
#DirectoryMaker/usr/lib/boa/boa_indexer


# If DirectoryIndex does not exist, and Directorymaker is commented, then use the Boa-built index generator to generate the index file of the directory and output it to the directory below, which must be a BOA capable to read and write
# Directorycache/var/spool/boa/dircache


# The maximum number of HTTP persistent requests allowed for a connection, comments or set to 0 will turn off HTTP persistence
Keepalivemax 1000


the number of times, in seconds, that the server waits between two requests in a persistent #HTTP to close the connection
KeepAliveTimeout 10


# Indicates the location of the Mime.types file. If it does not start with/, it means starting from the root path of the server. Can be commented out to avoid the use of mime.types files, at this time need to use AddType in this document to specify
Mimetypes/etc/mime.types


# The default MIME type used if the file extension is not known or unknown
DefaultType Text/plain


# provides the PATH environment variable value of the CGI program
Cgipath/bin:/usr/bin:/usr/local/bin


# Associate the file name extension with the MIME type, as with the Mime.types file. If you use a mime.types file, then comment out, if you do not use the Mime.types file, you must use the
#AddType application/x-httpd-cgi CGI


# indicates document redirection path
#Redirect/bar Http://elsewhere/feh/bar


# Add an alias to the path
Alias/doc/usr/doc


# Very important, indicate the actual path corresponding to the virtual path of the CGI script. Generally all CGI scripts are placed in the actual path , when the user accesses execution, enter the site + virtual path +cgi script name
scriptalias/cgi-bin//var/www/cgi-bin/

Users can modify the boa.conf according to their own needs, but must ensure that the other auxiliary files and settings must match the configuration in the boa.conf,
Otherwise Boa will not work properly. In the example above, we also need to create the directory where the log files are located/var/log/boa, Create the home directory of the HTML document/VAR/WWW, copy the Mime.types file to the/etc directory, create the directory where the CGI script is located/ var/www/cgi-bin/. The Mime.types file is used to indicate the MIME type of different file extensions, which can usually be copied directly from the Linux host, mostly in the host's/etc directory.


===================================================================

(3) System test
1. Enter boa-0.94.13/src
./configure
Make
2. Create a boa directory under etc/and copy the boa.conf to this directory. Change boa.conf
Group Nogroup = = = "Group 0"

3. Create a boa directory under/var/log/, which allows you to view the logs of a BOA server

4. Some other paths

By default, the content under/var/www can be accessed (documentroot/var/www)
Default cgi:scriptalias/cgi-bin//usr/lib/cgi-bin/(CGI executable placed in/usr/lib/cgi-bin/directory)
Example http://201.201.201.249/cgi-bin/cgi-test.cgi
Cgipath/bin:/usr/bin:/usr/local/bin

Only commands in these directories can be called, and if you want root permissions (such as ifconfig configuration IP) You need to add/sbin

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.