9 #include <botan/http_util.h>
10 #include <botan/parsing.h>
11 #include <botan/hex.h>
12 #include <botan/internal/os_utils.h>
13 #include <botan/internal/socket.h>
14 #include <botan/internal/stl_util.h>
27 std::string http_transact(
const std::string& hostname,
28 const std::string& message,
29 std::chrono::milliseconds timeout)
31 std::unique_ptr<OS::Socket> socket;
33 const std::chrono::system_clock::time_point start_time = std::chrono::system_clock::now();
41 catch(std::exception& e)
43 throw HTTP_Error(
"HTTP connection to " + hostname +
" failed: " + e.what());
50 if(std::chrono::system_clock::now() - start_time > timeout)
51 throw HTTP_Error(
"Timeout during writing message body");
53 std::ostringstream oss;
54 std::vector<uint8_t> buf(BOTAN_DEFAULT_BUFFER_SIZE);
57 const size_t got = socket->read(buf.data(), buf.size());
61 if(std::chrono::system_clock::now() - start_time > timeout)
62 throw HTTP_Error(
"Timeout while reading message body");
65 static_cast<std::streamsize>(got));
75 std::ostringstream out;
79 if(c >=
'A' && c <=
'Z')
81 else if(c >=
'a' && c <=
'z')
83 else if(c >=
'0' && c <=
'9')
85 else if(c ==
'-' || c ==
'_' || c ==
'.' || c ==
'~')
98 o <<
"Header '" << h.first <<
"' = '" << h.second <<
"'\n";
105 const std::string& verb,
106 const std::string& url,
107 const std::string& content_type,
108 const std::vector<uint8_t>& body,
109 size_t allowable_redirects)
114 const auto protocol_host_sep = url.find(
"://");
115 if(protocol_host_sep == std::string::npos)
116 throw HTTP_Error(
"Invalid URL '" + url +
"'");
118 const auto host_loc_sep = url.find(
'/', protocol_host_sep + 3);
120 std::string hostname, loc;
122 if(host_loc_sep == std::string::npos)
124 hostname = url.substr(protocol_host_sep + 3, std::string::npos);
129 hostname = url.substr(protocol_host_sep + 3, host_loc_sep-protocol_host_sep-3);
130 loc = url.substr(host_loc_sep, std::string::npos);
133 std::ostringstream outbuf;
135 outbuf << verb <<
" " << loc <<
" HTTP/1.0\r\n";
136 outbuf <<
"Host: " << hostname <<
"\r\n";
140 outbuf <<
"Accept: */*\r\n";
141 outbuf <<
"Cache-Control: no-cache\r\n";
143 else if(verb ==
"POST")
144 outbuf <<
"Content-Length: " << body.size() <<
"\r\n";
146 if(!content_type.empty())
147 outbuf <<
"Content-Type: " << content_type <<
"\r\n";
148 outbuf <<
"Connection: close\r\n\r\n";
151 std::istringstream io(http_transact(hostname, outbuf.str()));
154 std::getline(io, line1);
155 if(!io || line1.empty())
158 std::stringstream response_stream(line1);
159 std::string http_version;
160 unsigned int status_code;
161 std::string status_message;
163 response_stream >> http_version >> status_code;
165 std::getline(response_stream, status_message);
167 if(!response_stream || http_version.substr(0,5) !=
"HTTP/")
170 std::map<std::string, std::string> headers;
171 std::string header_line;
172 while (std::getline(io, header_line) && header_line !=
"\r")
174 auto sep = header_line.find(
": ");
175 if(sep == std::string::npos || sep > header_line.size() - 2)
176 throw HTTP_Error(
"Invalid HTTP header " + header_line);
177 const std::string key = header_line.substr(0, sep);
179 if(sep + 2 < header_line.size() - 1)
181 const std::string val = header_line.substr(sep + 2, (header_line.size() - 1) - (sep + 2));
186 if(status_code == 301 && headers.count(
"Location"))
188 if(allowable_redirects == 0)
189 throw HTTP_Error(
"HTTP redirection count exceeded");
190 return GET_sync(headers[
"Location"], allowable_redirects - 1);
193 std::vector<uint8_t> resp_body;
194 std::vector<uint8_t> buf(4096);
198 const size_t got =
static_cast<size_t>(io.gcount());
199 resp_body.insert(resp_body.end(), buf.data(), &buf[got]);
202 const std::string header_size =
search_map(headers, std::string(
"Content-Length"));
204 if(!header_size.empty())
206 if(resp_body.size() !=
to_u32bit(header_size))
207 throw HTTP_Error(
"Content-Length disagreement, header says " +
211 return Response(status_code, status_message, resp_body, headers);
215 const std::string& url,
216 const std::string& content_type,
217 const std::vector<uint8_t>& body,
218 size_t allowable_redirects,
219 std::chrono::milliseconds timeout)
221 auto transact_with_timeout =
222 [timeout](
const std::string& hostname,
const std::string& service)
224 return http_transact(hostname, service, timeout);
228 transact_with_timeout,
233 allowable_redirects);
237 size_t allowable_redirects,
238 std::chrono::milliseconds timeout)
240 return http_sync(
"GET", url,
"", std::vector<uint8_t>(), allowable_redirects, timeout);
244 const std::string& content_type,
245 const std::vector<uint8_t>& body,
246 size_t allowable_redirects,
247 std::chrono::milliseconds timeout)
249 return http_sync(
"POST", url, content_type, body, allowable_redirects, timeout);
void hex_encode(char output[], const uint8_t input[], size_t input_length, bool uppercase)
V search_map(const std::map< K, V > &mapping, const K &key, const V &null_result=V())
std::string status_message() const
const std::vector< uint8_t > & body() const
Response GET_sync(const std::string &url, 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::unique_ptr< Socket > BOTAN_TEST_API open_socket(const std::string &hostname, const std::string &service, std::chrono::milliseconds timeout)
const uint8_t * cast_char_ptr_to_uint8(const char *s)
std::string to_string(const BER_Object &obj)
uint32_t to_u32bit(const std::string &str)
std::function< std::string(const std::string &, const std::string &)> http_exch_fn
const std::map< std::string, std::string > & headers() const
unsigned int status_code() const
const char * cast_uint8_ptr_to_char(const uint8_t *b)
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)
std::string url_encode(const std::string &in)