summaryrefslogtreecommitdiffstats
path: root/comm/third_party/botan/src/lib/utils/http_util/http_util.h
blob: 7ad7c5828c3e50e360d27036e90691854333399e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
* HTTP utilities
* (C) 2013 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/

#ifndef BOTAN_UTILS_URLGET_H_
#define BOTAN_UTILS_URLGET_H_

#include <botan/types.h>
#include <botan/exceptn.h>
#include <vector>
#include <map>
#include <string>
#include <functional>
#include <chrono>

BOTAN_FUTURE_INTERNAL_HEADER(http_util.h)

namespace Botan {

namespace HTTP {

/**
* HTTP_Error Exception
*/
class BOTAN_PUBLIC_API(2,0) HTTP_Error final : public Exception
   {
   public:
      explicit HTTP_Error(const std::string& msg) :
         Exception("HTTP error " + msg)
         {}

      ErrorType error_type() const noexcept override { return ErrorType::HttpError; }

   };

class Response final
   {
   public:
      Response() : m_status_code(0), m_status_message("Uninitialized") {}

      Response(unsigned int status_code, const std::string& status_message,
               const std::vector<uint8_t>& body,
               const std::map<std::string, std::string>& headers) :
         m_status_code(status_code),
         m_status_message(status_message),
         m_body(body),
         m_headers(headers) {}

      unsigned int status_code() const { return m_status_code; }

      const std::vector<uint8_t>& body() const { return m_body; }

      const std::map<std::string, std::string>& headers() const { return m_headers; }

      std::string status_message() const { return m_status_message; }

      void throw_unless_ok()
         {
         if(status_code() != 200)
            throw HTTP_Error(status_message());
         }

   private:
      unsigned int m_status_code;
      std::string m_status_message;
      std::vector<uint8_t> m_body;
      std::map<std::string, std::string> m_headers;
   };

BOTAN_PUBLIC_API(2,0) std::ostream& operator<<(std::ostream& o, const Response& resp);

typedef std::function<std::string (const std::string&, const std::string&, const std::string&)> http_exch_fn;

BOTAN_PUBLIC_API(2,0) Response http_sync(http_exch_fn fn,
                                         const std::string& verb,
                                         const std::string& url,
                                         const std::string& content_type,
                                         const std::vector<uint8_t>& body,
                                         size_t allowable_redirects);

BOTAN_PUBLIC_API(2,0) Response http_sync(const std::string& verb,
                                         const std::string& url,
                                         const std::string& content_type,
                                         const std::vector<uint8_t>& body,
                                         size_t allowable_redirects,
                                         std::chrono::milliseconds timeout = std::chrono::milliseconds(3000));

BOTAN_PUBLIC_API(2,0) Response GET_sync(const std::string& url,
                                        size_t allowable_redirects = 1,
                                        std::chrono::milliseconds timeout = std::chrono::milliseconds(3000));

BOTAN_PUBLIC_API(2,0) Response POST_sync(const std::string& url,
                                         const std::string& content_type,
                                         const std::vector<uint8_t>& body,
                                         size_t allowable_redirects = 1,
                                         std::chrono::milliseconds timeout = std::chrono::milliseconds(3000));

BOTAN_PUBLIC_API(2,0) std::string url_encode(const std::string& url);

}

}

#endif