Botan  2.7.0
Crypto and TLS for C++11
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 namespace Botan {
20 
21 namespace HTTP {
22 
23 class Response final
24  {
25  public:
26  Response() : m_status_code(0), m_status_message("Uninitialized") {}
27 
28  Response(unsigned int status_code, const std::string& status_message,
29  const std::vector<uint8_t>& body,
30  const std::map<std::string, std::string>& headers) :
31  m_status_code(status_code),
32  m_status_message(status_message),
33  m_body(body),
34  m_headers(headers) {}
35 
36  unsigned int status_code() const { return m_status_code; }
37 
38  const std::vector<uint8_t>& body() const { return m_body; }
39 
40  const std::map<std::string, std::string>& headers() const { return m_headers; }
41 
42  std::string status_message() const { return m_status_message; }
43 
45  {
46  if(status_code() != 200)
47  throw Exception("HTTP error: " + status_message());
48  }
49 
50  private:
51  unsigned int m_status_code;
52  std::string m_status_message;
53  std::vector<uint8_t> m_body;
54  std::map<std::string, std::string> m_headers;
55  };
56 
57 /**
58 * HTTP_Error Exception
59 */
60 class BOTAN_PUBLIC_API(2,0) HTTP_Error final : public Exception
61  {
62  public:
63  explicit HTTP_Error(const std::string& msg) :
64  Exception("HTTP error " + msg)
65  {}
66  };
67 
68 BOTAN_PUBLIC_API(2,0) std::ostream& operator<<(std::ostream& o, const Response& resp);
69 
70 typedef std::function<std::string (const std::string&, const std::string&)> http_exch_fn;
71 
73  const std::string& verb,
74  const std::string& url,
75  const std::string& content_type,
76  const std::vector<uint8_t>& body,
77  size_t allowable_redirects);
78 
79 BOTAN_PUBLIC_API(2,0) Response http_sync(const std::string& verb,
80  const std::string& url,
81  const std::string& content_type,
82  const std::vector<uint8_t>& body,
83  size_t allowable_redirects,
84  std::chrono::milliseconds timeout = std::chrono::milliseconds(3000));
85 
86 BOTAN_PUBLIC_API(2,0) Response GET_sync(const std::string& url,
87  size_t allowable_redirects = 1,
88  std::chrono::milliseconds timeout = std::chrono::milliseconds(3000));
89 
90 BOTAN_PUBLIC_API(2,0) Response POST_sync(const std::string& url,
91  const std::string& content_type,
92  const std::vector<uint8_t>& body,
93  size_t allowable_redirects = 1,
94  std::chrono::milliseconds timeout = std::chrono::milliseconds(3000));
95 
96 BOTAN_PUBLIC_API(2,0) std::string url_encode(const std::string& url);
97 
98 }
99 
100 }
101 
102 #endif
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:28
Response GET_sync(const std::string &url, size_t allowable_redirects, std::chrono::milliseconds timeout)
Definition: http_util.cpp:235
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:242
unsigned int status_code() const
Definition: http_util.h:36
HTTP_Error(const std::string &msg)
Definition: http_util.h:63
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:27
Definition: bigint.h:796
const std::map< std::string, std::string > & headers() const
Definition: http_util.h:40
const std::vector< uint8_t > & body() const
Definition: http_util.h:38
std::function< std::string(const std::string &, const std::string &)> http_exch_fn
Definition: http_util.h:70
Definition: alg_id.cpp:13
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
std::string url_encode(const std::string &in)
Definition: http_util.cpp:73
std::string status_message() const
Definition: http_util.h:42