瀏覽代碼

http_parser response to HEAD

ithewei 4 年之前
父節點
當前提交
679f64db5e
共有 2 個文件被更改,包括 29 次插入14 次删除
  1. 21 14
      http/Http1Parser.cpp
  2. 8 0
      http/Http1Parser.h

+ 21 - 14
http/Http1Parser.cpp

@@ -26,6 +26,7 @@ Http1Parser::Http1Parser(http_session_type type) {
     }
     http_parser_init(&parser, HTTP_BOTH);
     parser.data = this;
+    flags = 0;
     state = HP_START_REQ_OR_RES;
     submited = NULL;
     parsed = NULL;
@@ -85,18 +86,8 @@ int on_headers_complete(http_parser* parser) {
     printd("on_headers_complete\n");
     Http1Parser* hp = (Http1Parser*)parser->data;
     hp->handle_header();
-    auto iter = hp->parsed->headers.find("content-type");
-    if (iter != hp->parsed->headers.end()) {
-        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);
-        }
-    }
+
+    bool skip_body = false;
     hp->parsed->http_major = parser->http_major;
     hp->parsed->http_minor = parser->http_minor;
     if (hp->parsed->type == HTTP_REQUEST) {
@@ -107,9 +98,26 @@ int on_headers_complete(http_parser* parser) {
     else if (hp->parsed->type == HTTP_RESPONSE) {
         HttpResponse* res = (HttpResponse*)hp->parsed;
         res->status_code = (http_status)parser->status_code;
+        // response to HEAD
+        if (res->status_code == 200 && hp->flags & F_SKIPBODY) {
+            skip_body = true;
+        }
+    }
+
+    auto iter = hp->parsed->headers.find("content-type");
+    if (iter != hp->parsed->headers.end()) {
+        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 ((!skip_body) && content_length > hp->parsed->body.capacity()) {
+            hp->parsed->body.reserve(content_length);
+        }
     }
     hp->state = HP_HEADERS_COMPLETE;
-    return 0;
+    return skip_body ? 1 : 0;
 }
 
 int on_message_complete(http_parser* parser) {
@@ -118,4 +126,3 @@ int on_message_complete(http_parser* parser) {
     hp->state = HP_MESSAGE_COMPLETE;
     return 0;
 }
-

+ 8 - 0
http/Http1Parser.h

@@ -20,6 +20,7 @@ class Http1Parser : public HttpParser {
 public:
     static http_parser_settings*    cbs;
     http_parser                     parser;
+    int                             flags;
     http_parser_state               state;
     HttpMessage*                    submited;
     HttpMessage*                    parsed;
@@ -85,6 +86,13 @@ public:
     // SubmitRequest -> while(GetSendData) {send} -> InitResponse -> do {recv -> FeedRecvData} while(WantRecv)
     virtual int SubmitRequest(HttpRequest* req) {
         submited = req;
+        if (req) {
+            if (req->method == HTTP_HEAD) {
+                flags |= F_SKIPBODY;
+            } else {
+                flags &= ~F_SKIPBODY;
+            }
+        }
         return 0;
     }