Botan  2.19.1
Crypto and TLS for C++11
Classes | Typedefs | Functions
Botan::HTTP Namespace Reference

Classes

class  HTTP_Error
 
class  Response
 

Typedefs

typedef std::function< std::string(const std::string &, const std::string &, const std::string &)> http_exch_fn
 

Functions

Response GET_sync (const std::string &url, size_t allowable_redirects, std::chrono::milliseconds timeout)
 
Response http_sync (http_exch_fn http_transact, const std::string &verb, const std::string &url, const std::string &content_type, const std::vector< uint8_t > &body, size_t allowable_redirects)
 
Response http_sync (const std::string &verb, const std::string &url, const std::string &content_type, const std::vector< uint8_t > &body, size_t allowable_redirects, std::chrono::milliseconds timeout)
 
std::ostream & operator<< (std::ostream &o, const Response &resp)
 
Response POST_sync (const std::string &url, const std::string &content_type, const std::vector< uint8_t > &body, size_t allowable_redirects, std::chrono::milliseconds timeout)
 
std::string url_encode (const std::string &in)
 

Typedef Documentation

typedef std::function<std::string (const std::string&, const std::string&, const std::string&)> Botan::HTTP::http_exch_fn

Definition at line 75 of file http_util.h.

Function Documentation

Response Botan::HTTP::GET_sync ( const std::string &  url,
size_t  allowable_redirects,
std::chrono::milliseconds  timeout 
)

Definition at line 249 of file http_util.cpp.

References http_sync().

Referenced by http_sync().

252  {
253  return http_sync("GET", url, "", std::vector<uint8_t>(), allowable_redirects, timeout);
254  }
Response http_sync(const std::string &verb, const std::string &url, const std::string &content_type, const std::vector< uint8_t > &body, size_t allowable_redirects, std::chrono::milliseconds timeout)
Definition: http_util.cpp:227
Response Botan::HTTP::http_sync ( http_exch_fn  http_transact,
const std::string &  verb,
const std::string &  url,
const std::string &  content_type,
const std::vector< uint8_t > &  body,
size_t  allowable_redirects 
)

Definition at line 105 of file http_util.cpp.

References Botan::cast_uint8_ptr_to_char(), GET_sync(), Botan::search_map(), Botan::ASN1::to_string(), and Botan::to_u32bit().

Referenced by GET_sync(), http_sync(), and POST_sync().

111  {
112  if(url.empty())
113  throw HTTP_Error("URL empty");
114 
115  const auto protocol_host_sep = url.find("://");
116  if(protocol_host_sep == std::string::npos)
117  throw HTTP_Error("Invalid URL '" + url + "'");
118 
119  const auto host_loc_sep = url.find('/', protocol_host_sep + 3);
120 
121  std::string hostname, loc, service;
122 
123  if(host_loc_sep == std::string::npos)
124  {
125  hostname = url.substr(protocol_host_sep + 3, std::string::npos);
126  loc = "/";
127  }
128  else
129  {
130  hostname = url.substr(protocol_host_sep + 3, host_loc_sep-protocol_host_sep-3);
131  loc = url.substr(host_loc_sep, std::string::npos);
132  }
133 
134  const auto port_sep = hostname.find(":");
135  if(port_sep == std::string::npos)
136  {
137  service = "http";
138  // hostname not modified
139  }
140  else
141  {
142  service = hostname.substr(port_sep + 1, std::string::npos);
143  hostname = hostname.substr(0, port_sep);
144  }
145 
146  std::ostringstream outbuf;
147 
148  outbuf << verb << " " << loc << " HTTP/1.0\r\n";
149  outbuf << "Host: " << hostname << "\r\n";
150 
151  if(verb == "GET")
152  {
153  outbuf << "Accept: */*\r\n";
154  outbuf << "Cache-Control: no-cache\r\n";
155  }
156  else if(verb == "POST")
157  outbuf << "Content-Length: " << body.size() << "\r\n";
158 
159  if(!content_type.empty())
160  outbuf << "Content-Type: " << content_type << "\r\n";
161  outbuf << "Connection: close\r\n\r\n";
162  outbuf.write(cast_uint8_ptr_to_char(body.data()), body.size());
163 
164  std::istringstream io(http_transact(hostname, service, outbuf.str()));
165 
166  std::string line1;
167  std::getline(io, line1);
168  if(!io || line1.empty())
169  throw HTTP_Error("No response");
170 
171  std::stringstream response_stream(line1);
172  std::string http_version;
173  unsigned int status_code;
174  std::string status_message;
175 
176  response_stream >> http_version >> status_code;
177 
178  std::getline(response_stream, status_message);
179 
180  if(!response_stream || http_version.substr(0,5) != "HTTP/")
181  throw HTTP_Error("Not an HTTP response");
182 
183  std::map<std::string, std::string> headers;
184  std::string header_line;
185  while (std::getline(io, header_line) && header_line != "\r")
186  {
187  auto sep = header_line.find(": ");
188  if(sep == std::string::npos || sep > header_line.size() - 2)
189  throw HTTP_Error("Invalid HTTP header " + header_line);
190  const std::string key = header_line.substr(0, sep);
191 
192  if(sep + 2 < header_line.size() - 1)
193  {
194  const std::string val = header_line.substr(sep + 2, (header_line.size() - 1) - (sep + 2));
195  headers[key] = val;
196  }
197  }
198 
199  if(status_code == 301 && headers.count("Location"))
200  {
201  if(allowable_redirects == 0)
202  throw HTTP_Error("HTTP redirection count exceeded");
203  return GET_sync(headers["Location"], allowable_redirects - 1);
204  }
205 
206  std::vector<uint8_t> resp_body;
207  std::vector<uint8_t> buf(4096);
208  while(io.good())
209  {
210  io.read(cast_uint8_ptr_to_char(buf.data()), buf.size());
211  const size_t got = static_cast<size_t>(io.gcount());
212  resp_body.insert(resp_body.end(), buf.data(), &buf[got]);
213  }
214 
215  const std::string header_size = search_map(headers, std::string("Content-Length"));
216 
217  if(!header_size.empty())
218  {
219  if(resp_body.size() != to_u32bit(header_size))
220  throw HTTP_Error("Content-Length disagreement, header says " +
221  header_size + " got " + std::to_string(resp_body.size()));
222  }
223 
224  return Response(status_code, status_message, resp_body, headers);
225  }
V search_map(const std::map< K, V > &mapping, const K &key, const V &null_result=V())
Definition: stl_util.h:52
Response GET_sync(const std::string &url, size_t allowable_redirects, std::chrono::milliseconds timeout)
Definition: http_util.cpp:249
std::string to_string(const BER_Object &obj)
Definition: asn1_obj.cpp:213
uint32_t to_u32bit(const std::string &str)
Definition: parsing.cpp:35
const char * cast_uint8_ptr_to_char(const uint8_t *b)
Definition: mem_ops.h:195
Response Botan::HTTP::http_sync ( const std::string &  verb,
const std::string &  url,
const std::string &  content_type,
const std::vector< uint8_t > &  body,
size_t  allowable_redirects,
std::chrono::milliseconds  timeout 
)

