本文主要是介绍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,以下是全部源代码:
- #include "tinyweb.h"
- #include <uv.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <assert.h>
- #include <string.h>
- #include <memory.h>
- uv_tcp_t _server;
- uv_tcp_t _client;
- uv_loop_t* _loop;
- static void tinyweb_on_connection(uv_stream_t* server, int status);
- void tinyweb_start(uv_loop_t* loop, const char* ip, int port) {
- _loop = loop;
- uv_tcp_init(_loop, &_server);
- uv_tcp_bind(&_server, uv_ip4_addr(ip&&ip[0]?ip:"0.0.0.0", port));
- uv_listen((uv_stream_t*)&_server, 8, tinyweb_on_connection);
- }
- static void after_uv_close(uv_handle_t* handle) {
- free(handle); //uv_tcp_t* client
- }
- static void after_uv_write(uv_write_t* w, int status) {
- if(w->data)
- free(w->data);
- uv_close((uv_handle_t*)w->handle, after_uv_close); //close client
- free(w);
- }
- static void write_uv_data(uv_stream_t* stream, const void* data, unsigned int len, int need_copy_data) {
- if(data == NULL || len == 0) return;
- if(len ==(unsigned int)-1)
- len = strlen(data);
- void* newdata = (void*)data;
- if(need_copy_data) {
- newdata = malloc(len);
- memcpy(newdata, data, len);
- }
- uv_buf_t buf = uv_buf_init(newdata, len);
- uv_write_t* w = (uv_write_t*)malloc(sizeof(uv_write_t));
- w->data = need_copy_data ? newdata : NULL;
- //free w and w->data in after_uv_write()
- uv_write(w, stream, &buf, 1, after_uv_write);
- }
- static const char* http_respone = "HTTP/1.1 200 OK\r\n"
- "Content-Type:text/html;charset=utf-8\r\n"
- "Content-Length:11\r\n"
- "\r\n"
- "Hello world";
- static void tinyweb_on_connection(uv_stream_t* server, int status) {
- assert(server == (uv_stream_t*)&_server);
- if(status == 0) {
- uv_tcp_t* client = (uv_tcp_t*)malloc(sizeof(uv_tcp_t));
- uv_tcp_init(_loop, client);
- uv_accept((uv_stream_t*)&_server, (uv_stream_t*)client);
- write_uv_data((uv_stream_t*)client, http_respone, -1, 0);
- //close client after uv_write, and free it in after_uv_close()
- }
- }
启动代码:
- #include "tinyweb.h"
- int main() {
- tinyweb_start(uv_default_loop(), "127.0.0.1", 8080);
- uv_run(uv_default_loop(), UV_RUN_DEFAULT);
- }
这篇关于tinyweb: C语言 + libuv 开发的最精简的WebServer (附源码)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!