Botan 3.4.0
Crypto and TLS for C&
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/exceptn.h>
12#include <botan/types.h>
13#include <chrono>
14#include <functional>
15#include <map>
16#include <string>
17#include <vector>
18
19namespace Botan::HTTP {
20
21/**
22* HTTP_Error Exception
23*/
24class HTTP_Error final : public Exception {
25 public:
26 explicit HTTP_Error(const std::string& msg) : Exception("HTTP error " + msg) {}
27
28 ErrorType error_type() const noexcept override { return ErrorType::HttpError; }
29};
30
32 public:
33 Response() : m_status_code(0), m_status_message("Uninitialized") {}
34
35 Response(unsigned int status_code,
36 std::string_view status_message,
37 const std::vector<uint8_t>& body,
38 const std::map<std::string, std::string>& headers) :
39 m_status_code(status_code), m_status_message(status_message), m_body(body), m_headers(headers) {}
40
41 unsigned int status_code() const { return m_status_code; }
42
43 const std::vector<uint8_t>& body() const { return m_body; }
44
45 const std::map<std::string, std::string>& headers() const { return m_headers; }
46
47 std::string status_message() const { return m_status_message; }
48
49 void throw_unless_ok() const {
50 if(status_code() != 200) {
52 }
53 }
54
55 private:
56 unsigned int m_status_code;
57 std::string m_status_message;
58 std::vector<uint8_t> m_body;
59 std::map<std::string, std::string> m_headers;
60};
61
62BOTAN_TEST_API std::ostream& operator<<(std::ostream& o, const Response& resp);
63
64typedef std::function<std::string(std::string_view, std::string_view, std::string_view)> http_exch_fn;
65
67 std::string_view verb,
68 std::string_view url,
69 std::string_view content_type,
70 const std::vector<uint8_t>& body,
71 size_t allowable_redirects);
72
73Response http_sync(std::string_view verb,
74 std::string_view url,
75 std::string_view content_type,
76 const std::vector<uint8_t>& body,
77 size_t allowable_redirects,
78 std::chrono::milliseconds timeout = std::chrono::milliseconds(3000));
79
80Response BOTAN_TEST_API GET_sync(std::string_view url,
81 size_t allowable_redirects = 1,
82 std::chrono::milliseconds timeout = std::chrono::milliseconds(3000));
83
84Response POST_sync(std::string_view url,
85 std::string_view content_type,
86 const std::vector<uint8_t>& body,
87 size_t allowable_redirects = 1,
88 std::chrono::milliseconds timeout = std::chrono::milliseconds(3000));
89
90std::string url_encode(std::string_view url);
91
92} // namespace Botan::HTTP
93
94#endif
ErrorType error_type() const noexcept override
Definition http_util.h:28
HTTP_Error(const std::string &msg)
Definition http_util.h:26
const std::vector< uint8_t > & body() const
Definition http_util.h:43
const std::map< std::string, std::string > & headers() const
Definition http_util.h:45
Response(unsigned int status_code, std::string_view status_message, const std::vector< uint8_t > &body, const std::map< std::string, std::string > &headers)
Definition http_util.h:35
unsigned int status_code() const
Definition http_util.h:41
std::string status_message() const
Definition http_util.h:47
void throw_unless_ok() const
Definition http_util.h:49
int(* final)(unsigned char *, CTX *)
#define BOTAN_TEST_API
Definition compiler.h:51
std::function< std::string(std::string_view, std::string_view, std::string_view)> http_exch_fn
Definition http_util.h:64
Response POST_sync(std::string_view url, std::string_view content_type, const std::vector< uint8_t > &body, size_t allowable_redirects, std::chrono::milliseconds timeout)
Response http_sync(const http_exch_fn &http_transact, std::string_view verb, std::string_view url, std::string_view content_type, const std::vector< uint8_t > &body, size_t allowable_redirects)
std::string url_encode(std::string_view in)
Definition http_util.cpp:88
Response GET_sync(std::string_view url, size_t allowable_redirects, std::chrono::milliseconds timeout)
std::ostream & operator<<(std::ostream &o, const Response &resp)
ErrorType
Definition exceptn.h:20