Skip to content

Commit

Permalink
add some methods (qicosmos#489)
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos authored Jan 15, 2024
1 parent ec7f8d6 commit 595a530
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
30 changes: 30 additions & 0 deletions include/cinatra/coro_http_connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,36 @@ class coro_http_connection
co_return true;
}

std::string local_address() {
if (has_closed_) {
return "";
}

std::stringstream ss;
std::error_code ec;
ss << socket_.local_endpoint(ec);
if (ec) {
return "";
}
return ss.str();
}

std::string remote_address() {
static std::string remote_addr;
if (has_closed_) {
return remote_addr;
}

std::stringstream ss;
std::error_code ec;
ss << socket_.remote_endpoint(ec);
if (ec) {
return remote_addr;
}
remote_addr = ss.str();
return ss.str();
}

async_simple::coro::Lazy<bool> write_data(std::string_view message) {
std::vector<asio::const_buffer> buffers;
buffers.push_back(asio::buffer(message));
Expand Down
4 changes: 4 additions & 0 deletions include/cinatra/coro_http_request.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ class coro_http_request {
return content_type::unknown;
}

std::string_view get_url() { return parser_.url(); }

std::string_view get_method() { return parser_.method(); }

std::string_view get_boundary() {
auto content_type = get_header_value("content-type");
if (content_type.empty()) {
Expand Down
5 changes: 5 additions & 0 deletions tests/test_coro_http_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,11 @@ TEST_CASE("get post") {

server.set_http_handler<cinatra::GET, cinatra::POST>(
"/test1", [](coro_http_request &req, coro_http_response &resp) {
CHECK(req.get_method() == "POST");
CHECK(req.get_url() == "/test1");
CHECK(req.get_conn()->local_address() == "127.0.0.1:9001");
CHECK(req.get_conn()->remote_address().find("127.0.0.1:") !=
std::string::npos);
resp.add_header("Host", "Cinatra");
resp.set_status_and_content(cinatra::status_type::ok, "hello world");
});
Expand Down

0 comments on commit 595a530

Please sign in to comment.