blob: d1a6ac053a8992142ca4d46ff7c45a076dcae9e4 [file] [log] [blame]
Ed Tanous7045c8d2017-04-03 10:04:37 -07001#pragma once
2
Ed Tanousb2896142024-01-31 15:25:47 -08003#include "http_body.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>
Manojkiran Eda44250442020-06-16 12:51:38 +05309#include <boost/beast/websocket.hpp>
Ed Tanous39662a32023-02-06 15:09:46 -080010#include <boost/url/url.hpp>
raviteja-b4722efe2020-02-03 12:23:18 -060011
Ed Tanous8a9a25c2021-05-11 14:50:58 -070012#include <string>
13#include <string_view>
Ed Tanousf79b7a52021-09-22 19:04:29 -070014#include <system_error>
Ed Tanous8a9a25c2021-05-11 14:50:58 -070015
Ed Tanous1abe55e2018-09-05 08:30:59 -070016namespace crow
17{
Ed Tanous7045c8d2017-04-03 10:04:37 -070018
Ed Tanous1abe55e2018-09-05 08:30:59 -070019struct Request
20{
Ed Tanousb2896142024-01-31 15:25:47 -080021 boost::beast::http::request<bmcweb::HttpBody> req;
Ed Tanous50b8a432022-02-03 16:29:50 -080022
Ed Tanous39662a32023-02-06 15:09:46 -080023 private:
Ed Tanous47f29342024-03-19 12:18:06 -070024 boost::urls::url urlBase;
Ed Tanous39662a32023-02-06 15:09:46 -080025
26 public:
Ed Tanous1abe55e2018-09-05 08:30:59 -070027 bool isSecure{false};
Ed Tanous7045c8d2017-04-03 10:04:37 -070028
Ed Tanous47f29342024-03-19 12:18:06 -070029 boost::asio::io_context* ioService = nullptr;
30 boost::asio::ip::address ipAddress;
Ed Tanous7045c8d2017-04-03 10:04:37 -070031
Ed Tanous52cc1122020-07-18 13:51:21 -070032 std::shared_ptr<persistent_data::UserSession> session;
Ratan Gupta6f359562019-04-03 10:39:08 +053033
Ed Tanous47f29342024-03-19 12:18:06 -070034 std::string userRole;
Ed Tanousb2896142024-01-31 15:25:47 -080035 Request(boost::beast::http::request<bmcweb::HttpBody> reqIn,
Ed Tanousf79b7a52021-09-22 19:04:29 -070036 std::error_code& ec) :
Ed Tanous33c6b582023-02-14 15:05:48 -080037 req(std::move(reqIn))
38 {
39 if (!setUrlInfo())
40 {
41 ec = std::make_error_code(std::errc::invalid_argument);
42 }
43 }
44
Ed Tanous818db202023-09-11 09:58:39 -070045 Request(std::string_view bodyIn, std::error_code& /*ec*/) : req({}, bodyIn)
46 {}
Ed Tanous7045c8d2017-04-03 10:04:37 -070047
Ed Tanousfca2cbe2021-01-28 14:49:59 -080048 Request() = default;
49
Ed Tanous39662a32023-02-06 15:09:46 -080050 Request(const Request& other) = default;
51 Request(Request&& other) = default;
Ed Tanous2d6cb562022-07-07 20:44:54 -070052
Ed Tanousf42e8592023-08-25 10:47:44 -070053 Request& operator=(const Request&) = default;
54 Request& operator=(Request&&) = default;
Ed Tanousecd6a3a2022-01-07 09:18:40 -080055 ~Request() = default;
Ed Tanous597d2b12021-09-16 15:07:53 -070056
Ed Tanous6fb96ce2024-01-28 21:30:06 -080057 void addHeader(std::string_view key, std::string_view value)
58 {
59 req.insert(key, value);
60 }
61
62 void addHeader(boost::beast::http::field key, std::string_view value)
63 {
64 req.insert(key, value);
65 }
66
Ed Tanous52e31622024-01-23 16:31:11 -080067 void clear()
68 {
69 req.clear();
70 urlBase.clear();
71 isSecure = false;
72 ioService = nullptr;
73 ipAddress = boost::asio::ip::address();
74 session = nullptr;
75 userRole = "";
76 }
77
Ed Tanous271584a2019-07-09 16:24:22 -070078 boost::beast::http::verb method() const
Ed Tanous1abe55e2018-09-05 08:30:59 -070079 {
80 return req.method();
81 }
Myung Bae1873a042024-04-01 09:27:39 -050082 void method(boost::beast::http::verb verb)
83 {
Ed Tanous1919a032024-04-03 15:56:36 -070084 req.method(verb);
Myung Bae1873a042024-04-01 09:27:39 -050085 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070086
Ed Tanous3174e4d2020-10-07 11:41:22 -070087 std::string_view getHeaderValue(std::string_view key) const
Ed Tanous1abe55e2018-09-05 08:30:59 -070088 {
89 return req[key];
90 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070091
Ed Tanous3174e4d2020-10-07 11:41:22 -070092 std::string_view getHeaderValue(boost::beast::http::field key) const
Ed Tanous1abe55e2018-09-05 08:30:59 -070093 {
94 return req[key];
95 }
Ed Tanouse0d918b2018-03-27 17:41:04 -070096
Myung Bae1873a042024-04-01 09:27:39 -050097 void clearHeader(boost::beast::http::field key)
98 {
99 req.erase(key);
100 }
101
Ed Tanous3174e4d2020-10-07 11:41:22 -0700102 std::string_view methodString() const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700103 {
104 return req.method_string();
105 }
Ed Tanouse0d918b2018-03-27 17:41:04 -0700106
Ed Tanous3174e4d2020-10-07 11:41:22 -0700107 std::string_view target() const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700108 {
109 return req.target();
110 }
Ed Tanouse0d918b2018-03-27 17:41:04 -0700111
Ed Tanous39662a32023-02-06 15:09:46 -0800112 boost::urls::url_view url() const
113 {
114 return {urlBase};
115 }
116
Ed Tanous98fe7402023-02-14 14:50:33 -0800117 const boost::beast::http::fields& fields() const
118 {
119 return req.base();
120 }
121
Ed Tanous33c6b582023-02-14 15:05:48 -0800122 const std::string& body() const
123 {
Ed Tanous52e31622024-01-23 16:31:11 -0800124 return req.body().str();
Ed Tanous33c6b582023-02-14 15:05:48 -0800125 }
126
Ed Tanous26ccae32023-02-16 10:28:44 -0800127 bool target(std::string_view target)
Ed Tanousf79b7a52021-09-22 19:04:29 -0700128 {
129 req.target(target);
130 return setUrlInfo();
131 }
132
Ed Tanous3174e4d2020-10-07 11:41:22 -0700133 unsigned version() const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700134 {
135 return req.version();
136 }
Ed Tanouse0d918b2018-03-27 17:41:04 -0700137
Ed Tanous3174e4d2020-10-07 11:41:22 -0700138 bool isUpgrade() const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700139 {
140 return boost::beast::websocket::is_upgrade(req);
141 }
Ed Tanouse0d918b2018-03-27 17:41:04 -0700142
Ed Tanous3174e4d2020-10-07 11:41:22 -0700143 bool keepAlive() const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700144 {
145 return req.keep_alive();
146 }
Ed Tanousf79b7a52021-09-22 19:04:29 -0700147
148 private:
Ed Tanous67df0732021-10-26 11:23:56 -0700149 bool setUrlInfo()
150 {
Ed Tanous079360a2022-06-29 10:05:19 -0700151 auto result = boost::urls::parse_relative_ref(target());
Ed Tanous67df0732021-10-26 11:23:56 -0700152
153 if (!result)
154 {
155 return false;
156 }
Ed Tanous39662a32023-02-06 15:09:46 -0800157 urlBase = *result;
Ed Tanous67df0732021-10-26 11:23:56 -0700158 return true;
159 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700160};
Ed Tanouse0d918b2018-03-27 17:41:04 -0700161
Ed Tanous1abe55e2018-09-05 08:30:59 -0700162} // namespace crow