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

Classes

class  HTTP_Error
 
class  Response
 

Typedefs

typedef std::function< std::string(std::string_view, std::string_view, std::string_view)> http_exch_fn
 

Functions

Response GET_sync (std::string_view url, 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)
 
Response http_sync (std::string_view verb, std::string_view url, std::string_view content_type, const std::vector< uint8_t > &body, size_t allowable_redirects, std::chrono::milliseconds timeout)
 
std::ostream & operator<< (std::ostream &o, const Response &resp)
 
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)
 
std::string url_encode (std::string_view in)
 

Typedef Documentation

◆ http_exch_fn

typedef std::function<std::string(std::string_view, std::string_view, std::string_view)> Botan::HTTP::http_exch_fn

Definition at line 64 of file http_util.h.

Function Documentation

◆ GET_sync()

Response BOTAN_TEST_API Botan::HTTP::GET_sync ( std::string_view url,
size_t allowable_redirects,
std::chrono::milliseconds timeout )

Definition at line 241 of file http_util.cpp.

241 {
242 return http_sync("GET", url, "", std::vector<uint8_t>(), allowable_redirects, timeout);
243}
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)

References http_sync().

Referenced by http_sync().

◆ http_sync() [1/2]

Response Botan::HTTP::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 )

Definition at line 111 of file http_util.cpp.

116 {
117 if(url.empty()) {
118 throw HTTP_Error("URL empty");
119 }
120
121 const auto protocol_host_sep = url.find("://");
122 if(protocol_host_sep == std::string::npos) {
123 throw HTTP_Error(fmt("Invalid URL '{}'", url));
124 }
125
126 const auto host_loc_sep = url.find('/', protocol_host_sep + 3);
127
128 std::string hostname, loc, service;
129
130 if(host_loc_sep == std::string::npos) {
131 hostname = url.substr(protocol_host_sep + 3, std::string::npos);
132 loc = "/";
133 } else {
134 hostname = url.substr(protocol_host_sep + 3, host_loc_sep - protocol_host_sep - 3);
135 loc = url.substr(host_loc_sep, std::string::npos);
136 }
137
138 const auto port_sep = hostname.find(':');
139 if(port_sep == std::string::npos) {
140 service = "http";
141 // hostname not modified
142 } else {
143 service = hostname.substr(port_sep + 1, std::string::npos);
144 hostname = hostname.substr(0, port_sep);
145 }
146
147 std::ostringstream outbuf;
148
149 outbuf << verb << " " << loc << " HTTP/1.0\r\n";
150 outbuf << "Host: " << hostname << "\r\n";
151
152 if(verb == "GET") {
153 outbuf << "Accept: */*\r\n";
154 outbuf << "Cache-Control: no-cache\r\n";
155 } else if(verb == "POST") {
156 outbuf << "Content-Length: " << body.size() << "\r\n";
157 }
158
159 if(!content_type.empty()) {
160 outbuf << "Content-Type: " << content_type << "\r\n";
161 }
162 outbuf << "Connection: close\r\n\r\n";
163 outbuf.write(cast_uint8_ptr_to_char(body.data()), body.size());
164
165 std::istringstream io(http_transact(hostname, service, outbuf.str()));
166
167 std::string line1;
168 std::getline(io, line1);
169 if(!io || line1.empty()) {
170 throw HTTP_Error("No response");
171 }
172
173 std::stringstream response_stream(line1);
174 std::string http_version;
175 unsigned int status_code;
176 std::string status_message;
177
178 response_stream >> http_version >> status_code;
179
180 std::getline(response_stream, status_message);
181
182 if(!response_stream || http_version.substr(0, 5) != "HTTP/") {
183 throw HTTP_Error("Not an HTTP response");
184 }
185
186 std::map<std::string, std::string> headers;
187 std::string header_line;
188 while(std::getline(io, header_line) && header_line != "\r") {
189 auto sep = header_line.find(": ");
190 if(sep == std::string::npos || sep > header_line.size() - 2) {
191 throw HTTP_Error(fmt("Invalid HTTP header '{}'", header_line));
192 }
193 const std::string key = header_line.substr(0, sep);
194
195 if(sep + 2 < header_line.size() - 1) {
196 const std::string val = header_line.substr(sep + 2, (header_line.size() - 1) - (sep + 2));
197 headers[key] = val;
198 }
199 }
200
201 if(status_code == 301 && headers.contains("Location")) {
202 if(allowable_redirects == 0) {
203 throw HTTP_Error("HTTP redirection count exceeded");
204 }
205 return GET_sync(headers["Location"], allowable_redirects - 1);
206 }
207
208 std::vector<uint8_t> resp_body;
209 std::vector<uint8_t> buf(4096);
210 while(io.good()) {
211 io.read(cast_uint8_ptr_to_char(buf.data()), buf.size());
212 const size_t got = static_cast<size_t>(io.gcount());
213 resp_body.insert(resp_body.end(), buf.data(), &buf[got]);
214 }
215
216 auto cl_hdr = headers.find("Content-Length");
217 if(cl_hdr != headers.end()) {
218 const std::string header_size = cl_hdr->second;
219 if(resp_body.size() != to_u32bit(header_size)) {
220 throw HTTP_Error(fmt("Content-Length disagreement, header says {} got {}", header_size, resp_body.size()));
221 }
222 }
223
224 return Response(status_code, status_message, resp_body, headers);
225}
Response GET_sync(std::string_view url, size_t allowable_redirects, std::chrono::milliseconds timeout)
uint32_t to_u32bit(std::string_view str_view)
Definition parsing.cpp:32
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:278

