113 std::string_view verb,
114 std::string_view url,
115 std::string_view content_type,
116 const std::vector<uint8_t>& body,
117 size_t allowable_redirects) {
122 const auto protocol_host_sep = url.find(
"://");
123 if(protocol_host_sep == std::string::npos) {
127 const auto host_loc_sep = url.find(
'/', protocol_host_sep + 3);
129 std::string hostname, loc, service;
131 if(host_loc_sep == std::string::npos) {
132 hostname = url.substr(protocol_host_sep + 3, std::string::npos);
135 hostname = url.substr(protocol_host_sep + 3, host_loc_sep - protocol_host_sep - 3);
136 loc = url.substr(host_loc_sep, std::string::npos);
139 const auto port_sep = hostname.find(
':');
140 if(port_sep == std::string::npos) {
144 service = hostname.substr(port_sep + 1, std::string::npos);
145 hostname = hostname.substr(0, port_sep);
148 std::ostringstream outbuf;
150 outbuf << verb <<
" " << loc <<
" HTTP/1.0\r\n";
151 outbuf <<
"Host: " << hostname <<
"\r\n";
154 outbuf <<
"Accept: */*\r\n";
155 outbuf <<
"Cache-Control: no-cache\r\n";
156 }
else if(verb ==
"POST") {
157 outbuf <<
"Content-Length: " << body.size() <<
"\r\n";
160 if(!content_type.empty()) {
161 outbuf <<
"Content-Type: " << content_type <<
"\r\n";
163 outbuf <<
"Connection: close\r\n\r\n";
166 std::istringstream io(http_transact(hostname, service, outbuf.str()));
169 std::getline(io, line1);
170 if(!io || line1.empty()) {
174 std::stringstream response_stream(line1);
175 std::string http_version;
176 unsigned int status_code;
177 std::string status_message;
179 response_stream >> http_version >> status_code;
181 std::getline(response_stream, status_message);
183 if(!response_stream || http_version.substr(0, 5) !=
"HTTP/") {
187 std::map<std::string, std::string> headers;
188 std::string header_line;
189 while(std::getline(io, header_line) && header_line !=
"\r") {
190 auto sep = header_line.find(
": ");
191 if(sep == std::string::npos || sep > header_line.size() - 2) {
192 throw HTTP_Error(
fmt(
"Invalid HTTP header '{}'", header_line));
194 const std::string key = header_line.substr(0, sep);
196 if(sep + 2 < header_line.size() - 1) {
197 const std::string val = header_line.substr(sep + 2, (header_line.size() - 1) - (sep + 2));
202 if(status_code == 301 && headers.contains(
"Location")) {
203 if(allowable_redirects == 0) {
204 throw HTTP_Error(
"HTTP redirection count exceeded");
206 return GET_sync(headers[
"Location"], allowable_redirects - 1);
209 std::vector<uint8_t> resp_body;
210 std::vector<uint8_t> buf(4096);
213 const size_t got =
static_cast<size_t>(io.gcount());
214 resp_body.insert(resp_body.end(), buf.data(), &buf[got]);
217 auto cl_hdr = headers.find(
"Content-Length");
218 if(cl_hdr != headers.end()) {
219 const std::string header_size = cl_hdr->second;
220 if(resp_body.size() !=
to_u32bit(header_size)) {
221 throw HTTP_Error(
fmt(
"Content-Length disagreement, header says {} got {}", header_size, resp_body.size()));
225 return Response(status_code, status_message, resp_body, headers);
229 std::string_view url,
230 std::string_view content_type,
231 const std::vector<uint8_t>& body,
232 size_t allowable_redirects,
233 std::chrono::milliseconds timeout) {
234 auto transact_with_timeout = [timeout](
235 std::string_view hostname, std::string_view service, std::string_view message) {
236 return http_transact(hostname, service, message, timeout);
239 return http_sync(transact_with_timeout, verb, url, content_type, body, allowable_redirects);