121 std::string_view verb,
122 std::string_view url,
123 std::string_view content_type,
124 const std::vector<uint8_t>& body,
125 size_t allowable_redirects) {
130 const auto protocol_host_sep = url.find(
"://");
131 if(protocol_host_sep == std::string::npos) {
135 const auto host_loc_sep = url.find(
'/', protocol_host_sep + 3);
137 std::string hostname;
141 if(host_loc_sep == std::string::npos) {
142 hostname = url.substr(protocol_host_sep + 3);
145 hostname = url.substr(protocol_host_sep + 3, host_loc_sep - protocol_host_sep - 3);
146 loc = url.substr(host_loc_sep);
149 const auto port_sep = hostname.find(
':');
150 if(port_sep == std::string::npos) {
154 service = hostname.substr(port_sep + 1, std::string::npos);
155 hostname = hostname.substr(0, port_sep);
158 check_no_crlf_nul(
"verb", verb);
159 check_no_crlf_nul(
"hostname", hostname);
160 check_no_crlf_nul(
"port", service);
161 check_no_crlf_nul(
"path", loc);
162 check_no_crlf_nul(
"content type", content_type);
164 std::ostringstream outbuf;
166 outbuf << verb <<
" " << loc <<
" HTTP/1.0\r\n";
167 outbuf <<
"Host: " << hostname <<
"\r\n";
170 outbuf <<
"Accept: */*\r\n";
171 outbuf <<
"Cache-Control: no-cache\r\n";
172 }
else if(verb ==
"POST") {
173 outbuf <<
"Content-Length: " << body.size() <<
"\r\n";
176 if(!content_type.empty()) {
177 outbuf <<
"Content-Type: " << content_type <<
"\r\n";
179 outbuf <<
"Connection: close\r\n\r\n";
182 std::istringstream io(http_transact(hostname, service, outbuf.str()));
185 std::getline(io, line1);
186 if(!io || line1.empty()) {
190 std::stringstream response_stream(line1);
191 std::string http_version;
192 unsigned int status_code = 0;
193 std::string status_message;
195 response_stream >> http_version >> status_code;
197 std::getline(response_stream, status_message);
199 if(!response_stream || !http_version.starts_with(
"HTTP/")) {
203 std::map<std::string, std::string> headers;
204 std::string header_line;
205 while(std::getline(io, header_line) && header_line !=
"\r") {
206 auto sep = header_line.find(
": ");
207 if(sep == std::string::npos || sep > header_line.size() - 2) {
208 throw HTTP_Error(
fmt(
"Invalid HTTP header '{}'", header_line));
210 const std::string key = header_line.substr(0, sep);
212 if(sep + 2 < header_line.size() - 1) {
213 const std::string val = header_line.substr(sep + 2, (header_line.size() - 1) - (sep + 2));
218 if(status_code == 301 && headers.contains(
"Location")) {
219 if(allowable_redirects == 0) {
220 throw HTTP_Error(
"HTTP redirection count exceeded");
222 return GET_sync(headers[
"Location"], allowable_redirects - 1);
225 std::vector<uint8_t> resp_body;
226 std::vector<uint8_t> buf(4096);
229 const size_t got =
static_cast<size_t>(io.gcount());
230 resp_body.insert(resp_body.end(), buf.data(), &buf[got]);
233 auto cl_hdr = headers.find(
"Content-Length");
234 if(cl_hdr != headers.end()) {
235 const std::string header_size = cl_hdr->second;
236 if(resp_body.size() !=
to_u32bit(header_size)) {
237 throw HTTP_Error(
fmt(
"Content-Length disagreement, header says {} got {}", header_size, resp_body.size()));
241 return Response(status_code, status_message, resp_body, headers);
245 std::string_view url,
246 std::string_view content_type,
247 const std::vector<uint8_t>& body,
248 size_t allowable_redirects,
249 std::chrono::milliseconds timeout) {
250 auto transact_with_timeout = [timeout](
251 std::string_view hostname, std::string_view service, std::string_view message) {
252 return http_transact(hostname, service, message, timeout);
255 return http_sync(transact_with_timeout, verb, url, content_type, body, allowable_redirects);