|
@@ -10,6 +10,8 @@ static int on_body(http_parser* parser, const char *at, size_t length);
|
|
|
static int on_message_begin(http_parser* parser);
|
|
static int on_message_begin(http_parser* parser);
|
|
|
static int on_headers_complete(http_parser* parser);
|
|
static int on_headers_complete(http_parser* parser);
|
|
|
static int on_message_complete(http_parser* parser);
|
|
static int on_message_complete(http_parser* parser);
|
|
|
|
|
+static int on_chunk_header(http_parser* parser);
|
|
|
|
|
+static int on_chunk_complete(http_parser* parser);
|
|
|
|
|
|
|
|
http_parser_settings* Http1Parser::cbs = NULL;
|
|
http_parser_settings* Http1Parser::cbs = NULL;
|
|
|
|
|
|
|
@@ -25,6 +27,8 @@ Http1Parser::Http1Parser(http_session_type type) {
|
|
|
cbs->on_headers_complete = on_headers_complete;
|
|
cbs->on_headers_complete = on_headers_complete;
|
|
|
cbs->on_body = on_body;
|
|
cbs->on_body = on_body;
|
|
|
cbs->on_message_complete = on_message_complete;
|
|
cbs->on_message_complete = on_message_complete;
|
|
|
|
|
+ cbs->on_chunk_header = on_chunk_header;
|
|
|
|
|
+ cbs->on_chunk_complete = on_chunk_complete;
|
|
|
}
|
|
}
|
|
|
http_parser_init(&parser, HTTP_BOTH);
|
|
http_parser_init(&parser, HTTP_BOTH);
|
|
|
parser.data = this;
|
|
parser.data = this;
|
|
@@ -46,7 +50,7 @@ int on_url(http_parser* parser, const char *at, size_t length) {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
int on_status(http_parser* parser, const char *at, size_t length) {
|
|
int on_status(http_parser* parser, const char *at, size_t length) {
|
|
|
- printd("on_status:%.*s\n", (int)length, at);
|
|
|
|
|
|
|
+ printd("on_status:%d %.*s\n", (int)parser->status_code, (int)length, at);
|
|
|
Http1Parser* hp = (Http1Parser*)parser->data;
|
|
Http1Parser* hp = (Http1Parser*)parser->data;
|
|
|
hp->state = HP_STATUS;
|
|
hp->state = HP_STATUS;
|
|
|
return 0;
|
|
return 0;
|
|
@@ -62,7 +66,7 @@ int on_header_field(http_parser* parser, const char *at, size_t length) {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
int on_header_value(http_parser* parser, const char *at, size_t length) {
|
|
int on_header_value(http_parser* parser, const char *at, size_t length) {
|
|
|
- printd("on_header_value:%.*s""\n", (int)length, at);
|
|
|
|
|
|
|
+ printd("on_header_value:%.*s\n", (int)length, at);
|
|
|
Http1Parser* hp = (Http1Parser*)parser->data;
|
|
Http1Parser* hp = (Http1Parser*)parser->data;
|
|
|
hp->state = HP_HEADER_VALUE;
|
|
hp->state = HP_HEADER_VALUE;
|
|
|
hp->header_value.append(at, length);
|
|
hp->header_value.append(at, length);
|
|
@@ -70,7 +74,8 @@ int on_header_value(http_parser* parser, const char *at, size_t length) {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
int on_body(http_parser* parser, const char *at, size_t length) {
|
|
int on_body(http_parser* parser, const char *at, size_t length) {
|
|
|
- //printd("on_body:%.*s""\n", (int)length, at);
|
|
|
|
|
|
|
+ printd("on_body:%d\n", (int)length);
|
|
|
|
|
+ // printd("on_body:%.*s\n", (int)length, at);
|
|
|
Http1Parser* hp = (Http1Parser*)parser->data;
|
|
Http1Parser* hp = (Http1Parser*)parser->data;
|
|
|
hp->state = HP_BODY;
|
|
hp->state = HP_BODY;
|
|
|
hp->parsed->body.append(at, length);
|
|
hp->parsed->body.append(at, length);
|
|
@@ -129,3 +134,26 @@ int on_message_complete(http_parser* parser) {
|
|
|
hp->state = HP_MESSAGE_COMPLETE;
|
|
hp->state = HP_MESSAGE_COMPLETE;
|
|
|
return 0;
|
|
return 0;
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+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()) {
|
|
|
|
|
+ hp->parsed->body.reserve(reserve_size);
|
|
|
|
|
+ }
|
|
|
|
|
+ return 0;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+int on_chunk_complete(http_parser* parser) {
|
|
|
|
|
+ printd("on_chunk_complete\n");
|
|
|
|
|
+ Http1Parser* hp = (Http1Parser*)parser->data;
|
|
|
|
|
+ hp->state = HP_CHUNK_COMPLETE;
|
|
|
|
|
+ if (hp->parsed->chunked_cb) {
|
|
|
|
|
+ hp->parsed->chunked_cb(hp->parsed->body.c_str(), hp->parsed->body.size());
|
|
|
|
|
+ }
|
|
|
|
|
+ return 0;
|
|
|
|
|
+}
|