Botan 3.14.1
Crypto and TLS for C&
Botan::HTTP Namespace Reference

Classes

struct  Case_Insensitive_Less
class  HTTP_Error
class  RequestLimits
class  Response

Typedefs

using Headers = std::map<std::string, std::string, Case_Insensitive_Less>
typedef std::function< Response(std::string_view, std::string_view, std::string_view, std::chrono::milliseconds, size_t)> http_exch_fn

Functions

Response GET_sync (const URI &uri, const RequestLimits &limits)
Response http_sync (const http_exch_fn &http_transact, std::string_view verb, const URI &uri, std::string_view content_type, const std::vector< uint8_t > &body, const RequestLimits &limits)
Response http_sync (std::string_view verb, const URI &uri, std::string_view content_type, const std::vector< uint8_t > &body, const RequestLimits &limits)
std::ostream & operator<< (std::ostream &o, const Response &resp)
Response POST_sync (const URI &uri, std::string_view content_type, const std::vector< uint8_t > &body, const RequestLimits &limits)
Response read_response_from_socket (OS::Socket &socket, std::chrono::milliseconds timeout, size_t max_body_size)
std::string url_encode (std::string_view in)

Typedef Documentation

◆ Headers

using Botan::HTTP::Headers = std::map<std::string, std::string, Case_Insensitive_Less>

Definition at line 60 of file http_util.h.

◆ http_exch_fn

typedef std::function<Response(std::string_view, std::string_view, std::string_view, std::chrono::milliseconds, size_t)> Botan::HTTP::http_exch_fn

Performs one request/response exchange: (hostname, service, request message, timeout, max body size)

Definition at line 136 of file http_util.h.

Function Documentation

◆ GET_sync()

Response BOTAN_TEST_API Botan::HTTP::GET_sync ( const URI & uri,
const RequestLimits & limits )

Definition at line 431 of file http_util.cpp.

431 {
432 return http_sync("GET", uri, "", std::vector<uint8_t>(), limits);
433}
Response http_sync(const http_exch_fn &http_transact, std::string_view verb, const URI &uri, std::string_view content_type, const std::vector< uint8_t > &body, const RequestLimits &limits)

References GET_sync(), and http_sync().

Referenced by GET_sync().

◆ http_sync() [1/2]

Response BOTAN_TEST_API Botan::HTTP::http_sync ( const http_exch_fn & http_transact,
std::string_view verb,
const URI & uri,
std::string_view content_type,
const std::vector< uint8_t > & body,
const RequestLimits & limits )

Definition at line 413 of file http_util.cpp.

418 {
419 const auto deadline = std::chrono::steady_clock::now() + limits.timeout();
420 return http_sync_until(http_transact, verb, uri, content_type, body, limits, deadline);
421}
std::chrono::milliseconds timeout() const
Definition http_util.h:106

References http_sync(), and Botan::HTTP::RequestLimits::timeout().

Referenced by GET_sync(), http_sync(), http_sync(), and POST_sync().

◆ http_sync() [2/2]

Response Botan::HTTP::http_sync ( std::string_view verb,
const URI & uri,
std::string_view content_type,
const std::vector< uint8_t > & body,
const RequestLimits & limits )

Definition at line 423 of file http_util.cpp.

427 {
428 return http_sync(http_transact, verb, uri, content_type, body, limits);
429}

References http_sync().

◆ operator<<()

BOTAN_TEST_API std::ostream & Botan::HTTP::operator<< ( std::ostream & o,
const Response & resp )

Definition at line 301 of file http_util.cpp.

301 {
302 o << "HTTP " << resp.status_code() << " " << resp.status_message() << "\n";
303 for(const auto& h : resp.headers()) {
304 o << "Header '" << h.first << "' = '" << h.second << "'\n";
305 }
306 o << "Body " << std::to_string(resp.body().size()) << " bytes:\n";
307 o.write(cast_uint8_ptr_to_char(resp.body().data()), resp.body().size());
308 return o;
309}
const std::vector< uint8_t > & body() const
Definition http_util.h:74
const Headers & headers() const
Definition http_util.h:76
unsigned int status_code() const
Definition http_util.h:72
std::string status_message() const
Definition http_util.h:78
const char * cast_uint8_ptr_to_char(const uint8_t *b)
Definition mem_ops.h:323

References Botan::HTTP::Response::body(), Botan::cast_uint8_ptr_to_char(), Botan::HTTP::Response::headers(), Botan::HTTP::Response::status_code(), and Botan::HTTP::Response::status_message().

◆ POST_sync()

Response Botan::HTTP::POST_sync ( const URI & uri,
std::string_view content_type,
const std::vector< uint8_t > & body,
const RequestLimits & limits )

Definition at line 435 of file http_util.cpp.

438 {
439 return http_sync("POST", uri, content_type, body, limits);
440}

References http_sync(), and POST_sync().

