update:http&&md5
This commit is contained in:
@@ -198,7 +198,7 @@ namespace ntq
|
||||
impl_->cache.clear();
|
||||
}
|
||||
|
||||
std::optional<HttpResponse> NetRequest::Get(const std::string &path,
|
||||
ntq::optional<HttpResponse> NetRequest::Get(const std::string &path,
|
||||
const httplib::Params &query,
|
||||
const httplib::Headers &headers,
|
||||
ErrorCode *err)
|
||||
@@ -221,7 +221,7 @@ namespace ntq
|
||||
}
|
||||
}
|
||||
|
||||
std::optional<HttpResponse> result;
|
||||
ntq::optional<HttpResponse> result;
|
||||
ErrorCode local_err = ErrorCode::None;
|
||||
|
||||
const auto full_path = impl_->build_full_path(path);
|
||||
@@ -280,7 +280,7 @@ namespace ntq
|
||||
{
|
||||
impl_->stats.total_errors++;
|
||||
if (err) *err = local_err;
|
||||
return std::nullopt;
|
||||
return ntq::nullopt;
|
||||
}
|
||||
|
||||
if (impl_->cache_enabled)
|
||||
@@ -293,7 +293,7 @@ namespace ntq
|
||||
return result;
|
||||
}
|
||||
|
||||
std::optional<HttpResponse> NetRequest::PostJson(const std::string &path,
|
||||
ntq::optional<HttpResponse> NetRequest::PostJson(const std::string &path,
|
||||
const std::string &json,
|
||||
const httplib::Headers &headers,
|
||||
ErrorCode *err)
|
||||
@@ -302,7 +302,7 @@ namespace ntq
|
||||
impl_->stats.total_requests++;
|
||||
auto start = std::chrono::steady_clock::now();
|
||||
|
||||
std::optional<HttpResponse> result;
|
||||
ntq::optional<HttpResponse> result;
|
||||
ErrorCode local_err = ErrorCode::None;
|
||||
|
||||
const auto full_path = impl_->build_full_path(path);
|
||||
@@ -349,13 +349,13 @@ namespace ntq
|
||||
{
|
||||
impl_->stats.total_errors++;
|
||||
if (err) *err = local_err;
|
||||
return std::nullopt;
|
||||
return ntq::nullopt;
|
||||
}
|
||||
if (err) *err = ErrorCode::None;
|
||||
return result;
|
||||
}
|
||||
|
||||
std::optional<HttpResponse> NetRequest::PostForm(const std::string &path,
|
||||
ntq::optional<HttpResponse> NetRequest::PostForm(const std::string &path,
|
||||
const httplib::Params &form,
|
||||
const httplib::Headers &headers,
|
||||
ErrorCode *err)
|
||||
@@ -364,7 +364,7 @@ namespace ntq
|
||||
impl_->stats.total_requests++;
|
||||
auto start = std::chrono::steady_clock::now();
|
||||
|
||||
std::optional<HttpResponse> result;
|
||||
ntq::optional<HttpResponse> result;
|
||||
ErrorCode local_err = ErrorCode::None;
|
||||
|
||||
const auto full_path = impl_->build_full_path(path);
|
||||
@@ -411,13 +411,13 @@ namespace ntq
|
||||
{
|
||||
impl_->stats.total_errors++;
|
||||
if (err) *err = local_err;
|
||||
return std::nullopt;
|
||||
return ntq::nullopt;
|
||||
}
|
||||
if (err) *err = ErrorCode::None;
|
||||
return result;
|
||||
}
|
||||
|
||||
std::future<std::optional<HttpResponse>> NetRequest::GetAsync(const std::string &path,
|
||||
std::future<ntq::optional<HttpResponse>> NetRequest::GetAsync(const std::string &path,
|
||||
const httplib::Params &query,
|
||||
const httplib::Headers &headers,
|
||||
ErrorCode *err)
|
||||
@@ -430,7 +430,7 @@ namespace ntq
|
||||
});
|
||||
}
|
||||
|
||||
std::future<std::optional<HttpResponse>> NetRequest::PostJsonAsync(const std::string &path,
|
||||
std::future<ntq::optional<HttpResponse>> NetRequest::PostJsonAsync(const std::string &path,
|
||||
const std::string &json,
|
||||
const httplib::Headers &headers,
|
||||
ErrorCode *err)
|
||||
@@ -443,7 +443,7 @@ namespace ntq
|
||||
});
|
||||
}
|
||||
|
||||
std::future<std::optional<HttpResponse>> NetRequest::PostFormAsync(const std::string &path,
|
||||
std::future<ntq::optional<HttpResponse>> NetRequest::PostFormAsync(const std::string &path,
|
||||
const httplib::Params &form,
|
||||
const httplib::Headers &headers,
|
||||
ErrorCode *err)
|
||||
@@ -560,4 +560,76 @@ namespace ntq
|
||||
{
|
||||
return impl_->stats;
|
||||
}
|
||||
|
||||
// ------------------------- Quick helpers -------------------------
|
||||
namespace {
|
||||
struct ParsedURL {
|
||||
std::string scheme;
|
||||
std::string host;
|
||||
int port = 0;
|
||||
std::string path_and_query;
|
||||
bool ok = false;
|
||||
};
|
||||
|
||||
static ParsedURL parse_url(const std::string &url)
|
||||
{
|
||||
ParsedURL p; p.ok = false;
|
||||
// very small parser: scheme://host[:port]/path[?query]
|
||||
auto pos_scheme = url.find("://");
|
||||
if (pos_scheme == std::string::npos) return p;
|
||||
p.scheme = url.substr(0, pos_scheme);
|
||||
size_t pos_host = pos_scheme + 3;
|
||||
|
||||
size_t pos_path = url.find('/', pos_host);
|
||||
std::string hostport = pos_path == std::string::npos ? url.substr(pos_host)
|
||||
: url.substr(pos_host, pos_path - pos_host);
|
||||
auto pos_colon = hostport.find(':');
|
||||
if (pos_colon == std::string::npos) {
|
||||
p.host = hostport;
|
||||
p.port = (p.scheme == "https") ? 443 : 80;
|
||||
} else {
|
||||
p.host = hostport.substr(0, pos_colon);
|
||||
std::string port_str = hostport.substr(pos_colon + 1);
|
||||
p.port = port_str.empty() ? ((p.scheme == "https") ? 443 : 80) : std::atoi(port_str.c_str());
|
||||
}
|
||||
p.path_and_query = (pos_path == std::string::npos) ? "/" : url.substr(pos_path);
|
||||
p.ok = !p.host.empty();
|
||||
return p;
|
||||
}
|
||||
}
|
||||
|
||||
ntq::optional<HttpResponse> NetRequest::QuickGet(const std::string &url,
|
||||
const httplib::Headers &headers,
|
||||
ErrorCode *err)
|
||||
{
|
||||
auto p = parse_url(url);
|
||||
if (!p.ok) { if (err) *err = ErrorCode::InvalidURL; return std::nullopt; }
|
||||
RequestOptions opt; opt.scheme = p.scheme; opt.host = p.host; opt.port = p.port;
|
||||
NetRequest req(opt);
|
||||
return req.Get(p.path_and_query, {}, headers, err);
|
||||
}
|
||||
|
||||
ntq::optional<HttpResponse> NetRequest::QuickPostJson(const std::string &url,
|
||||
const std::string &json,
|
||||
const httplib::Headers &headers,
|
||||
ErrorCode *err)
|
||||
{
|
||||
auto p = parse_url(url);
|
||||
if (!p.ok) { if (err) *err = ErrorCode::InvalidURL; return std::nullopt; }
|
||||
RequestOptions opt; opt.scheme = p.scheme; opt.host = p.host; opt.port = p.port;
|
||||
NetRequest req(opt);
|
||||
return req.PostJson(p.path_and_query, json, headers, err);
|
||||
}
|
||||
|
||||
ntq::optional<HttpResponse> NetRequest::QuickPostForm(const std::string &url,
|
||||
const httplib::Params &form,
|
||||
const httplib::Headers &headers,
|
||||
ErrorCode *err)
|
||||
{
|
||||
auto p = parse_url(url);
|
||||
if (!p.ok) { if (err) *err = ErrorCode::InvalidURL; return std::nullopt; }
|
||||
RequestOptions opt; opt.scheme = p.scheme; opt.host = p.host; opt.port = p.port;
|
||||
NetRequest req(opt);
|
||||
return req.PostForm(p.path_and_query, form, headers, err);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user