In Fire We Trust

In the darkest corner light will shine



18 Jul

Print This Post A dead simple webserver in bash
чети на bulgarian 

Here i will show you a very simple idea for a poorman’s webserver. I warn you it lacks functionality and is prety useless.

Why
For a number of reasons. You can want a very simple statistics, lets say average load. You may want to park a very simple page. You may wish no to start a full blown webserver or any other reasons.

Prerequisites
We assume the following:

  • This web server will serve static content
  • Will serve only one simple page regardless of clients desire
  • It will listen on port 8080

Theory of operation

  • We will need something to take the incoming request - we will be using netcat which is known popularly as “TCP/IP swiss army knife” along with a simple wrapper script.
  • We have to distinguish that a request has arrived - in the current situation we don’t really care. The moment we see a traffic at our listening port means that someone is making a request, as soon as the traffic stops the request has arrived and we will send our response.
  • We have to construct a proper response so the client feels well.
  • After that we slip our file/message.
  • and of course terminate the session.

Realisation
The wrapper script for netcat has one and only purpose. To start netcat after it’s exit, while also adding a certain delay. The delay is needed for a simple protection from DoS attack. It is not effective but it’s simple. And of course we don’t expect to see this in a production machine.

/var/www/bashserver/netcat-server.sh

#!/bin/bash
#
# Simple wrapper for keeping nc alive as it exits after successfull request
# at least on debian it acts this way.
# Author: vvitkov
# ver: 0.1
# date: 18.07.2007
# licence: CC NC-BY-SA 3.0
 
NC=$(which nc)
while [ ] ; do
  $NC -l -p 8080 -e /var/www/bashserver/webserver.sh
  sleep 1
done

The idea here is to roll in an infinite loop starting nc with certain parameters, waiting for it to exit, taking a short nap and then starting the whole thing over again.
The parameters for the nc invocation mean:

  • -l - local mode. Meaning that nc will work as a server.
  • -p - the port we are listening on.
  • - upon connection execute this script and return everything.

With this we have accomplished the task for welcoming the traffic, now for the rest.

/var/www/bashserver/webserver.sh

#!/bin/bash
#
# Simple bash webserver. Nothing fancy
# Author: vvitkov
# ver: 0.1
# date: 18.07.2007
# licence: CC NC-BY-SA 3.0
 
# Our simple file which we will serve
FILE="/var/www/bashserver/message.txt"
# Construct propper headers
echo -e "HTTP/1.1 200 OK\r"
echo -e "Content-Type: `/usr/bin/file -bi \"$FILE\"`\r"
echo -e "\r"
 
# Now lets spit our data
cat "$FILE"
 
# And lets finish the transmission.
echo -e "\r"

Simple yea. And the interesting part is it works.

Ideas for development
Modify webserver.sh so it can accept a parameter for the listening port. Second parameter which specifies which file to execute, and not be hardcoded for webserver.sh. Eventually third parameter which should be forwarded to the second script denoting the file to be returned.
Modifying webserver.sh so it can understand the requests and return not only single file.

Conclusion
This web server is with a very limited application and barely usable, but for some purposes does a perfect job.

3 Responses to “A dead simple webserver in bash”

  1. 1
    bat.Serjo Says:

    1. По-голяма боза (претендираща да бъде уеб сървър) не съм виждал.
    2. На питон можеш на 20 реда да направиш истински уеб сървър, който да ти прави какви ли не статистики.
    3. Всякакви опити да се пише на език който няма елементарна концепция за типове е садистично. Ако sh, ksh, ash, bash, tcsh, csh, zsh, i t.n sh(it)-ове бяха пълнценни програмни езици днес света щеше да е едно по-добро място, и нямаше да има толкова интерпретации по темата обвивка.

  2. 2
    zeridon Says:

    Никой не е казал че това е пълноценен web сървър. Това е просто тъп прост и направо идиотски сървър който има за цел да сервира нещо супер елементарно.

  3. 3
    t3rr10n Says:

    wow, bat.Serjo ne si popliuva i mrazi shell-a :) dotolkova go mrazi, 4e sigurno i v benzinostancii shell ne stypva

Leave a comment

You must be logged in to post a comment.


In Fire We Trust

Say NO to trud