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]fix content_view #668

Merged
merged 2 commits into from
Apr 25, 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
8 changes: 5 additions & 3 deletions include/ylt/standalone/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/ylt/standalone/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/ylt/standalone/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: 25 additions & 0 deletions src/coro_http/examples/example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,22 @@ async_simple::coro::Lazy<void> basic_usage() {
response.set_status_and_content(status_type::ok, "ok");
});

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.set_default_handler(
[](coro_http_request &req,
coro_http_response &resp) -> async_simple::coro::Lazy<void> {
resp.set_status_and_content(status_type::ok, "default response");
co_return;
});

person_t person{};
server.set_http_handler<GET>("/person", &person_t::foo, person);

Expand All @@ -377,6 +393,11 @@ async_simple::coro::Lazy<void> basic_usage() {
assert(result.status == 200);
assert(result.resp_body == "post string");

result = co_await client.async_post("/view", "post string",
req_content_type::string);
assert(result.status == 200);
assert(result.resp_body == "post string");

client.add_header("name", "tom");
client.add_header("age", "20");
result = co_await client.async_get("/headers");
Expand All @@ -389,6 +410,10 @@ async_simple::coro::Lazy<void> basic_usage() {
"http://127.0.0.1:9001/users/ultramarines/subscriptions/guilliman");
assert(result.status == 200);

result = co_await client.async_get("/not_exist");
assert(result.status == 200);
assert(result.resp_body == "default response");

// make sure you have install openssl and enable CINATRA_ENABLE_SSL
#ifdef CINATRA_ENABLE_SSL
coro_http_client client2{};
Expand Down
Loading