All Projects → cluoma → bittyhttp

cluoma / bittyhttp

Licence: GPL-3.0 license
A threaded HTTP library for building REST services in C.

Programming Languages

c
50402 projects - #5 most used programming language
Makefile
30231 projects

Projects that are alternatives of or similar to bittyhttp

Farwest
Framework for building RESTful HATEOAS-driven applications.
Stars: ✭ 18 (+50%)
Mutual labels:  http-server, rest-client
hunt-http
http library for D, support http 1.1 / http 2.0 (http2) / websocket server and client.
Stars: ✭ 29 (+141.67%)
Mutual labels:  http-server
poloniex-php-client
An efficient poloniex rest api client, built on top of guzzle
Stars: ✭ 18 (+50%)
Mutual labels:  rest-client
simple http server
simple http server for upload and download
Stars: ✭ 101 (+741.67%)
Mutual labels:  http-server
AutoIt-HTTP-Server
A simple http server powered by AutoIt
Stars: ✭ 14 (+16.67%)
Mutual labels:  http-server
csharp-http-client
Twilio SendGrid's C# HTTP Client for calling APIs
Stars: ✭ 25 (+108.33%)
Mutual labels:  rest-client
drogon
Drogon: A C++14/17/20 based HTTP web application framework running on Linux/macOS/Unix/Windows
Stars: ✭ 7,742 (+64416.67%)
Mutual labels:  http-server
http-graceful-shutdown
Gracefully terminates HTTP servers in Node.js
Stars: ✭ 79 (+558.33%)
Mutual labels:  http-server
zenith
⚡ Functional Scala HTTP server, client, and toolkit.
Stars: ✭ 15 (+25%)
Mutual labels:  http-server
http-server-static-content
Static content / file serving for Amp's HTTP server.
Stars: ✭ 19 (+58.33%)
Mutual labels:  http-server
dummyhttp
Super simple HTTP server that replies a fixed body with a fixed response code
Stars: ✭ 25 (+108.33%)
Mutual labels:  http-server
fusionauth-netcore-client
The .NET Core client for FusionAuth
Stars: ✭ 18 (+50%)
Mutual labels:  rest-client
Rhythm-CB-Scripts
Collection of scripts for use with Carbon Black Cb Response API
Stars: ✭ 14 (+16.67%)
Mutual labels:  rest-client
sevenbridges-python
SevenBridges Python Api bindings
Stars: ✭ 41 (+241.67%)
Mutual labels:  rest-client
NETMF-Toolkit
The .NET Micro Framework Toolkit (using Microsoft .NET Micro Framework)
Stars: ✭ 13 (+8.33%)
Mutual labels:  http-server
zhamao-framework
协程、高性能、灵活的聊天机器人 & Web 开发框架(炸毛框架)
Stars: ✭ 99 (+725%)
Mutual labels:  http-server
zipstream
A command line tool that allows you to easily share files and directories over the network
Stars: ✭ 49 (+308.33%)
Mutual labels:  http-server
aiohttp-json-rpc
Implements JSON-RPC 2.0 using aiohttp
Stars: ✭ 54 (+350%)
Mutual labels:  http-server
Mgx
🌈 A high performance network framework written in c++ (support tcp and http)
Stars: ✭ 15 (+25%)
Mutual labels:  http-server
ZiAPI
The elected Zia API for {Epitech} promo 2024 (Paris and Marseille)
Stars: ✭ 29 (+141.67%)
Mutual labels:  http-server

bittyhttp

A Linux, threaded HTTP library and basic webserver for creating REST services in C.

Building

make lib
make example

Basic Usage

#include "server.h"

int helloworld_handler(bhttp_request *req, bhttp_response *res);

