ithewei 4 vuotta sitten
vanhempi
commit
c0d23f05fe
2 muutettua tiedostoa jossa 17 lisäystä ja 7 poistoa
  1. 14 5
      examples/httpd/handler.cpp
  2. 3 2
      http/server/HttpService.h

+ 14 - 5
examples/httpd/handler.cpp

@@ -12,11 +12,7 @@
 int Handler::preprocessor(HttpRequest* req, HttpResponse* resp) {
     // printf("%s:%d\n", req->client_addr.ip.c_str(), req->client_addr.port);
     // printf("%s\n", req->Dump(true, true).c_str());
-    // if (req->content_type != APPLICATION_JSON) {
-    //     return response_status(resp, HTTP_STATUS_BAD_REQUEST);
-    // }
-    req->ParseBody();
-    resp->content_type = APPLICATION_JSON;
+
     // cors
     resp->headers["Access-Control-Allow-Origin"] = "*";
     if (req->method == HTTP_OPTIONS) {
@@ -25,6 +21,18 @@ int Handler::preprocessor(HttpRequest* req, HttpResponse* resp) {
         resp->headers["Access-Control-Allow-Headers"] = req->GetHeader("Access-Control-Request-Headers", "Content-Type");
         return HTTP_STATUS_NO_CONTENT;
     }
+
+    // Unified verification request Content-Type?
+    // if (req->content_type != APPLICATION_JSON) {
+    //     return response_status(resp, HTTP_STATUS_BAD_REQUEST);
+    // }
+
+    // Deserialize request body to json, form, etc.
+    req->ParseBody();
+
+    // Unified setting response Content-Type?
+    resp->content_type = APPLICATION_JSON;
+
 #if 0
     // authentication sample code
     if (strcmp(req->path.c_str(), "/login") != 0) {
@@ -40,6 +48,7 @@ int Handler::preprocessor(HttpRequest* req, HttpResponse* resp) {
         return HTTP_STATUS_UNFINISHED;
     }
 #endif
+
     return HTTP_STATUS_UNFINISHED;
 }
 

+ 3 - 2
http/server/HttpService.h

@@ -3,6 +3,7 @@
 
 #include <string>
 #include <map>
+#include <unordered_map>
 #include <list>
 #include <memory>
 #include <functional>
@@ -47,9 +48,9 @@ struct http_method_handler {
     }
 };
 // method => http_method_handler
-typedef std::list<http_method_handler>                                  http_method_handlers;
+typedef std::list<http_method_handler>                                          http_method_handlers;
 // path => http_method_handlers
-typedef std::map<std::string, std::shared_ptr<http_method_handlers>>    http_api_handlers;
+typedef std::unordered_map<std::string, std::shared_ptr<http_method_handlers>>  http_api_handlers;
 
 namespace hv {