Web Application Backend by Busybox HTTPD

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):

  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.
  2. 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.
  3. Run make menuconfig and make as normal. Busybox will now contain an application called httpappd.
  4. 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:

  1. Write your application main function e.g. “int newcommand_main()”, that includes “busybox.h”, into a busybox subdir.
  2. Edit “makefile.in” and “config.in” to add the .o file and the configuration name.
  3. Edit “include/applets.h” and “include/usage.h” to declare the command, the main, and usage.
  4. Build.

This is a simple and straight solution.

Or, check out the more complete article on an IBM page. (There is a link on the page to a tiny linux, another interesting project!)


About minghuasweblog

a long time coder
This entry was posted in All, C/C++, LAMP and Drupal and tagged , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s