2019-02-10 21:46:39 -05:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <winhttp.h>
|
2020-12-14 06:26:01 -07:00
|
|
|
#include <variant>
|
2019-02-10 21:46:39 -05:00
|
|
|
|
|
|
|
using InternetHandle = AutoHandle<Functor<WinHttpCloseHandle>>;
|
|
|
|
|
2019-06-13 04:01:29 -04:00
|
|
|
struct HttpRequest
|
2019-02-10 21:46:39 -05:00
|
|
|
{
|
2019-06-13 04:01:29 -04:00
|
|
|
HttpRequest(
|
|
|
|
const wchar_t* agentName,
|
|
|
|
const wchar_t* serverName,
|
|
|
|
const wchar_t* action,
|
|
|
|
const wchar_t* objectName,
|
2020-03-29 20:55:12 -06:00
|
|
|
std::string body = "",
|
|
|
|
const wchar_t* headers = NULL,
|
2021-01-15 06:07:23 -07:00
|
|
|
DWORD port = INTERNET_DEFAULT_PORT,
|
2020-03-29 20:55:12 -06:00
|
|
|
const wchar_t* referrer = NULL,
|
2019-06-13 04:01:29 -04:00
|
|
|
DWORD requestFlags = WINHTTP_FLAG_SECURE | WINHTTP_FLAG_ESCAPE_DISABLE,
|
|
|
|
const wchar_t* httpVersion = NULL,
|
2021-01-15 06:07:23 -07:00
|
|
|
const wchar_t** acceptTypes = NULL
|
2019-06-13 04:01:29 -04:00
|
|
|
);
|
|
|
|
operator bool() { return errorCode == ERROR_SUCCESS; }
|
|
|
|
|
|
|
|
std::wstring response;
|
2020-03-26 05:41:21 -06:00
|
|
|
std::wstring headers;
|
2019-06-13 04:01:29 -04:00
|
|
|
InternetHandle connection = NULL;
|
|
|
|
InternetHandle request = NULL;
|
|
|
|
DWORD errorCode = ERROR_SUCCESS;
|
2019-06-05 20:26:50 -04:00
|
|
|
};
|
2019-06-13 04:01:29 -04:00
|
|
|
|
|
|
|
std::wstring Escape(const std::wstring& text);
|
2020-12-14 06:26:01 -07:00
|
|
|
std::string Escape(const std::string& text);
|
2020-03-29 20:55:12 -06:00
|
|
|
|
2021-03-09 21:32:56 -07:00
|
|
|
namespace HTML
|
|
|
|
{
|
|
|
|
template <typename C>
|
|
|
|
std::basic_string<C> Unescape(std::basic_string<C> text)
|
|
|
|
{
|
|
|
|
constexpr C
|
|
|
|
lt[] = { '&', 'l', 't', ';' },
|
|
|
|
gt[] = { '&', 'g', 't', ';' },
|
|
|
|
apos1[] = { '&', 'a', 'p', 'o', 's', ';' },
|
|
|
|
apos2[] = { '&', '#', '3', '9', ';' },
|
|
|
|
apos3[] = { '&', '#', 'x', '2', '7', ';' },
|
|
|
|
apos4[] = { '&', '#', 'X', '2', '7', ';' },
|
|
|
|
quot[] = { '&', 'q', 'u', 'o', 't', ';' },
|
|
|
|
amp[] = { '&', 'a', 'm', 'p', ';' };
|
|
|
|
for (int i = 0; i < text.size(); ++i)
|
|
|
|
if (text[i] == '&')
|
|
|
|
for (auto [original, length, replacement] : Array<const C*, size_t, C>{
|
|
|
|
{ lt, std::size(lt), '<' },
|
|
|
|
{ gt, std::size(gt), '>' },
|
|
|
|
{ apos1, std::size(apos1), '\'' },
|
|
|
|
{ apos2, std::size(apos2), '\'' },
|
|
|
|
{ apos3, std::size(apos3), '\'' },
|
|
|
|
{ apos4, std::size(apos4), '\'' },
|
|
|
|
{ quot, std::size(quot), '"' },
|
|
|
|
{ amp, std::size(amp), '&' }
|
|
|
|
}) if (std::char_traits<C>::compare(text.data() + i, original, length) == 0) text.replace(i, length, 1, replacement);
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-29 20:55:12 -06:00
|
|
|
namespace JSON
|
|
|
|
{
|
2020-12-14 19:10:47 -07:00
|
|
|
template <typename C>
|
|
|
|
std::basic_string<C> Escape(std::basic_string<C> text)
|
2020-12-14 06:26:01 -07:00
|
|
|
{
|
2020-12-14 19:10:47 -07:00
|
|
|
int oldSize = text.size();
|
2021-01-15 06:07:23 -07:00
|
|
|
text.resize(text.size() + std::count_if(text.begin(), text.end(), [](C ch) { return ch == '\n' || ch == '\r' || ch == '\t' || ch == '\\' || ch == '"'; }));
|
2020-12-14 19:10:47 -07:00
|
|
|
auto out = text.rbegin();
|
|
|
|
for (int i = oldSize - 1; i >= 0; --i)
|
|
|
|
{
|
|
|
|
if (text[i] == '\n') *out++ = 'n';
|
|
|
|
else if (text[i] == '\t') *out++ = 't';
|
|
|
|
else if (text[i] == '\r') *out++ = 'r';
|
|
|
|
else if (text[i] == '\\' || text[i] == '"') *out++ = text[i];
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*out++ = text[i];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
*out++ = '\\';
|
|
|
|
}
|
|
|
|
text.erase(std::remove_if(text.begin(), text.end(), [](uint64_t ch) { return ch < 0x20 || ch == 0x7f; }), text.end());
|
|
|
|
return text;
|
2020-12-14 06:26:01 -07:00
|
|
|
}
|
|
|
|
|
2020-12-14 19:10:47 -07:00
|
|
|
template <typename C> struct UTF {};
|
|
|
|
template <> struct UTF<wchar_t>
|
|
|
|
{
|
2021-01-15 06:07:23 -07:00
|
|
|
inline static std::wstring FromCodepoint(unsigned codepoint) { return { (wchar_t)codepoint }; } // TODO: surrogate pairs
|
2020-12-14 19:10:47 -07:00
|
|
|
};
|
|
|
|
|
2020-12-14 06:26:01 -07:00
|
|
|
template <typename C>
|
2021-01-15 06:07:23 -07:00
|
|
|
struct Value : private std::variant<std::monostate, std::nullptr_t, bool, double, std::basic_string<C>, std::vector<Value<C>>, std::unordered_map<std::basic_string<C>, Value<C>>>
|
2020-12-14 06:26:01 -07:00
|
|
|
{
|
2021-01-15 06:07:23 -07:00
|
|
|
using std::variant<std::monostate, std::nullptr_t, bool, double, std::basic_string<C>, std::vector<Value<C>>, std::unordered_map<std::basic_string<C>, Value<C>>>::variant;
|
2020-12-14 06:26:01 -07:00
|
|
|
|
|
|
|
explicit operator bool() const { return index(); }
|
|
|
|
bool IsNull() const { return index() == 1; }
|
|
|
|
auto Boolean() const { return std::get_if<bool>(this); }
|
2020-12-15 07:28:12 -07:00
|
|
|
auto Number() const { return std::get_if<double>(this); }
|
2020-12-14 06:26:01 -07:00
|
|
|
auto String() const { return std::get_if<std::basic_string<C>>(this); }
|
|
|
|
auto Array() const { return std::get_if<std::vector<Value<C>>>(this); }
|
|
|
|
auto Object() const { return std::get_if<std::unordered_map<std::basic_string<C>, Value<C>>>(this); }
|
|
|
|
|
|
|
|
const Value<C>& operator[](std::basic_string<C> key) const
|
|
|
|
{
|
|
|
|
if (auto object = Object()) if (auto it = object->find(key); it != object->end()) return it->second;
|
|
|
|
return failure;
|
|
|
|
}
|
|
|
|
const Value<C>& operator[](int i) const
|
|
|
|
{
|
|
|
|
if (auto array = Array()) if (i < array->size()) return array->at(i);
|
|
|
|
return failure;
|
|
|
|
}
|
2021-01-15 06:07:23 -07:00
|
|
|
|
|
|
|
static const Value<C> failure;
|
2020-12-14 06:26:01 -07:00
|
|
|
};
|
2021-01-15 06:07:23 -07:00
|
|
|
template <typename C> const Value<C> Value<C>::failure;
|
2020-12-14 06:26:01 -07:00
|
|
|
|
2020-12-14 19:10:47 -07:00
|
|
|
template <typename C, int maxDepth = 25>
|
2020-12-15 07:28:12 -07:00
|
|
|
Value<C> Parse(const std::basic_string<C>& text, int64_t& i, int depth)
|
2020-12-14 06:26:01 -07:00
|
|
|
{
|
2020-12-14 19:10:47 -07:00
|
|
|
if (depth > maxDepth) return {};
|
2020-12-14 06:26:01 -07:00
|
|
|
C ch;
|
|
|
|
auto SkipWhitespace = [&]
|
|
|
|
{
|
|
|
|
while (i < text.size() && (text[i] == ' ' || text[i] == '\n' || text[i] == '\r' || text[i] == '\t')) ++i;
|
|
|
|
if (i >= text.size()) return true;
|
|
|
|
ch = text[i];
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
auto ExtractString = [&]
|
|
|
|
{
|
2020-12-15 07:28:12 -07:00
|
|
|
std::basic_string<C> unescaped;
|
2020-12-14 06:26:01 -07:00
|
|
|
i += 1;
|
2020-12-15 07:28:12 -07:00
|
|
|
for (; i < text.size(); ++i)
|
|
|
|
{
|
|
|
|
auto ch = text[i];
|
|
|
|
if (ch == '"') return i += 1, unescaped;
|
|
|
|
if (ch == '\\')
|
|
|
|
{
|
|
|
|
ch = text[i + 1];
|
|
|
|
if (ch == 'u' && isxdigit(text[i + 2]) && isxdigit(text[i + 3]) && isxdigit(text[i + 4]) && isxdigit(text[i + 5]))
|
|
|
|
{
|
2021-02-21 14:15:59 -07:00
|
|
|
char charCode[] = { (char)text[i + 2], (char)text[i + 3], (char)text[i + 4], (char)text[i + 5], 0 };
|
2021-01-15 06:07:23 -07:00
|
|
|
unescaped += UTF<C>::FromCodepoint(strtoul(charCode, nullptr, 16));
|
2020-12-15 07:28:12 -07:00
|
|
|
i += 5;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (auto [original, value] : Array<char, char>{ { 'b', '\b' }, {'f', '\f'}, {'n', '\n'}, {'r', '\r'}, {'t', '\t'} }) if (ch == original)
|
|
|
|
{
|
|
|
|
unescaped.push_back(value);
|
|
|
|
goto replaced;
|
|
|
|
}
|
|
|
|
unescaped.push_back(ch);
|
|
|
|
replaced: i += 1;
|
|
|
|
}
|
|
|
|
else unescaped.push_back(ch);
|
|
|
|
}
|
|
|
|
return unescaped;
|
2020-12-14 06:26:01 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
if (SkipWhitespace()) return {};
|
|
|
|
|
2021-03-09 21:32:56 -07:00
|
|
|
constexpr C nullStr[] = { 'n', 'u', 'l', 'l' }, trueStr[] = { 't', 'r', 'u', 'e' }, falseStr[] = { 'f', 'a', 'l', 's', 'e' };
|
2020-12-14 06:26:01 -07:00
|
|
|
if (ch == nullStr[0])
|
2021-01-15 06:07:23 -07:00
|
|
|
if (std::char_traits<C>::compare(text.data() + i, nullStr, std::size(nullStr)) == 0) return i += std::size(nullStr), nullptr;
|
2020-12-14 06:26:01 -07:00
|
|
|
else return {};
|
|
|
|
if (ch == trueStr[0])
|
|
|
|
if (std::char_traits<C>::compare(text.data() + i, trueStr, std::size(trueStr)) == 0) return i += std::size(trueStr), true;
|
|
|
|
else return {};
|
|
|
|
if (ch == falseStr[0])
|
|
|
|
if (std::char_traits<C>::compare(text.data() + i, falseStr, std::size(falseStr)) == 0) return i += std::size(falseStr), false;
|
|
|
|
else return {};
|
|
|
|
|
|
|
|
if (ch == '-' || (ch >= '0' && ch <= '9'))
|
|
|
|
{
|
2020-12-15 07:28:12 -07:00
|
|
|
std::string number;
|
|
|
|
for (; i < text.size() && ((text[i] >= '0' && text[i] <= '9') || text[i] == '-' || text[i] == '+' || text[i] == 'e' || text[i] == 'E' || text[i] == '.'); ++i)
|
|
|
|
number.push_back(text[i]);
|
|
|
|
return strtod(number.c_str(), NULL);
|
2020-12-14 06:26:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ch == '"') return ExtractString();
|
|
|
|
|
|
|
|
if (ch == '[')
|
|
|
|
{
|
|
|
|
std::vector<Value<C>> array;
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
i += 1;
|
|
|
|
if (SkipWhitespace()) return {};
|
|
|
|
if (ch == ']') return i += 1, Value<C>(array);
|
2020-12-14 19:10:47 -07:00
|
|
|
if (!array.emplace_back(Parse<C, maxDepth>(text, i, depth + 1))) return {};
|
2020-12-14 06:26:01 -07:00
|
|
|
if (SkipWhitespace()) return {};
|
|
|
|
if (ch == ']') return i += 1, Value<C>(array);
|
|
|
|
if (ch != ',') return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ch == '{')
|
|
|
|
{
|
|
|
|
std::unordered_map<std::basic_string<C>, Value<C>> object;
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
i += 1;
|
|
|
|
if (SkipWhitespace()) return {};
|
|
|
|
if (ch == '}') return i += 1, Value<C>(object);
|
|
|
|
if (ch != '"') return {};
|
|
|
|
auto key = ExtractString();
|
|
|
|
if (SkipWhitespace() || ch != ':') return {};
|
|
|
|
i += 1;
|
2020-12-14 19:10:47 -07:00
|
|
|
if (!(object[std::move(key)] = Parse<C, maxDepth>(text, i, depth + 1))) return {};
|
2020-12-14 06:26:01 -07:00
|
|
|
if (SkipWhitespace()) return {};
|
|
|
|
if (ch == '}') return i += 1, Value<C>(object);
|
|
|
|
if (ch != ',') return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename C>
|
|
|
|
Value<C> Parse(const std::basic_string<C>& text)
|
|
|
|
{
|
|
|
|
int64_t start = 0;
|
2020-12-15 07:28:12 -07:00
|
|
|
return Parse(text, start, 0);
|
2020-12-14 06:26:01 -07:00
|
|
|
}
|
2020-03-29 20:55:12 -06:00
|
|
|
}
|