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