Browse Source

body.reserve(content_lenght)

hewei.it 5 years ago
parent
commit
afcc3186d7
1 changed files with 8 additions and 0 deletions
  1. 8 0
      http/Http1Parser.cpp

+ 8 - 0
http/Http1Parser.cpp

@@ -89,6 +89,14 @@ int on_headers_complete(http_parser* parser) {
     if (iter != hp->parsed->headers.end()) {
     if (iter != hp->parsed->headers.end()) {
         hp->parsed->content_type = http_content_type_enum(iter->second.c_str());
         hp->parsed->content_type = http_content_type_enum(iter->second.c_str());
     }
     }
+    iter = hp->parsed->headers.find("content-length");
+    if (iter != hp->parsed->headers.end()) {
+        int content_length = atoi(iter->second.c_str());
+        hp->parsed->content_length = content_length;
+        if (content_length > hp->parsed->body.capacity()) {
+            hp->parsed->body.reserve(content_length);
+        }
+    }
     hp->parsed->http_major = parser->http_major;
     hp->parsed->http_major = parser->http_major;
     hp->parsed->http_minor = parser->http_minor;
     hp->parsed->http_minor = parser->http_minor;
     if (hp->parsed->type == HTTP_REQUEST) {
     if (hp->parsed->type == HTTP_REQUEST) {