tinyweb: C语言 + libuv 开发的最精简的WebServer (附源码)

libuv 是一个高性能事件驱动网络库,是 Node.js 的底层实现。经过我(Liigo)在实际项目中的深度应用,发现 libuv 在代码质量、运行效率、网络吞吐量、稳定性、跨平台等多方面都相当优秀,是一款不可多得的开源产品,可以说从质量到名气都不差。libuv 的缺点是易用性太差,文档严重不足,入手门槛较高。在这些方面它跟陈硕的muduo 库差距很大,muduo的易用性太棒了,还有一本作者的专注《Linux多线程服务端编程:使用muduo C++网络库》质量很高。muduo的性能应该很好,可惜仅它支持Linux系统,跨平台特性为零,而且C++编译出来的可执行文件尺寸较大,部署到嵌入式平台时有很大的局限性。关于libuv和muduo的选择,我(Liigo)的观点是:如果条件允许使用muduo,我建议您优先考虑muduo;否则,libuv也是很好的方案。

  好了,言归正传。因为项目需要在软件中内嵌一个极简单的Web Server,于是我用libuv写了这个tinyweb,以下是全部源代码:



[cpp]  view plain copy
  1. #include "tinyweb.h"  
  2. #include <uv.h>  
  3. #include <stdlib.h>  
  4. #include <stdio.h>  
  5. #include <assert.h>  
  6. #include <string.h>  
  7. #include <memory.h>  
  8.   
  9. uv_tcp_t   _server;  
  10. uv_tcp_t   _client;  
  11. uv_loop_t* _loop;  
  12.   
  13. static void tinyweb_on_connection(uv_stream_t* server, int status);  
  14.   
  15. void tinyweb_start(uv_loop_t* loop, const char* ip, int port) {  
  16.     _loop = loop;  
  17.     uv_tcp_init(_loop, &_server);  
  18.     uv_tcp_bind(&_server, uv_ip4_addr(ip&&ip[0]?ip:"0.0.0.0", port));  
  19.     uv_listen((uv_stream_t*)&_server, 8, tinyweb_on_connection);  
  20. }  
  21.   
  22. static void after_uv_close(uv_handle_t* handle) {  
  23.     free(handle); //uv_tcp_t* client  
  24. }  
  25.   
  26. static void after_uv_write(uv_write_t* w, int status) {  
  27.     if(w->data)  
  28.         free(w->data);  
  29.     uv_close((uv_handle_t*)w->handle, after_uv_close); //close client  
  30.     free(w);  
  31. }  
  32.   
  33. static void write_uv_data(uv_stream_t* stream, const void* data, unsigned int len, int need_copy_data) {  
  34.     if(data == NULL || len == 0) return;  
  35.     if(len ==(unsigned int)-1)  
  36.         len = strlen(data);  
  37.   
  38.     void* newdata  = (void*)data;  
  39.     if(need_copy_data) {  
  40.         newdata = malloc(len);  
  41.         memcpy(newdata, data, len);  
  42.     }  
  43.   
  44.     uv_buf_t buf = uv_buf_init(newdata, len);  
  45.     uv_write_t* w = (uv_write_t*)malloc(sizeof(uv_write_t));  
  46.     w->data = need_copy_data ? newdata : NULL;  
  47.     //free w and w->data in after_uv_write()  
  48.     uv_write(w, stream, &buf, 1, after_uv_write);  
  49. }  
  50.   
  51. static const char* http_respone = "HTTP/1.1 200 OK\r\n"  
  52.     "Content-Type:text/html;charset=utf-8\r\n"  
  53.     "Content-Length:11\r\n"  
  54.     "\r\n"  
  55.     "Hello world";  
  56.   
  57. static void tinyweb_on_connection(uv_stream_t* server, int status) {  
  58.     assert(server == (uv_stream_t*)&_server);  
  59.     if(status == 0) {  
  60.         uv_tcp_t* client = (uv_tcp_t*)malloc(sizeof(uv_tcp_t));  
  61.         uv_tcp_init(_loop, client);  
  62.         uv_accept((uv_stream_t*)&_server, (uv_stream_t*)client);  
  63.         write_uv_data((uv_stream_t*)client, http_respone, -1, 0);  
  64.         //close client after uv_write, and free it in after_uv_close()  
  65.     }  
  66. }  

启动代码:

[cpp]  view plain copy
  1. #include "tinyweb.h"  
  2. int main() {  
  3.     tinyweb_start(uv_default_loop(), "127.0.0.1", 8080);  
  4.     uv_run(uv_default_loop(), UV_RUN_DEFAULT);  
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值