How To Develop A Simple Plug In (Module) For Lighttpd

By | 10:42 AM 1 comment

This tutorial shows you step by step to develop a simple plugin (module) of lighttpd.
What do you need to develop a plug in?
- Add a plugin into lighttpd package! 
- Handle a URI!
- send a simple message to client!

Now, Start!
A/ Add a plugin into lighttpd package!
The best start point is the inherited mod_skeleton.c which just contains a basic skeleton and a good simple guide. The mod_skeleton.c is placed in <lighttpd directory>/src/mod_skeleton.c.
Copy & replace "skeleton" by your plugin's name as below (use "Terminal").
assume your plugin's name is "xxx":
$cd <lighttpd directory>
$cd src
$cp mod_skeleton.c mod_xxx.c
$vi mod_xxx.c
How to compile?
Paste the following into <lighttpd directory>/src/
mod_xxx_la_SOURCES = mod_xxx.c
mod_xxx_la_LDFLAGS = -module -export-dynamic -avoid-version -no-undefined
mod_xxx_la_LIBADD = $(common_libadd)
Then reconfigure and compile lighttpd package for ARM CROSS COMPILE again!

After all, the lighttpd still unknown your plug in, you have to add your plugin into lighttpd's configuration file as below:
server.modules = (
More information about lighttpd's configuration file, refer at


B/ Handle a URI!
Now, take look at your plugin, mod_xxx.c, you should see:
mod_xxx_plugin_init(plugin *p):
Each plugin has to have a ..._plugin_init function which is called when the plugin is called. In this function, need to initial plugin structure P as below:
    size_t version;

    buffer *name; /* name of the plugin */

    void *(* init)();
    handler_t (* set_defaults) (server *srv, void *p_d);
    handler_t (* cleanup) (server *srv, void *p_d);
    handler_t (* handle_trigger) (server *srv, void *p_d); /*is called once a second */
    handler_t (* handle_sighup) (server *srv, void *p_d);/* is called at a signup */

    handler_t (* handle_uri_raw)  (server *srv, connection *con, void *p_d);  /*is called after uri_raw is set */
    handler_t (* handle_uri_clean)       (server *srv, connection *con, void *p_d);    /*is called after uri is set */
    handler_t (* handle_docroot)         (server *srv, connection *con, void *p_d);    /*is called getting the document-root */
    handler_t (* handle_physical)        (server *srv, connection *con, void *p_d);    /*is called mapping url to physical path */
    handler_t (* handle_request_done)    (server *srv, connection *con, void *p_d);    /*is called at the end of a request */
    handler_t (* handle_connection_close)(server *srv, connection *con, void *p_d);    /*is called at the end of a connection */
    handler_t (* handle_joblist)         (server *srv, connection *con, void *p_d);    /*is called after all events are handled */

    handler_t (* handle_subrequest_start)(server *srv, connection *con, void *p_d); /*is called when a handler for the request has to be found*/

    handler_t (* handle_subrequest)      (server *srv, connection *con, void *p_d); 

    handler_t (* connection_reset)       (server *srv, connection *con, void *p_d);
    void *data;
    void *lib;
In this tutorial, URI will be handled by function which assign to handle_uri_clean. In your plugin, you can use others.

Other information about URI, query,.... refer in connection structure.

C/ Send a response message!
The example below show you a simple way to send response message to client:
URIHANDLER_FUNC(mod_xxx_uri_handler) {
    plugin_data *p = p_d;
    buffer *b;


    if (con->mode != DIRECT) return HANDLER_GO_ON;

    if (con->uri.path->used == 0) return HANDLER_GO_ON;

    mod_xxx_patch_connection(srv, con, p);
    //Prepare response message
    b = chunkqueue_get_append_buffer(con->write_queue);
    buffer_copy_string_len(b, CONST_STR_LEN("This is query of request:"));
    //Send header
    response_header_overwrite(srv, con, CONST_STR_LEN("Content-Type"),
    con->file_finished = 1;
Below is lighttpd package & mod_xxx.c:
Newer Post Older Post Home

1 comment:

  1. Slots, Live Casino, and Table Games at Wynn Las
    Wynn Las Vegas and Encore 남양주 출장샵 are two extraordinary 의정부 출장마사지 gaming 구리 출장마사지 destinations in Las Vegas, created to 울산광역 출장안마 cater 문경 출장안마 all type of players. At Wynn Las Vegas, you