Close

Overview of the code

A project log for micro HTTP server in C

Connect your browser to your smart devices, using a minimalist HTTP compliant server written in POSIX/C

Yann Guidon / YGDESYann Guidon / YGDES 03/28/2017 at 03:052 Comments

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:

If you enable the "file server" (by compiling with the -DHTTAP_SERVE_FILES option), more parameters appear:

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:

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

Morning.Star wrote 03/31/2017 at 06:21 point

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

Yann Guidon / YGDES wrote 03/31/2017 at 06:35 point

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