| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- /*
- * sample http server
- * more detail see examples/httpd
- *
- */
- #include "HttpServer.h"
- #include "hssl.h"
- /*
- * #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;
- router.GET("/ping", [](HttpRequest* req, HttpResponse* resp) {
- return resp->String("pong");
- });
- router.GET("/data", [](HttpRequest* req, HttpResponse* resp) {
- static char data[] = "0123456789";
- return resp->Data(data, 10);
- });
- router.GET("/paths", [&router](HttpRequest* req, HttpResponse* resp) {
- return resp->Json(router.Paths());
- });
- 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;
- });
- router.POST("/echo", [](HttpRequest* req, HttpResponse* resp) {
- resp->content_type = req->content_type;
- resp->body = req->body;
- return 200;
- });
- http_server_t server;
- server.service = &router;
- server.port = port;
- #if TEST_HTTPS
- server.https_port = 8443;
- hssl_ctx_init_param_t param;
- memset(¶m, 0, sizeof(param));
- param.crt_file = "cert/server.crt";
- param.key_file = "cert/server.key";
- if (hssl_ctx_init(¶m) == NULL) {
- fprintf(stderr, "SSL certificate verify failed!\n");
- return -20;
- }
- #endif
- // uncomment to test multi-processes
- // server.worker_processes = 4;
- // uncomment to test multi-threads
- // server.worker_threads = 4;
- #if 1
- http_server_run(&server);
- #else
- // test http_server_stop
- http_server_run(&server, 0);
- hv_sleep(10);
- http_server_stop(&server);
- #endif
- return 0;
- }
|