{"id":17184607,"url":"https://github.com/josexy/socnet-cpp","last_synced_at":"2025-04-13T18:07:21.521Z","repository":{"id":50347204,"uuid":"365759750","full_name":"josexy/socnet-cpp","owner":"josexy","description":"A high performance HTTP server based on linux epoll designed by C++ 20","archived":false,"fork":false,"pushed_at":"2021-11-23T08:07:34.000Z","size":1387,"stargazers_count":34,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-27T08:58:12.976Z","etag":null,"topics":["cpp20","epoll","http-server","https-server","php-fpm","ssl"],"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/josexy.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}},"created_at":"2021-05-09T13:29:43.000Z","updated_at":"2025-03-08T09:56:08.000Z","dependencies_parsed_at":"2022-09-10T08:51:02.485Z","dependency_job_id":null,"html_url":"https://github.com/josexy/socnet-cpp","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/josexy%2Fsocnet-cpp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/josexy%2Fsocnet-cpp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/josexy%2Fsocnet-cpp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/josexy%2Fsocnet-cpp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/josexy","download_url":"https://codeload.github.com/josexy/socnet-cpp/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248758434,"owners_count":21156957,"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":["cpp20","epoll","http-server","https-server","php-fpm","ssl"],"created_at":"2024-10-15T00:43:48.376Z","updated_at":"2025-04-13T18:07:21.485Z","avatar_url":"https://github.com/josexy.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# socnet-cpp(socket of c++)\nA high performance HTTP server based on linux epoll designed by C++ 20\n\n用C++20实现的基于linux epoll高性能简单HTTP服务器，可在docker容器中运行，仅仅用于学习。\n\n目前GitHub有很多使用C++实现的Web服务器，本人使用C++造轮子主要是从底层理解高性能Web服务器的实现以及如何分模块构建一个小型项目。\n\n特点:\n- 模块化设计，分而治之\n- 基于linux epoll 边缘模式ET+非阻塞+线程池，提高服务器处理客户端连接的并发性\n- 实现一个最小堆定时器，用于关闭空闲连接\n- 利用状态机解析TCP数据流并转化为HTTP Request对象\n- 通过OpenSSL实现HTTPS安全连接\n- 支持Gzip压缩算法\n- 通过php-fpm解析PHP文件，实现动态web服务器\n- 采用json配置文件\n- 支持sendfile和mmap （OpenSSL不支持sendfile，默认mmap）\n- ...\n\n## C++17/20特性\n- 采用 `std::string_view` 避免string对象频繁拷贝和析构，加快服务器解析HTTP请求\n- 采用 `std::optional` 处理返回值\n- if/switch初始化\n- ...\n\n## 依赖库\n- pthread\n- zlib\n- openssl\n\n## Quick start\n```bash\ngit clone https://github.com/josexy/socnet-cpp.git\ncd socnet-cpp/build\ncmake ..\nmake\n./socnet\n```\n\n下面是一个非常简单的例子：\n```cpp\n#include \"soc/http/include/HttpServer.h\"\n\nusing namespace std;\nusing namespace soc::http;\n\nclass TestService : public HttpService {\npublic:\n  void doGet(const HttpRequest \u0026req, HttpResponse \u0026resp) override {\n    req.getHeader().forEach(\n        [](auto \u0026k, auto \u0026v) { cout \u003c\u003c k \u003c\u003c \": \" \u003c\u003c v \u003c\u003c '\\n'; });\n    resp.setContentType(\"text/plain\").setBody(\"hello world\");\n  }\n};\n\nint main() {\n  HttpServer server;\n  server.addService\u003cTestService\u003e(\"/\");\n  server.start();\n  return 0;\n}\n\n```\n\n## JSON配置文件\n```json\n{\n    \"server\": {\n        \"listen_ip\": \"0.0.0.0\",\n        \"listen_port\": 5555,\n        \"idle_timeout\": 2000,\n        \"server_hostname\": \"localhost\",\n        \"enable_https\": false,\n        \"enable_php\": false,\n        \"enable_sendfile\": false,\n        \"default_page\": [\n            \"index.php\",\n            \"index.html\"\n        ],\n        \"user_pass_file\": \"./pass_store/user_password\",\n        \"authenticate_realm\": \"socnet@test\",\n        \"session_lifetime\": 10\n    },\n    \"https\": {\n        \"cert_file\": \"./ssl/cert.crt\",\n        \"private_key_file\": \"./ssl/private.pem\",\n        \"password\": \"\"\n    },\n    \"php-fpm\": {\n        \"tcp_or_domain\": true,\n        \"server_ip\": \"127.0.0.1\",\n        \"server_port\": 9000,\n        \"sock_path\": \"/run/php-fpm/php-fpm.sock\"\n    }\n}\n```\n\nPS: 可能需要修改php-fpm配置文件中 `user` 和 `group` 为当前用户名。\n\n## Docker \n该项目可在docker中运行：\n```shell\n# 构建\ndocker build -t web-socnet:v1 .\n# 创建并运行容器\ndocker run -itd --rm --name socnet -p 8080:5555 web-socnet:v1\n# 访问\ncurl 127.0.0.1:8080\n# 进入容器\ndocker exec -it socnet /bin/bash\n# 停止容器\ndocker stop socnet\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjosexy%2Fsocnet-cpp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjosexy%2Fsocnet-cpp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjosexy%2Fsocnet-cpp/lists"}