diff --git a/README.md b/README.md index d4c0b677..adc9ce73 100644 --- a/README.md +++ b/README.md @@ -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; } diff --git a/example/main.cpp b/example/main.cpp index 6af3de68..33d36fb8 100644 --- a/example/main.cpp +++ b/example/main.cpp @@ -301,16 +301,14 @@ async_simple::coro::Lazy basic_usage() { "/form_data", [](coro_http_request &req, coro_http_response &resp) -> async_simple::coro::Lazy { - 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; } diff --git a/include/cinatra/coro_http_client.hpp b/include/cinatra/coro_http_client.hpp index 2c3501e3..3b92efa4 100644 --- a/include/cinatra/coro_http_client.hpp +++ b/include/cinatra/coro_http_client.hpp @@ -1640,7 +1640,7 @@ class coro_http_client : public std::enable_shared_from_this { 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; } diff --git a/include/cinatra/coro_http_request.hpp b/include/cinatra/coro_http_request.hpp index ea6574fd..a41df18e 100644 --- a/include/cinatra/coro_http_request.hpp +++ b/include/cinatra/coro_http_request.hpp @@ -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_; } @@ -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 aspect_data_; std::string cached_session_id_; }; diff --git a/include/cinatra/multipart.hpp b/include/cinatra/multipart.hpp index d40aaa71..b85b5001 100644 --- a/include/cinatra/multipart.hpp +++ b/include/cinatra/multipart.hpp @@ -11,7 +11,12 @@ class multipart_reader_t { head_buf_(conn_->head_buf_), chunked_buf_(conn_->chunked_buf_) {} - async_simple::coro::Lazy read_part_head() { + async_simple::coro::Lazy 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(head_buf_.data()); chunked_buf_.sputn(data_ptr, head_buf_.size()); diff --git a/lang/coroutine_based_http_lib.md b/lang/coroutine_based_http_lib.md index cfda7801..8487d0a1 100644 --- a/lang/coroutine_based_http_lib.md +++ b/lang/coroutine_based_http_lib.md @@ -528,7 +528,7 @@ async_simple::coro::Lazy 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; } diff --git a/lang/english/README.md b/lang/english/README.md index 82cf4579..c7e9ae49 100644 --- a/lang/english/README.md +++ b/lang/english/README.md @@ -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; } diff --git a/tests/test_cinatra.cpp b/tests/test_cinatra.cpp index a66b8fa3..d1a3139c 100644 --- a/tests/test_cinatra.cpp +++ b/tests/test_cinatra.cpp @@ -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; } @@ -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; }