{"id":13441641,"url":"https://github.com/qinguoyi/TinyWebServer","last_synced_at":"2025-03-20T12:32:10.047Z","repository":{"id":37382014,"uuid":"189205663","full_name":"qinguoyi/TinyWebServer","owner":"qinguoyi","description":":fire: Linux下C++轻量级WebServer服务器","archived":false,"fork":false,"pushed_at":"2024-07-05T13:35:04.000Z","size":57302,"stargazers_count":16625,"open_issues_count":103,"forks_count":3918,"subscribers_count":89,"default_branch":"master","last_synced_at":"2024-10-15T21:41:48.690Z","etag":null,"topics":["cpp","http-server","tinywebserver","webbench","webserver"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/qinguoyi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-05-29T10:41:34.000Z","updated_at":"2024-10-15T18:58:48.000Z","dependencies_parsed_at":"2024-09-30T16:50:42.688Z","dependency_job_id":null,"html_url":"https://github.com/qinguoyi/TinyWebServer","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qinguoyi%2FTinyWebServer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qinguoyi%2FTinyWebServer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qinguoyi%2FTinyWebServer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qinguoyi%2FTinyWebServer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/qinguoyi","download_url":"https://codeload.github.com/qinguoyi/TinyWebServer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221760089,"owners_count":16876350,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cpp","http-server","tinywebserver","webbench","webserver"],"created_at":"2024-07-31T03:01:36.437Z","updated_at":"2025-03-20T12:32:10.040Z","avatar_url":"https://github.com/qinguoyi.png","language":"C++","readme":"\n\nTinyWebServer\n===============\nLinux下C++轻量级Web服务器，助力初学者快速实践网络编程，搭建属于自己的服务器.\n\n* 使用 **线程池 + 非阻塞socket + epoll(ET和LT均实现) + 事件处理(Reactor和模拟Proactor均实现)** 的并发模型\n* 使用**状态机**解析HTTP请求报文，支持解析**GET和POST**请求\n* 访问服务器数据库实现web端用户**注册、登录**功能，可以请求服务器**图片和视频文件**\n* 实现**同步/异步日志系统**，记录服务器运行状态\n* 经Webbench压力测试可以实现**上万的并发连接**数据交换\n\n\n写在前面\n----\n* 本项目开发维护过程中，很多童鞋曾发红包支持，我都一一谢绝。我现在不会，将来也不会将本项目包装成任何课程售卖，更不会开通任何支持通道。\n* 目前网络上有人或对本项目，或对游双大佬的项目包装成课程售卖。请各位童鞋擦亮眼，辨识各大学习/求职网站的C++服务器项目，不要盲目付费。\n* 有面试官大佬通过项目信息在公司内找到我，发现很多童鞋简历上都用了这个项目。但，在面试过程中发现`很多童鞋通过本项目入门了，但是对于一些东西还是属于知其然不知其所以然的状态，需要加强下基础知识的学习`，推荐认真阅读下\n    * 《unix环境高级编程》\n    * 《unix网络编程》\n* 感谢各位大佬，各位朋友，各位童鞋的认可和支持。如果本项目能带你入门，将是我莫大的荣幸。\n\n\n目录\n-----\n\n| [概述](#概述) | [框架](#框架) | [Demo演示](#Demo演示) | [压力测试](#压力测试) |[更新日志](#更新日志) |[源码下载](#源码下载) | [快速运行](#快速运行) | [个性化运行](#个性化运行) | [庖丁解牛](#庖丁解牛) | [CPP11实现](#CPP11实现) |[致谢](#致谢) |\n|:--------:|:--------:|:--------:|:--------:|:--------:|:--------:|:--------:|:--------:|:--------:|:--------:|:--------:|\n\n\n概述\n----------\n\n\u003e * C/C++\n\u003e * B/S模型\n\u003e * [线程同步机制包装类](https://github.com/qinguoyi/TinyWebServer/tree/master/lock)\n\u003e * [http连接请求处理类](https://github.com/qinguoyi/TinyWebServer/tree/master/http)\n\u003e * [半同步/半反应堆线程池](https://github.com/qinguoyi/TinyWebServer/tree/master/threadpool)\n\u003e * [定时器处理非活动连接](https://github.com/qinguoyi/TinyWebServer/tree/master/timer)\n\u003e * [同步/异步日志系统 ](https://github.com/qinguoyi/TinyWebServer/tree/master/log)  \n\u003e * [数据库连接池](https://github.com/qinguoyi/TinyWebServer/tree/master/CGImysql) \n\u003e * [同步线程注册和登录校验](https://github.com/qinguoyi/TinyWebServer/tree/master/CGImysql) \n\u003e * [简易服务器压力测试](https://github.com/qinguoyi/TinyWebServer/tree/master/test_presure)\n\n\n框架\n-------------\n\u003cdiv align=center\u003e\u003cimg src=\"http://ww1.sinaimg.cn/large/005TJ2c7ly1ge0j1atq5hj30g60lm0w4.jpg\" height=\"765\"/\u003e \u003c/div\u003e\n\nDemo演示\n----------\n\u003e * 注册演示\n\n\u003cdiv align=center\u003e\u003cimg src=\"http://ww1.sinaimg.cn/large/005TJ2c7ly1ge0iz0dkleg30m80bxjyj.gif\" height=\"429\"/\u003e \u003c/div\u003e\n\n\u003e * 登录演示\n\n\u003cdiv align=center\u003e\u003cimg src=\"https://github.com/qinguoyi/TinyWebServer/blob/master/root/login.gif\" height=\"429\"/\u003e \u003c/div\u003e\n\n\u003e * 请求图片文件演示(6M)\n\n\u003cdiv align=center\u003e\u003cimg src=\"http://ww1.sinaimg.cn/large/005TJ2c7ly1ge0juxrnlfg30go07x4qr.gif\" height=\"429\"/\u003e \u003c/div\u003e\n\n\u003e * 请求视频文件演示(39M)\n\n\u003cdiv align=center\u003e\u003cimg src=\"http://ww1.sinaimg.cn/large/005TJ2c7ly1ge0jtxie8ng30go07xb2b.gif\" height=\"429\"/\u003e \u003c/div\u003e\n\n\n压力测试\n-------------\n在关闭日志后，使用Webbench对服务器进行压力测试，对listenfd和connfd分别采用ET和LT模式，均可实现上万的并发连接，下面列出的是两者组合后的测试结果. \n\n\u003e * Proactor，LT + LT，93251 QPS\n\n\u003cdiv align=center\u003e\u003cimg src=\"http://ww1.sinaimg.cn/large/005TJ2c7ly1gfjqu2hptkj30gz07474n.jpg\" height=\"201\"/\u003e \u003c/div\u003e\n\n\u003e * Proactor，LT + ET，97459 QPS\n\n\u003cdiv align=center\u003e\u003cimg src=\"http://ww1.sinaimg.cn/large/005TJ2c7ly1gfjr1xppdgj30h206zdg6.jpg\" height=\"201\"/\u003e \u003c/div\u003e\n\n\u003e * Proactor，ET + LT，80498 QPS\n\n\u003cdiv align=center\u003e\u003cimg src=\"http://ww1.sinaimg.cn/large/005TJ2c7ly1gfjr24vmjtj30gz0720t3.jpg\" height=\"201\"/\u003e \u003c/div\u003e\n\n\u003e * Proactor，ET + ET，92167 QPS\n\n\u003cdiv align=center\u003e\u003cimg src=\"http://ww1.sinaimg.cn/large/005TJ2c7ly1gfjrflrebdj30gz06z0t3.jpg\" height=\"201\"/\u003e \u003c/div\u003e\n\n\u003e * Reactor，LT + ET，69175 QPS\n\n\u003cdiv align=center\u003e\u003cimg src=\"http://ww1.sinaimg.cn/large/005TJ2c7ly1gfjr1humcbj30h207474n.jpg\" height=\"201\"/\u003e \u003c/div\u003e\n\n\u003e * 并发连接总数：10500\n\u003e * 访问服务器时间：5s\n\u003e * 所有访问均成功\n\n**注意：** 使用本项目的webbench进行压测时，若报错显示webbench命令找不到，将可执行文件webbench删除后，重新编译即可。\n\n更新日志\n-------\n- [x] 解决请求服务器上大文件的Bug\n- [x] 增加请求视频文件的页面\n- [x] 解决数据库同步校验内存泄漏\n- [x] 实现非阻塞模式下的ET和LT触发，并完成压力测试\n- [x] 完善`lock.h`中的封装类，统一使用该同步机制\n- [x] 改进代码结构，更新局部变量懒汉单例模式\n- [x] 优化数据库连接池信号量与代码结构\n- [x] 使用RAII机制优化数据库连接的获取与释放\n- [x] 优化代码结构，封装工具类以减少全局变量\n- [x] 编译一次即可，命令行进行个性化测试更加友好\n- [x] main函数封装重构\n- [x] 新增命令行日志开关，关闭日志后更新压力测试结果\n- [x] 改进编译方式，只配置一次SQL信息即可\n- [x] 新增Reactor模式，并完成压力测试\n\n源码下载\n-------\n目前有两个版本，版本间的代码结构有较大改动，文档和代码运行方法也不一致。重构版本更简洁，原始版本(raw_version)更大保留游双代码的原汁原味，从原始版本更容易入手.\n\n如果遇到github代码下载失败，或访问太慢，可以从以下链接下载，与Github最新提交同步.\n\n* 重构版本下载地址 : [BaiduYun](https://pan.baidu.com/s/1PozKji8Oop-1BYcfixZR0g)\n    *  提取码 : vsqq\n* 原始版本(raw_version)下载地址 : [BaiduYun](https://pan.baidu.com/s/1asMNDW-zog92DZY1Oa4kaQ)\n    * 提取码 : 9wye\n    * 原始版本运行请参考[原始文档](https://github.com/qinguoyi/TinyWebServer/tree/raw_version)\n\n快速运行\n------------\n* 服务器测试环境\n\t* Ubuntu版本16.04\n\t* MySQL版本5.7.29\n* 浏览器测试环境\n\t* Windows、Linux均可\n\t* Chrome\n\t* FireFox\n\t* 其他浏览器暂无测试\n\n* 测试前确认已安装MySQL数据库\n\n    ```C++\n    // 建立yourdb库\n    create database yourdb;\n\n    // 创建user表\n    USE yourdb;\n    CREATE TABLE user(\n        username char(50) NULL,\n        passwd char(50) NULL\n    )ENGINE=InnoDB;\n\n    // 添加数据\n    INSERT INTO user(username, passwd) VALUES('name', 'passwd');\n    ```\n\n* 修改main.cpp中的数据库初始化信息\n\n    ```C++\n    //数据库登录名,密码,库名\n    string user = \"root\";\n    string passwd = \"root\";\n    string databasename = \"yourdb\";\n    ```\n\n* build\n\n    ```C++\n    sh ./build.sh\n    ```\n\n* 启动server\n\n    ```C++\n    ./server\n    ```\n\n* 浏览器端\n\n    ```C++\n    ip:9006\n    ```\n\n个性化运行\n------\n\n```C++\n./server [-p port] [-l LOGWrite] [-m TRIGMode] [-o OPT_LINGER] [-s sql_num] [-t thread_num] [-c close_log] [-a actor_model]\n```\n\n温馨提示:以上参数不是非必须，不用全部使用，根据个人情况搭配选用即可.\n\n* -p，自定义端口号\n\t* 默认9006\n* -l，选择日志写入方式，默认同步写入\n\t* 0，同步写入\n\t* 1，异步写入\n* -m，listenfd和connfd的模式组合，默认使用LT + LT\n\t* 0，表示使用LT + LT\n\t* 1，表示使用LT + ET\n    * 2，表示使用ET + LT\n    * 3，表示使用ET + ET\n* -o，优雅关闭连接，默认不使用\n\t* 0，不使用\n\t* 1，使用\n* -s，数据库连接数量\n\t* 默认为8\n* -t，线程数量\n\t* 默认为8\n* -c，关闭日志，默认打开\n\t* 0，打开日志\n\t* 1，关闭日志\n* -a，选择反应堆模型，默认Proactor\n\t* 0，Proactor模型\n\t* 1，Reactor模型\n\n测试示例命令与含义\n\n```C++\n./server -p 9007 -l 1 -m 0 -o 1 -s 10 -t 10 -c 1 -a 1\n```\n\n- [x] 端口9007\n- [x] 异步写入日志\n- [x] 使用LT + LT组合\n- [x] 使用优雅关闭连接\n- [x] 数据库连接池内有10条连接\n- [x] 线程池内有10条线程\n- [x] 关闭日志\n- [x] Reactor反应堆模型\n\n庖丁解牛\n------------\n近期版本迭代较快，以下内容多以旧版本(raw_version)代码为蓝本进行详解.\n\n* [小白视角：一文读懂社长的TinyWebServer](https://huixxi.github.io/2020/06/02/%E5%B0%8F%E7%99%BD%E8%A7%86%E8%A7%92%EF%BC%9A%E4%B8%80%E6%96%87%E8%AF%BB%E6%87%82%E7%A4%BE%E9%95%BF%E7%9A%84TinyWebServer/#more)\n* [最新版Web服务器项目详解 - 01 线程同步机制封装类](https://mp.weixin.qq.com/s?__biz=MzAxNzU2MzcwMw==\u0026mid=2649274278\u0026idx=3\u0026sn=5840ff698e3f963c7855d702e842ec47\u0026chksm=83ffbefeb48837e86fed9754986bca6db364a6fe2e2923549a378e8e5dec6e3cf732cdb198e2\u0026scene=0\u0026xtrack=1#rd)\n* [最新版Web服务器项目详解 - 02 半同步半反应堆线程池（上）](https://mp.weixin.qq.com/s?__biz=MzAxNzU2MzcwMw==\u0026mid=2649274278\u0026idx=4\u0026sn=caa323faf0c51d882453c0e0c6a62282\u0026chksm=83ffbefeb48837e841a6dbff292217475d9075e91cbe14042ad6e55b87437dcd01e6d9219e7d\u0026scene=0\u0026xtrack=1#rd)\n* [最新版Web服务器项目详解 - 03 半同步半反应堆线程池（下）](https://mp.weixin.qq.com/s/PB8vMwi8sB4Jw3WzAKpWOQ)\n* [最新版Web服务器项目详解 - 04 http连接处理（上）](https://mp.weixin.qq.com/s/BfnNl-3jc_x5WPrWEJGdzQ)\n* [最新版Web服务器项目详解 - 05 http连接处理（中）](https://mp.weixin.qq.com/s/wAQHU-QZiRt1VACMZZjNlw)\n* [最新版Web服务器项目详解 - 06 http连接处理（下）](https://mp.weixin.qq.com/s/451xNaSFHxcxfKlPBV3OCg)\n* [最新版Web服务器项目详解 - 07 定时器处理非活动连接（上）](https://mp.weixin.qq.com/s/mmXLqh_NywhBXJvI45hchA)\n* [最新版Web服务器项目详解 - 08 定时器处理非活动连接（下）](https://mp.weixin.qq.com/s/fb_OUnlV1SGuOUdrGrzVgg)\n* [最新版Web服务器项目详解 - 09 日志系统（上）](https://mp.weixin.qq.com/s/IWAlPzVDkR2ZRI5iirEfCg)\n* [最新版Web服务器项目详解 - 10 日志系统（下）](https://mp.weixin.qq.com/s/f-ujwFyCe1LZa3EB561ehA)\n* [最新版Web服务器项目详解 - 11 数据库连接池](https://mp.weixin.qq.com/s?__biz=MzAxNzU2MzcwMw==\u0026mid=2649274326\u0026idx=1\u0026sn=5af78e2bf6552c46ae9ab2aa22faf839\u0026chksm=83ffbe8eb4883798c3abb82ddd124c8100a39ef41ab8d04abe42d344067d5e1ac1b0cac9d9a3\u0026token=1450918099\u0026lang=zh_CN#rd)\n* [最新版Web服务器项目详解 - 12 注册登录](https://mp.weixin.qq.com/s?__biz=MzAxNzU2MzcwMw==\u0026mid=2649274431\u0026idx=4\u0026sn=7595a70f06a79cb7abaebcd939e0cbee\u0026chksm=83ffb167b4883871ce110aeb23e04acf835ef41016517247263a2c3ab6f8e615607858127ea6\u0026token=1686112912\u0026lang=zh_CN#rd)\n* [最新版Web服务器项目详解 - 13 踩坑与面试题](https://mp.weixin.qq.com/s?__biz=MzAxNzU2MzcwMw==\u0026mid=2649274431\u0026idx=1\u0026sn=2dd28c92f5d9704a57c001a3d2630b69\u0026chksm=83ffb167b48838715810b27b8f8b9a576023ee5c08a8e5d91df5baf396732de51268d1bf2a4e\u0026token=1686112912\u0026lang=zh_CN#rd)\n* 已更新完毕\n\nStar History\n---------\n[![Star History Chart](https://api.star-history.com/svg?repos=qinguoyi/TinyWebServer\u0026type=Date)](https://star-history.com/#qinguoyi/TinyWebServer\u0026Date)\n\nCPP11实现\n------------\n更简洁，更优雅的CPP11实现：[Webserver](https://github.com/markparticle/WebServer)\n\n\n致谢\n------------\nLinux高性能服务器编程，游双著.\n\n感谢以下朋友的PR和帮助: [@RownH](https://github.com/RownH)，[@mapleFU](https://github.com/mapleFU)，[@ZWiley](https://github.com/ZWiley)，[@zjuHong](https://github.com/zjuHong)，[@mamil](https://github.com/mamil)，[@byfate](https://github.com/byfate)，[@MaJun827](https://github.com/MaJun827)，[@BBLiu-coder](https://github.com/BBLiu-coder)，[@smoky96](https://github.com/smoky96)，[@yfBong](https://github.com/yfBong)，[@liuwuyao](https://github.com/liuwuyao)，[@Huixxi](https://github.com/Huixxi)，[@markparticle](https://github.com/markparticle)，[@blogg9ggg](https://github.com/Blogg9ggg).\n","funding_links":[],"categories":["HarmonyOS","C++","Projects"],"sub_categories":["Windows Manager","Cpp"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fqinguoyi%2FTinyWebServer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fqinguoyi%2FTinyWebServer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fqinguoyi%2FTinyWebServer/lists"}