Browse Source

optimize DumpBody

hewei.it 4 years ago
parent
commit
18060534e6
2 changed files with 13 additions and 12 deletions
  1. 12 12
      http/HttpMessage.cpp
  2. 1 0
      http/HttpMessage.h

+ 12 - 12
http/HttpMessage.cpp

@@ -258,6 +258,15 @@ void HttpMessage::DumpBody() {
 #endif
 }
 
+void HttpMessage::DumpBody(std::string& str) {
+    DumpBody();
+    const char* content = (const char*)Content();
+    int content_length = ContentLength();
+    if (content && content_length) {
+        str.append(content, content_length);
+    }
+}
+
 int HttpMessage::ParseBody() {
     if (body.size() == 0) {
         return -1;
@@ -306,10 +315,7 @@ std::string HttpMessage::Dump(bool is_dump_headers, bool is_dump_body) {
     }
     str += "\r\n";
     if (is_dump_body) {
-        DumpBody();
-        if (ContentLength() != 0) {
-            str.insert(str.size(), (const char*)Content(), ContentLength());
-        }
+        DumpBody(str);
     }
     return str;
 }
@@ -408,10 +414,7 @@ std::string HttpRequest::Dump(bool is_dump_headers, bool is_dump_body) {
     }
     str += "\r\n";
     if (is_dump_body) {
-        DumpBody();
-        if (ContentLength() != 0) {
-            str.insert(str.size(), (const char*)Content(), ContentLength());
-        }
+        DumpBody(str);
     }
     return str;
 }
@@ -428,10 +431,7 @@ std::string HttpResponse::Dump(bool is_dump_headers, bool is_dump_body) {
     }
     str += "\r\n";
     if (is_dump_body) {
-        DumpBody();
-        if (ContentLength() != 0) {
-            str.insert(str.size(), (const char*)Content(), ContentLength());
-        }
+        DumpBody(str);
     }
     return str;
 }

+ 1 - 0
http/HttpMessage.h

@@ -182,6 +182,7 @@ public:
     void DumpHeaders(std::string& str);
     // structured content -> body
     void DumpBody();
+    void DumpBody(std::string& str);
     // body -> structured content
     // @retval 0:succeed
     int  ParseBody();