8 std::cerr <<
"error: " << error.message() << std::endl;
13 std::cerr <<
"invalid http request: " << message << std::endl;
18 static const boost::regex reg(
"Content-Length:\\s[-0-9]+",
20 boost::match_results<std::string::const_iterator> results;
22 if (boost::regex_search(response, results, reg)) {
23 if (results.size() > 0) {
24 std::string key = results[0];
25 key.erase(std::remove(key.begin(),
29 std::string hay =
": ";
30 std::size_t i = key.find(hay);
31 if (i != std::string::npos) {
32 length = boost::lexical_cast<std::size_t>(
33 key.substr(i+hay.size(), std::string::npos));
36 std::cerr <<
"malformed `Content-Lengtht` delimiter" << std::endl;
44 static const boost::regex reg(
"Content-Length:\\s[-0-9]+", boost::regex::icase);
45 boost::match_results<std::string::const_iterator> results;
47 if (boost::regex_search(response, results, reg)) {
48 return (results.size() > 0 ?
true :
false);
58 std::size_t i = response.find(
"\r\n\r\n");
59 if (i != std::string::npos) {
60 return response.substr(i+4, std::string::npos);
63 throw std::runtime_error(
"no double return after header");
69 std::string chars(
"abcdefghijklmnopqrstuvwxyz"
70 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
72 boost::random::random_device rng;
75 boost::random::uniform_int_distribution<> index_dist(0, chars.size() - 1);
76 for (
int i = 0; i < 16; ++i){
77 uid.push_back(chars[index_dist(rng)]);
84 std::ostringstream ss;
85 for (
auto iter = str.cbegin(); iter != str.cend(); iter++) {
87 case '\\': ss <<
"\\\\";
break;
88 case '"': ss <<
"\\\"";
break;
89 case '/': ss <<
"\\/";
break;
90 case '\b': ss <<
"\\b";
break;
91 case '\f': ss <<
"\\f";
break;
92 case '\n': ss <<
"\\n";
break;
93 case '\r': ss <<
"\\r";
break;
94 case '\t': ss <<
"\\t";
break;
95 default: ss << *iter;
break;
103 using namespace boost::archive::iterators;
104 using It = transform_width<binary_from_base64<std::string::const_iterator>, 8, 6>;
105 return boost::algorithm::trim_right_copy_if(std::string(It(std::begin(val)),
107 [](
char c) {
return c ==
'\0';});
112 using namespace boost::archive::iterators;
113 using It = base64_from_binary<transform_width<std::string::const_iterator, 6, 8>>;
114 auto tmp = std::string(It(std::begin(val)), It(std::end(val)));
115 return tmp.append((3 - val.size() % 3) % 3,
'=');
void invalid_request(const std::string message)
Handle Invalid Query - e.g.: response which states our query was invalid.
std::string random_boundary() const
Create a random boundary for the multipart/form in HTTP.
std::string escape_string(const std::string &str)
escape JSON strings when sending them over the socket
std::string decode64(const std::string &val)
decode base64
std::string encode64(const std::string &val)
encode base64
void content_length(std::string response, std::size_t &length)
get the content leangth from
bool has_content_length(std::string response)
examine if the header response contains a content-length filed
std::string strip_header(std::string response)
remove/strip the HTTP header and
void error_handler(const boost::system::error_code &error)
Handle an Error.