http_server_test.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * sample http server
  3. * more detail see examples/httpd
  4. *
  5. */
  6. #include "HttpServer.h"
  7. using namespace hv;
  8. /*
  9. * #define TEST_HTTPS 1
  10. *
  11. * @build ./configure --with-openssl && make clean && make
  12. *
  13. * @server bin/http_server_test 8080
  14. *
  15. * @client curl -v http://127.0.0.1:8080/ping
  16. * curl -v https://127.0.0.1:8443/ping --insecure
  17. * bin/curl -v http://127.0.0.1:8080/ping
  18. * bin/curl -v https://127.0.0.1:8443/ping
  19. *
  20. */
  21. #define TEST_HTTPS 0
  22. int main(int argc, char** argv) {
  23. HV_MEMCHECK;
  24. int port = 0;
  25. if (argc > 1) {
  26. port = atoi(argv[1]);
  27. }
  28. if (port == 0) port = 8080;
  29. HttpService router;
  30. // curl -v http://ip:port/
  31. router.Static("/", "./html");
  32. // curl -v http://ip:port/proxy/get
  33. router.Proxy("/proxy/", "http://httpbin.org/");
  34. // curl -v http://ip:port/ping
  35. router.GET("/ping", [](HttpRequest* req, HttpResponse* resp) {
  36. return resp->String("pong");
  37. });
  38. // curl -v http://ip:port/data
  39. router.GET("/data", [](HttpRequest* req, HttpResponse* resp) {
  40. static char data[] = "0123456789";
  41. return resp->Data(data, 10 /*, false */);
  42. });
  43. // curl -v http://ip:port/paths
  44. router.GET("/paths", [&router](HttpRequest* req, HttpResponse* resp) {
  45. return resp->Json(router.Paths());
  46. });
  47. // curl -v http://ip:port/get?env=1
  48. router.GET("/get", [](HttpRequest* req, HttpResponse* resp) {
  49. resp->json["origin"] = req->client_addr.ip;
  50. resp->json["url"] = req->url;
  51. resp->json["args"] = req->query_params;
  52. resp->json["headers"] = req->headers;
  53. return 200;
  54. });
  55. // curl -v http://ip:port/echo -d "hello,world!"
  56. router.POST("/echo", [](const HttpContextPtr& ctx) {
  57. return ctx->send(ctx->body(), ctx->type());
  58. });
  59. // curl -v http://ip:port/user/123
  60. router.GET("/user/{id}", [](const HttpContextPtr& ctx) {
  61. hv::Json resp;
  62. resp["id"] = ctx->param("id");
  63. return ctx->send(resp.dump(2));
  64. });
  65. HttpServer server;
  66. server.service = &router;
  67. server.port = port;
  68. #if TEST_HTTPS
  69. server.https_port = 8443;
  70. hssl_ctx_opt_t param;
  71. memset(&param, 0, sizeof(param));
  72. param.crt_file = "cert/server.crt";
  73. param.key_file = "cert/server.key";
  74. param.endpoint = HSSL_SERVER;
  75. if (server.newSslCtx(&param) != 0) {
  76. fprintf(stderr, "new SSL_CTX failed!\n");
  77. return -20;
  78. }
  79. #endif
  80. // uncomment to test multi-processes
  81. // server.setProcessNum(4);
  82. // uncomment to test multi-threads
  83. // server.setThreadNum(4);
  84. server.start();
  85. // press Enter to stop
  86. while (getchar() != '\n');
  87. return 0;
  88. }