blob: cb7b71bfa21b75043f8fd78808fe47527bceb7e3 [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 Tanous597d2b12021-09-16 15:07:53 -070048 Request(const Request&) = delete;
Ed Tanousecd6a3a2022-01-07 09:18:40 -080049 Request(const Request&&) = delete;
Ed Tanous597d2b12021-09-16 15:07:53 -070050 Request& operator=(const Request&) = delete;
Ed Tanousecd6a3a2022-01-07 09:18:40 -080051 Request& operator=(const Request&&) = delete;
52 ~Request() = default;
Ed Tanous597d2b12021-09-16 15:07:53 -070053
Ed Tanous271584a2019-07-09 16:24:22 -070054 boost::beast::http::verb method() const
Ed Tanous1abe55e2018-09-05 08:30:59 -070055 {
56 return req.method();
57 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070058
Ed Tanous3174e4d2020-10-07 11:41:22 -070059 std::string_view getHeaderValue(std::string_view key) const
Ed Tanous1abe55e2018-09-05 08:30:59 -070060 {
61 return req[key];
62 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070063
Ed Tanous3174e4d2020-10-07 11:41:22 -070064 std::string_view getHeaderValue(boost::beast::http::field key) const
Ed Tanous1abe55e2018-09-05 08:30:59 -070065 {
66 return req[key];
67 }
Ed Tanouse0d918b2018-03-27 17:41:04 -070068
Ed Tanous3174e4d2020-10-07 11:41:22 -070069 std::string_view methodString() const
Ed Tanous1abe55e2018-09-05 08:30:59 -070070 {
71 return req.method_string();
72 }
Ed Tanouse0d918b2018-03-27 17:41:04 -070073
Ed Tanous3174e4d2020-10-07 11:41:22 -070074 std::string_view target() const
Ed Tanous1abe55e2018-09-05 08:30:59 -070075 {
76 return req.target();
77 }
Ed Tanouse0d918b2018-03-27 17:41:04 -070078
Ed Tanousf79b7a52021-09-22 19:04:29 -070079 bool target(const std::string_view target)
80 {
81 req.target(target);
82 return setUrlInfo();
83 }
84
Ed Tanous3174e4d2020-10-07 11:41:22 -070085 unsigned version() const
Ed Tanous1abe55e2018-09-05 08:30:59 -070086 {
87 return req.version();
88 }
Ed Tanouse0d918b2018-03-27 17:41:04 -070089
Ed Tanous3174e4d2020-10-07 11:41:22 -070090 bool isUpgrade() const
Ed Tanous1abe55e2018-09-05 08:30:59 -070091 {
92 return boost::beast::websocket::is_upgrade(req);
93 }
Ed Tanouse0d918b2018-03-27 17:41:04 -070094
Ed Tanous3174e4d2020-10-07 11:41:22 -070095 bool keepAlive() const
Ed Tanous1abe55e2018-09-05 08:30:59 -070096 {
97 return req.keep_alive();
98 }
Ed Tanousf79b7a52021-09-22 19:04:29 -070099
100 private:
Ed Tanous67df0732021-10-26 11:23:56 -0700101 bool setUrlInfo()
102 {
103 auto result = boost::urls::parse_relative_ref(
104 boost::urls::string_view(target().data(), target().size()));
105
106 if (!result)
107 {
108 return false;
109 }
110 urlView = *result;
111 url = std::string_view(urlView.encoded_path().data(),
112 urlView.encoded_path().size());
113 return true;
114 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700115};
Ed Tanouse0d918b2018-03-27 17:41:04 -0700116
Ed Tanous1abe55e2018-09-05 08:30:59 -0700117} // namespace crow