基於libuv的最精簡Web伺服器:tinyweb v1 v2 v3 (C語言原始碼)
阿新 • • 發佈:2019-01-04
一年之前(2013年6月22日),我發表過一篇部落格《tinyweb: C語言 + libuv 開發的最精簡的WebServer (附原始碼)》,介紹了我用libuv開發的一個最精簡的Web server伺服器——tinyweb。事實上,那個版本的 tinyweb(v1)是我從真實專案中剝離出來的,同時剝離出來的還有 tinyweb 的 v2 和 v3 版本。從 v1 到 v2 到 v3,就是一個Web server從雛形到基本功能逐漸豐富的過程。
tinyweb v1,是最基礎的libuv的hello world,可用來學習libuv的入門用法;tinyweb v2,在v1的基礎上,添加了解析HTTP GET請求(Request)提取path_info和query_string併發送回復(Respone)的功能;tinyweb v3,在v2的基礎上,又添加了對靜態檔案的支援。
真正在專案中有實用價值的,我認為應該是從tinyweb v2開始引入的對path_info的響應處理:在專案中嵌入tinyweb伺服器,響應特定path_info,或輸出內部執行狀態,或觸發某個動作,如此一來,使用者(或開發者自己)通過Web瀏覽器即可輕鬆完成與專案程式的有效溝通,至少免除了程序通訊之類的東西吧,通過特殊的path_info(比如http://localhost/hibos)給自己的程式留一個小小的後門也是輕而易舉。
下面僅列出 tinyweb v1 的原始碼:
#include "tinyweb1.h" #include <uv.h> #include <stdlib.h> #include <stdio.h> #include <assert.h> #include <string.h> #include <memory.h> // Tinyweb v1, a tiny web server based on libuv, by liigo, 2013-6-20. uv_tcp_t _server; uv_tcp_t _client; uv_loop_t* _loop; static void tinyweb_on_connection(uv_stream_t* server, int status); //start web server, linstening ip:port //ip can be NULL or "", which means "0.0.0.0" void tinyweb_start(uv_loop_t* loop, const char* ip, int port) { struct sockaddr_in addr; uv_ip4_addr((ip && ip[0]) ? ip : "0.0.0.0", port, &addr); _loop = loop; uv_tcp_init(_loop, &_server); uv_tcp_bind(&_server, (const struct sockaddr*) &addr, 0); 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, see tinyweb_on_connection() } 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) { uv_buf_t buf; uv_write_t* w; void* newdata = (void*)data; if(data == NULL || len == 0) return; if(len ==(unsigned int)-1) len = strlen(data); if(need_copy_data) { newdata = malloc(len); memcpy(newdata, data, len); } buf = uv_buf_init(newdata, len); w = (uv_write_t*)malloc(sizeof(uv_write_t)); w->data = need_copy_data ? newdata : NULL; uv_write(w, stream, &buf, 1, after_uv_write); // free w and w->data in 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:18\r\n" "\r\n" "Welcome to tinyweb"; 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 <stdlib.h>
#include <uv.h>
#include "../tinyweb1.h"
int main()
{
tinyweb_start(uv_default_loop(), "127.0.0.1", 8080);
uv_run(uv_default_loop(), UV_RUN_DEFAULT);
}
相對於去年釋出的 tinyweb,此v1版本採用更新的libuv(0.11.16+)。