RAPP Platform API
 All Classes Namespaces Files Functions Variables Typedefs
asio_handler.cpp
Go to the documentation of this file.
1 #include "asio_handler.hpp"
2 namespace rapp {
3 namespace cloud {
4 
5 
6 void asio_handler::error_handler(const boost::system::error_code & error)
7 {
8  std::cerr << "error: " << error.message() << std::endl;
9 }
10 
11 void asio_handler::invalid_request(const std::string message)
12 {
13  std::cerr << "invalid http request: " << message << std::endl;
14 }
15 
16 void asio_handler::content_length(std::string response, std::size_t & length)
17 {
18  static const boost::regex reg("Content-Length:\\s[-0-9]+",
19  boost::regex::icase);
20  boost::match_results<std::string::const_iterator> results;
21  // search for matching regex
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(),
26  key.end(), '\n'),
27  key.end());
28  // find the `: `
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));
34  }
35  else {
36  std::cerr << "malformed `Content-Lengtht` delimiter" << std::endl;
37  }
38  }
39  }
40 }
41 
42 bool asio_handler::has_content_length(std::string response)
43 {
44  static const boost::regex reg("Content-Length:\\s[-0-9]+", boost::regex::icase);
45  boost::match_results<std::string::const_iterator> results;
46  // search for matching regex
47  if (boost::regex_search(response, results, reg)) {
48  return (results.size() > 0 ? true : false);
49  }
50  else {
51  return false;
52  }
53 }
54 
55 std::string asio_handler::strip_header(std::string response)
56 {
57  // find the "\r\n\r\n" double return after the header
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);
61  }
62  else {
63  throw std::runtime_error("no double return after header");
64  }
65 }
66 
67 std::string asio_handler::random_boundary() const
68 {
69  std::string chars("abcdefghijklmnopqrstuvwxyz"
70  "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
71  "1234567890");
72  boost::random::random_device rng;
73  std::string uid;
74  // Randomly chose 16 characters
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)]);
78  }
79  return uid;
80 }
81 
82 std::string asio_handler::escape_string(const std::string & str)
83 {
84  std::ostringstream ss;
85  for (auto iter = str.cbegin(); iter != str.cend(); iter++) {
86  switch (*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;
96  }
97  }
98  return ss.str();
99 }
100 
101 std::string asio_handler::decode64(const std::string &val)
102 {
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)),
106  It(std::end(val))),
107  [](char c) {return c == '\0';});
108 }
109 
110 std::string asio_handler::encode64(const std::string &val)
111 {
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, '=');
116 }
117 
118 }
119 }
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.
Definition: asio_handler.cpp:6