Referenced by POST_sync().

◆ read_response_from_socket()

Response BOTAN_TEST_API Botan::HTTP::read_response_from_socket ( OS::Socket & socket,
std::chrono::milliseconds timeout,
size_t max_body_size )

Read a complete HTTP/1.0 response from an already-connected socket and return the parsed Response. Enforces header- and body-size limits during the read. Exposed via BOTAN_TEST_API so the parser can be exercised against a fake socket; production code reaches it through http_transact.

Definition at line 207 of file http_util.cpp.

207 {
208 const auto start_time = std::chrono::steady_clock::now();
209 const auto deadline_exceeded = [&] { return std::chrono::steady_clock::now() - start_time > timeout; };
210
211 if(deadline_exceeded()) {
212 throw HTTP_Error("Timeout before reading response");
213 }
214
215 std::string buf;
216 std::vector<uint8_t> chunk(DefaultBufferSize);
217 size_t header_end = std::string::npos;
218
219 while(header_end == std::string::npos) {
220 const size_t got = socket.read(chunk.data(), chunk.size());
221 if(got == 0) {
222 throw HTTP_Error("Server closed connection before headers complete");
223 }
224 if(deadline_exceeded()) {
225 throw HTTP_Error("Timeout while reading headers");
226 }
227 buf.append(cast_uint8_ptr_to_char(chunk.data()), got);
228 header_end = buf.find("\r\n\r\n");
229 if(header_end == std::string::npos && buf.size() > MaxHeaderBytes) {
230 throw HTTP_Error("HTTP headers exceed maximum size");
231 }
232 }
233
234 // Same cap re-checked once the terminator is found, since the terminator
235 // can arrive in the chunk that crosses the limit.
236 if(header_end > MaxHeaderBytes) {
237 throw HTTP_Error("HTTP headers exceed maximum size");
238 }
239
240 auto parsed = parse_status_and_headers(std::string_view(buf).substr(0, header_end));
241 const auto content_length = validate_response_headers(parsed.headers, max_body_size);
242
243 const size_t body_cap = std::min(max_body_size, content_length.value_or(std::numeric_limits<size_t>::max()));
244
245 std::vector<uint8_t> body;
246 if(content_length) {
247 // Already checked against max_body_size by validate_response_headers
248 body.reserve(*content_length);
249 }
250 const size_t body_start = header_end + 4;
251 if(body_start < buf.size()) {
252 const size_t spill = buf.size() - body_start;
253 if(spill > body_cap) {
254 throw HTTP_Error("Response body exceeds maximum size");
255 }
256 body.insert(body.end(),
257 reinterpret_cast<const uint8_t*>(buf.data() + body_start),
258 reinterpret_cast<const uint8_t*>(buf.data() + buf.size()));
259 }
260
261 while(!content_length || body.size() < *content_length) {
262 const size_t got = socket.read(chunk.data(), chunk.size());
263 if(got == 0) {
264 break;
265 }
266 if(deadline_exceeded()) {
267 throw HTTP_Error("Timeout while reading body");
268 }
269 if(body.size() + got > body_cap) {
270 throw HTTP_Error("Response body exceeds maximum size");
271 }
272 body.insert(body.end(), chunk.data(), chunk.data() + got);
273 }
274
275 if(content_length && body.size() != *content_length) {
276 throw HTTP_Error(fmt("Content-Length disagreement, header says {} got {}", *content_length, body.size()));
277 }
278
279 return Response(parsed.status_code, std::move(parsed.status_message), std::move(body), std::move(parsed.headers));
280}
virtual size_t read(uint8_t buf[], size_t len)=0
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
constexpr size_t DefaultBufferSize
Definition types.h:150

References Botan::cast_uint8_ptr_to_char(), Botan::DefaultBufferSize, Botan::fmt(), and Botan::OS::Socket::read().

◆ url_encode()

std::string BOTAN_TEST_API Botan::HTTP::url_encode ( std::string_view in)

Definition at line 282 of file http_util.cpp.

282 {
283 constexpr auto needs_url_encoding = CharacterValidityTable::alpha_numeric_plus("-_.~").invert();
284 constexpr std::string_view hex_digits = "0123456789ABCDEF";
285
286 std::string out;
287 out.reserve(in.size());
288 for(const char c : in) {
289 if(needs_url_encoding(c)) {
290 const auto byte = static_cast<uint8_t>(c);
291 out += '%';
292 out += hex_digits[byte >> 4];
293 out += hex_digits[byte & 0x0F];
294 } else {
295 out += c;
296 }
297 }
298 return out;
299}
static constexpr CharacterValidityTable alpha_numeric_plus(std::string_view extras)
Definition charset.h:114
constexpr CharacterValidityTable invert() const
Definition charset.h:130

References Botan::CharacterValidityTable::alpha_numeric_plus(), and Botan::CharacterValidityTable::invert().