Bladeren bron

HttpMessage::GetJson GetForm GetUrlEncoded SetFormData SetUrlEncoded

ithewei 4 jaren geleden
bovenliggende
commit
b12d3f6ebe
3 gewijzigde bestanden met toevoegingen van 74 en 30 verwijderingen
  1. 3 2
      examples/http_client_test.cpp
  2. 57 5
      http/HttpMessage.h
  3. 14 23
      http/server/HttpContext.h

+ 3 - 2
examples/http_client_test.cpp

@@ -81,8 +81,9 @@ static void test_requests() {
     requests::Request req(new HttpRequest);
     req->method = HTTP_POST;
     req->url = "http://127.0.0.1:8080/echo";
-    req->form["username"] = FormData("admin");
-    req->form["avata"] = FormFile("avatar.jpg");
+    req->content_type = MULTIPART_FORM_DATA;
+    req->SetFormData("username", "admin");
+    req->SetFormFile("avatar", "avatar.jpg");
     resp = requests::request(req);
     if (resp == NULL) {
         printf("request failed!\n");

+ 57 - 5
http/HttpMessage.h

@@ -17,12 +17,17 @@
  *
  * @function
  * Content, ContentLength, ContentType
- * Get, Set
+ * ParseUrl, ParseBody
+ * DumpUrl, DumpHeaders, DumpBody, Dump
+ * GetJson, GetForm, GetUrlEncoded
  * GetHeader, GetParam, GetString, GetBool, GetInt, GetFloat
- * String, Data, File, Json
+ * String, Data, File, Json, FormFile, SetFormData, SetUrlEncoded
+ * Get, Set
  *
  * @example
- * see examples/httpd
+ * examples/http_server_test.cpp
+ * examples/http_client_test.cpp
+ * examples/httpd
  *
  */
 
@@ -144,18 +149,46 @@ public:
                     {1, 2, 3}
                 ));
      */
+    // Content-Type: application/json
     template<typename T>
     int Json(const T& t) {
         content_type = APPLICATION_JSON;
         json = t;
         return 200;
     }
+    const hv::Json& GetJson() {
+        if (json.empty() && ContentType() == APPLICATION_JSON) {
+            ParseBody();
+        }
+        return json;
+    }
 
-    void FormFile(const char* name, const char* filepath) {
+    // Content-Type: multipart/form-data
+    template<typename T>
+    void SetFormData(const char* name, const T& t) {
+        form[name] = FormData(t);
+    }
+    void SetFormFile(const char* name, const char* filepath) {
+        form[name] = FormData(NULL, filepath);
+    }
+    int FormFile(const char* name, const char* filepath) {
         content_type = MULTIPART_FORM_DATA;
         form[name] = FormData(NULL, filepath);
+        return 200;
+    }
+    const MultiPart& GetForm() {
+        if (form.empty() && ContentType() == MULTIPART_FORM_DATA) {
+            ParseBody();
+        }
+        return form;
+    }
+    std::string GetFormData(const char* name, const std::string& defvalue = "") {
+        if (form.empty() && ContentType() == MULTIPART_FORM_DATA) {
+            ParseBody();
+        }
+        auto iter = form.find(name);
+        return iter == form.end() ? defvalue : iter->second.content;
     }
-
     int SaveFormFile(const char* name, const char* path) {
         if (ContentType() != MULTIPART_FORM_DATA) {
             return HTTP_STATUS_BAD_REQUEST;
@@ -175,6 +208,25 @@ public:
         file.write(formdata.content.data(), formdata.content.size());
         return 200;
     }
+
+    // Content-Type: application/x-www-form-urlencoded
+    template<typename T>
+    void SetUrlEncoded(const char* key, const T& t) {
+        kv[key] = hv::to_string(t);
+    }
+    const hv::KeyValue& GetUrlEncoded() {
+        if (kv.empty() && ContentType() == X_WWW_FORM_URLENCODED) {
+            ParseBody();
+        }
+        return kv;
+    }
+    std::string GetUrlEncoded(const char* key, const std::string& defvalue = "") {
+        if (kv.empty() && ContentType() == X_WWW_FORM_URLENCODED) {
+            ParseBody();
+        }
+        auto iter = kv.find(key);
+        return iter == kv.end() ? defvalue : iter->second;
+    }
 #endif
 
     HttpMessage() {

+ 14 - 23
http/server/HttpContext.h

@@ -61,11 +61,11 @@ struct HV_EXPORT HttpContext {
     }
 
     bool is(http_content_type content_type) {
-        return request->content_type == content_type;
+        return request->ContentType() == content_type;
     }
 
     bool is(const char* content_type) {
-        return request->content_type == http_content_type_enum(content_type);
+        return request->ContentType() == http_content_type_enum(content_type);
     }
 
     std::string& body() {
@@ -73,34 +73,25 @@ struct HV_EXPORT HttpContext {
     }
 
 #ifndef WITHOUT_HTTP_CONTENT
+    // Content-Type: application/json
     const hv::Json& json() {
-        // Content-Type: application/json
-        if (request->content_type == APPLICATION_JSON &&
-            request->json.empty() &&
-            !request->body.empty()) {
-            request->ParseBody();
-        }
-        return request->json;
+        return request->GetJson();
     }
 
+    // Content-Type: multipart/form-data
     const MultiPart& form() {
-        // Content-Type: multipart/form-data
-        if (request->content_type == MULTIPART_FORM_DATA &&
-            request->form.empty() &&
-            !request->body.empty()) {
-            request->ParseBody();
-        }
-        return request->form;
+        return request->GetForm();
+    }
+    std::string form(const char* name, const std::string& defvalue = "") {
+        return request->GetFormData(name);
     }
 
+    // Content-Type: application/x-www-form-urlencoded
     const hv::KeyValue& urlencoded() {
-        // Content-Type: application/x-www-form-urlencoded
-        if (request->content_type == X_WWW_FORM_URLENCODED &&
-            request->kv.empty() &&
-            !request->body.empty()) {
-            request->ParseBody();
-        }
-        return request->kv;
+        return request->GetUrlEncoded();
+    }
+    std::string urlencoded(const char* key, const std::string& defvalue = "") {
+        return request->GetUrlEncoded(key);
     }
 
     // T=[bool, int, int64_t, float, double]