Skip to content
This repository has been archived by the owner on Jun 23, 2022. It is now read-only.

feat(http): support parsing content-type of HTTP request #593

Merged
merged 1 commit into from
Aug 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/http/http_message_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,12 @@ http_message_parser::http_message_parser()
// msg->buffers[0] = header
// msg->buffers[1] = body (blob())
msg.reset(message_ex::create_receive_message_with_standalone_header(blob()));
msg->buffers.resize(4);

message_header *header = msg->header;
header->hdr_length = sizeof(message_header);
header->hdr_crc32 = header->body_crc32 = CRC_INVALID;
strcpy(header->rpc_name, RPC_HTTP_SERVICE.to_string());
strcpy(header->rpc_name, "RPC_HTTP_SERVICE");
return 0;
};

Expand All @@ -96,13 +97,22 @@ http_message_parser::http_message_parser()
[](http_parser *parser, const char *at, size_t length) -> int {
http_message_parser *msg_parser = reinterpret_cast<parser_context *>(parser->data)->parser;
msg_parser->_stage = HTTP_ON_HEADER_FIELD;
if (strncmp(at, "Content-Type", length) == 0) {
msg_parser->_is_field_content_type = true;
}
return 0;
};

_parser_setting.on_header_value =
[](http_parser *parser, const char *at, size_t length) -> int {
http_message_parser *msg_parser = reinterpret_cast<parser_context *>(parser->data)->parser;
msg_parser->_stage = HTTP_ON_HEADER_VALUE;
if (msg_parser->_is_field_content_type) {
auto &msg = msg_parser->_current_message;
// msg->buffers[3] = content-type
msg->buffers[3] = blob::create_from_bytes(at, length);
msg_parser->_is_field_content_type = false;
}
return 0;
};

Expand All @@ -113,7 +123,7 @@ http_message_parser::http_message_parser()
auto &msg = msg_parser->_current_message;

// msg->buffers[2] = url
msg->buffers.emplace_back(blob::create_from_bytes(std::move(msg_parser->_url)));
msg->buffers[2] = blob::create_from_bytes(std::move(msg_parser->_url));

message_header *header = msg->header;
if (parser->type == HTTP_REQUEST && parser->method == HTTP_GET) {
Expand Down
2 changes: 2 additions & 0 deletions src/http/http_message_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ DEFINE_CUSTOMIZED_ID(network_header_format, NET_HDR_HTTP)
// msg->buffers[0] = header
// msg->buffers[1] = body
// msg->buffers[2] = url
// msg->buffers[3] = content-type
//

enum http_parser_stage
Expand Down Expand Up @@ -90,6 +91,7 @@ class http_message_parser : public message_parser
http_parser_settings _parser_setting;
http_parser _parser;

bool _is_field_content_type{false};
std::unique_ptr<message_ex> _current_message;
http_parser_stage _stage{HTTP_INVALID};
std::string _url;
Expand Down
10 changes: 6 additions & 4 deletions src/http/test/http_server_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class http_message_parser_test : public testing::Test
ASSERT_EQ(msg->hdr_format, NET_HDR_HTTP);
ASSERT_EQ(msg->header->hdr_type, http_method::HTTP_METHOD_GET);
ASSERT_EQ(msg->header->context.u.is_request, 1);
ASSERT_EQ(msg->buffers.size(), 3);
ASSERT_EQ(msg->buffers.size(), 4);
ASSERT_EQ(msg->buffers[2].size(), 1); // url

// ensure states are reset
Expand Down Expand Up @@ -167,11 +167,12 @@ TEST_F(http_message_parser_test, parse_request)
ASSERT_EQ(msg->hdr_format, NET_HDR_HTTP);
ASSERT_EQ(msg->header->hdr_type, http_method::HTTP_METHOD_POST);
ASSERT_EQ(msg->header->context.u.is_request, 1);
ASSERT_EQ(msg->buffers.size(), 3);
ASSERT_EQ(msg->buffers.size(), 4);
ASSERT_EQ(msg->buffers[1].to_string(), "Message Body sdfsdf"); // body
ASSERT_EQ( // url
msg->buffers[2].to_string(),
std::string("/path/file.html?sdfsdf=sdfs&sldf1=sdf"));
ASSERT_EQ(msg->buffers[3].to_string(), std::string("json"));
}

TEST_F(http_message_parser_test, eof)
Expand Down Expand Up @@ -217,14 +218,15 @@ TEST_F(http_message_parser_test, eof)
ASSERT_EQ(msg->hdr_format, NET_HDR_HTTP);
ASSERT_EQ(msg->header->hdr_type, http_method::HTTP_METHOD_GET);
ASSERT_EQ(msg->header->context.u.is_request, 1);
ASSERT_EQ(msg->buffers.size(), 3);
ASSERT_EQ(msg->buffers.size(), 4);
ASSERT_EQ(msg->buffers[1].to_string(), ""); // body
ASSERT_EQ( // url
msg->buffers[2].to_string(),
std::string("/CloudApiControl/HttpServer/telematics/v3/"
"weather?location=%E6%B5%B7%E5%8D%97%E7%9C%81%E7%9B%B4%E8%BE%96%E5%8E%BF%E7%BA%"
"A7%E8%A1%8C%"
"E6%94%BF%E5%8D%95%E4%BD%8D&output=json&ak=0l3FSP6qA0WbOzGRaafbmczS"));
ASSERT_EQ(msg->buffers[3].to_string(), std::string("application/json;charset=utf8"));
}

TEST_F(http_message_parser_test, parse_bad_request) { parse_bad_request(); }
Expand All @@ -247,7 +249,7 @@ TEST_F(http_message_parser_test, parse_long_url)
ASSERT_EQ(msg->hdr_format, NET_HDR_HTTP);
ASSERT_EQ(msg->header->hdr_type, http_method::HTTP_METHOD_GET);
ASSERT_EQ(msg->header->context.u.is_request, 1);
ASSERT_EQ(msg->buffers.size(), 3);
ASSERT_EQ(msg->buffers.size(), 4);
ASSERT_EQ(msg->buffers[2].size(), 4097); // url
}

Expand Down