Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 1 | #pragma once |
2 | |||||
Ed Tanous | b289614 | 2024-01-31 15:25:47 -0800 | [diff] [blame] | 3 | #include "http_body.hpp" |
Ratan Gupta | 6f35956 | 2019-04-03 10:39:08 +0530 | [diff] [blame] | 4 | #include "sessions.hpp" |
5 | |||||
Ed Tanous | 8f62635 | 2018-12-19 14:51:54 -0800 | [diff] [blame] | 6 | #include <boost/asio/io_context.hpp> |
Sunitha Harish | c0ea7ae | 2020-10-30 02:37:30 -0500 | [diff] [blame] | 7 | #include <boost/asio/ip/address.hpp> |
Ed Tanous | d43cd0c | 2020-09-30 20:46:53 -0700 | [diff] [blame] | 8 | #include <boost/beast/http/message.hpp> |
Manojkiran Eda | 4425044 | 2020-06-16 12:51:38 +0530 | [diff] [blame] | 9 | #include <boost/beast/websocket.hpp> |
Ed Tanous | 39662a3 | 2023-02-06 15:09:46 -0800 | [diff] [blame] | 10 | #include <boost/url/url.hpp> |
raviteja-b | 4722efe | 2020-02-03 12:23:18 -0600 | [diff] [blame] | 11 | |
Ed Tanous | 8a9a25c | 2021-05-11 14:50:58 -0700 | [diff] [blame] | 12 | #include <string> |
13 | #include <string_view> | ||||
Ed Tanous | f79b7a5 | 2021-09-22 19:04:29 -0700 | [diff] [blame] | 14 | #include <system_error> |
Ed Tanous | 8a9a25c | 2021-05-11 14:50:58 -0700 | [diff] [blame] | 15 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 16 | namespace crow |
17 | { | ||||
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 18 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 19 | struct Request |
20 | { | ||||
Jonathan Doman | 102a4cd | 2024-04-15 16:56:23 -0700 | [diff] [blame] | 21 | using Body = boost::beast::http::request<bmcweb::HttpBody>; |
22 | Body req; | ||||
Ed Tanous | 50b8a43 | 2022-02-03 16:29:50 -0800 | [diff] [blame] | 23 | |
Ed Tanous | 39662a3 | 2023-02-06 15:09:46 -0800 | [diff] [blame] | 24 | private: |
Ed Tanous | 47f2934 | 2024-03-19 12:18:06 -0700 | [diff] [blame] | 25 | boost::urls::url urlBase; |
Ed Tanous | 39662a3 | 2023-02-06 15:09:46 -0800 | [diff] [blame] | 26 | |
27 | public: | ||||
Ed Tanous | 47f2934 | 2024-03-19 12:18:06 -0700 | [diff] [blame] | 28 | boost::asio::io_context* ioService = nullptr; |
29 | boost::asio::ip::address ipAddress; | ||||
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 30 | |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 31 | std::shared_ptr<persistent_data::UserSession> session; |
Ratan Gupta | 6f35956 | 2019-04-03 10:39:08 +0530 | [diff] [blame] | 32 | |
Ed Tanous | 47f2934 | 2024-03-19 12:18:06 -0700 | [diff] [blame] | 33 | std::string userRole; |
Jonathan Doman | 102a4cd | 2024-04-15 16:56:23 -0700 | [diff] [blame] | 34 | Request(Body reqIn, std::error_code& ec) : req(std::move(reqIn)) |
Ed Tanous | 33c6b58 | 2023-02-14 15:05:48 -0800 | [diff] [blame] | 35 | { |
36 | if (!setUrlInfo()) | ||||
37 | { | ||||
38 | ec = std::make_error_code(std::errc::invalid_argument); | ||||
39 | } | ||||
40 | } | ||||
41 | |||||
Ed Tanous | 818db20 | 2023-09-11 09:58:39 -0700 | [diff] [blame] | 42 | Request(std::string_view bodyIn, std::error_code& /*ec*/) : req({}, bodyIn) |
43 | {} | ||||
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 44 | |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 45 | Request() = default; |
46 | |||||
Ed Tanous | 39662a3 | 2023-02-06 15:09:46 -0800 | [diff] [blame] | 47 | Request(const Request& other) = default; |
48 | Request(Request&& other) = default; | ||||
Ed Tanous | 2d6cb56 | 2022-07-07 20:44:54 -0700 | [diff] [blame] | 49 | |
Ed Tanous | f42e859 | 2023-08-25 10:47:44 -0700 | [diff] [blame] | 50 | Request& operator=(const Request&) = default; |
51 | Request& operator=(Request&&) = default; | ||||
Ed Tanous | ecd6a3a | 2022-01-07 09:18:40 -0800 | [diff] [blame] | 52 | ~Request() = default; |
Ed Tanous | 597d2b1 | 2021-09-16 15:07:53 -0700 | [diff] [blame] | 53 | |
Ed Tanous | 6fb96ce | 2024-01-28 21:30:06 -0800 | [diff] [blame] | 54 | void addHeader(std::string_view key, std::string_view value) |
55 | { | ||||
56 | req.insert(key, value); | ||||
57 | } | ||||
58 | |||||
59 | void addHeader(boost::beast::http::field key, std::string_view value) | ||||
60 | { | ||||
61 | req.insert(key, value); | ||||
62 | } | ||||
63 | |||||
Ed Tanous | 52e3162 | 2024-01-23 16:31:11 -0800 | [diff] [blame] | 64 | void clear() |
65 | { | ||||
66 | req.clear(); | ||||
67 | urlBase.clear(); | ||||
Ed Tanous | 52e3162 | 2024-01-23 16:31:11 -0800 | [diff] [blame] | 68 | ioService = nullptr; |
69 | ipAddress = boost::asio::ip::address(); | ||||
70 | session = nullptr; | ||||
71 | userRole = ""; | ||||
72 | } | ||||
73 | |||||
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 74 | boost::beast::http::verb method() const |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 75 | { |
76 | return req.method(); | ||||
77 | } | ||||
Ed Tanous | 50bfc91 | 2024-07-29 14:20:50 -0700 | [diff] [blame] | 78 | |
Myung Bae | 1873a04 | 2024-04-01 09:27:39 -0500 | [diff] [blame] | 79 | void method(boost::beast::http::verb verb) |
80 | { | ||||
Ed Tanous | 1919a03 | 2024-04-03 15:56:36 -0700 | [diff] [blame] | 81 | req.method(verb); |
Myung Bae | 1873a04 | 2024-04-01 09:27:39 -0500 | [diff] [blame] | 82 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 83 | |
Ed Tanous | 50bfc91 | 2024-07-29 14:20:50 -0700 | [diff] [blame] | 84 | std::string_view methodString() |
85 | { | ||||
86 | return req.method_string(); | ||||
87 | } | ||||
88 | |||||
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 89 | std::string_view getHeaderValue(std::string_view key) const |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 90 | { |
91 | return req[key]; | ||||
92 | } | ||||
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 93 | |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 94 | std::string_view getHeaderValue(boost::beast::http::field key) const |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 95 | { |
96 | return req[key]; | ||||
97 | } | ||||
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 98 | |
Myung Bae | 1873a04 | 2024-04-01 09:27:39 -0500 | [diff] [blame] | 99 | void clearHeader(boost::beast::http::field key) |
100 | { | ||||
101 | req.erase(key); | ||||
102 | } | ||||
103 | |||||
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 104 | std::string_view methodString() const |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 105 | { |
106 | return req.method_string(); | ||||
107 | } | ||||
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 108 | |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 109 | std::string_view target() const |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 110 | { |
111 | return req.target(); | ||||
112 | } | ||||
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 113 | |
Ed Tanous | 39662a3 | 2023-02-06 15:09:46 -0800 | [diff] [blame] | 114 | boost::urls::url_view url() const |
115 | { | ||||
116 | return {urlBase}; | ||||
117 | } | ||||
118 | |||||
Ed Tanous | 98fe740 | 2023-02-14 14:50:33 -0800 | [diff] [blame] | 119 | const boost::beast::http::fields& fields() const |
120 | { | ||||
121 | return req.base(); | ||||
122 | } | ||||
123 | |||||
Ed Tanous | 33c6b58 | 2023-02-14 15:05:48 -0800 | [diff] [blame] | 124 | const std::string& body() const |
125 | { | ||||
Ed Tanous | 52e3162 | 2024-01-23 16:31:11 -0800 | [diff] [blame] | 126 | return req.body().str(); |
Ed Tanous | 33c6b58 | 2023-02-14 15:05:48 -0800 | [diff] [blame] | 127 | } |
128 | |||||
Ed Tanous | 26ccae3 | 2023-02-16 10:28:44 -0800 | [diff] [blame] | 129 | bool target(std::string_view target) |
Ed Tanous | f79b7a5 | 2021-09-22 19:04:29 -0700 | [diff] [blame] | 130 | { |
131 | req.target(target); | ||||
132 | return setUrlInfo(); | ||||
133 | } | ||||
134 | |||||
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 135 | unsigned version() const |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 136 | { |
137 | return req.version(); | ||||
138 | } | ||||
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 139 | |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 140 | bool isUpgrade() const |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 141 | { |
142 | return boost::beast::websocket::is_upgrade(req); | ||||
143 | } | ||||
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 144 | |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 145 | bool keepAlive() const |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 146 | { |
147 | return req.keep_alive(); | ||||
148 | } | ||||
Ed Tanous | f79b7a5 | 2021-09-22 19:04:29 -0700 | [diff] [blame] | 149 | |
150 | private: | ||||
Ed Tanous | 67df073 | 2021-10-26 11:23:56 -0700 | [diff] [blame] | 151 | bool setUrlInfo() |
152 | { | ||||
Ed Tanous | 079360a | 2022-06-29 10:05:19 -0700 | [diff] [blame] | 153 | auto result = boost::urls::parse_relative_ref(target()); |
Ed Tanous | 67df073 | 2021-10-26 11:23:56 -0700 | [diff] [blame] | 154 | |
155 | if (!result) | ||||
156 | { | ||||
157 | return false; | ||||
158 | } | ||||
Ed Tanous | 39662a3 | 2023-02-06 15:09:46 -0800 | [diff] [blame] | 159 | urlBase = *result; |
Ed Tanous | 67df073 | 2021-10-26 11:23:56 -0700 | [diff] [blame] | 160 | return true; |
161 | } | ||||
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 162 | }; |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 163 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 164 | } // namespace crow |