基于libuv的最精简Web服务器:tinyweb v1 v2 v3 (C语言源码)

一年之前(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(tinyweb1.c)v2(tinyweb2.c)v3(tinyweb3.c)三个版本的C语言源代码都已经发布到Github,项目地址是:https://github.com/liigo/tinyweb/

下面仅列出 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+)。


发布了275 篇原创文章 · 获赞 442 · 访问量 244万+

猜你喜欢

转载自blog.csdn.net/liigo/article/details/38424417
今日推荐