mirror of
https://github.com/mkxp-z/mkxp-z.git
synced 2025-08-06 06:55:40 +02:00
Raise Ruby exceptions when creation of HTTP client fails
This commit is contained in:
parent
f9c8e0e900
commit
f5d7f79cde
1 changed files with 44 additions and 17 deletions
|
@ -115,24 +115,33 @@ StringMap &HTTPRequest::headers() {
|
||||||
HTTPResponse HTTPRequest::get() {
|
HTTPResponse HTTPRequest::get() {
|
||||||
HTTPResponse ret;
|
HTTPResponse ret;
|
||||||
auto target = readURL(destination.c_str());
|
auto target = readURL(destination.c_str());
|
||||||
httplib::Client client(getHost(target).c_str());
|
|
||||||
|
httplib::Client *client = nullptr;
|
||||||
|
try {
|
||||||
|
client = new httplib::Client(getHost(target).c_str());
|
||||||
|
}
|
||||||
|
catch (std::exception &e) {
|
||||||
|
delete client;
|
||||||
|
throw Exception(Exception::MKXPError, "Failed to create HTTP client (%s)", e.what());
|
||||||
|
}
|
||||||
|
|
||||||
httplib::Headers head;
|
httplib::Headers head;
|
||||||
|
|
||||||
// Seems to need to be disabled for now, at least on macOS
|
// Seems to need to be disabled for now, at least on macOS
|
||||||
#ifdef MKXPZ_SSL
|
#ifdef MKXPZ_SSL
|
||||||
client.enable_server_certificate_verification(false);
|
client->enable_server_certificate_verification(false);
|
||||||
#endif
|
#endif
|
||||||
client.set_follow_location(follow_location);
|
client->set_follow_location(follow_location);
|
||||||
|
|
||||||
for (auto const h : _headers)
|
for (auto const &h : _headers)
|
||||||
head.emplace(h.first, h.second);
|
head.emplace(h.first, h.second);
|
||||||
|
|
||||||
if (auto result = client.Get(getPath(target).c_str(), head)) {
|
if (auto result = client->Get(getPath(target).c_str(), head)) {
|
||||||
auto response = result.value();
|
auto response = result.value();
|
||||||
ret._status = response.status;
|
ret._status = response.status;
|
||||||
ret._body = response.body;
|
ret._body = response.body;
|
||||||
|
|
||||||
for (auto const h : response.headers)
|
for (auto const &h : response.headers)
|
||||||
ret._headers.emplace(h.first, h.second);
|
ret._headers.emplace(h.first, h.second);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -147,23 +156,32 @@ HTTPResponse HTTPRequest::get() {
|
||||||
HTTPResponse HTTPRequest::post(StringMap &postData) {
|
HTTPResponse HTTPRequest::post(StringMap &postData) {
|
||||||
HTTPResponse ret;
|
HTTPResponse ret;
|
||||||
auto target = readURL(destination.c_str());
|
auto target = readURL(destination.c_str());
|
||||||
httplib::Client client(getHost(target).c_str());
|
|
||||||
|
httplib::Client *client = nullptr;
|
||||||
|
try {
|
||||||
|
client = new httplib::Client(getHost(target).c_str());
|
||||||
|
}
|
||||||
|
catch (std::exception &e) {
|
||||||
|
delete client;
|
||||||
|
throw Exception(Exception::MKXPError, "Failed to create HTTP client (%s)", e.what());
|
||||||
|
}
|
||||||
|
|
||||||
httplib::Headers head;
|
httplib::Headers head;
|
||||||
httplib::Params params;
|
httplib::Params params;
|
||||||
|
|
||||||
// Seems to need to be disabled for now, at least on macOS
|
// Seems to need to be disabled for now, at least on macOS
|
||||||
#ifdef MKXPZ_SSL
|
#ifdef MKXPZ_SSL
|
||||||
client.enable_server_certificate_verification(false);
|
client->enable_server_certificate_verification(false);
|
||||||
#endif
|
#endif
|
||||||
client.set_follow_location(follow_location);
|
client->set_follow_location(follow_location);
|
||||||
|
|
||||||
for (auto const h : _headers)
|
for (auto const &h : _headers)
|
||||||
head.emplace(h.first, h.second);
|
head.emplace(h.first, h.second);
|
||||||
|
|
||||||
for (auto const &p : postData)
|
for (auto const &p : postData)
|
||||||
params.emplace(p.first, p.second);
|
params.emplace(p.first, p.second);
|
||||||
|
|
||||||
if (auto result = client.Post(getPath(target).c_str(), head, params)) {
|
if (auto result = client->Post(getPath(target).c_str(), head, params)) {
|
||||||
auto response = result.value();
|
auto response = result.value();
|
||||||
ret._status = response.status;
|
ret._status = response.status;
|
||||||
ret._body = response.body;
|
ret._body = response.body;
|
||||||
|
@ -182,24 +200,33 @@ HTTPResponse HTTPRequest::post(StringMap &postData) {
|
||||||
HTTPResponse HTTPRequest::post(const char *body, const char *content_type) {
|
HTTPResponse HTTPRequest::post(const char *body, const char *content_type) {
|
||||||
HTTPResponse ret;
|
HTTPResponse ret;
|
||||||
auto target = readURL(destination.c_str());
|
auto target = readURL(destination.c_str());
|
||||||
httplib::Client client(getHost(target).c_str());
|
|
||||||
|
httplib::Client *client = nullptr;
|
||||||
|
try {
|
||||||
|
client = new httplib::Client(getHost(target).c_str());
|
||||||
|
}
|
||||||
|
catch (std::exception &e) {
|
||||||
|
delete client;
|
||||||
|
throw Exception(Exception::MKXPError, "Failed to create HTTP client (%s)", e.what());
|
||||||
|
}
|
||||||
|
|
||||||
httplib::Headers head;
|
httplib::Headers head;
|
||||||
|
|
||||||
// Seems to need to be disabled for now, at least on macOS
|
// Seems to need to be disabled for now, at least on macOS
|
||||||
#ifdef MKXPZ_SSL
|
#ifdef MKXPZ_SSL
|
||||||
client.enable_server_certificate_verification(false);
|
client->enable_server_certificate_verification(false);
|
||||||
#endif
|
#endif
|
||||||
client.set_follow_location(true);
|
client->set_follow_location(true);
|
||||||
|
|
||||||
for (auto const h : _headers)
|
for (auto const &h : _headers)
|
||||||
head.emplace(h.first, h.second);
|
head.emplace(h.first, h.second);
|
||||||
|
|
||||||
if (auto result = client.Post(getPath(target).c_str(), head, body, content_type)) {
|
if (auto result = client->Post(getPath(target).c_str(), head, body, content_type)) {
|
||||||
auto response = result.value();
|
auto response = result.value();
|
||||||
ret._status = response.status;
|
ret._status = response.status;
|
||||||
ret._body = response.body;
|
ret._body = response.body;
|
||||||
|
|
||||||
for (auto const h : response.headers)
|
for (auto const &h : response.headers)
|
||||||
ret._headers.emplace(h.first, h.second);
|
ret._headers.emplace(h.first, h.second);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
Loading…
Add table
Reference in a new issue