forked from hiimgoodpack/brainlet-client
85 lines
2.7 KiB
C++
85 lines
2.7 KiB
C++
// Copyright (c) 2021 hiimgoodpack
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
// this software and associated documentation files (the "Software"), to deal in
|
|
// the Software without restriction, including without limitation the rights to
|
|
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
|
// of the Software, and to permit persons to whom the Software is furnished to do
|
|
// so, subject to the following conditions:
|
|
// The above copyright notice and this permission notice shall be included in all
|
|
// copies or substantial portions of the Software.
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
// SOFTWARE.
|
|
|
|
#ifndef HTTP_H
|
|
#define HTTP_H
|
|
|
|
#include <string>
|
|
#include <curl/curl.h>
|
|
#include <stdexcept>
|
|
#include <cstdint>
|
|
|
|
namespace Http {
|
|
class Error : public std::runtime_error {
|
|
public:
|
|
using runtime_error::runtime_error;
|
|
};
|
|
|
|
class Request {
|
|
private:
|
|
CURL* handle;
|
|
std::string url;
|
|
std::string postFields;
|
|
|
|
static std::size_t write(void* buffer, std::size_t blocks, std::size_t blockSize, std::string* result);
|
|
|
|
public:
|
|
std::string buffer;
|
|
long int status;
|
|
|
|
Request(const std::string& url);
|
|
void setPostFields(std::string fields);
|
|
void perform();
|
|
~Request();
|
|
};
|
|
}
|
|
|
|
#endif
|
|
|
|
#if defined(HTTP_IMPLEMENTATION) && !defined(HTTP_IMPLEMENTED)
|
|
#define HTTP_IMPLEMENTED
|
|
|
|
namespace Http {
|
|
Request::Request(const std::string& newUrl) : url(newUrl) {
|
|
handle = curl_easy_init();
|
|
|
|
curl_easy_setopt(handle, CURLOPT_URL, url.c_str());
|
|
curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &status);
|
|
|
|
curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write);
|
|
curl_easy_setopt(handle, CURLOPT_WRITEDATA, &buffer);
|
|
}
|
|
std::size_t Request::write(void* buffer, std::size_t blocks, std::size_t blockSize, std::string* result) {
|
|
const std::size_t size = blocks * blockSize;
|
|
result->append((char*)buffer, size);
|
|
return size;
|
|
}
|
|
void Request::setPostFields(std::string newFields) {
|
|
postFields = newFields;
|
|
curl_easy_setopt(handle, CURLOPT_POSTFIELDS, postFields.c_str());
|
|
curl_easy_setopt(handle, CURLOPT_POSTFIELDSIZE, postFields.length()+1);
|
|
}
|
|
void Request::perform() {
|
|
CURLcode code = curl_easy_perform(handle);
|
|
if (code != CURLE_OK)
|
|
throw Error(curl_easy_strerror(code));
|
|
}
|
|
Request::~Request() {
|
|
curl_easy_cleanup(handle);
|
|
}
|
|
}
|
|
#endif
|