2017-12-29 06:40:58 +00:00
|
|
|
|
# libuhttp
|
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-29 06:40:58 +00:00
|
|
|
|
* 使用[libubox]作为其事件后端
|
|
|
|
|
* 支持HTTPS: OpenSSL, mbedtls and CyaSSl(wolfssl)
|
2017-11-30 00:46:38 +00:00
|
|
|
|
* 可伸缩:你可以非常方便的扩展你的应用程序,使之具备HTTP/HTTPS服务
|
|
|
|
|
* 代码结构简洁通俗易懂,亦适合学习
|
|
|
|
|
|
2017-12-29 06:40:58 +00:00
|
|
|
|
# 依赖
|
|
|
|
|
* [libubox]
|
|
|
|
|
* [ustream-ssl]: 如果你需要支持SSL
|
|
|
|
|
* [mbedtls]: 如果你选择mbedtls作为你的SSL后端
|
|
|
|
|
* [CyaSSl(wolfssl)]: 如果你选择wolfssl作为你的SSL后端
|
|
|
|
|
* [openssl]: 如果你选择openssl作为你的SSL后端
|
2017-11-30 00:46:38 +00:00
|
|
|
|
|
|
|
|
|
## 配置
|
|
|
|
|
查看支持哪些配置选项
|
|
|
|
|
|
2017-12-29 06:46:40 +00:00
|
|
|
|
~/libuhttpd/build$ cmake .. -L
|
|
|
|
|
~/libuhttpd/build$ cmake .. -LH
|
2017-11-30 00:46:38 +00:00
|
|
|
|
|
|
|
|
|
## 运行例子
|
|
|
|
|
首先生成SSL证书文件
|
|
|
|
|
|
2017-12-29 06:46:40 +00:00
|
|
|
|
~/libuhttpd/build$ cd ..
|
|
|
|
|
~/libuhttpd$ ./gen_cert.sh
|
2017-11-30 00:46:38 +00:00
|
|
|
|
|
|
|
|
|
运行
|
|
|
|
|
|
2017-12-29 06:46:40 +00:00
|
|
|
|
~/libuhttpd$ ./build/example/helloworld
|
2017-11-30 00:46:38 +00:00
|
|
|
|
|
|
|
|
|
然后使用命令curl或者浏览器进行测试
|
|
|
|
|
|
2017-12-29 06:40:58 +00:00
|
|
|
|
$ curl -k 'https://127.0.0.1:8000/hello?name=test' -v
|
|
|
|
|
|
|
|
|
|
# 示例程序]
|
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-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
|