Skip to content

Commit

Permalink
Add body_cb, chunked_cb
Browse files Browse the repository at this point in the history
  • Loading branch information
hewei.it committed Sep 3, 2021
1 parent 3f3ace2 commit ae7fb89
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
13 changes: 10 additions & 3 deletions examples/curl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
8 changes: 6 additions & 2 deletions http/Http1Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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()) {
Expand All @@ -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;
}
7 changes: 4 additions & 3 deletions http/HttpMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -83,7 +84,8 @@ class HV_EXPORT HttpMessage {
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
Expand Down Expand Up @@ -179,7 +181,6 @@ class HV_EXPORT HttpMessage {
content = NULL;
content_length = 0;
content_type = CONTENT_TYPE_NONE;
chunked_cb = NULL;
}

virtual void Reset() {
Expand Down

0 comments on commit ae7fb89

Please sign in to comment.