Skip to content

Commit f501671

Browse files
authored
fix content view (#568)
1 parent 42d4844 commit f501671

File tree

4 files changed

+37
-18
lines changed

4 files changed

+37
-18
lines changed

include/cinatra/coro_http_connection.hpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ class coro_http_connection
204204
}
205205
else {
206206
if (default_handler_) {
207-
default_handler_(request_, response_);
207+
co_await default_handler_(request_, response_);
208208
}
209209
else {
210210
bool is_exist = false;
@@ -409,7 +409,8 @@ class coro_http_connection
409409
void set_multi_buf(bool r) { multi_buf_ = r; }
410410

411411
void set_default_handler(
412-
std::function<void(coro_http_request &, coro_http_response &)> &handler) {
412+
std::function<async_simple::coro::Lazy<void>(
413+
coro_http_request &, coro_http_response &)> &handler) {
413414
default_handler_ = handler;
414415
}
415416

@@ -904,7 +905,8 @@ class coro_http_connection
904905
#endif
905906
bool need_shrink_every_time_ = false;
906907
bool multi_buf_ = true;
907-
std::function<void(coro_http_request &, coro_http_response &)>
908+
std::function<async_simple::coro::Lazy<void>(coro_http_request &,
909+
coro_http_response &)>
908910
default_handler_ = nullptr;
909911
std::string chunk_size_str_;
910912
std::string remote_addr_;

include/cinatra/coro_http_response.hpp

+6-8
Original file line numberDiff line numberDiff line change
@@ -173,21 +173,19 @@ class coro_http_response {
173173
: resp_str.append(CONN_CLOSE_SV);
174174
}
175175

176-
if (content_view_.empty()) {
177-
resp_str.append(content_);
178-
}
179-
else {
180-
resp_str.append(content_view_);
181-
}
182-
183176
append_header_str(resp_str, resp_headers_);
184177

185178
if (!resp_header_span_.empty()) {
186179
append_header_str(resp_str, resp_header_span_);
187180
}
188181

189182
resp_str.append(CRCF);
190-
resp_str.append(content_);
183+
if (content_view_.empty()) {
184+
resp_str.append(content_);
185+
}
186+
else {
187+
resp_str.append(content_view_);
188+
}
191189
}
192190

193191
void append_header_str(auto &resp_str, auto &resp_headers) {

include/cinatra/coro_http_server.hpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -550,8 +550,9 @@ class coro_http_server {
550550

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

553-
void set_default_handler(
554-
std::function<void(coro_http_request &, coro_http_response &)> handler) {
553+
void set_default_handler(std::function<async_simple::coro::Lazy<void>(
554+
coro_http_request &, coro_http_response &)>
555+
handler) {
555556
default_handler_ = std::move(handler);
556557
}
557558

@@ -923,7 +924,8 @@ class coro_http_server {
923924
#endif
924925
coro_http_router router_;
925926
bool need_shrink_every_time_ = false;
926-
std::function<void(coro_http_request &, coro_http_response &)>
927+
std::function<async_simple::coro::Lazy<void>(coro_http_request &,
928+
coro_http_response &)>
927929
default_handler_ = nullptr;
928930
};
929931

tests/test_cinatra.cpp

+21-4
Original file line numberDiff line numberDiff line change
@@ -399,10 +399,22 @@ async_simple::coro::Lazy<void> test_collect_all() {
399399

400400
TEST_CASE("test default http handler") {
401401
coro_http_server server(1, 9001);
402-
server.set_default_handler([](coro_http_request &req,
403-
coro_http_response &resp) {
404-
resp.set_status_and_content(status_type::ok, "It is from default handler");
405-
});
402+
server.set_default_handler(
403+
[](coro_http_request &req,
404+
coro_http_response &resp) -> async_simple::coro::Lazy<void> {
405+
resp.set_status_and_content(status_type::ok,
406+
"It is from default handler");
407+
co_return;
408+
});
409+
server.set_http_handler<POST>(
410+
"/view",
411+
[](coro_http_request &req,
412+
coro_http_response &resp) -> async_simple::coro::Lazy<void> {
413+
resp.set_delay(true);
414+
resp.set_status_and_content_view(status_type::ok,
415+
req.get_body()); // no copy
416+
co_await resp.get_conn()->reply();
417+
});
406418
server.async_start();
407419

408420
for (int i = 0; i < 5; i++) {
@@ -414,6 +426,10 @@ TEST_CASE("test default http handler") {
414426
CHECK(data.resp_body == "It is from default handler");
415427
data = client.get("/any");
416428
CHECK(data.resp_body == "It is from default handler");
429+
data = async_simple::coro::syncAwait(
430+
client.async_post("/view", "post string", req_content_type::string));
431+
CHECK(data.status == 200);
432+
CHECK(data.resp_body == "post string");
417433
}
418434
}
419435

@@ -562,6 +578,7 @@ TEST_CASE("test head put and some other request") {
562578
std::string result = ec ? "delete failed" : "delete ok";
563579
resp.set_status_and_content(status_type::ok, result);
564580
});
581+
565582
server.async_start();
566583
std::this_thread::sleep_for(300ms);
567584

0 commit comments

Comments
 (0)