Definition at line 227 of file http_util.cpp.

References http_sync().

233  {
234  auto transact_with_timeout =
235  [timeout](const std::string& hostname, const std::string& service, const std::string& message)
236  {
237  return http_transact(hostname, service, message, timeout);
238  };
239 
240  return http_sync(
241  transact_with_timeout,
242  verb,
243  url,
244  content_type,
245  body,
246  allowable_redirects);
247  }
Response http_sync(const std::string &verb, const std::string &url, const std::string &content_type, const std::vector< uint8_t > &body, size_t allowable_redirects, std::chrono::milliseconds timeout)
Definition: http_util.cpp:227
std::ostream & Botan::HTTP::operator<< ( std::ostream &  o,
const Response resp 
)

Definition at line 95 of file http_util.cpp.

References Botan::HTTP::Response::body(), Botan::cast_uint8_ptr_to_char(), Botan::HTTP::Response::headers(), Botan::HTTP::Response::status_code(), Botan::HTTP::Response::status_message(), and Botan::ASN1::to_string().

96  {
97  o << "HTTP " << resp.status_code() << " " << resp.status_message() << "\n";
98  for(auto h : resp.headers())
99  o << "Header '" << h.first << "' = '" << h.second << "'\n";
100  o << "Body " << std::to_string(resp.body().size()) << " bytes:\n";
101  o.write(cast_uint8_ptr_to_char(resp.body().data()), resp.body().size());
102  return o;
103  }
std::string to_string(const BER_Object &obj)
Definition: asn1_obj.cpp:213
const char * cast_uint8_ptr_to_char(const uint8_t *b)
Definition: mem_ops.h:195
Response Botan::HTTP::POST_sync ( const std::string &  url,
const std::string &  content_type,
const std::vector< uint8_t > &  body,
size_t  allowable_redirects,
std::chrono::milliseconds  timeout 
)

Definition at line 256 of file http_util.cpp.

References http_sync().

261  {
262  return http_sync("POST", url, content_type, body, allowable_redirects, timeout);
263  }
Response http_sync(const std::string &verb, const std::string &url, const std::string &content_type, const std::vector< uint8_t > &body, size_t allowable_redirects, std::chrono::milliseconds timeout)
Definition: http_util.cpp:227
std::string Botan::HTTP::url_encode ( const std::string &  in)

Definition at line 74 of file http_util.cpp.

References Botan::cast_char_ptr_to_uint8(), and Botan::hex_encode().

75  {
76  std::ostringstream out;
77 
78  for(auto c : in)
79  {
80  if(c >= 'A' && c <= 'Z')
81  out << c;
82  else if(c >= 'a' && c <= 'z')
83  out << c;
84  else if(c >= '0' && c <= '9')
85  out << c;
86  else if(c == '-' || c == '_' || c == '.' || c == '~')
87  out << c;
88  else
89  out << '%' << hex_encode(cast_char_ptr_to_uint8(&c), 1);
90  }
91 
92  return out.str();
93  }
void hex_encode(char output[], const uint8_t input[], size_t input_length, bool uppercase)
Definition: hex.cpp:31
const uint8_t * cast_char_ptr_to_uint8(const char *s)
Definition: mem_ops.h:190