Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 1 | #pragma once |
2 | |||||
Ed Tanous | 04e438c | 2020-10-03 08:06:26 -0700 | [diff] [blame] | 3 | #include "common.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> |
9 | #include <boost/beast/http/string_body.hpp> | ||||
Manojkiran Eda | 4425044 | 2020-06-16 12:51:38 +0530 | [diff] [blame] | 10 | #include <boost/beast/websocket.hpp> |
Ed Tanous | 39662a3 | 2023-02-06 15:09:46 -0800 | [diff] [blame] | 11 | #include <boost/url/url.hpp> |
raviteja-b | 4722efe | 2020-02-03 12:23:18 -0600 | [diff] [blame] | 12 | |
Ed Tanous | 8a9a25c | 2021-05-11 14:50:58 -0700 | [diff] [blame] | 13 | #include <string> |
14 | #include <string_view> | ||||
Ed Tanous | f79b7a5 | 2021-09-22 19:04:29 -0700 | [diff] [blame] | 15 | #include <system_error> |
Ed Tanous | 8a9a25c | 2021-05-11 14:50:58 -0700 | [diff] [blame] | 16 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 17 | namespace crow |
18 | { | ||||
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 19 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 20 | struct Request |
21 | { | ||||
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 22 | boost::beast::http::request<boost::beast::http::string_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: |
25 | boost::urls::url urlBase{}; | ||||
26 | |||||
27 | public: | ||||
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 28 | bool isSecure{false}; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 29 | |
Ed Tanous | 8f62635 | 2018-12-19 14:51:54 -0800 | [diff] [blame] | 30 | boost::asio::io_context* ioService{}; |
Sunitha Harish | c0ea7ae | 2020-10-30 02:37:30 -0500 | [diff] [blame] | 31 | boost::asio::ip::address ipAddress{}; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 32 | |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 33 | std::shared_ptr<persistent_data::UserSession> session; |
Ratan Gupta | 6f35956 | 2019-04-03 10:39:08 +0530 | [diff] [blame] | 34 | |
Gunnar Mills | 59ba638 | 2023-08-01 12:41:17 -0500 | [diff] [blame^] | 35 | std::string userRole{}; |
Ed Tanous | f79b7a5 | 2021-09-22 19:04:29 -0700 | [diff] [blame] | 36 | Request(boost::beast::http::request<boost::beast::http::string_body> reqIn, |
37 | std::error_code& ec) : | ||||
Ed Tanous | 33c6b58 | 2023-02-14 15:05:48 -0800 | [diff] [blame] | 38 | req(std::move(reqIn)) |
39 | { | ||||
40 | if (!setUrlInfo()) | ||||
41 | { | ||||
42 | ec = std::make_error_code(std::errc::invalid_argument); | ||||
43 | } | ||||
44 | } | ||||
45 | |||||
46 | Request(std::string_view bodyIn, std::error_code& ec) : req({}, bodyIn) | ||||
Ed Tanous | f79b7a5 | 2021-09-22 19:04:29 -0700 | [diff] [blame] | 47 | { |
48 | if (!setUrlInfo()) | ||||
49 | { | ||||
50 | ec = std::make_error_code(std::errc::invalid_argument); | ||||
51 | } | ||||
52 | } | ||||
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 53 | |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 54 | Request() = default; |
55 | |||||
Ed Tanous | 39662a3 | 2023-02-06 15:09:46 -0800 | [diff] [blame] | 56 | Request(const Request& other) = default; |
57 | Request(Request&& other) = default; | ||||
Ed Tanous | 2d6cb56 | 2022-07-07 20:44:54 -0700 | [diff] [blame] | 58 | |
Ed Tanous | 597d2b1 | 2021-09-16 15:07:53 -0700 | [diff] [blame] | 59 | Request& operator=(const Request&) = delete; |
Ed Tanous | ecd6a3a | 2022-01-07 09:18:40 -0800 | [diff] [blame] | 60 | Request& operator=(const Request&&) = delete; |
61 | ~Request() = default; | ||||
Ed Tanous | 597d2b1 | 2021-09-16 15:07:53 -0700 | [diff] [blame] | 62 | |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 63 | boost::beast::http::verb method() const |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 64 | { |
65 | return req.method(); | ||||
66 | } | ||||
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 67 | |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 68 | std::string_view getHeaderValue(std::string_view key) const |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 69 | { |
70 | return req[key]; | ||||
71 | } | ||||
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 72 | |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 73 | std::string_view getHeaderValue(boost::beast::http::field key) const |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 74 | { |
75 | return req[key]; | ||||
76 | } | ||||
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 77 | |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 78 | std::string_view methodString() const |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 79 | { |
80 | return req.method_string(); | ||||
81 | } | ||||
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 82 | |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 83 | std::string_view target() const |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 84 | { |
85 | return req.target(); | ||||
86 | } | ||||
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 87 | |
Ed Tanous | 39662a3 | 2023-02-06 15:09:46 -0800 | [diff] [blame] | 88 | boost::urls::url_view url() const |
89 | { | ||||
90 | return {urlBase}; | ||||
91 | } | ||||
92 | |||||
Ed Tanous | 98fe740 | 2023-02-14 14:50:33 -0800 | [diff] [blame] | 93 | const boost::beast::http::fields& fields() const |
94 | { | ||||
95 | return req.base(); | ||||
96 | } | ||||
97 | |||||
Ed Tanous | 33c6b58 | 2023-02-14 15:05:48 -0800 | [diff] [blame] | 98 | const std::string& body() const |
99 | { | ||||
100 | return req.body(); | ||||
101 | } | ||||
102 | |||||
Ed Tanous | 26ccae3 | 2023-02-16 10:28:44 -0800 | [diff] [blame] | 103 | bool target(std::string_view target) |
Ed Tanous | f79b7a5 | 2021-09-22 19:04:29 -0700 | [diff] [blame] | 104 | { |
105 | req.target(target); | ||||
106 | return setUrlInfo(); | ||||
107 | } | ||||
108 | |||||
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 109 | unsigned version() const |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 110 | { |
111 | return req.version(); | ||||
112 | } | ||||
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 113 | |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 114 | bool isUpgrade() const |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 115 | { |
116 | return boost::beast::websocket::is_upgrade(req); | ||||
117 | } | ||||
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 118 | |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 119 | bool keepAlive() const |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 120 | { |
121 | return req.keep_alive(); | ||||
122 | } | ||||
Ed Tanous | f79b7a5 | 2021-09-22 19:04:29 -0700 | [diff] [blame] | 123 | |
124 | private: | ||||
Ed Tanous | 67df073 | 2021-10-26 11:23:56 -0700 | [diff] [blame] | 125 | bool setUrlInfo() |
126 | { | ||||
Ed Tanous | 079360a | 2022-06-29 10:05:19 -0700 | [diff] [blame] | 127 | auto result = boost::urls::parse_relative_ref(target()); |
Ed Tanous | 67df073 | 2021-10-26 11:23:56 -0700 | [diff] [blame] | 128 | |
129 | if (!result) | ||||
130 | { | ||||
131 | return false; | ||||
132 | } | ||||
Ed Tanous | 39662a3 | 2023-02-06 15:09:46 -0800 | [diff] [blame] | 133 | urlBase = *result; |
Ed Tanous | 67df073 | 2021-10-26 11:23:56 -0700 | [diff] [blame] | 134 | return true; |
135 | } | ||||
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 136 | }; |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 137 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 138 | } // namespace crow |