后端程序员之路 28、一个轻量级HTTP Server的实现

提到http server,一般用到的都是Apache和nginx这样的成熟软件,但是,有的情况下,我们也许也会用一些非常轻量级的http server。
http server的c++轻量级实现里,Mongoose和tinyhttpd这两个比较有名,而且很有参考价值。

Mongoose只有一个.h一个.c,而且能够很好的跨平台。在工作中,有过Mongoose运用在PC软件和android app的实践,效果也还可以。

cesanta/mongoose: Mongoose Embedded Web Server Library - Mongoose is more than an embedded webserver. It is a multi-protocol embedded networking library with functions including TCP, HTTP client and server, WebSocket client and server, MQTT client and broker and much more.
https://github.com/cesanta/mongoose

tinyhttpd则更加精简,仅有一个.c,500行代码。但是麻雀虽小五脏俱全,许多人推荐tinyhttpd给新手阅读。

Tiny HTTPd download | SourceForge.net
https://sourceforge.net/projects/tinyhttpd/

【源码剖析】tinyhttpd —— C 语言实现最简单的 HTTP 服务器 - Just Coding! - 博客频道 - CSDN.NET
http://blog.csdn.net/jcjc918/article/details/42129311

接下来,考虑使用面向对象的思想,用c++再自己实现个极简的http server。
1、仍然是常规的http_request_t、http_response_t、http_handler_t
2、http_server_t在start时开一个线程,监听端口
3、接到客户端请求时按行recv,并解析uri和http包
3、分配到特定的http_handler_t处理完,把返回内容按http协议send给客户端

原文地址:https://www.cnblogs.com/zapline/p/6656880.html