Shell script implementation of a simple Web server example sharing _linux shell

Source: Internet
Author: User
Tags http request

Suppose you want to test the web and some CGI, and you don't want to trouble Apache to install the full package. This quick shell script may be just the thing you need.

In short, a Web server is an application that sends a local text file to a customer's request over the network. If you allow another program (such as INETD) to handle network situations, the Web server can be reduced to only cat "filename" sent to stdout. Of course, the difficulty will fetch the partial file name of the HTTP request string: Any bash script cannot be easily done.

Script

Our script should be like any other script, plus some definitions:

Copy Code code as follows:

#!/bin/bash
Base=/var/www

INETD will pass the data received from the remote host to our script, the first line is the standard HTTP request, followed by 0 or more header files. We record the request and exit hibernation:
Copy Code code as follows:

Read request

While/bin/true; Todo
Read header
["$header" = = $ ' \ r '] && break;
Done


The most troublesome part: Extract the URL from the requested data and locate the corresponding file in the local file:
Copy Code code as follows:

Url= "${request#get}"
Url= "${url% http/*}"
Filename= "$base $url"

Returns the contents of the file containing the header information.
Copy Code code as follows:

If [-F "$filename"]; Then
Echo-e "http/1.1 ok\r"
Echo-e "Content-type: '/usr/bin/file-bi \ ' $filename \ ' \ r '
Echo-e "\ r"
Cat "$filename"
Echo-e "\ r"
Else
Echo-e "http/1.1 404 Not Found\r"
Echo-e "Content-type:text/html\r"
Echo-e "\ r"
Echo-e "404 Not Found\r"
Echo-e "Not Found
The requested resource is not found\r "
Echo-e "\ r"
Fi

Well, the script's over.

Installation

To make it work, you must add the following line to the/etc/inetd.conf file:

Copy Code code as follows:

www stream tcp nowait nobody/usr/local/bin/webd webd

WEBD is the name of the script you just created.

After you use/etc/init.d/inetd restart to make the script effective, you can test it. In/var/www some HTML files, open your favorite Web browser to enter the following address test: http://localhost/FILENAME.html

Please note that if your computer is connected to this insecure network, this script may not be a smart thing to do, because anyone can access the files on your hard drive via port 80. A better approach is to use TCPD to ensure that only local connections are allowed. I will write some relevant information, play your imagination to do it!

What is a CGI

Like this, a Web server is useless, it can't do anything, and you can access your files in other ways. We need CGI support (even if it's very simple).

Theory: Do not send a text file over the network, we run an executable file and output it. Before that, we have processed the HTTP request step by establishing a query_string variable output to the executable file.

To do this, you just have to replace the code in the third step with this one.

Copy Code code as follows:

Url= "${request#get}"
Url= "${url% http/*}"
Query= "${url#*\?}"
Url= "${url%%\?*}"

Filename= "$base $url"

If ["$query"!= "$url"-a-x "$filename"]; Then
Export query_string= "$query"
Echo-e "http/1.1 ok\r"
"$filename"
Echo-e "\ r"
Exit 0
Fi

Of course, this gadget's performance cannot compare with Apache, it's just a gadget.

Related Article

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.