114 std::string_view verb,
115 std::string_view url,
116 std::string_view content_type,
117 const std::vector<uint8_t>& body,
118 size_t allowable_redirects) {
123 const auto protocol_host_sep = url.find(
"://");
124 if(protocol_host_sep == std::string::npos) {
128 const auto host_loc_sep = url.find(
'/', protocol_host_sep + 3);
130 std::string hostname;
134 if(host_loc_sep == std::string::npos) {
135 hostname = url.substr(protocol_host_sep + 3);
138 hostname = url.substr(protocol_host_sep + 3, host_loc_sep - protocol_host_sep - 3);
139 loc = url.substr(host_loc_sep);
142 const auto port_sep = hostname.find(
':');
143 if(port_sep == std::string::npos) {
147 service = hostname.substr(port_sep + 1, std::string::npos);
148 hostname = hostname.substr(0, port_sep);
151 std::ostringstream outbuf;
153 outbuf << verb <<
" " << loc <<
" HTTP/1.0\r\n";
154 outbuf <<
"Host: " << hostname <<
"\r\n";
157 outbuf <<
"Accept: */*\r\n";
158 outbuf <<
"Cache-Control: no-cache\r\n";
159 }
else if(verb ==
"POST") {
160 outbuf <<
"Content-Length: " << body.size() <<
"\r\n";
163 if(!content_type.empty()) {
164 outbuf <<
"Content-Type: " << content_type <<
"\r\n";
166 outbuf <<
"Connection: close\r\n\r\n";
169 std::istringstream io(http_transact(hostname, service, outbuf.str()));
172 std::getline(io, line1);
173 if(!io || line1.empty()) {
177 std::stringstream response_stream(line1);
178 std::string http_version;
179 unsigned int status_code = 0;
180 std::string status_message;
182 response_stream >> http_version >> status_code;
184 std::getline(response_stream, status_message);
186 if(!response_stream || !http_version.starts_with(
"HTTP/")) {
190 std::map<std::string, std::string> headers;
191 std::string header_line;
192 while(std::getline(io, header_line) && header_line !=
"\r") {
193 auto sep = header_line.find(
": ");
194 if(sep == std::string::npos || sep > header_line.size() - 2) {
195 throw HTTP_Error(
fmt(
"Invalid HTTP header '{}'", header_line));
197 const std::string key = header_line.substr(0, sep);
199 if(sep + 2 < header_line.size() - 1) {
200 const std::string val = header_line.substr(sep + 2, (header_line.size() - 1) - (sep + 2));
205 if(status_code == 301 && headers.contains(
"Location")) {
206 if(allowable_redirects == 0) {
207 throw HTTP_Error(
"HTTP redirection count exceeded");
209 return GET_sync(headers[
"Location"], allowable_redirects - 1);
212 std::vector<uint8_t> resp_body;
213 std::vector<uint8_t> buf(4096);
216 const size_t got =
static_cast<size_t>(io.gcount());
217 resp_body.insert(resp_body.end(), buf.data(), &buf[got]);
220 auto cl_hdr = headers.find(
"Content-Length");
221 if(cl_hdr != headers.end()) {
222 const std::string header_size = cl_hdr->second;
223 if(resp_body.size() !=
to_u32bit(header_size)) {
224 throw HTTP_Error(
fmt(
"Content-Length disagreement, header says {} got {}", header_size, resp_body.size()));
228 return Response(status_code, status_message, resp_body, headers);
232 std::string_view url,
233 std::string_view content_type,
234 const std::vector<uint8_t>& body,
235 size_t allowable_redirects,
236 std::chrono::milliseconds timeout) {
237 auto transact_with_timeout = [timeout](
238 std::string_view hostname, std::string_view service, std::string_view message) {
239 return http_transact(hostname, service, message, timeout);
242 return http_sync(transact_with_timeout, verb, url, content_type, body, allowable_redirects);