11 #include <boost/xpressive/xpressive.hpp> 12 #include <boost/archive/iterators/binary_from_base64.hpp> 13 #include <boost/archive/iterators/base64_from_binary.hpp> 14 #include <boost/archive/iterators/transform_width.hpp> 15 #include <boost/algorithm/string.hpp> 17 #include "xxhrtypes.hpp" 23 inline Header parseHeader(
const std::string& headers);
24 inline size_t writeFunction(
void* ptr,
size_t size,
size_t nmemb, std::string* data);
25 inline std::vector<std::string> split(
const std::string& to_split,
char delimiter);
30 inline std::string urlEncode(
const std::string& response);
31 inline std::string decode64(
const std::string &val);
32 inline std::string encode64(
const std::string &val);
52 std::string parameters;
59 return protocol ==
"https";
64 inline url_parts parse_url(
const std::string &url);
77 inline Header parseHeader(
const std::string& headers) {
79 std::vector<std::string> lines;
80 std::istringstream stream(headers);
83 while (std::getline(stream, line,
'\n')) {
84 lines.push_back(line);
88 for (
auto& line : lines) {
89 if (line.substr(0, 5) ==
"HTTP/") {
93 if (line.length() > 0) {
94 auto found = line.find(
":");
95 if (found != std::string::npos) {
96 auto value = line.substr(found + 2, line.length() - 1);
97 if (value.back() ==
'\r') {
98 value = value.substr(0, value.length() - 1);
100 header[line.substr(0, found)] = value;
108 std::vector<std::string> split(
const std::string& to_split,
char delimiter) {
109 std::vector<std::string> tokens;
111 std::stringstream stream(to_split);
113 while (std::getline(stream, item, delimiter)) {
114 tokens.push_back(item);
120 inline size_t writeFunction(
void* ptr,
size_t size,
size_t nmemb, std::string* data) {
121 data->append((
char*) ptr, size * nmemb);
125 inline std::string urlEncode(
const std::string& value) {
126 std::ostringstream escaped;
130 for (
auto i = value.cbegin(), n = value.cend(); i != n; ++i) {
131 std::string::value_type c = (*i);
133 if (std::isalnum(c) || c ==
'-' || c ==
'_' || c ==
'.' || c ==
'~') {
138 escaped <<
'%' << std::setw(2) << std::int32_t((
unsigned char) c);
141 return escaped.str();
144 inline std::string decode64(
const std::string &val) {
145 using namespace boost::archive::iterators;
146 using It = transform_width<binary_from_base64<std::string::const_iterator>, 8, 6>;
147 return boost::algorithm::trim_right_copy_if(std::string(It(std::begin(val)), It(std::end(val))), [](
char c) {
152 inline std::string encode64(
const std::string &val) {
153 using namespace boost::archive::iterators;
154 using It = base64_from_binary<transform_width<std::string::const_iterator, 6, 8>>;
155 auto tmp = std::string(It(std::begin(val)), It(std::end(val)));
156 return tmp.append((3 - val.size() % 3) % 3,
'=');
160 const boost::xpressive::sregex url_regex =
161 boost::xpressive::sregex::compile(
"(http|https)://([^/ :]+):?([^/ ]*)(/?[^ #?]*)\\x3f?([^ #]*)#?([^ ]*)");
163 inline url_parts parse_url(
const std::string &url) {
170 using namespace boost::xpressive;
172 if( regex_match( url, what, url_regex ) ) {
174 ret.protocol = what[1];
178 if (ret.port.empty()) {
179 ret.port = (ret.protocol ==
"https") ?
"443" :
"80";
183 ret.parameters = what[5];
184 ret.fragment = what[6];
main library namespace
Definition: api.hpp:19
bool https() const
Wether the url requires TLS.
Definition: util.hpp:58
std::map< std::string, std::string, CaseInsensitiveCompare > Header
HTTP Headers to add to the request or received in xxhr::Response.
Definition: xxhrtypes.hpp:23
Parts of a parsed url.
Definition: util.hpp:37