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;
133 if(host_loc_sep == std::string::npos) {
134 hostname = url.substr(protocol_host_sep + 3);
137 hostname = url.substr(protocol_host_sep + 3, host_loc_sep - protocol_host_sep - 3);
138 loc = url.substr(host_loc_sep);
141 const auto port_sep = hostname.find(
':');
142 if(port_sep == std::string::npos) {
146 service = hostname.substr(port_sep + 1, std::string::npos);
147 hostname = hostname.substr(0, port_sep);
150 std::ostringstream outbuf;
152 outbuf << verb <<
" " << loc <<
" HTTP/1.0\r\n";
153 outbuf <<
"Host: " << hostname <<
"\r\n";
156 outbuf <<
"Accept: */*\r\n";
157 outbuf <<
"Cache-Control: no-cache\r\n";
158 }
else if(verb ==
"POST") {
159 outbuf <<
"Content-Length: " << body.size() <<
"\r\n";
162 if(!content_type.empty()) {
163 outbuf <<
"Content-Type: " << content_type <<
"\r\n";
165 outbuf <<
"Connection: close\r\n\r\n";
168 std::istringstream io(http_transact(hostname, service, outbuf.str()));
171 std::getline(io, line1);
172 if(!io || line1.empty()) {
176 std::stringstream response_stream(line1);
177 std::string http_version;
178 unsigned int status_code = 0;
179 std::string status_message;
181 response_stream >> http_version >> status_code;
183 std::getline(response_stream, status_message);
185 if(!response_stream || !http_version.starts_with(
"HTTP/")) {
189 std::map<std::string, std::string> headers;
190 std::string header_line;
191 while(std::getline(io, header_line) && header_line !=
"\r") {
192 auto sep = header_line.find(
": ");
193 if(sep == std::string::npos || sep > header_line.size() - 2) {
194 throw HTTP_Error(
fmt(
"Invalid HTTP header '{}'", header_line));
196 const std::string key = header_line.substr(0, sep);
198 if(sep + 2 < header_line.size() - 1) {
199 const std::string val = header_line.substr(sep + 2, (header_line.size() - 1) - (sep + 2));
204 if(status_code == 301 && headers.contains(
"Location")) {
205 if(allowable_redirects == 0) {
206 throw HTTP_Error(
"HTTP redirection count exceeded");
208 return GET_sync(headers[
"Location"], allowable_redirects - 1);
211 std::vector<uint8_t> resp_body;
212 std::vector<uint8_t> buf(4096);
215 const size_t got =
static_cast<size_t>(io.gcount());
216 resp_body.insert(resp_body.end(), buf.data(), &buf[got]);
219 auto cl_hdr = headers.find(
"Content-Length");
220 if(cl_hdr != headers.end()) {
221 const std::string header_size = cl_hdr->second;
222 if(resp_body.size() !=
to_u32bit(header_size)) {
223 throw HTTP_Error(
fmt(
"Content-Length disagreement, header says {} got {}", header_size, resp_body.size()));
227 return Response(status_code, status_message, resp_body, headers);
231 std::string_view url,
232 std::string_view content_type,
233 const std::vector<uint8_t>& body,
234 size_t allowable_redirects,
235 std::chrono::milliseconds timeout) {
236 auto transact_with_timeout = [timeout](
237 std::string_view hostname, std::string_view service, std::string_view message) {
238 return http_transact(hostname, service, message, timeout);
241 return http_sync(transact_with_timeout, verb, url, content_type, body, allowable_redirects);