2017-12-29 03:44:03 +00:00
|
|
|
# libuhttpd([中文](https://github.com/zhaojh329/libuhttpd/blob/master/README_ZH.md))
|
2017-11-09 04:54:28 +00:00
|
|
|
|
|
|
|
![](https://img.shields.io/badge/license-GPLV3-brightgreen.svg?style=plastic "License")
|
|
|
|
|
2017-12-29 06:40:58 +00:00
|
|
|
[libubox]: https://git.lede-project.org/?p=project/libubox.git
|
|
|
|
[uhttpd]: https://git.lede-project.org/?p=project/uhttpd.git
|
|
|
|
[ustream-ssl]: https://git.lede-project.org/?p=project/ustream-ssl.git
|
|
|
|
[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
|
|
|
|
2017-12-29 06:40:58 +00:00
|
|
|
A very tiny and fast 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
|
2017-11-12 08:28:09 +00:00
|
|
|
* tiny and fast
|
2017-12-29 06:40:58 +00:00
|
|
|
* use [libubox] as its event backend
|
|
|
|
* support HTTPS: OpenSSL, mbedtls and CyaSSl(wolfssl)
|
2017-11-12 08:28:09 +00:00
|
|
|
* flexible and you can easily extend your application to have HTTP/HTTPS services
|
2017-11-15 05:21:02 +00:00
|
|
|
* code structure is concise and understandable, also suitable for learning
|
2017-11-12 08:28:09 +00:00
|
|
|
|
2017-12-29 06:40:58 +00:00
|
|
|
# Dependencies
|
|
|
|
* [libubox]
|
|
|
|
* [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
|
|
|
|
|
|
|
## Configure
|
|
|
|
See which configuration are supported
|
|
|
|
|
2017-12-29 06:46:40 +00:00
|
|
|
~/libuhttpd/build$ cmake .. -L
|
|
|
|
~/libuhttpd/build$ cmake .. -LH
|
2017-11-12 08:28:09 +00:00
|
|
|
|
|
|
|
## Run the Example
|
|
|
|
First generate the SSL certificate file
|
|
|
|
|
2017-12-29 06:46:40 +00:00
|
|
|
~/libuhttpd/build$ cd ..
|
|
|
|
~/libuhttpd$ ./gen_cert.sh
|
2017-11-10 11:16:47 +00:00
|
|
|
|
2017-11-12 08:28:09 +00:00
|
|
|
Run
|
|
|
|
|
2017-12-29 06:46:40 +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 06:40:58 +00:00
|
|
|
$ curl -k 'https://127.0.0.1:8000/hello?name=test' -v
|
2017-11-10 11:16:47 +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>
|
|
|
|
|
|
|
|
#define port "8000"
|
|
|
|
|
|
|
|
static void hello_action(struct uh_client *cl)
|
|
|
|
{
|
|
|
|
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>PATH: %s</h1>", cl->get_path(cl));
|
|
|
|
cl->chunk_printf(cl, "<h1>QUERY: %s</h1>", cl->get_query(cl));
|
|
|
|
cl->request_done(cl);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
struct uh_server *srv = NULL;
|
|
|
|
|
|
|
|
uh_log_debug("libuhttpd version: %s", UHTTPD_VERSION_STRING);
|
|
|
|
|
|
|
|
uloop_init();
|
|
|
|
|
|
|
|
srv = uh_server_new("0.0.0.0", port);
|
|
|
|
if (!srv)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
uh_log_debug("Listen on: *:%s", port);
|
|
|
|
|
|
|
|
#if (UHTTPD_SSL_SUPPORT)
|
|
|
|
if (srv->ssl_init(srv, "server-key.pem", "server-cert.pem") < 0)
|
|
|
|
goto done;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
uh_add_action(srv, "/hello", hello_action);
|
|
|
|
|
|
|
|
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.
|