Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[coro_http_server]improve #585

Merged
merged 2 commits into from
May 21, 2024
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ void start_server() {
auto boundary = req.get_boundary();
multipart_reader_t multipart(req.get_conn());
while (true) {
auto part_head = co_await multipart.read_part_head();
auto part_head = co_await multipart.read_part_head(boundary);
if (part_head.ec) {
co_return;
}
Expand Down
10 changes: 4 additions & 6 deletions example/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,16 +301,14 @@ async_simple::coro::Lazy<void> basic_usage() {
"/form_data",
[](coro_http_request &req,
coro_http_response &resp) -> async_simple::coro::Lazy<void> {
if (req.get_content_type() != content_type::multipart) {
resp.set_status_and_content(status_type::bad_request,
"bad request, not multipart request");
co_return;
}
assert(req.get_content_type() == content_type::multipart);
auto boundary = req.get_boundary();
multipart_reader_t multipart(req.get_conn());
while (true) {
auto part_head = co_await multipart.read_part_head();
auto part_head = co_await multipart.read_part_head(boundary);
if (part_head.ec) {
resp.set_status_and_content(status_type::bad_request,
"bad_request");
co_return;
}

Expand Down
2 changes: 1 addition & 1 deletion include/cinatra/coro_http_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1640,7 +1640,7 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
std::string boundary = std::string{parser_.get_boundary()};
multipart_reader_t multipart(this);
while (true) {
auto part_head = co_await multipart.read_part_head();
auto part_head = co_await multipart.read_part_head(boundary);
if (part_head.ec) {
co_return part_head.ec;
}
Expand Down
10 changes: 8 additions & 2 deletions include/cinatra/coro_http_request.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,13 @@ class coro_http_request {
if (content_type.empty()) {
return {};
}
return content_type.substr(content_type.rfind("=") + 1);

auto pos = content_type.rfind("=");
if (pos == std::string_view::npos) {
return "";
}

return content_type.substr(pos + 1);
}

coro_http_connection *get_conn() { return conn_; }
Expand Down Expand Up @@ -278,7 +284,7 @@ class coro_http_request {
http_parser &parser_;
std::string_view body_;
coro_http_connection *conn_;
bool is_websocket_;
bool is_websocket_ = false;
std::vector<std::string> aspect_data_;
std::string cached_session_id_;
};
Expand Down
7 changes: 6 additions & 1 deletion include/cinatra/multipart.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ class multipart_reader_t {
head_buf_(conn_->head_buf_),
chunked_buf_(conn_->chunked_buf_) {}

async_simple::coro::Lazy<part_head_t> read_part_head() {
async_simple::coro::Lazy<part_head_t> read_part_head(
std::string_view boundary) {
if (boundary.empty()) {
co_return part_head_t{std::make_error_code(std::errc::protocol_error)};
}

if (head_buf_.size() > 0) {
const char *data_ptr = asio::buffer_cast<const char *>(head_buf_.data());
chunked_buf_.sputn(data_ptr, head_buf_.size());
Expand Down
2 changes: 1 addition & 1 deletion lang/coroutine_based_http_lib.md
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ async_simple::coro::Lazy<void> byte_ranges_download() {
auto boundary = req.get_boundary();
multipart_reader_t multipart(req.get_conn());
while (true) {
auto part_head = co_await multipart.read_part_head();
auto part_head = co_await multipart.read_part_head(boundary);
if (part_head.ec) {
co_return;
}
Expand Down
2 changes: 1 addition & 1 deletion lang/english/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ void start_server() {
auto boundary = req.get_boundary();
multipart_reader_t multipart(req.get_conn());
while (true) {
auto part_head = co_await multipart.read_part_head();
auto part_head = co_await multipart.read_part_head(boundary);
if (part_head.ec) {
co_return;
}
Expand Down
4 changes: 2 additions & 2 deletions tests/test_cinatra.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ TEST_CASE("test upload file") {
auto boundary = req.get_boundary();
multipart_reader_t multipart(req.get_conn());
while (true) {
auto part_head = co_await multipart.read_part_head();
auto part_head = co_await multipart.read_part_head(boundary);
if (part_head.ec) {
co_return;
}
Expand Down Expand Up @@ -897,7 +897,7 @@ TEST_CASE("test coro_http_client multipart upload") {
auto boundary = req.get_boundary();
multipart_reader_t multipart(req.get_conn());
while (true) {
auto part_head = co_await multipart.read_part_head();
auto part_head = co_await multipart.read_part_head(boundary);
if (part_head.ec) {
co_return;
}
Expand Down
Loading