I have chosen to avoid using a run-time configuration file. Most parameters are defined in a .h file and some can be overridden through the environment variables. Here is the list of the parameters:
- $HTTAP_TCPPORT : sets the listening port of the server. Similar to Apache's Listen in ports.conf.
- $HTTAP_KEEPALIVE : number of seconds for the HTTP timeout (bound between 3 and 200, default is 15). This is equivalent to KeepAliveTimeout in Apache's configuration file.
If you enable the "file server" (by compiling with the -DHTTAP_SERVE_FILES option), more parameters appear:
- $HTTAP_STATICPATH : when set, sets the working directory where files are stored. If running as root, chroot() to it. Similar to Apache's DocumentRoot
- $HTTAP_ROOTPAGE : specifies the file that is handed when the resource path "/" is requested.
- $HTTAP_USER : if running as root, drop admin rights and change to this user (hopefully the system is configured to give this user minimal rights) Similar to APACHE_RUN_USER
- $HTTAP_GROUP : if $HTTAP_USER is set, also change the current group ID (similar to APACHE_RUN_GROUP)
Environment variables are both simpler than a configuration file and does not interfere with command line parameters, which are often critical to the main/host application. Furthermore, it's a bit more script-friendly and allows automation. This is excellent when you run several programs simultaneously, just increase some variables in a loop and you're done.
You could configure the server (before running it) with the following script:
# change the default TCP port and keepalive values export HTTAP_TCPPORT=61234 export HTTAP_KEEPALIVE=123 # use the current directory to serve the files unset HTTAP_STATICPATH # use the default root page unset HTTAP_ROOTPAGE # create an empty root page touch index.html # don't forget to run useradd and groupadd export HTTAP_USER=httap_sandbox export HTTAP_GROUP=httap_sandbox # start the server's program ./HTTaP
This will (one day) serve an empty page for the root in the current directory.
The call graph is :
MyApplication.c // contains your main() and infinite loop |__ HTTaP.h // user configurable values |__ HTTaP.c // the server's boring posixy code |___ your code to process dynamic addressesYou'll want to look at HTTaP.h which contains the user-modifiable default parameters, though most of them are preferably updated by the above environment variables.
It also contains compile-time flags that enable certain features:
- HTTAP_VERBOSE : printf() some useful information (good for debugging)
- SHOW_CLIENT : add more verbosity
- ENABLE_LOOPBACK : defined if a Loopback server is implemented.
- HTTAP_SERVE_FILES : this is the most important switch. This enables the static files server.
Inside your code, you can switch from polled mode to blocking mode (and vice versa) with the following functions:
void HTTaP_polled() void HTTaP_blocking()
The server starts in polled mode, which requires repetitive calls to the HTTaP_server() function. If your program has finished working and waits for more commands, you can save CPU cycles (and battery ?) by switching to the blocking mode. You can revert to polled when a valid command is received.