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

add some methods #489

Merged
merged 1 commit into from
Jan 15, 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
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
Loading