Thttpd CGI User Manual

Source: Internet
Author: User

   Thttpd CGI User Manual
1.Thttpd Introduction
Thttpd is a very small and lightweight Web server. It is very simple and only provides HTTP/1.1 and simple CGI support, on its official website, there is a comparison chart + benchmark with other Web servers (such as Apache and Zeus). For more information, see. In addition, Thttpd is similar to Lighttpd. For concurrent requests, the fork () method is not used to dispatch the child process, but multiplex technology is used. Therefore, the performance is very good.
Thttpd supports multiple platforms, such as FreeBSD, SunOS, Solaris, BSD, Linux, and OSF. For small Web servers, fast speed seems to be synonymous. With benchmark provided by official sites, Thttpd is at least as fast as the mainstream web servers and faster under high loads, because its resource usage is small.
Thttpd has another notable feature: URL-based file traffic limit, which is very convenient for download traffic control. Like Apache, it must be implemented using plug-ins, which is less efficient than thttpd.
2.CGI Introduction
  CGI is not a language. it's a simple protocol that can be used to communicate between web forms and your program. a cgi script can be written in any language that can read stdin, write to stdout, and read environment variables, I. e. virtually any
Programming Language, including C, Perl, or even shell scripting.
Structure of a CGI script
Here's the typical sequence of steps for a CGI script:
Read the user's form input.
Do what you want with the data.
Write the HTML response to stdout.
For more information, see network articles. CGI made really easyhttp: // www.jw.all.com/easy/cgi/
The Common Gateway Interface (CGI) is a programming standard that specifies the interface protocol standard for Web servers to call other executable programs (CGI programs. The CGI program calls other executable programs to interact with the running Web server through the Web server. It accepts and processes the information sent to the Web server by the web browser, then, the results are sent to the Web server and Web browser. CGI programs can be written in any programming language, such as shell, Perl, C, and Java. cgi programs written in C language are fast and secure.
CGI programs are usually used to add query, search, interactive applications, and other applications.              The CGI interface standard consists of three parts: input, environment variable, and standard output. The CGI program can obtain input information from the Web server through stdin. For example, it can obtain data from the form, this is a common POST method. Because different operating systems adopt different information exchange mechanisms, the parameter transmission process is also different. In UNIX and DOS, the environment information reflects some system conditions when the program is running at the same level, it can be used to transmit information between parent and subprograms. cgi transmits data between servers and clients by setting environment variables. Each operating system provides many environment variables, they define the execution environment of the program, and the application can access them. The Web server and CGI interface also set some environment variables to pass some important parameters. The CGI program sends the output information to the Web server through the standard output (stdout). The information sent to the server can be HTML or plain text. This article uses the C language to compile a CGI application-student achievement query system, and analyzes the methods, processes, and skills of CGI program design.          

3.Thttpd Installation
1)Download the installation package thttpd-2.25b.tar.gz
2)Tar-Xzvf thttpd-2.25b.tar.gz
3)CD thttpd-2.25b
4)./Configure
5)Make
6)Make installCopy to the/usr/local/sbin directory
7)ThttpdScript-based thttpd-D-C/home/www/thttpd. config
8)For the compilation of the script, refer to the network Article usethethttpdwebserver.
Thttpd supports writing configuration information to the thttpd executable file. You need to add the compilation option in./configure. thttpd also supports entering configuration information into the execution environment through execution parameters. It is best to use a configuration file.

4.Difficult
1)Chroot
What is chroot? Chroot basically defines the running environment of a program. More specifically, it redefines the "root" directory or "/" of a program (or login session "/". That is to say, for a chroot program or shell, directories outside the chroot environment do not exist.
So what's the purpose? If intruders intrude into your computer, they will not be able to see all the files in your system. In this way, intruders may be restricted from executing commands, thus prohibiting them from overwriting insecure files. But the only drawback is that I don't think this will prevent them from viewing network connections and other information. Therefore, you should do something that is not deeply involved in this article:
LProtect Network Ports.
LCheck whether all services run with non-root permissions. In addition, are all services chroot?
LTransfers System logs to other computers.
LAnalyze log files.
LAnalyze people trying to detect random ports on your computer.
LRestrict the CPU and memory resources occupied by the Service.

The chroot function ensures system security, but limits the execution environment of CGI programs. For example, the. C program must be compiled into-static. The interpreter of Perl and other interpretation languages must also be placed in the chroot directory. The following uses the sh script interpreter as an example to describe the solution:
See lightweight Web serving with thttpd
# Cd/home/WWW
# Mkdir Bin
# Cp/bin/sh Bin
# LDD bin/sh
Bin/sh:
       -Ledit.2 =>/lib/libedit. so.2
       -Ltermcap.0 =>/lib/libtermcap. so.0
       -Lc.12 =>/lib/libc. so.12
# Mkdir lib
# Cp/lib/libedit. so.2 lib
# Cp/lib/libtermcap. so.0 lib
# Cp/lib/libc. so.12 lib
# Mkdir libexec
# Cp/libexec/lD. elf_so libexec
# Chown-r root: wheel bin lib libexec
Depending on the applications you need inside the chroot, things will get more complicated: you may need to create device files, a user database, pipes, and so on.

2)Gcc-static Option
-Static
   On systems that support dynamic linking, this prevents linking with the shared libraries.On otherSystems, this option has no effect.
Compile some functions to the program statically without dynamic links.
3)Thttp. config
# Dir =/usr/local/www/
# Chroot
# Cgipat =/cgi-bin /*
# Logfile =/usr/local/www/logs/thttpd_log
# Pidfile =/var/run/thttpd. PID

5.Thttpd code analysis
The core part is how to associate get/post of web with stdin of CGI program, and the content of web with stdout.

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.