mirror of
https://github.com/Syping/dtranslatebot.git
synced 2026-04-21 14:40:41 +02:00
http_request: throw libcurl errors with curl_exception
This commit is contained in:
parent
8de0473669
commit
3bee78275b
1 changed files with 10 additions and 2 deletions
|
|
@ -16,6 +16,8 @@
|
||||||
* responsible for anything with use of the software, you are self responsible.
|
* responsible for anything with use of the software, you are self responsible.
|
||||||
*****************************************************************************/
|
*****************************************************************************/
|
||||||
|
|
||||||
|
#include <cstring>
|
||||||
|
#include "curl_exception.h"
|
||||||
#include "http_request.h"
|
#include "http_request.h"
|
||||||
using namespace bot::http;
|
using namespace bot::http;
|
||||||
|
|
||||||
|
|
@ -27,30 +29,36 @@ http_request::http_request() {
|
||||||
|
|
||||||
const http_response http_request::get(const std::string &url, const http_headers &headers) {
|
const http_response http_request::get(const std::string &url, const http_headers &headers) {
|
||||||
http_response response;
|
http_response response;
|
||||||
|
char error[CURL_ERROR_SIZE]{};
|
||||||
curl_easy_setopt(instance, CURLOPT_URL, url.c_str());
|
curl_easy_setopt(instance, CURLOPT_URL, url.c_str());
|
||||||
curl_easy_setopt(instance, CURLOPT_HTTPHEADER, headers.data());
|
curl_easy_setopt(instance, CURLOPT_HTTPHEADER, headers.data());
|
||||||
curl_easy_setopt(instance, CURLOPT_WRITEDATA, &response.content);
|
curl_easy_setopt(instance, CURLOPT_WRITEDATA, &response.content);
|
||||||
curl_easy_setopt(instance, CURLOPT_WRITEFUNCTION, &writer);
|
curl_easy_setopt(instance, CURLOPT_WRITEFUNCTION, &writer);
|
||||||
|
curl_easy_setopt(instance, CURLOPT_ERRORBUFFER, error);
|
||||||
CURLcode result = curl_easy_perform(instance);
|
CURLcode result = curl_easy_perform(instance);
|
||||||
if (result == CURLE_OK)
|
if (result == CURLE_OK)
|
||||||
curl_easy_getinfo(instance, CURLINFO_RESPONSE_CODE, &response.status);
|
curl_easy_getinfo(instance, CURLINFO_RESPONSE_CODE, &response.status);
|
||||||
curl_easy_reset(instance);
|
curl_easy_reset(instance);
|
||||||
return response;
|
return result == CURLE_OK ?
|
||||||
|
response : throw bot::exception::curl_exception(strlen(error) ? error : curl_easy_strerror(result), result);
|
||||||
}
|
}
|
||||||
|
|
||||||
const http_response http_request::post(const std::string &url, const std::string &content, const http_headers &headers) {
|
const http_response http_request::post(const std::string &url, const std::string &content, const http_headers &headers) {
|
||||||
http_response response;
|
http_response response;
|
||||||
|
char error[CURL_ERROR_SIZE]{};
|
||||||
curl_easy_setopt(instance, CURLOPT_URL, url.c_str());
|
curl_easy_setopt(instance, CURLOPT_URL, url.c_str());
|
||||||
curl_easy_setopt(instance, CURLOPT_HTTPHEADER, headers.data());
|
curl_easy_setopt(instance, CURLOPT_HTTPHEADER, headers.data());
|
||||||
curl_easy_setopt(instance, CURLOPT_POSTFIELDS, content.data());
|
curl_easy_setopt(instance, CURLOPT_POSTFIELDS, content.data());
|
||||||
curl_easy_setopt(instance, CURLOPT_POSTFIELDSIZE, content.size());
|
curl_easy_setopt(instance, CURLOPT_POSTFIELDSIZE, content.size());
|
||||||
curl_easy_setopt(instance, CURLOPT_WRITEDATA, &response.content);
|
curl_easy_setopt(instance, CURLOPT_WRITEDATA, &response.content);
|
||||||
curl_easy_setopt(instance, CURLOPT_WRITEFUNCTION, &writer);
|
curl_easy_setopt(instance, CURLOPT_WRITEFUNCTION, &writer);
|
||||||
|
curl_easy_setopt(instance, CURLOPT_ERRORBUFFER, error);
|
||||||
CURLcode result = curl_easy_perform(instance);
|
CURLcode result = curl_easy_perform(instance);
|
||||||
if (result == CURLE_OK)
|
if (result == CURLE_OK)
|
||||||
curl_easy_getinfo(instance, CURLINFO_RESPONSE_CODE, &response.status);
|
curl_easy_getinfo(instance, CURLINFO_RESPONSE_CODE, &response.status);
|
||||||
curl_easy_reset(instance);
|
curl_easy_reset(instance);
|
||||||
return response;
|
return result == CURLE_OK ?
|
||||||
|
response : throw bot::exception::curl_exception(strlen(error) ? error : curl_easy_strerror(result), result);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string http_request::legacy_url(const std::string &hostname, uint16_t port, const std::string &url, bool tls) {
|
std::string http_request::legacy_url(const std::string &hostname, uint16_t port, const std::string &url, bool tls) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue