/src/botan/build/include/botan/internal/http_util.h
Line | Count | Source (jump to first uncovered line) |
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 | | namespace Botan { |
20 | | |
21 | | namespace HTTP { |
22 | | |
23 | | /** |
24 | | * HTTP_Error Exception |
25 | | */ |
26 | | class HTTP_Error final : public Exception |
27 | | { |
28 | | public: |
29 | | explicit HTTP_Error(const std::string& msg) : |
30 | | Exception("HTTP error " + msg) |
31 | 0 | {} |
32 | | |
33 | 0 | ErrorType error_type() const noexcept override { return ErrorType::HttpError; } |
34 | | |
35 | | }; |
36 | | |
37 | | class Response final |
38 | | { |
39 | | public: |
40 | 0 | Response() : m_status_code(0), m_status_message("Uninitialized") {} |
41 | | |
42 | | Response(unsigned int status_code, const std::string& status_message, |
43 | | const std::vector<uint8_t>& body, |
44 | | const std::map<std::string, std::string>& headers) : |
45 | | m_status_code(status_code), |
46 | | m_status_message(status_message), |
47 | | m_body(body), |
48 | 0 | m_headers(headers) {} |
49 | | |
50 | 0 | unsigned int status_code() const { return m_status_code; } |
51 | | |
52 | 0 | const std::vector<uint8_t>& body() const { return m_body; } |
53 | | |
54 | 0 | const std::map<std::string, std::string>& headers() const { return m_headers; } |
55 | | |
56 | 0 | std::string status_message() const { return m_status_message; } |
57 | | |
58 | | void throw_unless_ok() |
59 | 0 | { |
60 | 0 | if(status_code() != 200) |
61 | 0 | throw HTTP_Error(status_message()); |
62 | 0 | } |
63 | | |
64 | | private: |
65 | | unsigned int m_status_code; |
66 | | std::string m_status_message; |
67 | | std::vector<uint8_t> m_body; |
68 | | std::map<std::string, std::string> m_headers; |
69 | | }; |
70 | | |
71 | | BOTAN_TEST_API std::ostream& operator<<(std::ostream& o, const Response& resp); |
72 | | |
73 | | typedef std::function<std::string (const std::string&, const std::string&, const std::string&)> http_exch_fn; |
74 | | |
75 | | Response http_sync(const http_exch_fn& fn, |
76 | | const std::string& verb, |
77 | | const std::string& url, |
78 | | const std::string& content_type, |
79 | | const std::vector<uint8_t>& body, |
80 | | size_t allowable_redirects); |
81 | | |
82 | | Response http_sync(const std::string& verb, |
83 | | const std::string& url, |
84 | | const std::string& content_type, |
85 | | const std::vector<uint8_t>& body, |
86 | | size_t allowable_redirects, |
87 | | std::chrono::milliseconds timeout = std::chrono::milliseconds(3000)); |
88 | | |
89 | | Response BOTAN_TEST_API GET_sync(const std::string& url, |
90 | | size_t allowable_redirects = 1, |
91 | | std::chrono::milliseconds timeout = std::chrono::milliseconds(3000)); |
92 | | |
93 | | Response POST_sync(const std::string& url, |
94 | | const std::string& content_type, |
95 | | const std::vector<uint8_t>& body, |
96 | | size_t allowable_redirects = 1, |
97 | | std::chrono::milliseconds timeout = std::chrono::milliseconds(3000)); |
98 | | |
99 | | std::string url_encode(const std::string& url); |
100 | | |
101 | | } |
102 | | |
103 | | } |
104 | | |
105 | | #endif |