2017-12-30 01:27:06 +00:00
|
|
|
|
# libuhttpd
|
2017-11-30 00:46:38 +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-30 00:46:38 +00:00
|
|
|
|
|
2017-12-29 06:40:58 +00:00
|
|
|
|
一个专门针对嵌入式Linux的非常小巧且快速的HTTP服务器C库,基于[libubox],参考了[uhttpd]。
|
2017-11-30 00:46:38 +00:00
|
|
|
|
|
|
|
|
|
`请保持关注以获取最新的项目动态`
|
|
|
|
|
|
|
|
|
|
# 特性
|
2017-12-30 04:41:43 +00:00
|
|
|
|
* action: 通过调用映射到特定路径的已注册C函数来处理请求。
|
2017-11-30 00:46:38 +00:00
|
|
|
|
* 小巧且快速
|
2017-12-29 06:40:58 +00:00
|
|
|
|
* 使用[libubox]作为其事件后端
|
2017-12-30 04:41:43 +00:00
|
|
|
|
* 支持HTTPS: OpenSSL, mbedtls 和 CyaSSl(wolfssl)
|
2017-11-30 00:46:38 +00:00
|
|
|
|
* 可伸缩:你可以非常方便的扩展你的应用程序,使之具备HTTP/HTTPS服务
|
|
|
|
|
* 代码结构简洁通俗易懂,亦适合学习
|
|
|
|
|
|
2017-12-29 06:40:58 +00:00
|
|
|
|
# 依赖
|
|
|
|
|
* [libubox]
|
2017-12-29 07:00:37 +00:00
|
|
|
|
* [ustream-ssl](https://git.lede-project.org/?p=project/ustream-ssl.git): 如果你需要支持SSL
|
|
|
|
|
* [mbedtls](https://github.com/ARMmbed/mbedtls): 如果你选择mbedtls作为你的SSL后端
|
|
|
|
|
* [CyaSSl(wolfssl)](https://github.com/wolfSSL/wolfssl): 如果你选择wolfssl作为你的SSL后端
|
|
|
|
|
* [openssl](https://github.com/openssl/openssl): 如果你选择openssl作为你的SSL后端
|
2017-11-30 00:46:38 +00:00
|
|
|
|
|
2017-12-30 04:41:43 +00:00
|
|
|
|
# 配置
|
2017-11-30 00:46:38 +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
|
|
|
|
|
|
|
|
|
# 编译
|
|
|
|
|
|
|
|
|
|
~/libuhttpd/build$ make && sudo make install
|
|
|
|
|
|
|
|
|
|
# 编译示例程序
|
|
|
|
|
|
|
|
|
|
~/libuhttpd/build$ cd ../example
|
|
|
|
|
~/libuhttpd/example$ mkdir build && cd build
|
|
|
|
|
~/libuhttpd/example/build$ cmake .. && make
|
|
|
|
|
|
|
|
|
|
# 运行示例程序
|
2017-11-30 00:46:38 +00:00
|
|
|
|
首先生成SSL证书文件
|
|
|
|
|
|
2017-12-30 04:41:43 +00:00
|
|
|
|
~/libuhttpd/example/build$ cd ..
|
|
|
|
|
~/libuhttpd/example$ ./gen_cert.sh
|
2017-11-30 00:46:38 +00:00
|
|
|
|
|
|
|
|
|
运行
|
|
|
|
|
|
2017-12-30 04:41:43 +00:00
|
|
|
|
~/libuhttpd/example$ ./build/helloworld
|
2017-11-30 00:46:38 +00:00
|
|
|
|
|
|
|
|
|
然后使用命令curl或者浏览器进行测试
|
|
|
|
|
|
2017-12-29 09:58:55 +00:00
|
|
|
|
$ curl -k 'https://127.0.0.1:8000/hello?name=test' -d '{"name":"libuhttpd"}' -v
|
2017-12-29 06:40:58 +00:00
|
|
|
|
|
2017-12-30 04:41:43 +00:00
|
|
|
|
# 示例程序
|
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)
|
|
|
|
|
{
|
2017-12-29 09:48:06 +00:00
|
|
|
|
int body_len = 0;
|
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));
|
|
|
|
|
cl->chunk_printf(cl, "<h1>PATH: %s</h1>", cl->get_path(cl));
|
|
|
|
|
cl->chunk_printf(cl, "<h1>QUERY: %s</h1>", cl->get_query(cl));
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-30 00:46:38 +00:00
|
|
|
|
|
|
|
|
|
# 贡献代码
|
2017-12-29 03:44:03 +00:00
|
|
|
|
如果你想帮助[libuhttpd](https://github.com/zhaojh329/libuhttpd)变得更好,请参考
|
|
|
|
|
[CONTRIBUTING_ZH.md](https://github.com/zhaojh329/libuhttpd/blob/master/CONTRIBUTING_ZH.md)。
|
2017-11-30 00:46:38 +00:00
|
|
|
|
|
2017-12-05 09:35:24 +00:00
|
|
|
|
# 技术交流
|
|
|
|
|
QQ群:153530783
|