106 const std::string& verb,
107 const std::string& url,
108 const std::string& content_type,
109 const std::vector<uint8_t>& body,
110 size_t allowable_redirects)
115 const auto protocol_host_sep = url.find(
"://");
116 if(protocol_host_sep == std::string::npos)
117 throw HTTP_Error(
"Invalid URL '" + url +
"'");
119 const auto host_loc_sep = url.find(
'/', protocol_host_sep + 3);
121 std::string hostname, loc, service;
123 if(host_loc_sep == std::string::npos)
125 hostname = url.substr(protocol_host_sep + 3, std::string::npos);
130 hostname = url.substr(protocol_host_sep + 3, host_loc_sep-protocol_host_sep-3);
131 loc = url.substr(host_loc_sep, std::string::npos);
134 const auto port_sep = hostname.find(
":");
135 if(port_sep == std::string::npos)
142 service = hostname.substr(port_sep + 1, std::string::npos);
143 hostname = hostname.substr(0, port_sep);
146 std::ostringstream outbuf;
148 outbuf << verb <<
" " << loc <<
" HTTP/1.0\r\n";
149 outbuf <<
"Host: " << hostname <<
"\r\n";
153 outbuf <<
"Accept: */*\r\n";
154 outbuf <<
"Cache-Control: no-cache\r\n";
156 else if(verb ==
"POST")
157 outbuf <<
"Content-Length: " << body.size() <<
"\r\n";
159 if(!content_type.empty())
160 outbuf <<
"Content-Type: " << content_type <<
"\r\n";
161 outbuf <<
"Connection: close\r\n\r\n";
164 std::istringstream io(http_transact(hostname, service, outbuf.str()));
167 std::getline(io, line1);
168 if(!io || line1.empty())
171 std::stringstream response_stream(line1);
172 std::string http_version;
173 unsigned int status_code;
174 std::string status_message;
176 response_stream >> http_version >> status_code;
178 std::getline(response_stream, status_message);
180 if(!response_stream || http_version.substr(0,5) !=
"HTTP/")
183 std::map<std::string, std::string> headers;
184 std::string header_line;
185 while (std::getline(io, header_line) && header_line !=
"\r")
187 auto sep = header_line.find(
": ");
188 if(sep == std::string::npos || sep > header_line.size() - 2)
189 throw HTTP_Error(
"Invalid HTTP header " + header_line);
190 const std::string key = header_line.substr(0, sep);
192 if(sep + 2 < header_line.size() - 1)
194 const std::string val = header_line.substr(sep + 2, (header_line.size() - 1) - (sep + 2));
199 if(status_code == 301 && headers.count(
"Location"))
201 if(allowable_redirects == 0)
202 throw HTTP_Error(
"HTTP redirection count exceeded");
203 return GET_sync(headers[
"Location"], allowable_redirects - 1);
206 std::vector<uint8_t> resp_body;
207 std::vector<uint8_t> buf(4096);
211 const size_t got =
static_cast<size_t>(io.gcount());
212 resp_body.insert(resp_body.end(), buf.data(), &buf[got]);
215 const std::string header_size =
search_map(headers, std::string(
"Content-Length"));
217 if(!header_size.empty())
219 if(resp_body.size() !=
to_u32bit(header_size))
220 throw HTTP_Error(
"Content-Length disagreement, header says " +
221 header_size +
" got " + std::to_string(resp_body.size()));
224 return Response(status_code, status_message, resp_body, headers);