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 addresses
You'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.
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.
Hey Yann.
You said "when set, sets the working directory where files are stored. If running as root, chroot() to it."
Does that mean I could serve a network OS over it by handing it the directory structures?? It could be useful for the metacompute networks to build themselves by spawning a copy of a parent to all the robots that join it. This would guarantee a controllable and known environment for the system to operate in without a central server or dedicated nodes - it would be a dynamically built static network satisfying my security concerns. It would also be a lot easier to administrate multi-network personalities depending on whose machine hosted the session, and share data among the nodes. Each robot has its own identity which it imposes upon the session it creates at run time, or runs stand-alone and can do singular research using it and bring that research to the table in a session. There then exists the possibility to join these networks together to make a still-wider network intelligence.
Are you sure? yes | no
You do what you want :-D
$HTTAP_STATICPATH is the environment variable that tells where to fetch static files, it's not meant to change at run time. It's just like Apache's DocumentRoot variable.
Are you sure? yes | no