Botan  2.1.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 <chrono>
16 #include <string>
17 
18 namespace Botan {
19 
20 namespace HTTP {
21 
22 struct Response
23  {
24  public:
25  Response() : m_status_code(0), m_status_message("Uninitialized") {}
26 
27  Response(unsigned int status_code, const std::string& status_message,
28  const std::vector<uint8_t>& body,
29  const std::map<std::string, std::string>& headers) :
30  m_status_code(status_code),
31  m_status_message(status_message),
32  m_body(body),
33  m_headers(headers) {}
34 
35  unsigned int status_code() const { return m_status_code; }
36 
37  const std::vector<uint8_t>& body() const { return m_body; }
38 
39  const std::map<std::string, std::string>& headers() const { return m_headers; }
40 
41  std::string status_message() const { return m_status_message; }
42 
44  {
45  if(status_code() != 200)
46  throw Exception("HTTP error: " + status_message());
47  }
48 
49  private:
50  unsigned int m_status_code;
51  std::string m_status_message;
52  std::vector<uint8_t> m_body;
53  std::map<std::string, std::string> m_headers;
54  };
55 
56 /**
57 * HTTP_Error Exception
58 */
59 struct BOTAN_DLL HTTP_Error : public Exception
60  {
61  explicit HTTP_Error(const std::string& msg) :
62  Exception("HTTP error " + msg)
63  {}
64  };
65 
66 BOTAN_DLL std::ostream& operator<<(std::ostream& o, const Response& resp);
67 
68 typedef std::function<std::string (const std::string&, const std::string&)> http_exch_fn;
69 
70 BOTAN_DLL Response http_sync(http_exch_fn fn,
71  const std::string& verb,
72  const std::string& url,
73  const std::string& content_type,
74  const std::vector<uint8_t>& body,
75  size_t allowable_redirects);
76 
77 BOTAN_DLL Response http_sync(const std::string& verb,
78  const std::string& url,
79  const std::string& content_type,
80  const std::vector<uint8_t>& body,
81  size_t allowable_redirects);
82 
83 BOTAN_DLL Response GET_sync(const std::string& url,
84  size_t allowable_redirects = 1);
85 
86 BOTAN_DLL Response POST_sync(const std::string& url,
87  const std::string& content_type,
88  const std::vector<uint8_t>& body,
89  size_t allowable_redirects = 1);
90 
91 BOTAN_DLL std::string url_encode(const std::string& url);
92 
93 }
94 
95 }
96 
97 #endif
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:165
HTTP_Error(const std::string &msg)
Definition: http_util.h:61
const std::vector< uint8_t > & body() const
Definition: http_util.h:37
const std::map< std::string, std::string > & headers() const
Definition: http_util.h:39
unsigned int status_code() const
Definition: http_util.h:35
std::string url_encode(const std::string &in)
Definition: http_util.cpp:134
std::function< std::string(const std::string &, const std::string &)> http_exch_fn
Definition: http_util.h:68
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:27
std::string status_message() const
Definition: http_util.h:41
Response POST_sync(const std::string &url, const std::string &content_type, const std::vector< uint8_t > &body, size_t allowable_redirects)
Definition: http_util.cpp:294
Definition: alg_id.cpp:13
std::ostream & operator<<(std::ostream &o, const Response &resp)
Definition: http_util.cpp:155
Response GET_sync(const std::string &url, size_t allowable_redirects)
Definition: http_util.cpp:289