References Botan::cast_uint8_ptr_to_char(), Botan::fmt(), GET_sync(), and Botan::to_u32bit().

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

◆ http_sync() [2/2]

Response Botan::HTTP::http_sync ( std::string_view verb,
std::string_view url,
std::string_view content_type,
const std::vector< uint8_t > & body,
size_t allowable_redirects,
std::chrono::milliseconds timeout )

Definition at line 227 of file http_util.cpp.

232 {
233 auto transact_with_timeout = [timeout](
234 std::string_view hostname, std::string_view service, std::string_view message) {
235 return http_transact(hostname, service, message, timeout);
236 };
237
238 return http_sync(transact_with_timeout, verb, url, content_type, body, allowable_redirects);
239}

References http_sync().

◆ operator<<()

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

Definition at line 101 of file http_util.cpp.

101 {
102 o << "HTTP " << resp.status_code() << " " << resp.status_message() << "\n";
103 for(const auto& h : resp.headers()) {
104 o << "Header '" << h.first << "' = '" << h.second << "'\n";
105 }
106 o << "Body " << std::to_string(resp.body().size()) << " bytes:\n";
107 o.write(cast_uint8_ptr_to_char(resp.body().data()), resp.body().size());
108 return o;
109}
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

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 ( std::string_view url,
std::string_view content_type,
const std::vector< uint8_t > & body,
size_t allowable_redirects,
std::chrono::milliseconds timeout )

Definition at line 245 of file http_util.cpp.

249 {
250 return http_sync("POST", url, content_type, body, allowable_redirects, timeout);
251}

References http_sync().

◆ url_encode()

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

Definition at line 87 of file http_util.cpp.

87 {
88 std::ostringstream out;
89
90 for(auto c : in) {
91 if(needs_url_encoding(c)) {
92 out << '%' << hex_encode(cast_char_ptr_to_uint8(&c), 1);
93 } else {
94 out << c;
95 }
96 }
97
98 return out.str();
99}
void hex_encode(char output[], const uint8_t input[], size_t input_length, bool uppercase)
Definition hex.cpp:35
const uint8_t * cast_char_ptr_to_uint8(const char *s)
Definition mem_ops.h:274

References Botan::cast_char_ptr_to_uint8(), and Botan::hex_encode().