1
0

init http framework

This commit is contained in:
2023-04-25 02:59:59 +00:00
parent f4e7319a44
commit 7b9fc827ca
12 changed files with 1693 additions and 39 deletions

111
src/http.cpp Normal file
View File

@@ -0,0 +1,111 @@
#include "http.h"
#include <sstream>
#ifndef CURL_PROGRESSFUNC_CONTINUE
#define CURL_PROGRESSFUNC_CONTINUE 0x10000001
#endif
class curl_error : public std::exception {
public:
explicit curl_error(CURLcode c) : code(c) {}
const char* what() const noexcept override { return curl_easy_strerror(this->code); }
private:
CURLcode code;
};
/// @brief curl context
HTTP::HTTP() : chunk(nullptr) {
this->easy = curl_easy_init();
curl_easy_setopt(this->easy, CURLOPT_USERAGENT, "curl/" LIBCURL_VERSION);
curl_easy_setopt(this->easy, CURLOPT_FOLLOWLOCATION, 1L);
// enable all supported built-in compressions
curl_easy_setopt(this->easy, CURLOPT_ACCEPT_ENCODING, "");
curl_easy_setopt(this->easy, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(this->easy, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(this->easy, CURLOPT_COOKIEJAR, "");
}
HTTP::~HTTP() {
if (this->chunk != nullptr) curl_slist_free_all(this->chunk);
if (this->easy != nullptr) curl_easy_cleanup(this->easy);
}
void HTTP::set_headers(const std::vector<std::string>& headers) {
if (this->chunk != nullptr) {
curl_slist_free_all(this->chunk);
this->chunk = nullptr;
}
for (auto& h : headers) {
this->chunk = curl_slist_append(this->chunk, h.c_str());
}
curl_easy_setopt(this->easy, CURLOPT_HTTPHEADER, this->chunk);
}
size_t HTTP::easy_write_cb(char* ptr, size_t size, size_t nmemb, void* userdata) {
std::ostream* ctx = reinterpret_cast<std::ostream*>(userdata);
size_t count = size * nmemb;
ctx->write(ptr, static_cast<std::streamsize>(count));
return count;
}
size_t HTTP::easy_read_cb(char* ptr, size_t size, size_t nmemb, void* userdata) {
std::istream* ctx = reinterpret_cast<std::istream*>(userdata);
size_t count = size * nmemb;
ctx->read(ptr, static_cast<std::streamsize>(count));
return ctx->gcount();
}
int HTTP::perform(std::ostream* body) {
curl_easy_setopt(this->easy, CURLOPT_WRITEFUNCTION, easy_write_cb);
curl_easy_setopt(this->easy, CURLOPT_WRITEDATA, body);
CURLcode res = curl_easy_perform(this->easy);
if (res != CURLE_OK) {
body->clear();
throw curl_error(res);
}
int status_code = 0;
curl_easy_getinfo(this->easy, CURLINFO_RESPONSE_CODE, &status_code);
return status_code;
}
std::string HTTP::encode_form(const Form& form) {
std::ostringstream ss;
char* escaped;
for (auto it = form.begin(); it != form.end(); ++it) {
if (it != form.begin()) ss << '&';
escaped = curl_escape(it->second.c_str(), it->second.size());
ss << it->first << '=' << escaped;
curl_free(escaped);
}
return ss.str();
}
int HTTP::get(const std::string& url, std::ostream* out) {
curl_easy_setopt(this->easy, CURLOPT_URL, url.c_str());
curl_easy_setopt(this->easy, CURLOPT_HTTPGET, 1L);
return this->perform(out);
}
std::string HTTP::put(const std::string& url, std::istream* data) {
std::ostringstream body;
curl_easy_setopt(this->easy, CURLOPT_URL, url.c_str());
curl_easy_setopt(this->easy, CURLOPT_UPLOAD, 1L);
curl_easy_setopt(this->easy, CURLOPT_READFUNCTION, easy_read_cb);
curl_easy_setopt(this->easy, CURLOPT_READDATA, data);
this->perform(&body);
return body.str();
}
std::string HTTP::post(const std::string& url, const std::string& data) {
std::ostringstream body;
curl_easy_setopt(this->easy, CURLOPT_URL, url.c_str());
curl_easy_setopt(this->easy, CURLOPT_POSTFIELDS, data.c_str());
curl_easy_setopt(this->easy, CURLOPT_POSTFIELDSIZE, data.size());
this->perform(&body);
return body.str();
}

25
src/main.cpp Normal file
View File

@@ -0,0 +1,25 @@
#include "http.h"
#include <qrcodegen/qrcodegen.h>
void printQr(const std::string& text) {
int border = 1;
std::vector<uint8_t> qrcode(qrcodegen_BUFFER_LEN_MAX), tmpbuf(qrcodegen_BUFFER_LEN_MAX);
qrcodegen_encodeText(text.c_str(), tmpbuf.data(), qrcode.data(), qrcodegen_Ecc_LOW, qrcodegen_VERSION_MIN,
qrcodegen_VERSION_MAX, qrcodegen_Mask_AUTO, true);
int width = qrcodegen_getSize(qrcode.data());
for (int y = -border; y < width + border; y++) {
for (int x = -border; x < width + border; x++) {
qrcodegen_getModule(qrcode.data(), x, y) ? printf("\033[40m \033[0m") : printf("\033[47m \033[0m");
}
printf("\n");
}
printf("\n");
}
int main(int argc, char* argv[]) {
curl_global_init(CURL_GLOBAL_ALL);
curl_global_cleanup();
return 0;
}

52
src/misc.cpp Normal file
View File

@@ -0,0 +1,52 @@
#include "drive.h"
#include <sstream>
#include <iomanip>
#ifdef __SWITCH__
#include <switch.h>
#elif defined(_WIN32)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#elif defined(__APPLE__)
#include <SystemConfiguration/SystemConfiguration.h>
#else
#include <unistd.h>
#endif
std::string hex_encode(const unsigned char* data, size_t len) {
std::stringstream ss;
for (size_t i = 0; i < len; i++) ss << std::hex << std::setw(2) << std::setfill('0') << (int)data[i];
return ss.str();
}
std::string device_name() {
#ifdef __SWITCH__
SetSysDeviceNickName nick;
if (R_SUCCEEDED(setsysGetDeviceNickname(&nick))) {
return nick.nickname;
}
#elif defined(_WIN32)
DWORD nSize = 128;
std::vector<WCHAR> buf(nSize);
if (GetComputerNameW(buf.data(), &nSize)) {
std::string name;
name.resize(nSize);
WideCharToMultiByte(CP_UTF8, 0, buf.data(), nSize, name.data(), name.size(), nullptr, nullptr);
return name;
}
#elif defined(__APPLE__)
CFStringRef nameRef = SCDynamicStoreCopyComputerName(nullptr, nullptr);
if (nameRef) {
std::vector<char> name(CFStringGetLength(nameRef) * 3);
CFStringGetCString(nameRef, name.data(), name.size(), kCFStringEncodingUTF8);
CFRelease(nameRef);
return name.data();
}
#else
std::vector<char> buf(128);
if (gethostname(buf.data(), buf.size()) == 0) {
return buf.data();
}
#endif
return "cDrive";
}