Sfoglia il codice sorgente

http_cb for curl --http2

ithewei 3 anni fa
parent
commit
15d192d72e
2 ha cambiato i file con 25 aggiunte e 1 eliminazioni
  1. 15 1
      http/Http2Parser.cpp
  2. 10 0
      http/client/http_client.cpp

+ 15 - 1
http/Http2Parser.cpp

@@ -338,6 +338,9 @@ int on_header_callback(nghttp2_session *session,
             HttpResponse* res = (HttpResponse*)hp->parsed;
             if (strcmp(name, ":status") == 0) {
                 res->status_code = (http_status)atoi(value);
+                if (res->http_cb) {
+                    res->http_cb(res, HP_MESSAGE_BEGIN, NULL, 0);
+                }
             }
         }
     }
@@ -369,7 +372,11 @@ int on_data_chunk_recv_callback(nghttp2_session *session,
             //printd("%.*s\n", (int)len, data);
         }
     }
-    hp->parsed->body.append((const char*)data, len);
+    if (hp->parsed->http_cb) {
+        hp->parsed->http_cb(hp->parsed, HP_BODY, (const char*)data, len);
+    } else {
+        hp->parsed->body.append((const char*)data, len);
+    }
     return 0;
 }
 
@@ -405,6 +412,13 @@ int on_frame_recv_callback(nghttp2_session *session,
             printd("on_stream_closed stream_id=%d\n", hp->stream_id);
             hp->stream_closed = 1;
             hp->frame_type_when_stream_closed = frame->hd.type;
+            if (hp->parsed->http_cb) {
+                if (hp->state == H2_RECV_HEADERS) {
+                    hp->parsed->http_cb(hp->parsed, HP_HEADERS_COMPLETE, NULL, 0);
+                } else if (hp->state == H2_RECV_DATA) {
+                    hp->parsed->http_cb(hp->parsed, HP_MESSAGE_COMPLETE, NULL, 0);
+                }
+            }
         }
     }
 

+ 10 - 0
http/client/http_client.cpp

@@ -269,6 +269,9 @@ static size_t s_header_cb(char* buf, size_t size, size_t cnt, void* userdata) {
             resp->http_major = http_major;
             resp->http_minor = http_minor;
             resp->status_code = (http_status)status_code;
+            if (resp->http_cb) {
+                resp->http_cb(resp, HP_MESSAGE_BEGIN, NULL, 0);
+            }
         }
     }
     else {
@@ -380,6 +383,9 @@ int __http_client_send(http_client_t* cli, HttpRequest* req, HttpResponse* resp)
         curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, req->body.size());
     }
 
+    if (req->connect_timeout > 0) {
+        curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, req->connect_timeout);
+    }
     if (req->timeout > 0) {
         curl_easy_setopt(curl, CURLOPT_TIMEOUT, req->timeout);
     }
@@ -416,6 +422,10 @@ int __http_client_send(http_client_t* cli, HttpRequest* req, HttpResponse* resp)
     }
     */
 
+    if (resp->http_cb) {
+        resp->http_cb(resp, HP_MESSAGE_COMPLETE, NULL, 0);
+    }
+
     return ret;
 }