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][fix and improve]do some update; add a check for client #476

Merged
merged 1 commit into from
Oct 19, 2023
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
16 changes: 14 additions & 2 deletions include/ylt/coro_http/coro_http_server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ using coro_http_request = cinatra::coro_http_request;
using coro_http_response = cinatra::coro_http_response;
using status_type = cinatra::status_type;
using http_method = cinatra::http_method;
constexpr auto GET = cinatra::GET;
constexpr auto POST = cinatra::POST;
using uri_t = cinatra::uri_t;
using req_content_type = cinatra::req_content_type;

constexpr inline auto GET = cinatra::http_method::GET;
constexpr inline auto POST = cinatra::http_method::POST;
constexpr inline auto DEL = cinatra::http_method::DEL;
constexpr inline auto HEAD = cinatra::http_method::HEAD;
constexpr inline auto PUT = cinatra::http_method::PUT;
constexpr inline auto CONNECT = cinatra::http_method::CONNECT;
#ifdef TRACE
#undef TRACE
constexpr inline auto TRACE = cinatra::http_method::TRACE;
#endif
constexpr inline auto OPTIONS = cinatra::http_method::OPTIONS;
} // namespace coro_http
86 changes: 55 additions & 31 deletions include/ylt/thirdparty/cinatra/coro_http_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ class coro_http_client {
~coro_http_client() { async_close(); }

void async_close() {
if (socket_->has_closed_)
if (socket_ == nullptr || socket_->has_closed_)
return;

asio::dispatch(executor_wrapper_.get_asio_executor(), [socket = socket_] {
Expand Down Expand Up @@ -365,33 +365,45 @@ class coro_http_client {
void set_read_fix() { read_fix_ = 1; }
#endif

async_simple::coro::Lazy<resp_data> async_patch(std::string uri) {
async_simple::coro::Lazy<resp_data> async_patch(
std::string uri,
std::unordered_map<std::string, std::string> headers = {}) {
return async_request(std::move(uri), cinatra::http_method::PATCH,
cinatra::req_context<>{});
cinatra::req_context<>{}, std::move(headers));
}

async_simple::coro::Lazy<resp_data> async_options(std::string uri) {
async_simple::coro::Lazy<resp_data> async_options(
std::string uri,
std::unordered_map<std::string, std::string> headers = {}) {
return async_request(std::move(uri), cinatra::http_method::OPTIONS,
cinatra::req_context<>{});
cinatra::req_context<>{}, std::move(headers));
}

async_simple::coro::Lazy<resp_data> async_trace(std::string uri) {
async_simple::coro::Lazy<resp_data> async_trace(
std::string uri,
std::unordered_map<std::string, std::string> headers = {}) {
return async_request(std::move(uri), cinatra::http_method::TRACE,
cinatra::req_context<>{});
cinatra::req_context<>{}, std::move(headers));
}

async_simple::coro::Lazy<resp_data> async_head(std::string uri) {
async_simple::coro::Lazy<resp_data> async_head(
std::string uri,
std::unordered_map<std::string, std::string> headers = {}) {
return async_request(std::move(uri), cinatra::http_method::HEAD,
cinatra::req_context<>{});
cinatra::req_context<>{}, std::move(headers));
}

// CONNECT example.com HTTP/1.1
async_simple::coro::Lazy<resp_data> async_http_connect(std::string uri) {
async_simple::coro::Lazy<resp_data> async_http_connect(
std::string uri,
std::unordered_map<std::string, std::string> headers = {}) {
return async_request(std::move(uri), cinatra::http_method::CONNECT,
cinatra::req_context<>{});
cinatra::req_context<>{}, std::move(headers));
}

async_simple::coro::Lazy<resp_data> async_get(std::string uri) {
async_simple::coro::Lazy<resp_data> async_get(
std::string uri,
std::unordered_map<std::string, std::string> headers = {}) {
resp_data data{};
#ifdef BENCHMARK_TEST
if (!req_str_.empty()) {
Expand Down Expand Up @@ -462,7 +474,7 @@ class coro_http_client {

req_context<> ctx{};
data = co_await async_request(std::move(uri), http_method::GET,
std::move(ctx));
std::move(ctx), std::move(headers));
#ifdef BENCHMARK_TEST
data.total = total_len_;
#endif
Expand All @@ -477,33 +489,41 @@ class coro_http_client {
}
}

resp_data get(std::string uri) {
return async_simple::coro::syncAwait(async_get(std::move(uri)));
resp_data get(std::string uri,
std::unordered_map<std::string, std::string> headers = {}) {
return async_simple::coro::syncAwait(
async_get(std::move(uri), std::move(headers)));
}

resp_data post(std::string uri, std::string content,
req_content_type content_type) {
return async_simple::coro::syncAwait(
async_post(std::move(uri), std::move(content), content_type));
req_content_type content_type,
std::unordered_map<std::string, std::string> headers = {}) {
return async_simple::coro::syncAwait(async_post(
std::move(uri), std::move(content), content_type, std::move(headers)));
}

async_simple::coro::Lazy<resp_data> async_post(
std::string uri, std::string content, req_content_type content_type) {
std::string uri, std::string content, req_content_type content_type,
std::unordered_map<std::string, std::string> headers = {}) {
req_context<> ctx{content_type, "", std::move(content)};
return async_request(std::move(uri), http_method::POST, std::move(ctx));
return async_request(std::move(uri), http_method::POST, std::move(ctx),
std::move(headers));
}

async_simple::coro::Lazy<resp_data> async_delete(
std::string uri, std::string content, req_content_type content_type) {
std::string uri, std::string content, req_content_type content_type,
std::unordered_map<std::string, std::string> headers = {}) {
req_context<> ctx{content_type, "", std::move(content)};
return async_request(std::move(uri), http_method::DEL, std::move(ctx));
return async_request(std::move(uri), http_method::DEL, std::move(ctx),
std::move(headers));
}

async_simple::coro::Lazy<resp_data> async_put(std::string uri,
std::string content,
req_content_type content_type) {
async_simple::coro::Lazy<resp_data> async_put(
std::string uri, std::string content, req_content_type content_type,
std::unordered_map<std::string, std::string> headers = {}) {
req_context<> ctx{content_type, "", std::move(content)};
return async_request(std::move(uri), http_method::PUT, std::move(ctx));
return async_request(std::move(uri), http_method::PUT, std::move(ctx),
std::move(headers));
}

bool add_str_part(std::string name, std::string content) {
Expand Down Expand Up @@ -1135,22 +1155,26 @@ class coro_http_client {
req_str.append(ctx.req_str);

size_t content_len = ctx.content.size();
bool should_add = false;
bool should_add_len = false;
if (content_len > 0) {
should_add = true;
should_add_len = true;
}
else {
if ((method == http_method::POST || method == http_method::PUT) &&
ctx.content_type != req_content_type::multipart) {
should_add = true;
should_add_len = true;
}
}

if (req_headers_.find("Content-Length") != req_headers_.end()) {
should_add_len = false;
}

if (is_chunked) {
should_add = false;
should_add_len = false;
}

if (should_add) {
if (should_add_len) {
char buf[32];
auto [ptr, ec] = std::to_chars(buf, buf + 32, content_len);
req_str.append("Content-Length: ")
Expand Down
Loading