Skip to content

Commit 15d192d

Browse files
committed
http_cb for curl --http2
1 parent 13e0e8d commit 15d192d

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

http/Http2Parser.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,9 @@ int on_header_callback(nghttp2_session *session,
338338
HttpResponse* res = (HttpResponse*)hp->parsed;
339339
if (strcmp(name, ":status") == 0) {
340340
res->status_code = (http_status)atoi(value);
341+
if (res->http_cb) {
342+
res->http_cb(res, HP_MESSAGE_BEGIN, NULL, 0);
343+
}
341344
}
342345
}
343346
}
@@ -369,7 +372,11 @@ int on_data_chunk_recv_callback(nghttp2_session *session,
369372
//printd("%.*s\n", (int)len, data);
370373
}
371374
}
372-
hp->parsed->body.append((const char*)data, len);
375+
if (hp->parsed->http_cb) {
376+
hp->parsed->http_cb(hp->parsed, HP_BODY, (const char*)data, len);
377+
} else {
378+
hp->parsed->body.append((const char*)data, len);
379+
}
373380
return 0;
374381
}
375382

@@ -405,6 +412,13 @@ int on_frame_recv_callback(nghttp2_session *session,
405412
printd("on_stream_closed stream_id=%d\n", hp->stream_id);
406413
hp->stream_closed = 1;
407414
hp->frame_type_when_stream_closed = frame->hd.type;
415+
if (hp->parsed->http_cb) {
416+
if (hp->state == H2_RECV_HEADERS) {
417+
hp->parsed->http_cb(hp->parsed, HP_HEADERS_COMPLETE, NULL, 0);
418+
} else if (hp->state == H2_RECV_DATA) {
419+
hp->parsed->http_cb(hp->parsed, HP_MESSAGE_COMPLETE, NULL, 0);
420+
}
421+
}
408422
}
409423
}
410424

http/client/http_client.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,9 @@ static size_t s_header_cb(char* buf, size_t size, size_t cnt, void* userdata) {
269269
resp->http_major = http_major;
270270
resp->http_minor = http_minor;
271271
resp->status_code = (http_status)status_code;
272+
if (resp->http_cb) {
273+
resp->http_cb(resp, HP_MESSAGE_BEGIN, NULL, 0);
274+
}
272275
}
273276
}
274277
else {
@@ -380,6 +383,9 @@ int __http_client_send(http_client_t* cli, HttpRequest* req, HttpResponse* resp)
380383
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, req->body.size());
381384
}
382385

386+
if (req->connect_timeout > 0) {
387+
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, req->connect_timeout);
388+
}
383389
if (req->timeout > 0) {
384390
curl_easy_setopt(curl, CURLOPT_TIMEOUT, req->timeout);
385391
}
@@ -416,6 +422,10 @@ int __http_client_send(http_client_t* cli, HttpRequest* req, HttpResponse* resp)
416422
}
417423
*/
418424

425+
if (resp->http_cb) {
426+
resp->http_cb(resp, HP_MESSAGE_COMPLETE, NULL, 0);
427+
}
428+
419429
return ret;
420430
}
421431

0 commit comments

Comments
 (0)