Bladeren bron

show HttpContext::send()

ithewei 4 jaren geleden
bovenliggende
commit
9e036f8d5a
5 gewijzigde bestanden met toevoegingen van 9 en 17 verwijderingen
  1. 2 4
      README-CN.md
  2. 2 4
      README.md
  3. 2 4
      examples/http_server_test.cpp
  4. 2 4
      examples/httpd/router.h
  5. 1 1
      http/server/HttpContext.h

+ 2 - 4
README-CN.md

@@ -131,10 +131,8 @@ int main() {
         return 200;
     });
 
-    router.POST("/echo", [](HttpRequest* req, HttpResponse* resp) {
-        resp->content_type = req->content_type;
-        resp->body = req->body;
-        return 200;
+    router.POST("/echo", [](const HttpContextPtr& ctx) {
+        return ctx->send(ctx->body(), ctx->type());
     });
 
     http_server_t server;

+ 2 - 4
README.md

@@ -129,10 +129,8 @@ int main() {
         return 200;
     });
 
-    router.POST("/echo", [](HttpRequest* req, HttpResponse* resp) {
-        resp->content_type = req->content_type;
-        resp->body = req->body;
-        return 200;
+    router.POST("/echo", [](const HttpContextPtr& ctx) {
+        return ctx->send(ctx->body(), ctx->type());
     });
 
     http_server_t server;

+ 2 - 4
examples/http_server_test.cpp

@@ -53,10 +53,8 @@ int main(int argc, char** argv) {
         return 200;
     });
 
-    router.POST("/echo", [](HttpRequest* req, HttpResponse* resp) {
-        resp->content_type = req->content_type;
-        resp->body = req->body;
-        return 200;
+    router.POST("/echo", [](const HttpContextPtr& ctx) {
+        return ctx->send(ctx->body(), ctx->type());
     });
 
     http_server_t server;

+ 2 - 4
examples/httpd/router.h

@@ -59,10 +59,8 @@ public:
         });
 
         // curl -v http://ip:port/echo -d "hello,world!"
-        router.POST("/echo", [](HttpRequest* req, HttpResponse* resp) {
-            resp->content_type = req->content_type;
-            resp->body = req->body;
-            return 200;
+        router.POST("/echo", [](const HttpContextPtr& ctx) {
+            return ctx->send(ctx->body(), ctx->type());
         });
 
         // wildcard *

+ 1 - 1
http/server/HttpContext.h

@@ -70,7 +70,7 @@ struct HV_EXPORT HttpContext {
     }
 
     std::string& body() {
-        return response->body;
+        return request->body;
     }
 
 #ifndef WITHOUT_HTTP_CONTENT