Botan
1.11.15
|
00001 /* 00002 * HTTP utilities 00003 * (C) 2013 Jack Lloyd 00004 * 00005 * Botan is released under the Simplified BSD License (see license.txt) 00006 */ 00007 00008 #ifndef BOTAN_UTILS_URLGET_H__ 00009 #define BOTAN_UTILS_URLGET_H__ 00010 00011 #include <botan/types.h> 00012 #include <future> 00013 #include <vector> 00014 #include <map> 00015 #include <chrono> 00016 #include <string> 00017 00018 namespace Botan { 00019 00020 namespace HTTP { 00021 00022 struct Response 00023 { 00024 public: 00025 Response() : m_status_code(0), m_status_message("Uninitialized") {} 00026 00027 Response(unsigned int status_code, const std::string& status_message, 00028 const std::vector<byte>& body, 00029 const std::map<std::string, std::string>& headers) : 00030 m_status_code(status_code), 00031 m_status_message(status_message), 00032 m_body(body), 00033 m_headers(headers) {} 00034 00035 unsigned int status_code() const { return m_status_code; } 00036 00037 const std::vector<byte>& body() const { return m_body; } 00038 00039 const std::map<std::string, std::string>& headers() const { return m_headers; } 00040 00041 std::string status_message() const { return m_status_message; } 00042 00043 void throw_unless_ok() 00044 { 00045 if(status_code() != 200) 00046 throw std::runtime_error("HTTP error: " + status_message()); 00047 } 00048 00049 private: 00050 unsigned int m_status_code; 00051 std::string m_status_message; 00052 std::vector<byte> m_body; 00053 std::map<std::string, std::string> m_headers; 00054 }; 00055 00056 BOTAN_DLL std::ostream& operator<<(std::ostream& o, const Response& resp); 00057 00058 typedef std::function<std::string (const std::string&, const std::string&)> http_exch_fn; 00059 00060 #if defined(BOTAN_HAS_BOOST_ASIO) 00061 std::string BOTAN_DLL http_transact_asio(const std::string& hostname, 00062 const std::string& message); 00063 #endif 00064 00065 std::string BOTAN_DLL http_transact_fail(const std::string& hostname, 00066 const std::string& message); 00067 00068 00069 BOTAN_DLL Response http_sync(http_exch_fn fn, 00070 const std::string& verb, 00071 const std::string& url, 00072 const std::string& content_type, 00073 const std::vector<byte>& body, 00074 size_t allowable_redirects); 00075 00076 BOTAN_DLL Response http_sync(const std::string& verb, 00077 const std::string& url, 00078 const std::string& content_type, 00079 const std::vector<byte>& body, 00080 size_t allowable_redirects); 00081 00082 BOTAN_DLL Response GET_sync(const std::string& url, 00083 size_t allowable_redirects = 1); 00084 00085 BOTAN_DLL Response POST_sync(const std::string& url, 00086 const std::string& content_type, 00087 const std::vector<byte>& body, 00088 size_t allowable_redirects = 1); 00089 00090 std::future<Response> BOTAN_DLL GET_async(const std::string& url, 00091 size_t allowable_redirects = 1); 00092 00093 BOTAN_DLL std::string url_encode(const std::string& url); 00094 00095 } 00096 00097 } 00098 00099 #endif