blob: deaba31c410ff9b417f7fa49d44c79567b3aa73c [file] [log] [blame]
Ed Tanous7045c8d2017-04-03 10:04:37 -07001#pragma once
2
Ed Tanous04e438c2020-10-03 08:06:26 -07003#include "common.hpp"
Ratan Gupta6f359562019-04-03 10:39:08 +05304#include "sessions.hpp"
5
Ed Tanous8f626352018-12-19 14:51:54 -08006#include <boost/asio/io_context.hpp>
Sunitha Harishc0ea7ae2020-10-30 02:37:30 -05007#include <boost/asio/ip/address.hpp>
Ed Tanousd43cd0c2020-09-30 20:46:53 -07008#include <boost/beast/http/message.hpp>
9#include <boost/beast/http/string_body.hpp>
Manojkiran Eda44250442020-06-16 12:51:38 +053010#include <boost/beast/websocket.hpp>
James Feist5a7e8772020-07-22 09:08:38 -070011#include <boost/url/url_view.hpp>
raviteja-b4722efe2020-02-03 12:23:18 -060012
Ed Tanous8a9a25c2021-05-11 14:50:58 -070013#include <string>
14#include <string_view>
Ed Tanousf79b7a52021-09-22 19:04:29 -070015#include <system_error>
Ed Tanous8a9a25c2021-05-11 14:50:58 -070016
Ed Tanous1abe55e2018-09-05 08:30:59 -070017namespace crow
18{
Ed Tanous7045c8d2017-04-03 10:04:37 -070019
Ed Tanous1abe55e2018-09-05 08:30:59 -070020struct Request
21{
John Edward Broadbent59b98b22021-07-13 15:36:32 -070022 boost::beast::http::request<boost::beast::http::string_body> req;
James Feistfe306722020-03-12 16:32:08 -070023 boost::beast::http::fields& fields;
Ed Tanous39e77502019-03-04 17:35:53 -080024 std::string_view url{};
James Feist5a7e8772020-07-22 09:08:38 -070025 boost::urls::url_view urlView{};
Ed Tanous50b8a432022-02-03 16:29:50 -080026
Ed Tanous1abe55e2018-09-05 08:30:59 -070027 bool isSecure{false};
Ed Tanous7045c8d2017-04-03 10:04:37 -070028
Willy Tu15ed6782021-12-14 11:03:16 -080029 std::string& body;
Ed Tanouse0d918b2018-03-27 17:41:04 -070030
Ed Tanous8f626352018-12-19 14:51:54 -080031 boost::asio::io_context* ioService{};
Sunitha Harishc0ea7ae2020-10-30 02:37:30 -050032 boost::asio::ip::address ipAddress{};
Ed Tanous7045c8d2017-04-03 10:04:37 -070033
Ed Tanous52cc1122020-07-18 13:51:21 -070034 std::shared_ptr<persistent_data::UserSession> session;
Ratan Gupta6f359562019-04-03 10:39:08 +053035
RAJESWARAN THILLAIGOVINDAN61dbeef2019-12-13 04:26:54 -060036 std::string userRole{};
Ed Tanousf79b7a52021-09-22 19:04:29 -070037 Request(boost::beast::http::request<boost::beast::http::string_body> reqIn,
38 std::error_code& ec) :
John Edward Broadbent59b98b22021-07-13 15:36:32 -070039 req(std::move(reqIn)),
40 fields(req.base()), body(req.body())
Ed Tanousf79b7a52021-09-22 19:04:29 -070041 {
42 if (!setUrlInfo())
43 {
44 ec = std::make_error_code(std::errc::invalid_argument);
45 }
46 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070047
Ed Tanous2d6cb562022-07-07 20:44:54 -070048 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 Tanous597d2b12021-09-16 15:07:53 -070066 Request& operator=(const Request&) = delete;
Ed Tanousecd6a3a2022-01-07 09:18:40 -080067 Request& operator=(const Request&&) = delete;
68 ~Request() = default;
Ed Tanous597d2b12021-09-16 15:07:53 -070069
Ed Tanous271584a2019-07-09 16:24:22 -070070 boost::beast::http::verb method() const
Ed Tanous1abe55e2018-09-05 08:30:59 -070071 {
72 return req.method();
73 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070074
Ed Tanous3174e4d2020-10-07 11:41:22 -070075 std::string_view getHeaderValue(std::string_view key) const
Ed Tanous1abe55e2018-09-05 08:30:59 -070076 {
77 return req[key];
78 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070079
Ed Tanous3174e4d2020-10-07 11:41:22 -070080 std::string_view getHeaderValue(boost::beast::http::field key) const
Ed Tanous1abe55e2018-09-05 08:30:59 -070081 {
82 return req[key];
83 }
Ed Tanouse0d918b2018-03-27 17:41:04 -070084
Ed Tanous3174e4d2020-10-07 11:41:22 -070085 std::string_view methodString() const
Ed Tanous1abe55e2018-09-05 08:30:59 -070086 {
87 return req.method_string();
88 }
Ed Tanouse0d918b2018-03-27 17:41:04 -070089
Ed Tanous3174e4d2020-10-07 11:41:22 -070090 std::string_view target() const
Ed Tanous1abe55e2018-09-05 08:30:59 -070091 {
92 return req.target();
93 }
Ed Tanouse0d918b2018-03-27 17:41:04 -070094
Ed Tanousf79b7a52021-09-22 19:04:29 -070095 bool target(const std::string_view target)
96 {
97 req.target(target);
98 return setUrlInfo();
99 }
100
Ed Tanous3174e4d2020-10-07 11:41:22 -0700101 unsigned version() const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700102 {
103 return req.version();
104 }
Ed Tanouse0d918b2018-03-27 17:41:04 -0700105
Ed Tanous3174e4d2020-10-07 11:41:22 -0700106 bool isUpgrade() const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700107 {
108 return boost::beast::websocket::is_upgrade(req);
109 }
Ed Tanouse0d918b2018-03-27 17:41:04 -0700110
Ed Tanous3174e4d2020-10-07 11:41:22 -0700111 bool keepAlive() const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700112 {
113 return req.keep_alive();
114 }
Ed Tanousf79b7a52021-09-22 19:04:29 -0700115
116 private:
Ed Tanous67df0732021-10-26 11:23:56 -0700117 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 Tanous7045c8d2017-04-03 10:04:37 -0700131};
Ed Tanouse0d918b2018-03-27 17:41:04 -0700132
Ed Tanous1abe55e2018-09-05 08:30:59 -0700133} // namespace crow