How to use the Busybox Web Server (httpd)
Posted: Fri 27 Dec 2019, 07:03
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:
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:
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
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
Code: Select all
httpd -v -f -h /var/www/html -c /usr/share/pkg/testing/webserver/httpd/httpd.conf
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>'
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]
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