Botan 3.14.1
Crypto and TLS for C&
http_util.h
Go to the documentation of this file.
1/*
2* HTTP utilities
3* (C) 2013 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#ifndef BOTAN_UTILS_HTTP_H_
9#define BOTAN_UTILS_HTTP_H_
10
11#include <botan/exceptn.h>
12#include <botan/types.h>
13#include <algorithm>
14#include <chrono>
15#include <functional>
16#include <map>
17#include <string>
18#include <string_view>
19#include <vector>
20
21namespace Botan {
22
23class URI;
24
25namespace OS {
26class Socket;
27}
28
29} // namespace Botan
30
31namespace Botan::HTTP {
32
33/**
34* HTTP_Error Exception
35*/
36class BOTAN_TEST_API HTTP_Error final : public Exception {
37 public:
38 explicit HTTP_Error(const std::string& msg) : Exception("HTTP error " + msg) {}
39
40 ErrorType error_type() const noexcept override { return ErrorType::HttpError; }
41};
42
43/**
44* Transparent case-insensitive comparator for HTTP field names.
45* RFC 9110 5.1: "field names are case-insensitive".
46*/
48 using is_transparent = void;
49
50 bool operator()(std::string_view a, std::string_view b) const {
51 const auto ascii_lower = [](unsigned char c) -> unsigned char {
52 return (c >= 'A' && c <= 'Z') ? static_cast<unsigned char>(c + 32) : c;
53 };
54 return std::lexicographical_compare(a.begin(), a.end(), b.begin(), b.end(), [&](char x, char y) {
55 return ascii_lower(static_cast<unsigned char>(x)) < ascii_lower(static_cast<unsigned char>(y));
56 });
57 }
58};
59
60using Headers = std::map<std::string, std::string, Case_Insensitive_Less>;
61
62class Response final {
63 public:
64 Response() : m_status_code(0), m_status_message("Uninitialized") {}
65
66 Response(unsigned int status_code, std::string status_message, std::vector<uint8_t> body, Headers headers) :
67 m_status_code(status_code),
68 m_status_message(std::move(status_message)),
69 m_body(std::move(body)),
70 m_headers(std::move(headers)) {}
71
72 unsigned int status_code() const { return m_status_code; }
73
74 const std::vector<uint8_t>& body() const { return m_body; }
75
76 const Headers& headers() const { return m_headers; }
77
78 std::string status_message() const { return m_status_message; }
79
80 void throw_unless_ok() const {
81 if(status_code() != 200) {
83 }
84 }
85
86 private:
87 unsigned int m_status_code;
88 std::string m_status_message;
89 std::vector<uint8_t> m_body;
90 Headers m_headers;
91};
92
93BOTAN_TEST_API std::ostream& operator<<(std::ostream& o, const Response& resp);
94
95/**
96* Per-request limits applied to an HTTP exchange.
97*
98* The timeout bounds the entire exchange, including any redirects.
99*/
100class RequestLimits final {
101 public:
102 RequestLimits() = default;
103
104 size_t max_redirects() const { return m_max_redirects; }
105
106 std::chrono::milliseconds timeout() const { return m_timeout; }
107
108 size_t max_body_size() const { return m_max_body_size; }
109
111 m_max_redirects = n;
112 return *this;
113 }
114
115 RequestLimits& set_timeout(std::chrono::milliseconds t) {
116 m_timeout = t;
117 return *this;
118 }
119
121 m_max_body_size = n;
122 return *this;
123 }
124
125 private:
126 size_t m_max_redirects = 0;
127 std::chrono::milliseconds m_timeout = std::chrono::milliseconds(3000);
128 size_t m_max_body_size = 8 * 1024 * 1024;
129};
130
131/**
132* Performs one request/response exchange: (hostname, service, request message,
133* timeout, max body size)
134*/
135typedef std::function<Response(std::string_view, std::string_view, std::string_view, std::chrono::milliseconds, size_t)>
137
139 std::string_view verb,
140 const URI& uri,
141 std::string_view content_type,
142 const std::vector<uint8_t>& body,
143 const RequestLimits& limits);
144
145Response http_sync(std::string_view verb,
146 const URI& uri,
147 std::string_view content_type,
148 const std::vector<uint8_t>& body,
149 const RequestLimits& limits = {});
150
151Response BOTAN_TEST_API GET_sync(const URI& uri, const RequestLimits& limits = {});
152
153Response POST_sync(const URI& uri,
154 std::string_view content_type,
155 const std::vector<uint8_t>& body,
156 const RequestLimits& limits = {});
157
158std::string BOTAN_TEST_API url_encode(std::string_view url);
159
160/**
161* Read a complete HTTP/1.0 response from an already-connected socket and
162* return the parsed Response. Enforces header- and body-size limits during
163* the read. Exposed via BOTAN_TEST_API so the parser can be exercised
164* against a fake socket; production code reaches it through http_transact.
165*/
167 std::chrono::milliseconds timeout,
168 size_t max_body_size);
169
170} // namespace Botan::HTTP
171
172#endif
#define BOTAN_TEST_API
Definition api.h:41
Exception(std::string_view msg)
Definition exceptn.cpp:71
ErrorType error_type() const noexcept override
Definition http_util.h:40
HTTP_Error(const std::string &msg)
Definition http_util.h:38
RequestLimits & set_timeout(std::chrono::milliseconds t)
Definition http_util.h:115
std::chrono::milliseconds timeout() const
Definition http_util.h:106
RequestLimits & set_max_body_size(size_t n)
Definition http_util.h:120
size_t max_body_size() const
Definition http_util.h:108
size_t max_redirects() const
Definition http_util.h:104
RequestLimits & set_max_redirects(size_t n)
Definition http_util.h:110
const std::vector< uint8_t > & body() const
Definition http_util.h:74
Response(unsigned int status_code, std::string status_message, std::vector< uint8_t > body, Headers headers)
Definition http_util.h:66
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
void throw_unless_ok() const
Definition http_util.h:80
std::string url_encode(std::string_view in)
std::function< Response(std::string_view, std::string_view, std::string_view, std::chrono::milliseconds, size_t)> http_exch_fn
Definition http_util.h:136
Response GET_sync(const URI &uri, const RequestLimits &limits)
std::map< std::string, std::string, Case_Insensitive_Less > Headers
Definition http_util.h:60
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 read_response_from_socket(OS::Socket &socket, std::chrono::milliseconds timeout, size_t max_body_size)
Response POST_sync(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)
ErrorType
Definition exceptn.h:21
bool operator()(std::string_view a, std::string_view b) const
Definition http_util.h:50