Busybox has a tiny, simple yet powerful http server. By modifying it a little bit, we can get a very simple and powerful web application with C efficiency.
To create a http based application, the good starting point is to copy httpd.c and modify to suit the new need. To make the change, a few things need to be done (busybox 1.20.1):
- Copy “networking/httpd.c” into “networking/httpappd.c”. Edit the “httpappd.c” to change the line with “//usage:…httpd_…” into “//usage:…httpappd_…”. These lines will be scanned and parsed by the build scripts to generate helper source code for busybox.
- Edit “networking/Kbuild.src”, “networking/Config.src” and “include/applets.src.h”. Copy the sections for “HTTPD” or for “httpd”, then modify the sections into for “HTTPAPPD” and “httpappd” respectively.
- Run make menuconfig and make as normal. Busybox will now contain an application called httpappd.
- Modify httpappd.c to implement the new application, and rebuild everything.
Since the cgi part of the source code shows a clear modular boundary between the http server and backend cgi handlers, it is a natural point to start the modification work. Change the “cgi-bin/” default url prefix to the top-level dir name as a key to the new application. And invoke a fixed app instead of an app resolved from the bottom name of the url path.
For example, if the url is “http://some.site.com/app/cat/box/search.cgi”, the original httpd code checks and see if “app” matches “cgi-bin”. If the code change to match “app” instead of “cgi-bin”, then in addition the originalcode will change into “app/cat/box” directory and invoke “search.cgi”. The modified code, will invoke a backend application, and pass the whole “app/cat/box/search.cgi” into the application to further decode.
An article on the web explains how you can add a command to earlier busybox versions:
- Write your application main function e.g. “int newcommand_main()”, that includes “busybox.h”, into a busybox subdir.
- Edit “makefile.in” and “config.in” to add the .o file and the configuration name.
- Edit “include/applets.h” and “include/usage.h” to declare the command, the main, and usage.
This is a simple and straight solution.