blob: c2778ca254d6b1c4e0096bea5ecb03e64215cba5 [file] [log] [blame]
Ed Tanous40e9b922024-09-10 13:50:16 -07001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright OpenBMC Authors
Ed Tanous7045c8d2017-04-03 10:04:37 -07003#pragma once
4
Ed Tanousb2896142024-01-31 15:25:47 -08005#include "http_body.hpp"
Ratan Gupta6f359562019-04-03 10:39:08 +05306#include "sessions.hpp"
7
Sunitha Harishc0ea7ae2020-10-30 02:37:30 -05008#include <boost/asio/ip/address.hpp>
Ed Tanousd7857202025-01-28 15:32:26 -08009#include <boost/beast/http/field.hpp>
10#include <boost/beast/http/fields.hpp>
Ed Tanousd43cd0c2020-09-30 20:46:53 -070011#include <boost/beast/http/message.hpp>
Ed Tanousd7857202025-01-28 15:32:26 -080012#include <boost/beast/http/verb.hpp>
13#include <boost/beast/websocket/rfc6455.hpp>
14#include <boost/url/parse.hpp>
Ed Tanous39662a32023-02-06 15:09:46 -080015#include <boost/url/url.hpp>
Ed Tanousd7857202025-01-28 15:32:26 -080016#include <boost/url/url_view.hpp>
raviteja-b4722efe2020-02-03 12:23:18 -060017
Ed Tanousd7857202025-01-28 15:32:26 -080018#include <memory>
Ed Tanous8a9a25c2021-05-11 14:50:58 -070019#include <string>
20#include <string_view>
Ed Tanousf79b7a52021-09-22 19:04:29 -070021#include <system_error>
Ed Tanousd7857202025-01-28 15:32:26 -080022#include <utility>
Ed Tanous8a9a25c2021-05-11 14:50:58 -070023
Ed Tanous1abe55e2018-09-05 08:30:59 -070024namespace crow
25{
Ed Tanous7045c8d2017-04-03 10:04:37 -070026
Ed Tanous1abe55e2018-09-05 08:30:59 -070027struct Request
28{
Jonathan Doman102a4cd2024-04-15 16:56:23 -070029 using Body = boost::beast::http::request<bmcweb::HttpBody>;
30 Body req;
Ed Tanous50b8a432022-02-03 16:29:50 -080031
Ed Tanous39662a32023-02-06 15:09:46 -080032 private:
Ed Tanous47f29342024-03-19 12:18:06 -070033 boost::urls::url urlBase;
Ed Tanous39662a32023-02-06 15:09:46 -080034
Ed Tanous608ad2c2024-05-20 19:14:50 -070035 Request(const Request& other) = default;
36
Ed Tanous39662a32023-02-06 15:09:46 -080037 public:
Ed Tanous47f29342024-03-19 12:18:06 -070038 boost::asio::ip::address ipAddress;
Ed Tanous7045c8d2017-04-03 10:04:37 -070039
Ed Tanous52cc1122020-07-18 13:51:21 -070040 std::shared_ptr<persistent_data::UserSession> session;
Ratan Gupta6f359562019-04-03 10:39:08 +053041
Ed Tanous47f29342024-03-19 12:18:06 -070042 std::string userRole;
Jonathan Doman102a4cd2024-04-15 16:56:23 -070043 Request(Body reqIn, std::error_code& ec) : req(std::move(reqIn))
Ed Tanous33c6b582023-02-14 15:05:48 -080044 {
45 if (!setUrlInfo())
46 {
47 ec = std::make_error_code(std::errc::invalid_argument);
48 }
49 }
50
Ed Tanous818db202023-09-11 09:58:39 -070051 Request(std::string_view bodyIn, std::error_code& /*ec*/) : req({}, bodyIn)
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(Request&& other) = default;
Ed Tanous2d6cb562022-07-07 20:44:54 -070057
Ed Tanous608ad2c2024-05-20 19:14:50 -070058 Request& operator=(const Request&) = delete;
Ed Tanousf42e8592023-08-25 10:47:44 -070059 Request& operator=(Request&&) = default;
Ed Tanousecd6a3a2022-01-07 09:18:40 -080060 ~Request() = default;
Ed Tanous597d2b12021-09-16 15:07:53 -070061
Ed Tanous608ad2c2024-05-20 19:14:50 -070062 Request copy() const
63 {
64 return {*this};
65 }
66
Ed Tanous6fb96ce2024-01-28 21:30:06 -080067 void addHeader(std::string_view key, std::string_view value)
68 {
69 req.insert(key, value);
70 }
71
72 void addHeader(boost::beast::http::field key, std::string_view value)
73 {
74 req.insert(key, value);
75 }
76
Ed Tanous52e31622024-01-23 16:31:11 -080077 void clear()
78 {
79 req.clear();
80 urlBase.clear();
Ed Tanous52e31622024-01-23 16:31:11 -080081 ipAddress = boost::asio::ip::address();
82 session = nullptr;
83 userRole = "";
84 }
85
Ed Tanous271584a2019-07-09 16:24:22 -070086 boost::beast::http::verb method() const
Ed Tanous1abe55e2018-09-05 08:30:59 -070087 {
88 return req.method();
89 }
Ed Tanous50bfc912024-07-29 14:20:50 -070090
Myung Bae1873a042024-04-01 09:27:39 -050091 void method(boost::beast::http::verb verb)
92 {
Ed Tanous1919a032024-04-03 15:56:36 -070093 req.method(verb);
Myung Bae1873a042024-04-01 09:27:39 -050094 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070095
Ed Tanous50bfc912024-07-29 14:20:50 -070096 std::string_view methodString()
97 {
98 return req.method_string();
99 }
100
Ed Tanous3174e4d2020-10-07 11:41:22 -0700101 std::string_view getHeaderValue(std::string_view key) const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700102 {
103 return req[key];
104 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700105
Ed Tanous3174e4d2020-10-07 11:41:22 -0700106 std::string_view getHeaderValue(boost::beast::http::field key) const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700107 {
108 return req[key];
109 }
Ed Tanouse0d918b2018-03-27 17:41:04 -0700110
Myung Bae1873a042024-04-01 09:27:39 -0500111 void clearHeader(boost::beast::http::field key)
112 {
113 req.erase(key);
114 }
115
Ed Tanous3174e4d2020-10-07 11:41:22 -0700116 std::string_view methodString() const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700117 {
118 return req.method_string();
119 }
Ed Tanouse0d918b2018-03-27 17:41:04 -0700120
Ed Tanous3174e4d2020-10-07 11:41:22 -0700121 std::string_view target() const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700122 {
123 return req.target();
124 }
Ed Tanouse0d918b2018-03-27 17:41:04 -0700125
Ed Tanous6282bc72024-08-19 11:46:20 -0700126 boost::urls::url& url()
127 {
128 return urlBase;
129 }
130
Ed Tanous39662a32023-02-06 15:09:46 -0800131 boost::urls::url_view url() const
132 {
133 return {urlBase};
134 }
135
Ed Tanous98fe7402023-02-14 14:50:33 -0800136 const boost::beast::http::fields& fields() const
137 {
138 return req.base();
139 }
140
Ed Tanous33c6b582023-02-14 15:05:48 -0800141 const std::string& body() const
142 {
Ed Tanous52e31622024-01-23 16:31:11 -0800143 return req.body().str();
Ed Tanous33c6b582023-02-14 15:05:48 -0800144 }
145
Ed Tanous26ccae32023-02-16 10:28:44 -0800146 bool target(std::string_view target)
Ed Tanousf79b7a52021-09-22 19:04:29 -0700147 {
148 req.target(target);
149 return setUrlInfo();
150 }
151
Ed Tanous3174e4d2020-10-07 11:41:22 -0700152 unsigned version() const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700153 {
154 return req.version();
155 }
Ed Tanouse0d918b2018-03-27 17:41:04 -0700156
Ed Tanous3174e4d2020-10-07 11:41:22 -0700157 bool isUpgrade() const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700158 {
Ed Tanousd7857202025-01-28 15:32:26 -0800159 // NOLINTNEXTLINE(misc-include-cleaner)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700160 return boost::beast::websocket::is_upgrade(req);
161 }
Ed Tanouse0d918b2018-03-27 17:41:04 -0700162
Ed Tanous3174e4d2020-10-07 11:41:22 -0700163 bool keepAlive() const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700164 {
165 return req.keep_alive();
166 }
Ed Tanousf79b7a52021-09-22 19:04:29 -0700167
168 private:
Ed Tanous67df0732021-10-26 11:23:56 -0700169 bool setUrlInfo()
170 {
Ed Tanous079360a2022-06-29 10:05:19 -0700171 auto result = boost::urls::parse_relative_ref(target());
Ed Tanous67df0732021-10-26 11:23:56 -0700172
173 if (!result)
174 {
175 return false;
176 }
Ed Tanous39662a32023-02-06 15:09:46 -0800177 urlBase = *result;
Ed Tanous67df0732021-10-26 11:23:56 -0700178 return true;
179 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700180};
Ed Tanouse0d918b2018-03-27 17:41:04 -0700181
Ed Tanous1abe55e2018-09-05 08:30:59 -0700182} // namespace crow