2018-01-12 05:16:05 +00:00
|
|
|
# libuhttpd([中文](/README_ZH.md))
|
2017-11-09 04:54:28 +00:00
|
|
|
|
2018-03-05 05:54:46 +00:00
|
|
|
[1]: https://img.shields.io/badge/license-LGPL2-brightgreen.svg?style=plastic
|
2018-01-21 11:15:09 +00:00
|
|
|
[2]: /LICENSE
|
|
|
|
[3]: https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=plastic
|
|
|
|
[4]: https://github.com/zhaojh329/libuhttpd/pulls
|
|
|
|
[5]: https://img.shields.io/badge/Issues-welcome-brightgreen.svg?style=plastic
|
|
|
|
[6]: https://github.com/zhaojh329/libuhttpd/issues/new
|
2018-03-12 02:58:43 +00:00
|
|
|
[7]: https://img.shields.io/badge/release-2.0.2-blue.svg?style=plastic
|
2018-01-21 11:15:09 +00:00
|
|
|
[8]: https://github.com/zhaojh329/libuhttpd/releases
|
2018-03-08 15:47:04 +00:00
|
|
|
[9]: https://travis-ci.org/zhaojh329/libuhttpd.svg?branch=master
|
|
|
|
[10]: https://travis-ci.org/zhaojh329/libuhttpd
|
2018-01-21 11:15:09 +00:00
|
|
|
|
|
|
|
[![license][1]][2]
|
|
|
|
[![PRs Welcome][3]][4]
|
|
|
|
[![Issue Welcome][5]][6]
|
|
|
|
[![Release Version][7]][8]
|
2018-03-08 15:47:04 +00:00
|
|
|
[![Build Status][9]][10]
|
2017-11-09 04:54:28 +00:00
|
|
|
|
2018-01-04 05:03:02 +00:00
|
|
|
[libubox]: https://git.openwrt.org/?p=project/libubox.git
|
|
|
|
[uhttpd]: https://git.openwrt.org/?p=project/uhttpd.git
|
|
|
|
[ustream-ssl]: https://git.openwrt.org/?p=project/ustream-ssl.git
|
2017-12-29 06:40:58 +00:00
|
|
|
[openssl]: https://github.com/openssl/openssl
|
|
|
|
[mbedtls]: https://github.com/ARMmbed/mbedtls
|
|
|
|
[CyaSSl(wolfssl)]: https://github.com/wolfSSL/wolfssl
|
2017-11-12 08:28:09 +00:00
|
|
|
|
2018-02-09 16:42:38 +00:00
|
|
|
A Lightweight and fully asynchronous HTTP server library based on [libubox] and referenced
|
|
|
|
from [uhttpd] for Embedded Linux.
|
2017-11-09 05:29:10 +00:00
|
|
|
|
2017-11-11 11:06:51 +00:00
|
|
|
`Keep Watching for More Actions on This Space`
|
|
|
|
|
2017-11-10 11:16:47 +00:00
|
|
|
# Features
|
2018-01-03 02:50:49 +00:00
|
|
|
* Action - processes requests by invoking registered C functions which mapped to a specific path.
|
2018-02-09 16:42:38 +00:00
|
|
|
* Lightweight and fully asynchronous
|
2018-01-03 02:50:49 +00:00
|
|
|
* 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
|
2018-02-09 16:42:38 +00:00
|
|
|
* Lua Template - Embed Lua code into HTML code, like embedding PHP into HTML
|
2018-01-03 02:50:49 +00:00
|
|
|
|
|
|
|
# TO DO
|
|
|
|
* Lua API - Using Lua programming
|
2017-11-12 08:28:09 +00:00
|
|
|
|
2017-12-29 06:40:58 +00:00
|
|
|
# Dependencies
|
|
|
|
* [libubox]
|
2018-01-03 02:50:49 +00:00
|
|
|
* [ustream-ssl] - If you need to support SSL
|
|
|
|
* [mbedtls] - If you choose mbedtls as your SSL backend
|
|
|
|
* [CyaSSl(wolfssl)] - If you choose wolfssl as your SSL backend
|
|
|
|
* [openssl] - If you choose openssl as your SSL backend
|
2017-11-12 08:28:09 +00:00
|
|
|
|
2017-12-30 04:41:43 +00:00
|
|
|
# Configure
|
2017-11-12 08:28:09 +00:00
|
|
|
See which configuration are supported
|
2018-02-19 06:14:20 +00:00
|
|
|
|
2017-12-30 04:41:43 +00:00
|
|
|
~/libuhttpd/$ mkdir build && cd build
|
2017-12-29 06:46:40 +00:00
|
|
|
~/libuhttpd/build$ cmake .. -L
|
|
|
|
~/libuhttpd/build$ cmake .. -LH
|
2017-12-30 04:41:43 +00:00
|
|
|
|
2017-12-30 09:42:50 +00:00
|
|
|
# Build and install
|
2017-12-30 04:41:43 +00:00
|
|
|
|
|
|
|
~/libuhttpd/build$ make && sudo make install
|
|
|
|
|
2018-01-05 13:46:03 +00:00
|
|
|
# Run Example
|
2017-11-12 08:28:09 +00:00
|
|
|
Run
|
|
|
|
|
2018-01-05 13:46:03 +00:00
|
|
|
~/libuhttpd/build$ ./example/helloworld
|
2017-11-12 08:28:09 +00:00
|
|
|
|
2017-11-14 14:16:26 +00:00
|
|
|
Then use the command curl or browser to test
|
2017-11-12 08:28:09 +00:00
|
|
|
|
2017-12-29 09:58:55 +00:00
|
|
|
$ curl -k 'https://127.0.0.1:8000/hello?name=test' -d '{"name":"libuhttpd"}' -v
|
2018-01-04 04:46:48 +00:00
|
|
|
|
2018-03-11 04:38:36 +00:00
|
|
|
# Install on OpenWrt
|
|
|
|
opkg update
|
|
|
|
opkg list | grep libuhttpd
|
|
|
|
opkg install libuhttpd-nossl
|
2018-01-04 04:46:48 +00:00
|
|
|
|
2018-03-11 04:38:36 +00:00
|
|
|
If the install command fails, you can [compile it yourself](/BUILDOPENWRT.md).
|
2018-01-04 04:46:48 +00:00
|
|
|
|
2017-12-29 06:40:58 +00:00
|
|
|
# Example
|
2017-12-29 06:48:59 +00:00
|
|
|
```
|
2017-12-29 06:40:58 +00:00
|
|
|
#include <uhttpd.h>
|
|
|
|
|
|
|
|
static void hello_action(struct uh_client *cl)
|
|
|
|
{
|
2017-12-29 09:48:06 +00:00
|
|
|
int body_len = 0;
|
2018-01-10 15:30:24 +00:00
|
|
|
|
2017-12-29 06:40:58 +00:00
|
|
|
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));
|
2018-01-01 03:44:46 +00:00
|
|
|
cl->chunk_printf(cl, "<h1>URL: %s</h1>", cl->get_url(cl));
|
2017-12-29 06:40:58 +00:00
|
|
|
cl->chunk_printf(cl, "<h1>PATH: %s</h1>", cl->get_path(cl));
|
|
|
|
cl->chunk_printf(cl, "<h1>QUERY: %s</h1>", cl->get_query(cl));
|
2018-01-01 03:44:46 +00:00
|
|
|
cl->chunk_printf(cl, "<h1>VAR name: %s</h1>", cl->get_var(cl, "name"));
|
2017-12-29 09:48:06 +00:00
|
|
|
cl->chunk_printf(cl, "<h1>BODY:%s</h1>", cl->get_body(cl, &body_len));
|
2017-12-29 06:40:58 +00:00
|
|
|
cl->request_done(cl);
|
|
|
|
}
|
|
|
|
|
2018-01-10 15:30:24 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-12-29 06:40:58 +00:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
struct uh_server *srv = NULL;
|
2018-01-10 15:30:24 +00:00
|
|
|
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);
|
2017-12-29 06:40:58 +00:00
|
|
|
|
|
|
|
uh_log_debug("libuhttpd version: %s", UHTTPD_VERSION_STRING);
|
|
|
|
|
|
|
|
uloop_init();
|
|
|
|
|
|
|
|
srv = uh_server_new("0.0.0.0", port);
|
|
|
|
if (!srv)
|
|
|
|
goto done;
|
|
|
|
|
2018-01-10 15:30:24 +00:00
|
|
|
#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)
|
2017-12-29 06:40:58 +00:00
|
|
|
goto done;
|
|
|
|
#endif
|
2018-01-10 15:30:24 +00:00
|
|
|
|
|
|
|
uh_log_debug("Listen on: %s *:%d", srv->ssl ? "https" : "http", port);
|
|
|
|
|
2018-01-01 02:30:41 +00:00
|
|
|
srv->add_action(srv, "/hello", hello_action);
|
2017-12-29 06:40:58 +00:00
|
|
|
|
|
|
|
uloop_run();
|
|
|
|
done:
|
|
|
|
uloop_done();
|
|
|
|
srv->free(srv);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2017-12-29 06:48:59 +00:00
|
|
|
```
|
2017-11-10 11:16:47 +00:00
|
|
|
|
|
|
|
# Contributing
|
2017-12-29 03:44:03 +00:00
|
|
|
If you would like to help making [libuhttpd](https://github.com/zhaojh329/libuhttpd) better,
|
|
|
|
see the [CONTRIBUTING.md](https://github.com/zhaojh329/libuhttpd/blob/master/CONTRIBUTING.md) file.
|
2018-03-16 06:06:22 +00:00
|
|
|
|
|
|
|
# QQ group: 153530783
|
|
|
|
|
|
|
|
# If the project is helpful to you, please do not hesitate to star. Thank you!
|