Skip to content

Instantly share code, notes, and snippets.

@HookedBehemoth
Last active May 10, 2020 23:58
Show Gist options
  • Save HookedBehemoth/ed3c7d3259c5cde92c865c70a30f430a to your computer and use it in GitHub Desktop.
Save HookedBehemoth/ed3c7d3259c5cde92c865c70a30f430a to your computer and use it in GitHub Desktop.
#include "json.hpp"
#include <cstdio>
#include <curl/curl.h>
#include <fstream>
#include <iostream>
using json = nlohmann::json;
size_t StringWrite(const char *contents, size_t size, size_t nmemb, std::string *userp) {
userp->append(contents, size * nmemb);
return size * nmemb;
}
std::string fix(const json &j, const std::string &response) {
std::string result;
/* Return full reponse if no scheme was found. */
if (!j.contains("URL"))
return response;
/* Get URL scheme. */
std::string url_scheme = j["URL"];
/* Reserve at least size of scheme. */
result.reserve(url_scheme.size());
/* Get pointer to scheme data for easy traversal. */
const char *ptr = url_scheme.c_str();
/* Read while string goes on. */
while (*ptr) {
/* Append non-scheme characters. */
while (*ptr != '$') {
result += *ptr++;
}
if (memcmp(ptr, "$json:", 6) == 0) { /* Parse json. */
ptr += 6;
/* Parse response as json data. */
json current = json::parse(response);
/* Read while scheme is going. */
while (*ptr != '$') {
std::string key;
if (isalpha(*ptr)) {
/* Append object key character. */
while (isalpha(*ptr)) {
key += *ptr++;
}
current = current[key];
} else if (*ptr == '[') {
ptr++;
/* Append array index character. */
while (isdigit(*ptr)) {
key += *ptr++;
}
/* Array index termination. */
if (*ptr++ == ']') {
current = current[std::strtoul(key.c_str(), nullptr, 10)];
} else {
std::cout << ptr;
throw std::runtime_error("Unexpected token for array index termination");
}
} else if (*ptr == '.') {
ptr++;
} else {
throw std::runtime_error("Unexpected token in scheme");
}
}
result += current;
ptr++;
} else { /* Unsupported or unknown scheme type. */
ptr++;
while (*ptr++ != '$')
;
result += "UNSUPPORTED";
}
}
return result;
}
int main(int argc, char *argv[]) {
try {
std::fstream f("cust/telegram.sxcu");
json j;
f >> j;
std::string url = j["RequestURL"];
std::string name = j["FileFormName"];
bool first = true;
for (auto &[k, v] : j["Parameters"].items()) {
url += first ? "?" : "&";
url += k + "=";
url += v;
}
std::cout << "URL: " << url << std::endl;
/* Init curl. */
CURL *curl = curl_easy_init();
if (curl == nullptr)
throw std::runtime_error("curl init");
/* Init mime. */
curl_mime *mime = curl_mime_init(curl);
/* Make file part. */
curl_mimepart *file_part = curl_mime_addpart(mime);
curl_mime_name(file_part, name.c_str());
curl_mime_filedata(file_part, "libtesla_1586986830.jpg");
/* Append body arguments. */
for (auto &[k, v] : j["Arguments"].items()) {
/* Append header entry. String will be duped. */
auto *arg = curl_mime_addpart(mime);
curl_mime_name(arg, k.c_str());
std::string str_value = v;
curl_mime_data(arg, str_value.c_str(), CURL_ZERO_TERMINATED);
std::cout << "Appending header string: " << str_value.c_str() << std::endl;
}
/* Make custom http header. */
curl_slist *chunk = NULL;
for (auto &[k, v] : j["Headers"].items()) {
/* Append header entry. String will be duped. */
auto auth_str = std::string(k + ": " + v.get<std::string>());
chunk = curl_slist_append(chunk, auth_str.c_str());
std::cout << "Appending header string: " << auth_str << std::endl;
}
std::string urlresponse;
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, StringWrite);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &urlresponse);
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime);
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
CURLcode res = curl_easy_perform(curl);
long http_code = 0;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
std::cout << "res: " << res << " http " << http_code << std::endl;
std::cout << fix(j, urlresponse) << std::endl;
curl_mime_free(mime);
curl_easy_cleanup(curl);
} catch (std::exception &e) {
std::printf("What: %s\n", e.what());
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment