Browse Source

Add body_cb, chunked_cb

hewei.it 4 years ago
parent
commit
ae7fb89442
3 changed files with 20 additions and 8 deletions
  1. 10 3
      examples/curl.cpp
  2. 6 2
      http/Http1Parser.cpp
  3. 4 3
      http/HttpMessage.h

+ 10 - 3
examples/curl.cpp

@@ -223,16 +223,23 @@ int main(int argc, char* argv[]) {
         }
     }
     HttpResponse res;
+    /*
+    res.body_cb = [](const char* data, size_t size){
+        printf("%.*s", (int)size, data);
+    };
+    */
+    res.chunked_cb = [](const char* data, size_t size){
+        printf("%.*s", (int)size, data);
+    };
     http_client_t* hc = http_client_new();
 send:
-    ret = http_client_send(hc, &req, &res);
     if (verbose) {
         printf("%s\n", req.Dump(true,true).c_str());
     }
+    ret = http_client_send(hc, &req, &res);
     if (ret != 0) {
         printf("* Failed:%s:%d\n", http_client_strerror(ret), ret);
-    }
-    else {
+    } else {
         if (verbose) {
             printf("%s\n", res.Dump(true,true).c_str());
         }

+ 6 - 2
http/Http1Parser.cpp

@@ -78,7 +78,11 @@ int on_body(http_parser* parser, const char *at, size_t length) {
     // printd("on_body:%.*s\n", (int)length, at);
     Http1Parser* hp = (Http1Parser*)parser->data;
     hp->state = HP_BODY;
-    hp->parsed->body.append(at, length);
+    if (hp->parsed->body_cb) {
+        hp->parsed->body_cb(at, length);
+    } else {
+        hp->parsed->body.append(at, length);
+    }
     return 0;
 }
 
@@ -139,7 +143,6 @@ int on_chunk_header(http_parser* parser) {
     printd("on_chunk_header:%llu\n", parser->content_length);
     Http1Parser* hp = (Http1Parser*)parser->data;
     hp->state = HP_CHUNK_HEADER;
-    hp->parsed->body.clear();
     int chunk_size = parser->content_length;
     int reserve_size = MIN(chunk_size + 1, MAX_CONTENT_LENGTH);
     if (reserve_size > hp->parsed->body.capacity()) {
@@ -154,6 +157,7 @@ int on_chunk_complete(http_parser* parser) {
     hp->state = HP_CHUNK_COMPLETE;
     if (hp->parsed->chunked_cb) {
         hp->parsed->chunked_cb(hp->parsed->body.c_str(), hp->parsed->body.size());
+        hp->parsed->body.clear();
     }
     return 0;
 }

+ 4 - 3
http/HttpMessage.h

@@ -71,7 +71,8 @@ struct HV_EXPORT HttpCookie {
 typedef std::map<std::string, std::string, StringCaseLess>  http_headers;
 typedef std::vector<HttpCookie>                             http_cookies;
 typedef std::string                                         http_body;
-typedef std::function<void(const char* data, size_t size)>  http_chuncked_cb;
+typedef std::function<void(const char* data, size_t size)>  http_body_cb;
+typedef std::function<void(const char* data, size_t size)>  http_chunked_cb;
 
 class HV_EXPORT HttpMessage {
 public:
@@ -83,7 +84,8 @@ public:
     http_headers        headers;
     http_cookies        cookies;
     http_body           body;
-    http_chuncked_cb    chunked_cb;
+    http_body_cb        body_cb;
+    http_chunked_cb     chunked_cb; // Transfer-Encoding: chunked
 
     // structured content
     void*               content;    // DATA_NO_COPY
@@ -179,7 +181,6 @@ public:
         content = NULL;
         content_length = 0;
         content_type = CONTENT_TYPE_NONE;
-        chunked_cb = NULL;
     }
 
     virtual void Reset() {