Tag

Showing blog posts sorted under the tag: rest

More Threading Possibilities in bittyhttp

bittyhttp is a C library that I've been working on that aims to make building web services in C as easy as possible. Check out my last post for a better description and to see some examples.

One use-case that I see for bittyhttp is adding HTTP functionality to existing C applications. For example, if there is a long-running process somewhere on a server, it would be easy to expose some of its configuration over a web interface using bittyhttp. So to target this use-case there are a few changes to bittyhttp to make this particular situation easier.

Users can now start the server in its own thread. For example:

int
main(int argc, char **argv)
{
    bhttp_server *server = bhttp_server_new();
    /* setup server, add handlers, etc. */
    bhttp_server_start(server, 1);
   
    /* continue with normal application execution */

    bhttp_server_stop(server);
    bhttp_server_free(server);
    return 0;
}

This starts bittyhttp in a separate thread and returns immediately. So one could insert this at the appropriate place in an application to easily spin off an HTTP server. Then, when needed, a simple call to bhttp_server_stop and bhttp_server_free will shut down the server and free resources.

In this mode it is also possible to register new callback handlers while the server is running, adding even more flexibility. Overall, this is a small update but I think it makes it much easier to use and incorporate into other applications.

In other developments, I've also been experimenting with some sort of Lua interface to allow writing callbacks in Lua. I've actually used this in another project of mine but I'm not particularly happy with the implementation. So it's still squarely in the experimental category.

bittyhttp is distributed under GPLv3. However, if you're interested in incorporating it into your project, and need another license, feel free to reach out.

Thanks for reading!


Tags:


bittyhttp - A Threaded Library for Building REST Services in C

bittyhttp is a new library that I've been working on that aims to make building web services in C as easy as possible. Microservices and HTTP APIs are very common these days and bittyhttp offers the ability to implement these in C without much hassle. It takes care of running the server so all the user needs to do is implement their callbacks.

When using bittyhttp, the user registers handlers to URLs with a callback function pointer. If an HTTP request is received that matches the handler URL, the callback is executed. Inside the callback, information about the HTTP request is exposed and allows the user to decide how they would like to handle the request. If no handler is found, bittyhttp defaults to acting like a standard webserver.

Check out the full project on GitHub.

A Quick Example

For instance, we can register a simple handler like this:

bhttp_add_simple_handler(server,
                            BHTTP_GET | BHTTP_POST,  // http methods
                            "/helloworld",           // pattern to match
                            helloworld_handler);     // callback function

And then implement whatever logic we need in the callback like this:

int helloworld_handler(bhttp_request *req, bhttp_response *res)
{
    /* business logic */
    bstr bs;
    bstr_init(&bs);
    bstr_append_printf(&bs, "<html><p>Hello, world! from URL: %s</p><p>%s</p><p>%s</p></html>",
                      bstr_cstring(&req->uri),
                      bstr_cstring(&req->uri_path),
                      bstr_cstring(&req->uri_query));
    bhttp_res_set_body_text(res, bstr_cstring(&bs));
    bstr_free_contents(&bs);
    
    /* add custom headers and response code */
    bhttp_res_add_header(res, "content-type", "text/html");
    res->response_code = BHTTP_200_OK;
    return 0;
}

Because bittyhttp uses a separate thread to handle each request, some care needs to be taken in the callbacks to prevent race conditions. Changing data that is not allocated inside of a callback will often require the use of mutexes or similar data structures. A database connection pool, for example, would need to be properly managed.

Use Cases

I see bittyhttp having 2 primary use cases. The first being to implement often-used API endpoints in C, for performance reasons. Perhaps authentication endpoints get hit a lot and you want to speed these up.

The second is adding HTTP support to an existing C application. Maybe you have an existing long-running application on a server somewhere and you want to expose some of its configurations via HTTP. In that case, bittyhttp would be an excellent option.

bittyhttp is GPLv3 licensed, and available on GitHub, but I would be open to relicensing it for specific cases. If you are interested in using it in your application, feel free to get in contact with me at [email protected].

squid poll

As a fun, little proof-of-concept, I created a site called squid poll. It's basically a Straw Poll clone that uses bittyhttp as its API backend. Feel free to visit the site to create your own poll, or fill out the one below:


Tags: