http_server_test.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * sample http server
  3. * more detail see examples/httpd
  4. *
  5. */
  6. #include "HttpServer.h"
  7. #include "hthread.h" // import hv_gettid
  8. #include "hasync.h" // import hv::async
  9. using namespace hv;
  10. /*
  11. * #define TEST_HTTPS 1
  12. *
  13. * @build ./configure --with-openssl && make clean && make
  14. *
  15. * @server bin/http_server_test 8080
  16. *
  17. * @client curl -v http://127.0.0.1:8080/ping
  18. * curl -v https://127.0.0.1:8443/ping --insecure
  19. * bin/curl -v http://127.0.0.1:8080/ping
  20. * bin/curl -v https://127.0.0.1:8443/ping
  21. *
  22. */
  23. #define TEST_HTTPS 0
  24. int main(int argc, char** argv) {
  25. HV_MEMCHECK;
  26. int port = 0;
  27. if (argc > 1) {
  28. port = atoi(argv[1]);
  29. }
  30. if (port == 0) port = 8080;
  31. HttpService router;
  32. /* Static file service */
  33. // curl -v http://ip:port/
  34. router.Static("/", "./html");
  35. /* Forward proxy service */
  36. router.EnableForwardProxy();
  37. // curl -v http://httpbin.org/get --proxy http://127.0.0.1:8080
  38. router.AddTrustProxy("*httpbin.org");
  39. /* Reverse proxy service */
  40. // curl -v http://ip:port/httpbin/get
  41. router.Proxy("/httpbin/", "http://httpbin.org/");
  42. /* API handlers */
  43. // curl -v http://ip:port/ping
  44. router.GET("/ping", [](HttpRequest* req, HttpResponse* resp) {
  45. return resp->String("pong");
  46. });
  47. // curl -v http://ip:port/data
  48. router.GET("/data", [](HttpRequest* req, HttpResponse* resp) {
  49. static char data[] = "0123456789";
  50. return resp->Data(data, 10 /*, false */);
  51. });
  52. // curl -v http://ip:port/paths
  53. router.GET("/paths", [&router](HttpRequest* req, HttpResponse* resp) {
  54. return resp->Json(router.Paths());
  55. });
  56. // curl -v http://ip:port/get?env=1
  57. router.GET("/get", [](const HttpContextPtr& ctx) {
  58. hv::Json resp;
  59. resp["origin"] = ctx->ip();
  60. resp["url"] = ctx->url();
  61. resp["args"] = ctx->params();
  62. resp["headers"] = ctx->headers();
  63. return ctx->send(resp.dump(2));
  64. });
  65. // curl -v http://ip:port/echo -d "hello,world!"
  66. router.POST("/echo", [](const HttpContextPtr& ctx) {
  67. return ctx->send(ctx->body(), ctx->type());
  68. });
  69. // curl -v http://ip:port/user/123
  70. router.GET("/user/{id}", [](const HttpContextPtr& ctx) {
  71. hv::Json resp;
  72. resp["id"] = ctx->param("id");
  73. return ctx->send(resp.dump(2));
  74. });
  75. // curl -v http://ip:port/async
  76. router.GET("/async", [](const HttpRequestPtr& req, const HttpResponseWriterPtr& writer) {
  77. writer->Begin();
  78. writer->WriteHeader("X-Response-tid", hv_gettid());
  79. writer->WriteHeader("Content-Type", "text/plain");
  80. writer->WriteBody("This is an async response.\n");
  81. writer->End();
  82. });
  83. // middleware
  84. router.AllowCORS();
  85. router.Use([](HttpRequest* req, HttpResponse* resp) {
  86. resp->SetHeader("X-Request-tid", hv::to_string(hv_gettid()));
  87. return HTTP_STATUS_NEXT;
  88. });
  89. HttpServer server;
  90. server.service = &router;
  91. server.port = port;
  92. #if TEST_HTTPS
  93. server.https_port = 8443;
  94. hssl_ctx_opt_t param;
  95. memset(&param, 0, sizeof(param));
  96. param.crt_file = "cert/server.crt";
  97. param.key_file = "cert/server.key";
  98. param.endpoint = HSSL_SERVER;
  99. if (server.newSslCtx(&param) != 0) {
  100. fprintf(stderr, "new SSL_CTX failed!\n");
  101. return -20;
  102. }
  103. #endif
  104. // uncomment to test multi-processes
  105. // server.setProcessNum(4);
  106. // uncomment to test multi-threads
  107. // server.setThreadNum(4);
  108. server.start();
  109. // press Enter to stop
  110. while (getchar() != '\n');
  111. hv::async::cleanup();
  112. return 0;
  113. }