|
Botan
1.11.15
|
Classes | |
| struct | Response |
Typedefs | |
| typedef std::function < std::string(const std::string &, const std::string &)> | http_exch_fn |
Functions | |
| std::future< Response > | GET_async (const std::string &url, size_t allowable_redirects) |
| Response | GET_sync (const std::string &url, size_t allowable_redirects) |
| Response | http_sync (http_exch_fn http_transact, const std::string &verb, const std::string &url, const std::string &content_type, const std::vector< byte > &body, size_t allowable_redirects) |
| Response | http_sync (const std::string &verb, const std::string &url, const std::string &content_type, const std::vector< byte > &body, size_t allowable_redirects) |
| std::string | http_transact_fail (const std::string &hostname, const std::string &) |
| std::ostream & | operator<< (std::ostream &o, const Response &resp) |
| Response | POST_sync (const std::string &url, const std::string &content_type, const std::vector< byte > &body, size_t allowable_redirects) |
| std::string | url_encode (const std::string &in) |
| typedef std::function<std::string (const std::string&, const std::string&)> Botan::HTTP::http_exch_fn |
Definition at line 58 of file http_util.h.
| std::future< Response > BOTAN_DLL Botan::HTTP::GET_async | ( | const std::string & | url, |
| size_t | allowable_redirects | ||
| ) |
Definition at line 222 of file http_util.cpp.
References GET_sync().
{
return std::async(std::launch::async, GET_sync, url, allowable_redirects);
}
| BOTAN_DLL Response Botan::HTTP::GET_sync | ( | const std::string & | url, |
| size_t | allowable_redirects | ||
| ) |
Definition at line 209 of file http_util.cpp.
References http_sync().
Referenced by GET_async(), and http_sync().
{
return http_sync("GET", url, "", std::vector<byte>(), allowable_redirects);
}
| BOTAN_DLL 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< byte > & | body, | ||
| size_t | allowable_redirects | ||
| ) |
Definition at line 83 of file http_util.cpp.
References GET_sync(), Botan::search_map(), Botan::ASN1::to_string(), and Botan::to_u32bit().
Referenced by GET_sync(), http_sync(), and POST_sync().
{
const auto protocol_host_sep = url.find("://");
if(protocol_host_sep == std::string::npos)
throw std::runtime_error("Invalid URL " + url);
const std::string protocol = url.substr(0, protocol_host_sep);
const auto host_loc_sep = url.find('/', protocol_host_sep + 3);
std::string hostname, loc;
if(host_loc_sep == std::string::npos)
{
hostname = url.substr(protocol_host_sep + 3, std::string::npos);
loc = "/";
}
else
{
hostname = url.substr(protocol_host_sep + 3, host_loc_sep-protocol_host_sep-3);
loc = url.substr(host_loc_sep, std::string::npos);
}
std::ostringstream outbuf;
outbuf << verb << " " << loc << " HTTP/1.0\r\n";
outbuf << "Host: " << hostname << "\r\n";
if(verb == "GET")
{
outbuf << "Accept: */*\r\n";
outbuf << "Cache-Control: no-cache\r\n";
}
else if(verb == "POST")
outbuf << "Content-Length: " << body.size() << "\r\n";
if(content_type != "")
outbuf << "Content-Type: " << content_type << "\r\n";
outbuf << "Connection: close\r\n\r\n";
outbuf.write(reinterpret_cast<const char*>(&body[0]), body.size());
std::istringstream io(http_transact(hostname, outbuf.str()));
std::string line1;
std::getline(io, line1);
if(!io || line1.empty())
throw std::runtime_error("No response");
std::stringstream response_stream(line1);
std::string http_version;
unsigned int status_code;
std::string status_message;
response_stream >> http_version >> status_code;
std::getline(response_stream, status_message);
if(!response_stream || http_version.substr(0,5) != "HTTP/")
throw std::runtime_error("Not an HTTP response");
std::map<std::string, std::string> headers;
std::string header_line;
while (std::getline(io, header_line) && header_line != "\r")
{
auto sep = header_line.find(": ");
if(sep == std::string::npos || sep > header_line.size() - 2)
throw std::runtime_error("Invalid HTTP header " + header_line);
const std::string key = header_line.substr(0, sep);
if(sep + 2 < header_line.size() - 1)
{
const std::string val = header_line.substr(sep + 2, (header_line.size() - 1) - (sep + 2));
headers[key] = val;
}
}
if(status_code == 301 && headers.count("Location"))
{
if(allowable_redirects == 0)
throw std::runtime_error("HTTP redirection count exceeded");
return GET_sync(headers["Location"], allowable_redirects - 1);
}
std::vector<byte> resp_body;
std::vector<byte> buf(4096);
while(io.good())
{
io.read(reinterpret_cast<char*>(&buf[0]), buf.size());
resp_body.insert(resp_body.end(), &buf[0], &buf[io.gcount()]);
}
const std::string header_size = search_map(headers, std::string("Content-Length"));
if(header_size != "")
{
if(resp_body.size() != to_u32bit(header_size))
throw std::runtime_error("Content-Length disagreement, header says " +
header_size + " got " + std::to_string(resp_body.size()));
}
return Response(status_code, status_message, resp_body, headers);
}
| BOTAN_DLL Response Botan::HTTP::http_sync | ( | const std::string & | verb, |
| const std::string & | url, | ||
| const std::string & | content_type, | ||
| const std::vector< byte > & | body, | ||
| size_t | allowable_redirects | ||
| ) |
Definition at line 190 of file http_util.cpp.
References http_sync(), and http_transact_fail().
{
return http_sync(
#if defined(BOTAN_HAS_BOOST_ASIO)
http_transact_asio,
#else
http_transact_fail,
#endif
verb,
url,
content_type,
body,
allowable_redirects);
}
| std::string BOTAN_DLL Botan::HTTP::http_transact_fail | ( | const std::string & | hostname, |
| const std::string & | |||
| ) |
Definition at line 45 of file http_util.cpp.
Referenced by http_sync().
{
throw std::runtime_error("Cannot connect to " + hostname +
": network code disabled in build");
}
| BOTAN_DLL std::ostream & Botan::HTTP::operator<< | ( | std::ostream & | o, |
| const Response & | resp | ||
| ) |
Definition at line 73 of file http_util.cpp.
References Botan::HTTP::Response::body(), Botan::HTTP::Response::headers(), Botan::HTTP::Response::status_code(), Botan::HTTP::Response::status_message(), and Botan::ASN1::to_string().
{
o << "HTTP " << resp.status_code() << " " << resp.status_message() << "\n";
for(auto h : resp.headers())
o << "Header '" << h.first << "' = '" << h.second << "'\n";
o << "Body " << std::to_string(resp.body().size()) << " bytes:\n";
o.write(reinterpret_cast<const char*>(&resp.body()[0]), resp.body().size());
return o;
}
| BOTAN_DLL Response Botan::HTTP::POST_sync | ( | const std::string & | url, |
| const std::string & | content_type, | ||
| const std::vector< byte > & | body, | ||
| size_t | allowable_redirects | ||
| ) |
Definition at line 214 of file http_util.cpp.
References http_sync().
Referenced by Botan::OCSP::online_check().
{
return http_sync("POST", url, content_type, body, allowable_redirects);
}
| BOTAN_DLL std::string Botan::HTTP::url_encode | ( | const std::string & | in | ) |
Definition at line 52 of file http_util.cpp.
References c.
{
std::ostringstream out;
for(auto c : in)
{
if(c >= 'A' && c <= 'Z')
out << c;
else if(c >= 'a' && c <= 'z')
out << c;
else if(c >= '0' && c <= '9')
out << c;
else if(c == '-' || c == '_' || c == '.' || c == '~')
out << c;
else
out << '%' << hex_encode(reinterpret_cast<byte*>(&c), 1);
}
return out.str();
}
1.7.6.1