1 Star 0 Fork 270

HuBin95 / libhv

forked from libhv / libhv 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
http_server_test.cpp 2.65 KB
一键复制 编辑 原始数据 按行查看 历史
ithewei 提交于 2023-02-27 21:33 . Add ssl_ctx member for #338
/*
* sample http server
* more detail see examples/httpd
*
*/
#include "HttpServer.h"
using namespace hv;
/*
* #define TEST_HTTPS 1
*
* @build ./configure --with-openssl && make clean && make
*
* @server bin/http_server_test 8080
*
* @client curl -v http://127.0.0.1:8080/ping
* curl -v https://127.0.0.1:8443/ping --insecure
* bin/curl -v http://127.0.0.1:8080/ping
* bin/curl -v https://127.0.0.1:8443/ping
*
*/
#define TEST_HTTPS 0
int main(int argc, char** argv) {
HV_MEMCHECK;
int port = 0;
if (argc > 1) {
port = atoi(argv[1]);
}
if (port == 0) port = 8080;
HttpService router;
// curl -v http://ip:port/
router.Static("/", "./html");
// curl -v http://ip:port/proxy/get
router.Proxy("/proxy/", "http://httpbin.org/");
// curl -v http://ip:port/ping
router.GET("/ping", [](HttpRequest* req, HttpResponse* resp) {
return resp->String("pong");
});
// curl -v http://ip:port/data
router.GET("/data", [](HttpRequest* req, HttpResponse* resp) {
static char data[] = "0123456789";
return resp->Data(data, 10 /*, false */);
});
// curl -v http://ip:port/paths
router.GET("/paths", [&router](HttpRequest* req, HttpResponse* resp) {
return resp->Json(router.Paths());
});
// curl -v http://ip:port/get?env=1
router.GET("/get", [](HttpRequest* req, HttpResponse* resp) {
resp->json["origin"] = req->client_addr.ip;
resp->json["url"] = req->url;
resp->json["args"] = req->query_params;
resp->json["headers"] = req->headers;
return 200;
});
// curl -v http://ip:port/echo -d "hello,world!"
router.POST("/echo", [](const HttpContextPtr& ctx) {
return ctx->send(ctx->body(), ctx->type());
});
// curl -v http://ip:port/user/123
router.GET("/user/{id}", [](const HttpContextPtr& ctx) {
hv::Json resp;
resp["id"] = ctx->param("id");
return ctx->send(resp.dump(2));
});
HttpServer server;
server.service = &router;
server.port = port;
#if TEST_HTTPS
server.https_port = 8443;
hssl_ctx_opt_t param;
memset(&param, 0, sizeof(param));
param.crt_file = "cert/server.crt";
param.key_file = "cert/server.key";
param.endpoint = HSSL_SERVER;
if (server.newSslCtx(&param) != 0) {
fprintf(stderr, "new SSL_CTX failed!\n");
return -20;
}
#endif
// uncomment to test multi-processes
// server.setProcessNum(4);
// uncomment to test multi-threads
// server.setThreadNum(4);
server.start();
// press Enter to stop
while (getchar() != '\n');
return 0;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/hubin95/libhv.git
git@gitee.com:hubin95/libhv.git
hubin95
libhv
libhv
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891