Botan 2.13.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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_URLGET_H_
9 #define BOTAN_UTILS_URLGET_H_
10
11 #include <botan/types.h>
12 #include <botan/exceptn.h>
13 #include <vector>
14 #include <map>
15 #include <string>
16 #include <functional>
17 #include <chrono>
18
19 BOTAN_FUTURE_INTERNAL_HEADER(http_util.h)
20
21 namespace Botan {
22
23 namespace HTTP {
24
25 /**
26 * HTTP_Error Exception
27 */
28 class BOTAN_PUBLIC_API(2,0) HTTP_Error final : public Exception
29 {
30 public:
31 explicit HTTP_Error(const std::string& msg) :
32 Exception("HTTP error " + msg)
33 {}
34
35 ErrorType error_type() const noexcept override { return ErrorType::HttpError; }
36
37 };
38
39 class Response final
40 {
41 public:
42 Response() : m_status_code(0), m_status_message("Uninitialized") {}
43
44 Response(unsigned int status_code, const std::string& status_message,
45 const std::vector<uint8_t>& body,
46 const std::map<std::string, std::string>& headers) :
47 m_status_code(status_code),
48 m_status_message(status_message),
49 m_body(body),
50 m_headers(headers) {}
51
52 unsigned int status_code() const { return m_status_code; }
53
54 const std::vector<uint8_t>& body() const { return m_body; }
55
56 const std::map<std::string, std::string>& headers() const { return m_headers; }
57
58 std::string status_message() const { return m_status_message; }
59
60 void throw_unless_ok()
61 {
62 if(status_code() != 200)
63 throw HTTP_Error(status_message());
64 }
65
66 private:
67 unsigned int m_status_code;
68 std::string m_status_message;
69 std::vector<uint8_t> m_body;
70 std::map<std::string, std::string> m_headers;
71 };
72
73 BOTAN_PUBLIC_API(2,0) std::ostream& operator<<(std::ostream& o, const Response& resp);
74
75 typedef std::function<std::string (const std::string&, const std::string&)> http_exch_fn;
76
77 BOTAN_PUBLIC_API(2,0) Response http_sync(http_exch_fn fn,
78 const std::string& verb,
79 const std::string& url,
80 const std::string& content_type,
81 const std::vector<uint8_t>& body,
82 size_t allowable_redirects);
83
84 BOTAN_PUBLIC_API(2,0) Response http_sync(const std::string& verb,
85 const std::string& url,
86 const std::string& content_type,
87 const std::vector<uint8_t>& body,
88 size_t allowable_redirects,
89 std::chrono::milliseconds timeout = std::chrono::milliseconds(3000));
90
91 BOTAN_PUBLIC_API(2,0) Response GET_sync(const std::string& url,
92 size_t allowable_redirects = 1,
93 std::chrono::milliseconds timeout = std::chrono::milliseconds(3000));
94
95 BOTAN_PUBLIC_API(2,0) Response POST_sync(const std::string& url,
96 const std::string& content_type,
97 const std::vector<uint8_t>& body,
98 size_t allowable_redirects = 1,
99 std::chrono::milliseconds timeout = std::chrono::milliseconds(3000));
100
101 BOTAN_PUBLIC_API(2,0) std::string url_encode(const std::string& url);
102
103 }
104
105 }
106
107 #endif
Botan::HTTP::Response::status_message
std::string status_message() const
Definition: http_util.h:58
Botan::HTTP::Response::Response
Response(unsigned int status_code, const std::string &status_message, const std::vector< uint8_t > &body, const std::map< std::string, std::string > &headers)
Definition: http_util.h:44
Botan::Exception
Definition: exceptn.h:85
Botan::HTTP::Response::throw_unless_ok
void throw_unless_ok()
Definition: http_util.h:60
Botan::HTTP::Response::body
const std::vector< uint8_t > & body() const
Definition: http_util.h:54
Botan::HTTP::GET_sync
Response GET_sync(const std::string &url, size_t allowable_redirects, std::chrono::milliseconds timeout)
Definition: http_util.cpp:236
Botan::HTTP::POST_sync
Response POST_sync(const std::string &url, const std::string &content_type, const std::vector< uint8_t > &body, size_t allowable_redirects, std::chrono::milliseconds timeout)
Definition: http_util.cpp:243
Botan::HTTP::Response
Definition: http_util.h:39
final
int(* final)(unsigned char *, CTX *)
Definition: commoncrypto_hash.cpp:28
Botan::HTTP::HTTP_Error::HTTP_Error
HTTP_Error(const std::string &msg)
Definition: http_util.h:31
BOTAN_PUBLIC_API
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:31
std
Definition: bigint.h:1135
Botan::HTTP::http_exch_fn
std::function< std::string(const std::string &, const std::string &)> http_exch_fn
Definition: http_util.h:75
Botan::ErrorType
ErrorType
Definition: exceptn.h:20
Botan
Definition: alg_id.cpp:13
Botan::HTTP::Response::headers
const std::map< std::string, std::string > & headers() const
Definition: http_util.h:56
Botan::HTTP::Response::status_code
unsigned int status_code() const
Definition: http_util.h:52
Botan::HTTP::http_sync
Response http_sync(http_exch_fn http_transact, const std::string &verb, const std::string &url, const std::string &content_type, const std::vector< uint8_t > &body, size_t allowable_redirects)
Definition: http_util.cpp:104
Botan::HTTP::HTTP_Error::error_type
ErrorType error_type() const noexceptoverride
Definition: http_util.h:35
Botan::HTTP::url_encode
std::string url_encode(const std::string &in)
Definition: http_util.cpp:73
Botan::HTTP::HTTP_Error
Definition: http_util.h:28
Botan::HTTP::Response::Response
Response()
Definition: http_util.h:42
BOTAN_FUTURE_INTERNAL_HEADER
#define BOTAN_FUTURE_INTERNAL_HEADER(hdr)
Definition: compiler.h:139