libonion
|
libonion is a simple library to add HTTP functionality to your program. Be it a new program or to add HTTP functionality to an exisitng program, libonion makes it as easy as possible.
There are many documented examples at the examples directory (https://github.com/davidmoreno/onion/tree/master/examples/).
A basic example of a server with authentication, SSL support that serves a static directory is:
To be compiled as
gcc -o a a.c -I$ONION_DIR/src/ -L$ONION_DIR/src/onion/ -lonion_static -lpam -lgnutls -lgcrypt -lpthread
please modify -I and -L as appropiate to your installation.
pam, gnutls and pthread are necesary for pam auth, ssl and pthread support respectively.
libonion is based on request handlers to process all its request. The handlers may be nested creating onion like layers. The first layer may be checking the server name, the second some authentication, the third checks if you ask for the right path and the last returns a fixed message.
Handlers have always a next handler that is the handler that will be called if current handler do not want to process current request.
Normally for simple servers its much easier, as in the upper example.
Normal use of handler can be done using url handlers. They allow to create a named url and dispatch to a handler. It allows pattern capturing at the url using regex groups.
Check documentation at the url
module.
Creating a custom handler is just to create a couple of functions with this signature:
Then create a onion_handler object with
Normally this creation is encapsulated in a function that also sets up the private data, and that returns the onion_handler to be used where necesary.
As you can be seen it have its own private data that is passed at creation time, and that should be freed using the custom_handler_free when libonion feels its time.
The custom_handler must then make use of the onion_response object, created from the onion_request. For example:
libonion has support for some external library very usefull addons. Make sure you have the development libraries when compiling libonion to have support for it.
libonion has SSL support by using GNUTLS. This is however optional, and you can disable compilation by not having the development libraries, or modifing /CMakeLists.txt.
Once you have support most basic use is just to set a certificate and key file (can be be on the same PEM file). With just that you have SSL support on your server.
libonion has threads support. It is not heavily mutexed as this impacts performace, but basic structures that are used normally are properly mutexed.
Anyway its on your own handlers where you have to set your own mutex areas. See examples/oterm/oterm_handler.c for an example on how to do it.
Specifically its not mutexed the handler tree, as its designed to be ready on startup, and not modified in all its lifespan.
Also there is an option to listen on another thread, using the O_DETACH_LISTEN flag on onion creation.