How to use the Busybox Web Server (httpd)

How to do things, solutions, recipes, tutorials
Post Reply
Message
Author
s243a
Posts: 2580
Joined: Tue 02 Sep 2014, 04:48
Contact:

How to use the Busybox Web Server (httpd)

#1 Post by s243a »

As some people know, busybox comes with a web server called httpd, which is completely different then the openbsd server by the same name. Here is an example of how someone might start the busybox httpd web server:

Code: Select all

httpd -v -f -h /var/www/html -c /usr/share/pkg/testing/webserver/httpd/httpd.conf
You can use the -c option to specify a configuration file as I did above but at the moment I don't have much use for the configuation file. Some documentation about the config file can be found at [1]:
https://oldwiki.archive.openwrt.org/doc ... http.httpd

The "-v" is the verbose option and the "-f" means run the server in the forground. These two options are useful for troubleshooting. I learned this from [2]:
https://blog.madadipouya.com/2018/06/03 ... g-busybox/

Now for troubleshooting it might be helpful to type in a url in the web-server and see the contents of the directory. Unlike apache and openbsd-httpd, you cannot do this by specifing the option, "directory auto index". Instead, you have to do this -- as noted in the source -- with a cgi, script called cgi-bin/index.cgi.

There is an example "c-based" cgi script in the source to do this but in my opnion "c code" is much more complicated than a simple bash scripts. I wrote the following bash script to list the contents of the directory and all the enviornmental varialbes:

Code: Select all

#!/bin/sh
echo "Content-type: text/html" # Tells the browser what kind of content to expect
echo "" # An empty line. Mandatory, if it is missed the page content will not load
echo '<html>
<header><title>This is title</title></header>
<body><pre>'
ls -a "${PWD}/..${REQUEST_URI}"
echo ""
printenv
echo '</pre></body>
</html>'
The key thing here is that first you have to output the html header, followed by a blank line [2], and then followed by the html code. The script must be placed in the cgi-bin sub directory of the web server home directory. The web server home directory is specified by the "-h" option when starting httpd. For the cgi script to work it must have executable permissions.

Note that you can put html in any sub directory of the server home director and the httpd web server will render it. To start I recommend googling how to do a simple "HTML hello world".

P.S. here is some exampales of the exported cgi variables

Code: Select all

CONTENT_TYPE=application/x-www-form-urlencoded
GATEWAY_INTERFACE=CGI/1.1
REMOTE_ADDR=192.168.1.180
QUERY_STRING=Zbr=1234567&SrceMB=&ime=jhkjhlkh+klhlkjhlk+%A9%D0%C6%AE%C6%AE&prezime=&sektor=OP
REMOTE_PORT=2292
CONTENT_LENGTH=128
REQUEST_URI=/cgi-bin/test
SERVER_SOFTWARE=busybox httpd/1.35 6-Oct-2004
PATH=/bin:/sbin:/usr/bin:/usr/sbin
HTTP_REFERER=http://192.168.1.1/index1.html
SERVER_PROTOCOL=HTTP/1.0
PATH_INFO=
REQUEST_METHOD=POST
PWD=/www/cgi-bin
SERVER_PORT=80
SCRIPT_NAME=/cgi-bin/test
REMOTE_USER=[http basic auth username]
https://openwrt.org/docs/guide-user/ser ... figuration

but if you use the printenv command that I use in my example cgi script you can get real values rather than examples from the documentation.


Notes
------------
1 - https://oldwiki.archive.openwrt.org/doc ... http.httpd
2 - https://blog.madadipouya.com/2018/06/03 ... g-busybox/
3 - https://busybox.net/downloads/BusyBox.html
Find me on [url=https://www.minds.com/ns_tidder]minds[/url] and on [url=https://www.pearltrees.com/s243a/puppy-linux/id12399810]pearltrees[/url].

User avatar
sc0ttman
Posts: 2812
Joined: Wed 16 Sep 2009, 05:44
Location: UK

Server side includes with busybox httpd

#2 Post by sc0ttman »

Just found this the other day:

https://raw.githubusercontent.com/brgl/ ... ttpd_ssi.c

Compile it with:

Code: Select all

gcc -static httpd_ssi.c -o httpd_ssi
Then in your httpd.conf, put the following:

Code: Select all

*.html:/bin/httpd_ssi
*.htm:/bin/httpd_ssi
You can then use "Server side includes" (SSI) with your busybox httpd :)

Add the server side includes into your HTML pages like so:

Code: Select all

<p>Some text here</p>

<!--#include file="some-file.html" -->

<p>Some more</p>
[b][url=https://bit.ly/2KjtxoD]Pkg[/url], [url=https://bit.ly/2U6dzxV]mdsh[/url], [url=https://bit.ly/2G49OE8]Woofy[/url], [url=http://goo.gl/bzBU1]Akita[/url], [url=http://goo.gl/SO5ug]VLC-GTK[/url], [url=https://tiny.cc/c2hnfz]Search[/url][/b]

Post Reply