Botan 3.12.0
Crypto and TLS for C&
http_util.cpp
Go to the documentation of this file.
1/*
2* Sketchy HTTP client
3* (C) 2013,2016 Jack Lloyd
4* 2017 René Korthaus, Rohde & Schwarz Cybersecurity
5*
6* Botan is released under the Simplified BSD License (see license.txt)
7*/
8
9#include <botan/internal/http_util.h>
10
11#include <botan/mem_ops.h>
12#include <botan/internal/fmt.h>
13#include <botan/internal/mem_utils.h>
14#include <botan/internal/parsing.h>
15#include <botan/internal/socket.h>
16#include <iomanip>
17#include <sstream>
18
19namespace Botan::HTTP {
20
21namespace {
22
23/*
24* Connect to a host, write some bytes, then read until the server
25* closes the socket.
26*/
27std::string http_transact(std::string_view hostname,
28 std::string_view service,
29 std::string_view message,
30 std::chrono::milliseconds timeout) {
31 std::unique_ptr<OS::Socket> socket;
32
33 const std::chrono::system_clock::time_point start_time = std::chrono::system_clock::now();
34
35 try {
36 socket = OS::open_socket(hostname, service, timeout);
37 if(!socket) {
38 throw Not_Implemented("No socket support enabled in build");
39 }
40 } catch(std::exception& e) {
41 throw HTTP_Error(fmt("HTTP connection to {} failed: {}", hostname, e.what()));
42 }
43
44 // Blocks until entire message has been written
45 socket->write(as_span_of_bytes(message));
46
47 if(std::chrono::system_clock::now() - start_time > timeout) {
48 throw HTTP_Error("Timeout during writing message body");
49 }
50
51 std::ostringstream oss;
52 std::vector<uint8_t> buf(DefaultBufferSize);
53 while(true) {
54 const size_t got = socket->read(buf.data(), buf.size());
55 if(got == 0) { // EOF
56 break;
57 }
58
59 if(std::chrono::system_clock::now() - start_time > timeout) {
60 throw HTTP_Error("Timeout while reading message body");
61 }
62
63 oss.write(cast_uint8_ptr_to_char(buf.data()), static_cast<std::streamsize>(got));
64 }
65
66 return oss.str();
67}
68
69bool needs_url_encoding(char c) {
70 if(c >= 'A' && c <= 'Z') {
71 return false;
72 }
73 if(c >= 'a' && c <= 'z') {
74 return false;
75 }
76 if(c >= '0' && c <= '9') {
77 return false;
78 }
79 if(c == '-' || c == '_' || c == '.' || c == '~') {
80 return false;
81 }
82 return true;
83}
84
85void check_no_crlf_nul(std::string_view field, std::string_view value) {
86 for(const char c : value) {
87 if(c == '\r' || c == '\n' || c == '\0') {
88 throw HTTP_Error(fmt("Invalid character in HTTP {}", field));
89 }
90 }
91}
92
93} // namespace
94
95std::string url_encode(std::string_view in) {
96 std::ostringstream out;
97
98 for(auto c : in) {
99 if(needs_url_encoding(c)) {
100 out << '%' << std::uppercase << std::hex << std::setfill('0') << std::setw(2) << static_cast<int>(c);
101 out << std::dec << std::nouppercase; // reset flags
102 } else {
103 out << c;
104 }
105 }
106
107 return out.str();
108}
109
110std::ostream& operator<<(std::ostream& o, const Response& resp) {
111 o << "HTTP " << resp.status_code() << " " << resp.status_message() << "\n";
112 for(const auto& h : resp.headers()) {
113 o << "Header '" << h.first << "' = '" << h.second << "'\n";
114 }
115 o << "Body " << std::to_string(resp.body().size()) << " bytes:\n";
116 o.write(cast_uint8_ptr_to_char(resp.body().data()), resp.body().size());
117 return o;
118}
119
120Response http_sync(const http_exch_fn& http_transact,
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) {
126 if(url.empty()) {
127 throw HTTP_Error("URL empty");
128 }
129
130 const auto protocol_host_sep = url.find("://");
131 if(protocol_host_sep == std::string::npos) {
132 throw HTTP_Error(fmt("Invalid URL '{}'", url));
133 }
134
135 const auto host_loc_sep = url.find('/', protocol_host_sep + 3);
136
137 std::string hostname;
138 std::string loc;
139 std::string service;
140
141 if(host_loc_sep == std::string::npos) {
142 hostname = url.substr(protocol_host_sep + 3);
143 loc = "/";
144 } else {
145 hostname = url.substr(protocol_host_sep + 3, host_loc_sep - protocol_host_sep - 3);
146 loc = url.substr(host_loc_sep);
147 }
148
149 const auto port_sep = hostname.find(':');
150 if(port_sep == std::string::npos) {
151 service = "http";
152 // hostname not modified
153 } else {
154 service = hostname.substr(port_sep + 1, std::string::npos);
155 hostname = hostname.substr(0, port_sep);
156 }
157
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);
163
164 std::ostringstream outbuf;
165
166 outbuf << verb << " " << loc << " HTTP/1.0\r\n";
167 outbuf << "Host: " << hostname << "\r\n";
168
169 if(verb == "GET") {
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";
174 }
175
176 if(!content_type.empty()) {
177 outbuf << "Content-Type: " << content_type << "\r\n";
178 }
179 outbuf << "Connection: close\r\n\r\n";
180 outbuf.write(cast_uint8_ptr_to_char(body.data()), body.size());
181
182 std::istringstream io(http_transact(hostname, service, outbuf.str()));
183
184 std::string line1;
185 std::getline(io, line1);
186 if(!io || line1.empty()) {
187 throw HTTP_Error("No response");
188 }
189
190 std::stringstream response_stream(line1);
191 std::string http_version;
192 unsigned int status_code = 0;
193 std::string status_message;
194
195 response_stream >> http_version >> status_code;
196
197 std::getline(response_stream, status_message);
198
199 if(!response_stream || !http_version.starts_with("HTTP/")) {
200 throw HTTP_Error("Not an HTTP response");
201 }
202
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));
209 }
210 const std::string key = header_line.substr(0, sep);
211
212 if(sep + 2 < header_line.size() - 1) {
213 const std::string val = header_line.substr(sep + 2, (header_line.size() - 1) - (sep + 2));
214 headers[key] = val;
215 }
216 }
217
218 if(status_code == 301 && headers.contains("Location")) {
219 if(allowable_redirects == 0) {
220 throw HTTP_Error("HTTP redirection count exceeded");
221 }
222 return GET_sync(headers["Location"], allowable_redirects - 1);
223 }
224
225 std::vector<uint8_t> resp_body;
226 std::vector<uint8_t> buf(4096);
227 while(io.good()) {
228 io.read(cast_uint8_ptr_to_char(buf.data()), buf.size());
229 const size_t got = static_cast<size_t>(io.gcount());
230 resp_body.insert(resp_body.end(), buf.data(), &buf[got]);
231 }
232
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()));
238 }
239 }
240
241 return Response(status_code, status_message, resp_body, headers);
242}
243
244Response http_sync(std::string_view verb,
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);
253 };
254
255 return http_sync(transact_with_timeout, verb, url, content_type, body, allowable_redirects);
256}
257
258Response GET_sync(std::string_view url, size_t allowable_redirects, std::chrono::milliseconds timeout) {
259 return http_sync("GET", url, "", std::vector<uint8_t>(), allowable_redirects, timeout);
260}
261
262Response POST_sync(std::string_view url,
263 std::string_view content_type,
264 const std::vector<uint8_t>& body,
265 size_t allowable_redirects,
266 std::chrono::milliseconds timeout) {
267 return http_sync("POST", url, content_type, body, allowable_redirects, timeout);
268}
269
270} // namespace Botan::HTTP
const std::vector< uint8_t > & body() const
Definition http_util.h:43
const std::map< std::string, std::string > & headers() const
Definition http_util.h:45
unsigned int status_code() const
Definition http_util.h:41
std::string status_message() const
Definition http_util.h:47
std::function< std::string(std::string_view, std::string_view, std::string_view)> http_exch_fn
Definition http_util.h:64
Response POST_sync(std::string_view url, std::string_view content_type, const std::vector< uint8_t > &body, size_t allowable_redirects, std::chrono::milliseconds timeout)
Response http_sync(const http_exch_fn &http_transact, std::string_view verb, std::string_view url, std::string_view content_type, const std::vector< uint8_t > &body, size_t allowable_redirects)
std::string url_encode(std::string_view in)
Definition http_util.cpp:95
Response GET_sync(std::string_view url, size_t allowable_redirects, std::chrono::milliseconds timeout)
std::ostream & operator<<(std::ostream &o, const Response &resp)
std::unique_ptr< Socket > BOTAN_TEST_API open_socket(std::string_view hostname, std::string_view service, std::chrono::milliseconds timeout)
Definition socket.cpp:384
uint32_t to_u32bit(std::string_view str_view)
Definition parsing.cpp:31
std::span< const uint8_t > as_span_of_bytes(const char *s, size_t len)
Definition mem_utils.h:59
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
const char * cast_uint8_ptr_to_char(const uint8_t *b)
Definition mem_ops.h:281
constexpr size_t DefaultBufferSize
Definition types.h:137