This is a maintenance fork of libuhttpd
 
 
 
Go to file
Jianhui Zhao cefd7505c0 Update README
Signed-off-by: Jianhui Zhao <jianhuizhao329@gmail.com>
2018-03-11 12:43:57 +08:00
cmake/Modules Fix FindLibuhttpd 2018-01-23 10:29:17 +08:00
example change license to LGPL2 2018-03-03 17:33:49 +08:00
src Bump version 2.0.0 2018-03-03 22:54:24 +08:00
.gitignore Improve CMakeLists.txt 2017-12-30 12:24:00 +08:00
.travis.yml Update travis 2018-03-08 23:55:34 +08:00
BUILDOPENWRT.md Update README 2018-03-11 12:43:57 +08:00
BUILDOPENWRT_ZH.md Update README 2018-03-11 12:43:57 +08:00
CMakeLists.txt Optimize build for example 2018-01-05 21:46:03 +08:00
CONTRIBUTING.md Update CONTRIBUTING 2018-01-18 10:20:03 +08:00
CONTRIBUTING_ZH.md Update CONTRIBUTING 2018-01-18 10:20:03 +08:00
LICENSE change license to LGPL2 2018-03-03 17:33:49 +08:00
README.md Update README 2018-03-11 12:43:57 +08:00
README_ZH.md Update README 2018-03-11 12:43:57 +08:00
gen_cert.sh Optimize build for example 2018-01-05 21:46:03 +08:00
openssl.cnf Optimize build for example 2018-01-05 21:46:03 +08:00

README.md

libuhttpd(中文)

license PRs Welcome Issue Welcome Release Version Build Status

A Lightweight and fully asynchronous HTTP server library based on libubox and referenced from uhttpd for Embedded Linux.

Keep Watching for More Actions on This Space

Features

  • Action - processes requests by invoking registered C functions which mapped to a specific path.
  • Lightweight and fully asynchronous
  • Use libubox as its event backend
  • Support HTTPS - OpenSSL, mbedtls and CyaSSl(wolfssl)
  • Flexible - you can easily extend your application to have HTTP/HTTPS services
  • Code structure is concise and understandable, also suitable for learning
  • Lua Template - Embed Lua code into HTML code, like embedding PHP into HTML

TO DO

  • Lua API - Using Lua programming

Dependencies

Configure

See which configuration are supported

~/libuhttpd/$ mkdir build && cd build
~/libuhttpd/build$ cmake .. -L
~/libuhttpd/build$ cmake .. -LH

Build and install

~/libuhttpd/build$ make && sudo make install

Run Example

Run

~/libuhttpd/build$ ./example/helloworld

Then use the command curl or browser to test

$ curl -k 'https://127.0.0.1:8000/hello?name=test' -d '{"name":"libuhttpd"}' -v

Install on OpenWrt

opkg update
opkg list | grep libuhttpd
opkg install libuhttpd-nossl

If the install command fails, you can compile it yourself.

Example

#include <uhttpd.h>

static void hello_action(struct uh_client *cl)
{
    int body_len = 0;

    cl->send_header(cl, 200, "OK", -1);
    cl->append_header(cl, "Myheader", "Hello");
    cl->header_end(cl);

    cl->chunk_printf(cl, "<h1>Hello Libuhttpd %s</h1>", UHTTPD_VERSION_STRING);
    cl->chunk_printf(cl, "<h1>REMOTE_ADDR: %s</h1>", cl->get_peer_addr(cl));
    cl->chunk_printf(cl, "<h1>URL: %s</h1>", cl->get_url(cl));
    cl->chunk_printf(cl, "<h1>PATH: %s</h1>", cl->get_path(cl));
    cl->chunk_printf(cl, "<h1>QUERY: %s</h1>", cl->get_query(cl));
    cl->chunk_printf(cl, "<h1>VAR name: %s</h1>", cl->get_var(cl, "name"));
    cl->chunk_printf(cl, "<h1>BODY:%s</h1>", cl->get_body(cl, &body_len));
    cl->request_done(cl);
}

static void usage(const char *prog)
{
    fprintf(stderr, "Usage: %s [option]\n"
        "          -p port  # Default port is 8080\n"
        "          -s       # SSl on\n"
        "          -v       # verbose\n", prog);
    exit(1);
}

int main(int argc, char **argv)
{
    struct uh_server *srv = NULL;
    int verbose = false;
    int ssl = false;
    int port = 8080;
    int opt;

    while ((opt = getopt(argc, argv, "p:vs")) != -1) {
        switch (opt)
        {
        case 'p':
            port = atoi(optarg);
            break;
        case 's':
            ssl = true;
            break;
        case 'v':
            verbose = true;
            break;
        default: /* '?' */
            usage(argv[0]);
        }
    }

    if (!verbose)
        ulog_threshold(LOG_ERR);
    
    uh_log_debug("libuhttpd version: %s", UHTTPD_VERSION_STRING);

    uloop_init();

    srv = uh_server_new("0.0.0.0", port);
    if (!srv)
        goto done;

#if (!UHTTPD_SSL_SUPPORT)
    if (ssl)
        uh_log_debug("SSl is not compiled in");
#else
    if (ssl && srv->ssl_init(srv, "server-key.pem", "server-cert.pem") < 0)
        goto done;
#endif

    uh_log_debug("Listen on: %s *:%d", srv->ssl ? "https" : "http", port);

    srv->add_action(srv, "/hello", hello_action);
    
    uloop_run();
done:
    uloop_done();
    srv->free(srv);
    
    return 0;
}

Contributing

If you would like to help making libuhttpd better, see the CONTRIBUTING.md file.