int
main(int argc, char **argv)
{
    bhttp_server *server = bhttp_server_new();
    if (server == NULL) return 1;
    
    bhttp_server_set_ip(server, "0.0.0.0");
    bhttp_server_set_port(server, "8989");
    bhttp_server_set_docroot(server, "./www");
    bhttp_server_set_dfile(server, "index.html");
    
    bhttp_add_simple_handler(server,
                             BHTTP_GET | BHTTP_POST,  // declare supported http methods
                             "/helloworld",           // pattern to match uri path
                             helloworld_handler);     // callback function pointer
    
    bhttp_server_start(server, 0);
    
    return 0;
}

Starting Server

The second argument to bhttp_server_start decides how the server is started. Setting own_thread to 1 will start bittyhttp in a separate thread and return immediately. It is then up to the caller to later call bhttp_server_stop and bhttp_server_free.

While running in a separate thread, it is possible for the caller to add new handlers.

Alternatively, setting own_thread to 0 with take over the calling thread and will never return unless bittyhttp encounters an error.

Handlers

In addition to simply serving files, bittyhttp also has several different handler types that the user can define. Check out examples/examples.c for even more examples.

Handlers are matched in the order they are added. If two handlers would match the same uri path, then the handler added first will get the callback.

Simple Handler

Simple handlers must match the uri path exactly.

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;
}

bhttp_add_simple_handler(&server, BHTTP_GET, "/helloworld", helloworld_handler);

Regex Handler

Regex handlers use Linux's POSIX regex library to match on the uri path. Any matched groups will also be passed to the handler function.

int
helloworld_regex_handler(bhttp_request *req, bhttp_response *res, bvec *args)
{
    /* business logic */
    bstr bs;
    bstr_init(&bs);
    bstr_append_printf(&bs, "<html><p>Hello, Regex world! from URL: %s</p><p>%s</p><p>%s</p>",
                       bstr_cstring(&req->uri),
                       bstr_cstring(&req->uri_path),
                       bstr_cstring(&req->uri_query));
    /* check the request for a specific header */
    bhttp_header *h = bhttp_req_get_header(req, "accept-encoding");
    if (h)
        bstr_append_printf(&bs, "<p><b>accept-encoding</b>: %s</p>", bstr_cstring(&h->value));
    /* add all Regex matched groups to our output */
    for (int i = 0; i < bvec_count(args); i++)
    {
        bstr *arg = bvec_get(args, i);
        bstr_append_printf(&bs, "<p>arg: %d: %s</p>", i, bstr_cstring(arg));
    }
    bstr_append_cstring_nolen(&bs, "</html>");

    bhttp_res_set_body_text(res, bstr_cstring(&bs));
    bstr_free_contents(&bs);
    /* custom headers and response code */
    bhttp_res_add_header(res, "content-type", "text/html");
    res->response_code = BHTTP_200_OK;
    return 0;
}

bhttp_add_regex_handler(&server, BHTTP_GET | BHTTP_HEAD, "^/api/([^/]+)/([^/]+)$", helloworld_regex_handler);

File Handlers

Instead of using bhttp_res_set_body_text, we can use the function bhttp_set_body_file_rel/abs to return a file. This is more efficient than than supplying the binary data ourselves because sendfile can avoid unecessary data copying.

bhttp_res_set_body_file_rel will append the uri path to bittyhttps docroot. bhttp_res_set_body_file_abs will take the given filepath as is, and try to serve it.

If bittyhttp cannot read the file or the file is not found, a 404 message is returned.

int
rel_file_handler(bhttp_request *req, bhttp_response *res)
{
    bhttp_res_set_body_file_rel(res, "/hugo/404.html");
    res->response_code = BHTTP_200_OK;
    return 0;
}

bhttp_add_simple_handler(&server, BHTTP_GET, "/rel_file", rel_file_handler);

Threads

bittyhttp uses a new thread for each request. It is recommended that only threadsafe functions be used inside callback handlers. Additionally, appropriate structures should be used when callback handlers access the same data: mutexes, pools, etc.

Sites Using bittyhttp

  • squid poll - create and share polls for fun. it's squidtastic!

Roadmap

In the future I would like to add the following features to bittyhttp:

  • file/multipart upload support
  • Lua integration for handlers

Use of other code

This project would not be possible without the following resources:

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].