Skip to content

Commit

Permalink
fix content view (#568)
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos authored Apr 25, 2024
1 parent 42d4844 commit f501671
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 18 deletions.
8 changes: 5 additions & 3 deletions include/cinatra/coro_http_connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ class coro_http_connection
}
else {
if (default_handler_) {
default_handler_(request_, response_);
co_await default_handler_(request_, response_);
}
else {
bool is_exist = false;
Expand Down Expand Up @@ -409,7 +409,8 @@ class coro_http_connection
void set_multi_buf(bool r) { multi_buf_ = r; }

void set_default_handler(
std::function<void(coro_http_request &, coro_http_response &)> &handler) {
std::function<async_simple::coro::Lazy<void>(
coro_http_request &, coro_http_response &)> &handler) {
default_handler_ = handler;
}

Expand Down Expand Up @@ -904,7 +905,8 @@ class coro_http_connection
#endif
bool need_shrink_every_time_ = false;
bool multi_buf_ = true;
std::function<void(coro_http_request &, coro_http_response &)>
std::function<async_simple::coro::Lazy<void>(coro_http_request &,
coro_http_response &)>
default_handler_ = nullptr;
std::string chunk_size_str_;
std::string remote_addr_;
Expand Down
14 changes: 6 additions & 8 deletions include/cinatra/coro_http_response.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,21 +173,19 @@ class coro_http_response {
: resp_str.append(CONN_CLOSE_SV);
}

if (content_view_.empty()) {
resp_str.append(content_);
}
else {
resp_str.append(content_view_);
}

append_header_str(resp_str, resp_headers_);

if (!resp_header_span_.empty()) {
append_header_str(resp_str, resp_header_span_);
}

resp_str.append(CRCF);
resp_str.append(content_);
if (content_view_.empty()) {
resp_str.append(content_);
}
else {
resp_str.append(content_view_);
}
}

void append_header_str(auto &resp_str, auto &resp_headers) {
Expand Down
8 changes: 5 additions & 3 deletions include/cinatra/coro_http_server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -550,8 +550,9 @@ class coro_http_server {

void set_shrink_to_fit(bool r) { need_shrink_every_time_ = r; }

void set_default_handler(
std::function<void(coro_http_request &, coro_http_response &)> handler) {
void set_default_handler(std::function<async_simple::coro::Lazy<void>(
coro_http_request &, coro_http_response &)>
handler) {
default_handler_ = std::move(handler);
}

Expand Down Expand Up @@ -923,7 +924,8 @@ class coro_http_server {
#endif
coro_http_router router_;
bool need_shrink_every_time_ = false;
std::function<void(coro_http_request &, coro_http_response &)>
std::function<async_simple::coro::Lazy<void>(coro_http_request &,
coro_http_response &)>
default_handler_ = nullptr;
};

Expand Down
25 changes: 21 additions & 4 deletions tests/test_cinatra.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,10 +399,22 @@ async_simple::coro::Lazy<void> test_collect_all() {

TEST_CASE("test default http handler") {
coro_http_server server(1, 9001);
server.set_default_handler([](coro_http_request &req,
coro_http_response &resp) {
resp.set_status_and_content(status_type::ok, "It is from default handler");
});
server.set_default_handler(
[](coro_http_request &req,
coro_http_response &resp) -> async_simple::coro::Lazy<void> {
resp.set_status_and_content(status_type::ok,
"It is from default handler");
co_return;
});
server.set_http_handler<POST>(
"/view",
[](coro_http_request &req,
coro_http_response &resp) -> async_simple::coro::Lazy<void> {
resp.set_delay(true);
resp.set_status_and_content_view(status_type::ok,
req.get_body()); // no copy
co_await resp.get_conn()->reply();
});
server.async_start();

for (int i = 0; i < 5; i++) {
Expand All @@ -414,6 +426,10 @@ TEST_CASE("test default http handler") {
CHECK(data.resp_body == "It is from default handler");
data = client.get("/any");
CHECK(data.resp_body == "It is from default handler");
data = async_simple::coro::syncAwait(
client.async_post("/view", "post string", req_content_type::string));
CHECK(data.status == 200);
CHECK(data.resp_body == "post string");
}
}

Expand Down Expand Up @@ -562,6 +578,7 @@ TEST_CASE("test head put and some other request") {
std::string result = ec ? "delete failed" : "delete ok";
resp.set_status_and_content(status_type::ok, result);
});

server.async_start();
std::this_thread::sleep_for(300ms);

Expand Down

0 comments on commit f501671

Please sign in to comment.