112 std::string_view verb,
113 std::string_view url,
114 std::string_view content_type,
115 const std::vector<uint8_t>& body,
116 size_t allowable_redirects) {
121 const auto protocol_host_sep = url.find(
"://");
122 if(protocol_host_sep == std::string::npos) {
126 const auto host_loc_sep = url.find(
'/', protocol_host_sep + 3);
128 std::string hostname, loc, service;
130 if(host_loc_sep == std::string::npos) {
131 hostname = url.substr(protocol_host_sep + 3, std::string::npos);
134 hostname = url.substr(protocol_host_sep + 3, host_loc_sep - protocol_host_sep - 3);
135 loc = url.substr(host_loc_sep, std::string::npos);
138 const auto port_sep = hostname.find(
':');
139 if(port_sep == std::string::npos) {
143 service = hostname.substr(port_sep + 1, std::string::npos);
144 hostname = hostname.substr(0, port_sep);
147 std::ostringstream outbuf;
149 outbuf << verb <<
" " << loc <<
" HTTP/1.0\r\n";
150 outbuf <<
"Host: " << hostname <<
"\r\n";
153 outbuf <<
"Accept: */*\r\n";
154 outbuf <<
"Cache-Control: no-cache\r\n";
155 }
else if(verb ==
"POST") {
156 outbuf <<
"Content-Length: " << body.size() <<
"\r\n";
159 if(!content_type.empty()) {
160 outbuf <<
"Content-Type: " << content_type <<
"\r\n";
162 outbuf <<
"Connection: close\r\n\r\n";
165 std::istringstream io(http_transact(hostname, service, outbuf.str()));
168 std::getline(io, line1);
169 if(!io || line1.empty()) {
173 std::stringstream response_stream(line1);
174 std::string http_version;
175 unsigned int status_code;
176 std::string status_message;
178 response_stream >> http_version >> status_code;
180 std::getline(response_stream, status_message);
182 if(!response_stream || http_version.substr(0, 5) !=
"HTTP/") {
186 std::map<std::string, std::string> headers;
187 std::string header_line;
188 while(std::getline(io, header_line) && header_line !=
"\r") {
189 auto sep = header_line.find(
": ");
190 if(sep == std::string::npos || sep > header_line.size() - 2) {
191 throw HTTP_Error(
fmt(
"Invalid HTTP header '{}'", header_line));
193 const std::string key = header_line.substr(0, sep);
195 if(sep + 2 < header_line.size() - 1) {
196 const std::string val = header_line.substr(sep + 2, (header_line.size() - 1) - (sep + 2));
201 if(status_code == 301 && headers.contains(
"Location")) {
202 if(allowable_redirects == 0) {
203 throw HTTP_Error(
"HTTP redirection count exceeded");
205 return GET_sync(headers[
"Location"], allowable_redirects - 1);
208 std::vector<uint8_t> resp_body;
209 std::vector<uint8_t> buf(4096);
212 const size_t got =
static_cast<size_t>(io.gcount());
213 resp_body.insert(resp_body.end(), buf.data(), &buf[got]);
216 auto cl_hdr = headers.find(
"Content-Length");
217 if(cl_hdr != headers.end()) {
218 const std::string header_size = cl_hdr->second;
219 if(resp_body.size() !=
to_u32bit(header_size)) {
220 throw HTTP_Error(
fmt(
"Content-Length disagreement, header says {} got {}", header_size, resp_body.size()));
224 return Response(status_code, status_message, resp_body, headers);
228 std::string_view url,
229 std::string_view content_type,
230 const std::vector<uint8_t>& body,
231 size_t allowable_redirects,
232 std::chrono::milliseconds timeout) {
233 auto transact_with_timeout = [timeout](
234 std::string_view hostname, std::string_view service, std::string_view message) {
235 return http_transact(hostname, service, message, timeout);
238 return http_sync(transact_with_timeout, verb, url, content_type, body, allowable_redirects);