Przeglądaj źródła

Merge branch 'master' into annotate

hewei.it 4 lat temu
rodzic
commit
3d16507676

+ 8 - 0
README-CN.md

@@ -108,6 +108,14 @@ int main() {
         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;

+ 8 - 0
README.md

@@ -106,6 +106,14 @@ int main() {
         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;

+ 8 - 0
examples/http_server_test.cpp

@@ -39,6 +39,14 @@ int main() {
         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;

+ 9 - 0
examples/httpd/router.h

@@ -41,6 +41,15 @@ public:
             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", [](HttpRequest* req, HttpResponse* resp) {
             // 回显请求

+ 4 - 0
getting_started.sh

@@ -43,6 +43,10 @@ cmd="bin/curl -v localhost:8080/data" && run_cmd
 
 cmd="bin/curl -v localhost:8080/html/index.html" && run_cmd
 
+cmd="bin/curl -v localhost:8080/get?env=1" && run_cmd
+
+cmd="bin/curl -v localhost:8080/wildcard/test" && run_cmd
+
 cmd="bin/curl -v localhost:8080/echo -d 'hello,world!'" && echo_cmd
 bin/curl -v localhost:8080/echo -d 'hello,world!'
 

+ 1 - 1
http/HttpMessage.cpp

@@ -331,7 +331,7 @@ void HttpMessage::DumpBody() {
 #ifndef WITHOUT_HTTP_CONTENT
     switch(content_type) {
     case APPLICATION_JSON:
-        body = dump_json(json);
+        body = dump_json(json, 2);
         break;
     case MULTIPART_FORM_DATA:
     {

+ 2 - 2
http/http_content.cpp

@@ -231,8 +231,8 @@ int parse_multipart(std::string& str, MultiPart& mp, const char* boundary) {
     return nparse == str.size() ? 0 : -1;
 }
 
-std::string dump_json(hv::Json& json) {
-    return json.dump();
+std::string dump_json(hv::Json& json, int indent) {
+    return json.dump(indent);
 }
 
 int parse_json(const char* str, hv::Json& json, std::string& errmsg) {

+ 1 - 1
http/http_content.h

@@ -59,7 +59,7 @@ using Json = nlohmann::json;
 // using Json = nlohmann::ordered_json;
 }
 
-HV_EXPORT std::string dump_json(hv::Json& json);
+HV_EXPORT std::string dump_json(hv::Json& json, int indent = -1);
 HV_EXPORT int         parse_json(const char* str, hv::Json& json, std::string& errmsg);
 #endif