blob: 5ce434b9218154acb57bc2d3d9694ebc02488749 [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>
Ed Tanous39662a32023-02-06 15:09:46 -080011#include <boost/url/url.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;
Ed Tanous50b8a432022-02-03 16:29:50 -080023
Ed Tanous39662a32023-02-06 15:09:46 -080024 private:
25 boost::urls::url urlBase{};
26
27 public:
Ed Tanous1abe55e2018-09-05 08:30:59 -070028 bool isSecure{false};
Ed Tanous7045c8d2017-04-03 10:04:37 -070029
Ed Tanous8f626352018-12-19 14:51:54 -080030 boost::asio::io_context* ioService{};
Sunitha Harishc0ea7ae2020-10-30 02:37:30 -050031 boost::asio::ip::address ipAddress{};
Ed Tanous7045c8d2017-04-03 10:04:37 -070032
Ed Tanous52cc1122020-07-18 13:51:21 -070033 std::shared_ptr<persistent_data::UserSession> session;
Ratan Gupta6f359562019-04-03 10:39:08 +053034
Gunnar Mills59ba6382023-08-01 12:41:17 -050035 std::string userRole{};
Ed Tanousf79b7a52021-09-22 19:04:29 -070036 Request(boost::beast::http::request<boost::beast::http::string_body> reqIn,
37 std::error_code& ec) :
Ed Tanous33c6b582023-02-14 15:05:48 -080038 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 Tanousf79b7a52021-09-22 19:04:29 -070047 {
48 if (!setUrlInfo())
49 {
50 ec = std::make_error_code(std::errc::invalid_argument);
51 }
52 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070053
Ed Tanousfca2cbe2021-01-28 14:49:59 -080054 Request() = default;
55
Ed Tanous39662a32023-02-06 15:09:46 -080056 Request(const Request& other) = default;
57 Request(Request&& other) = default;
Ed Tanous2d6cb562022-07-07 20:44:54 -070058
Ed Tanous597d2b12021-09-16 15:07:53 -070059 Request& operator=(const Request&) = delete;
Ed Tanousecd6a3a2022-01-07 09:18:40 -080060 Request& operator=(const Request&&) = delete;
61 ~Request() = default;
Ed Tanous597d2b12021-09-16 15:07:53 -070062
Ed Tanous271584a2019-07-09 16:24:22 -070063 boost::beast::http::verb method() const
Ed Tanous1abe55e2018-09-05 08:30:59 -070064 {
65 return req.method();
66 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070067
Ed Tanous3174e4d2020-10-07 11:41:22 -070068 std::string_view getHeaderValue(std::string_view key) const
Ed Tanous1abe55e2018-09-05 08:30:59 -070069 {
70 return req[key];
71 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070072
Ed Tanous3174e4d2020-10-07 11:41:22 -070073 std::string_view getHeaderValue(boost::beast::http::field key) const
Ed Tanous1abe55e2018-09-05 08:30:59 -070074 {
75 return req[key];
76 }
Ed Tanouse0d918b2018-03-27 17:41:04 -070077
Ed Tanous3174e4d2020-10-07 11:41:22 -070078 std::string_view methodString() const
Ed Tanous1abe55e2018-09-05 08:30:59 -070079 {
80 return req.method_string();
81 }
Ed Tanouse0d918b2018-03-27 17:41:04 -070082
Ed Tanous3174e4d2020-10-07 11:41:22 -070083 std::string_view target() const
Ed Tanous1abe55e2018-09-05 08:30:59 -070084 {
85 return req.target();
86 }
Ed Tanouse0d918b2018-03-27 17:41:04 -070087
Ed Tanous39662a32023-02-06 15:09:46 -080088 boost::urls::url_view url() const
89 {
90 return {urlBase};
91 }
92
Ed Tanous98fe7402023-02-14 14:50:33 -080093 const boost::beast::http::fields& fields() const
94 {
95 return req.base();
96 }
97
Ed Tanous33c6b582023-02-14 15:05:48 -080098 const std::string& body() const
99 {
100 return req.body();
101 }
102
Ed Tanous26ccae32023-02-16 10:28:44 -0800103 bool target(std::string_view target)
Ed Tanousf79b7a52021-09-22 19:04:29 -0700104 {
105 req.target(target);
106 return setUrlInfo();
107 }
108
Ed Tanous3174e4d2020-10-07 11:41:22 -0700109 unsigned version() const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700110 {
111 return req.version();
112 }
Ed Tanouse0d918b2018-03-27 17:41:04 -0700113
Ed Tanous3174e4d2020-10-07 11:41:22 -0700114 bool isUpgrade() const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700115 {
116 return boost::beast::websocket::is_upgrade(req);
117 }
Ed Tanouse0d918b2018-03-27 17:41:04 -0700118
Ed Tanous3174e4d2020-10-07 11:41:22 -0700119 bool keepAlive() const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700120 {
121 return req.keep_alive();
122 }
Ed Tanousf79b7a52021-09-22 19:04:29 -0700123
124 private:
Ed Tanous67df0732021-10-26 11:23:56 -0700125 bool setUrlInfo()
126 {
Ed Tanous079360a2022-06-29 10:05:19 -0700127 auto result = boost::urls::parse_relative_ref(target());
Ed Tanous67df0732021-10-26 11:23:56 -0700128
129 if (!result)
130 {
131 return false;
132 }
Ed Tanous39662a32023-02-06 15:09:46 -0800133 urlBase = *result;
Ed Tanous67df0732021-10-26 11:23:56 -0700134 return true;
135 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700136};
Ed Tanouse0d918b2018-03-27 17:41:04 -0700137
Ed Tanous1abe55e2018-09-05 08:30:59 -0700138} // namespace crow