blob: ee940d658bd5c486e521fb9facaab665828fd05d [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"
Ed Tanousb2896142024-01-31 15:25:47 -08004#include "http_body.hpp"
Ratan Gupta6f359562019-04-03 10:39:08 +05305#include "sessions.hpp"
6
Ed Tanous8f626352018-12-19 14:51:54 -08007#include <boost/asio/io_context.hpp>
Sunitha Harishc0ea7ae2020-10-30 02:37:30 -05008#include <boost/asio/ip/address.hpp>
Ed Tanousd43cd0c2020-09-30 20:46:53 -07009#include <boost/beast/http/message.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{
Ed Tanousb2896142024-01-31 15:25:47 -080022 boost::beast::http::request<bmcweb::HttpBody> 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 Tanousb2896142024-01-31 15:25:47 -080036 Request(boost::beast::http::request<bmcweb::HttpBody> reqIn,
Ed Tanousf79b7a52021-09-22 19:04:29 -070037 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
Ed Tanous818db202023-09-11 09:58:39 -070046 Request(std::string_view bodyIn, std::error_code& /*ec*/) : req({}, bodyIn)
47 {}
Ed Tanous7045c8d2017-04-03 10:04:37 -070048
Ed Tanousfca2cbe2021-01-28 14:49:59 -080049 Request() = default;
50
Ed Tanous39662a32023-02-06 15:09:46 -080051 Request(const Request& other) = default;
52 Request(Request&& other) = default;
Ed Tanous2d6cb562022-07-07 20:44:54 -070053
Ed Tanousf42e8592023-08-25 10:47:44 -070054 Request& operator=(const Request&) = default;
55 Request& operator=(Request&&) = default;
Ed Tanousecd6a3a2022-01-07 09:18:40 -080056 ~Request() = default;
Ed Tanous597d2b12021-09-16 15:07:53 -070057
Ed Tanous6fb96ce2024-01-28 21:30:06 -080058 void addHeader(std::string_view key, std::string_view value)
59 {
60 req.insert(key, value);
61 }
62
63 void addHeader(boost::beast::http::field key, std::string_view value)
64 {
65 req.insert(key, value);
66 }
67
Ed Tanous52e31622024-01-23 16:31:11 -080068 void clear()
69 {
70 req.clear();
71 urlBase.clear();
72 isSecure = false;
73 ioService = nullptr;
74 ipAddress = boost::asio::ip::address();
75 session = nullptr;
76 userRole = "";
77 }
78
Ed Tanous271584a2019-07-09 16:24:22 -070079 boost::beast::http::verb method() const
Ed Tanous1abe55e2018-09-05 08:30:59 -070080 {
81 return req.method();
82 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070083
Ed Tanous3174e4d2020-10-07 11:41:22 -070084 std::string_view getHeaderValue(std::string_view key) const
Ed Tanous1abe55e2018-09-05 08:30:59 -070085 {
86 return req[key];
87 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070088
Ed Tanous3174e4d2020-10-07 11:41:22 -070089 std::string_view getHeaderValue(boost::beast::http::field key) const
Ed Tanous1abe55e2018-09-05 08:30:59 -070090 {
91 return req[key];
92 }
Ed Tanouse0d918b2018-03-27 17:41:04 -070093
Ed Tanous3174e4d2020-10-07 11:41:22 -070094 std::string_view methodString() const
Ed Tanous1abe55e2018-09-05 08:30:59 -070095 {
96 return req.method_string();
97 }
Ed Tanouse0d918b2018-03-27 17:41:04 -070098
Ed Tanous3174e4d2020-10-07 11:41:22 -070099 std::string_view target() const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700100 {
101 return req.target();
102 }
Ed Tanouse0d918b2018-03-27 17:41:04 -0700103
Ed Tanous39662a32023-02-06 15:09:46 -0800104 boost::urls::url_view url() const
105 {
106 return {urlBase};
107 }
108
Ed Tanous98fe7402023-02-14 14:50:33 -0800109 const boost::beast::http::fields& fields() const
110 {
111 return req.base();
112 }
113
Ed Tanous33c6b582023-02-14 15:05:48 -0800114 const std::string& body() const
115 {
Ed Tanous52e31622024-01-23 16:31:11 -0800116 return req.body().str();
Ed Tanous33c6b582023-02-14 15:05:48 -0800117 }
118
Ed Tanous26ccae32023-02-16 10:28:44 -0800119 bool target(std::string_view target)
Ed Tanousf79b7a52021-09-22 19:04:29 -0700120 {
121 req.target(target);
122 return setUrlInfo();
123 }
124
Ed Tanous3174e4d2020-10-07 11:41:22 -0700125 unsigned version() const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700126 {
127 return req.version();
128 }
Ed Tanouse0d918b2018-03-27 17:41:04 -0700129
Ed Tanous3174e4d2020-10-07 11:41:22 -0700130 bool isUpgrade() const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700131 {
132 return boost::beast::websocket::is_upgrade(req);
133 }
Ed Tanouse0d918b2018-03-27 17:41:04 -0700134
Ed Tanous3174e4d2020-10-07 11:41:22 -0700135 bool keepAlive() const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700136 {
137 return req.keep_alive();
138 }
Ed Tanousf79b7a52021-09-22 19:04:29 -0700139
140 private:
Ed Tanous67df0732021-10-26 11:23:56 -0700141 bool setUrlInfo()
142 {
Ed Tanous079360a2022-06-29 10:05:19 -0700143 auto result = boost::urls::parse_relative_ref(target());
Ed Tanous67df0732021-10-26 11:23:56 -0700144
145 if (!result)
146 {
147 return false;
148 }
Ed Tanous39662a32023-02-06 15:09:46 -0800149 urlBase = *result;
Ed Tanous67df0732021-10-26 11:23:56 -0700150 return true;
151 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700152};
Ed Tanouse0d918b2018-03-27 17:41:04 -0700153
Ed Tanous1abe55e2018-09-05 08:30:59 -0700154} // namespace crow