{"id":27118396,"url":"https://github.com/suconghou/httplib","last_synced_at":"2025-04-07T07:57:41.196Z","repository":{"id":282147739,"uuid":"947639701","full_name":"suconghou/httplib","owner":"suconghou","description":"simple async http server","archived":false,"fork":false,"pushed_at":"2025-04-02T15:09:17.000Z","size":118,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-02T15:32:13.096Z","etag":null,"topics":["async","event-loop","http-parser","http-server","poll","webserver"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/suconghou.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2025-03-13T02:30:01.000Z","updated_at":"2025-04-02T15:09:21.000Z","dependencies_parsed_at":null,"dependency_job_id":"43d4b616-fc00-47e7-b998-c32b17f420d5","html_url":"https://github.com/suconghou/httplib","commit_stats":null,"previous_names":["suconghou/httplib"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/suconghou%2Fhttplib","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/suconghou%2Fhttplib/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/suconghou%2Fhttplib/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/suconghou%2Fhttplib/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/suconghou","download_url":"https://codeload.github.com/suconghou/httplib/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247615474,"owners_count":20967183,"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":["async","event-loop","http-parser","http-server","poll","webserver"],"created_at":"2025-04-07T07:57:40.646Z","updated_at":"2025-04-07T07:57:41.162Z","avatar_url":"https://github.com/suconghou.png","language":"C++","readme":"# httplib\n\n基于poll实现的单线程异步HTTP服务器库\n\n基于 https://github.com/suconghou/poll_server 的异步IO\n\n使用状态机解析HTTP协议，支持 HTTP/1.0和HTTP/1.1协议\n\n- 异步非阻塞I/O\n- 支持GET、POST、PUT、PATCH、DELETE等HTTP方法\n- 支持正则路由匹配\n- 支持chunked传输编码\n- 内置URL解码和路径解析\n- 支持请求头、请求体、响应头的处理\n- 支持文件流传输\n- 支持keep-alive连接\n- 解析query和cookie的辅助方法\n- 支持chunked请求的trailer headers\n\n## 快速开始\n\n```cpp\n#include \"httplib.cpp\"\n\nint main() {\n    Server server;\n\n    // 注册路由\n    server.get(\"/hello\", [](Request* req, Response* res) {\n        res-\u003estatus(200)-\u003eend(\"Hello World\");\n    });\n\n    // 启动服务器\n    server.start(8080, [] { return 3500; });\n\n    return 0;\n}\n```\n\n## API 介绍\n\n### Server 类\n- **get**: 注册GET请求的路由。\n  ```cpp\n  server.get(\"/path\", [](Request* req, Response* res) {\n      // 处理逻辑\n  });\n  ```\n- **post**: 注册POST请求的路由。\n  ```cpp\n  server.post(\"/path\", [](Request* req, Response* res) {\n      // 处理逻辑\n  });\n  ```\n- **put**: 注册PUT请求的路由。\n  ```cpp\n  server.put(\"/path\", [](Request* req, Response* res) {\n      // 处理逻辑\n  });\n  ```\n- **patch**: 注册PATCH请求的路由。\n  ```cpp\n  server.patch(\"/path\", [](Request* req, Response* res) {\n      // 处理逻辑\n  });\n  ```\n- **delete_**: 注册DELETE请求的路由。\n  ```cpp\n  server.delete_(\"/path\", [](Request* req, Response* res) {\n      // 处理逻辑\n  });\n  ```\n- **options**: 注册OPTIONS请求的路由。\n  ```cpp\n  server.options(\"/path\", [](Request* req, Response* res) {\n      // 处理逻辑\n  });\n  ```\n- **start**: 启动服务器，监听指定端口。\n  ```cpp\n  server.start(8080, [] { return 3500; });\n  ```\n\n### 正则路由及捕获参数\n- 使用正则表达式注册路由，并捕获路径参数。\n  ```cpp\n  server.get(\"/user/(\\\\d+)\", [](Request* req, Response* res) {\n      std::string userId = req-\u003eparams[1]; // 捕获的参数\n      res-\u003estatus(200)-\u003eend(\"User ID: \" + userId);\n  });\n  ```\n\n### bind_use_body_handler 用法示例\n- 注册一个处理请求体的回调函数，并与路由注册结合使用。\n  ```cpp\n  server.post(\"/submit\", Server::bind_use_body_handler([](Request* req, Response* res, const char* data, size_t length) {\n      // 处理请求体数据\n      std::string body(data, length);\n      res-\u003eend(\"Received: \" + body);\n  }));\n  ```\n\n### chunked 传输编码说明\n- 在响应头中未指定`Content-Length`时，服务器会自动使用chunked编码。\n- 适用于流式传输或动态生成内容的场景。\n\n### Request 类\n- **method**: 请求方法（如GET、POST）。\n- **uri**: 请求URI。\n- **version**: http版本。\n- **path**: 请求的path部分。\n- **rawQuery**: 请求的query部分。\n- **params**: 路由参数\n- **headers**: 请求头信息。\n- **trailers**: chunked请求的trailer headers\n- **query()**: 获取解析后的查询参数。\n- **cookies()**: 获取解析后的Cookie。\n\n### Response 类\n- **status**: 设置响应状态码。\n  ```cpp\n  res-\u003estatus(200);\n  ```\n- **end**: 结束响应并发送数据。\n  ```cpp\n  res-\u003eend(\"Response Body\");\n  ```\n- **stream**: 以流的方式发送数据。\n  ```cpp\n  res-\u003estream(fileReader);\n  ```\n- **length**: 设置响应的内容长度。\n  - 如果设置了`length`，则使用普通编码；如果未设置，则使用chunked编码。\n  ```cpp\n  res-\u003elength(1024);\n  ```\n- **write**: 写入响应体数据。\n  - 在响应头发送后，使用此方法写入数据。\n  ```cpp\n  res-\u003ewrite(\"Partial Response\");\n  ```\n- **writeHead**: 发送响应头。\n  - 在发送响应体之前调用此方法。\n  ```cpp\n  res-\u003ewriteHead(200, {{\"Content-Type\", \"text/plain\"}});\n  ```\n\n## 构建\n\n编译时需先将 https://github.com/suconghou/poll_server 的`poll.cpp`文件拷贝到当前目录。\n\n`main.cpp` 和 `httplibext.cpp` 为一个示例的HTTP静态文件服务器，支持目录浏览和Range请求。\n\n```bash\ng++ -Wall -std=c++20 -O1 main.cpp\n```\n\n```bash\ng++ -Wall -std=c++20 -flto=auto -static-libstdc++ -static-libgcc --static -Wl,-Bstatic,--gc-sections -O3 -ffunction-sections -fdata-sections main.cpp -o fileserver\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsuconghou%2Fhttplib","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsuconghou%2Fhttplib","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsuconghou%2Fhttplib/lists"}