blob: bfbe2e39e9520c0f5f40b2b80b2f5ad20ccaed44 [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
35 public:
Ed Tanous47f29342024-03-19 12:18:06 -070036 boost::asio::ip::address ipAddress;
Ed Tanous7045c8d2017-04-03 10:04:37 -070037
Ed Tanous52cc1122020-07-18 13:51:21 -070038 std::shared_ptr<persistent_data::UserSession> session;
Ratan Gupta6f359562019-04-03 10:39:08 +053039
Ed Tanous47f29342024-03-19 12:18:06 -070040 std::string userRole;
Jonathan Doman102a4cd2024-04-15 16:56:23 -070041 Request(Body reqIn, std::error_code& ec) : req(std::move(reqIn))
Ed Tanous33c6b582023-02-14 15:05:48 -080042 {
43 if (!setUrlInfo())
44 {
45 ec = std::make_error_code(std::errc::invalid_argument);
46 }
47 }
48
Ed Tanous818db202023-09-11 09:58:39 -070049 Request(std::string_view bodyIn, std::error_code& /*ec*/) : req({}, bodyIn)
50 {}
Ed Tanous7045c8d2017-04-03 10:04:37 -070051
Ed Tanousfca2cbe2021-01-28 14:49:59 -080052 Request() = default;
53
Ed Tanous39662a32023-02-06 15:09:46 -080054 Request(const Request& other) = default;
55 Request(Request&& other) = default;
Ed Tanous2d6cb562022-07-07 20:44:54 -070056
Ed Tanousf42e8592023-08-25 10:47:44 -070057 Request& operator=(const Request&) = default;
58 Request& operator=(Request&&) = default;
Ed Tanousecd6a3a2022-01-07 09:18:40 -080059 ~Request() = default;
Ed Tanous597d2b12021-09-16 15:07:53 -070060
Ed Tanous6fb96ce2024-01-28 21:30:06 -080061 void addHeader(std::string_view key, std::string_view value)
62 {
63 req.insert(key, value);
64 }
65
66 void addHeader(boost::beast::http::field key, std::string_view value)
67 {
68 req.insert(key, value);
69 }
70
Ed Tanous52e31622024-01-23 16:31:11 -080071 void clear()
72 {
73 req.clear();
74 urlBase.clear();
Ed Tanous52e31622024-01-23 16:31:11 -080075 ipAddress = boost::asio::ip::address();
76 session = nullptr;
77 userRole = "";
78 }
79
Ed Tanous271584a2019-07-09 16:24:22 -070080 boost::beast::http::verb method() const
Ed Tanous1abe55e2018-09-05 08:30:59 -070081 {
82 return req.method();
83 }
Ed Tanous50bfc912024-07-29 14:20:50 -070084
Myung Bae1873a042024-04-01 09:27:39 -050085 void method(boost::beast::http::verb verb)
86 {
Ed Tanous1919a032024-04-03 15:56:36 -070087 req.method(verb);
Myung Bae1873a042024-04-01 09:27:39 -050088 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070089
Ed Tanous50bfc912024-07-29 14:20:50 -070090 std::string_view methodString()
91 {
92 return req.method_string();
93 }
94
Ed Tanous3174e4d2020-10-07 11:41:22 -070095 std::string_view getHeaderValue(std::string_view key) const
Ed Tanous1abe55e2018-09-05 08:30:59 -070096 {
97 return req[key];
98 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070099
Ed Tanous3174e4d2020-10-07 11:41:22 -0700100 std::string_view getHeaderValue(boost::beast::http::field key) const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700101 {
102 return req[key];
103 }
Ed Tanouse0d918b2018-03-27 17:41:04 -0700104
Myung Bae1873a042024-04-01 09:27:39 -0500105 void clearHeader(boost::beast::http::field key)
106 {
107 req.erase(key);
108 }
109
Ed Tanous3174e4d2020-10-07 11:41:22 -0700110 std::string_view methodString() const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700111 {
112 return req.method_string();
113 }
Ed Tanouse0d918b2018-03-27 17:41:04 -0700114
Ed Tanous3174e4d2020-10-07 11:41:22 -0700115 std::string_view target() const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700116 {
117 return req.target();
118 }
Ed Tanouse0d918b2018-03-27 17:41:04 -0700119
Ed Tanous6282bc72024-08-19 11:46:20 -0700120 boost::urls::url& url()
121 {
122 return urlBase;
123 }
124
Ed Tanous39662a32023-02-06 15:09:46 -0800125 boost::urls::url_view url() const
126 {
127 return {urlBase};
128 }
129
Ed Tanous98fe7402023-02-14 14:50:33 -0800130 const boost::beast::http::fields& fields() const
131 {
132 return req.base();
133 }
134
Ed Tanous33c6b582023-02-14 15:05:48 -0800135 const std::string& body() const
136 {
Ed Tanous52e31622024-01-23 16:31:11 -0800137 return req.body().str();
Ed Tanous33c6b582023-02-14 15:05:48 -0800138 }
139
Ed Tanous26ccae32023-02-16 10:28:44 -0800140 bool target(std::string_view target)
Ed Tanousf79b7a52021-09-22 19:04:29 -0700141 {
142 req.target(target);
143 return setUrlInfo();
144 }
145
Ed Tanous3174e4d2020-10-07 11:41:22 -0700146 unsigned version() const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700147 {
148 return req.version();
149 }
Ed Tanouse0d918b2018-03-27 17:41:04 -0700150
Ed Tanous3174e4d2020-10-07 11:41:22 -0700151 bool isUpgrade() const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700152 {
Ed Tanousd7857202025-01-28 15:32:26 -0800153 // NOLINTNEXTLINE(misc-include-cleaner)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700154 return boost::beast::websocket::is_upgrade(req);
155 }
Ed Tanouse0d918b2018-03-27 17:41:04 -0700156
Ed Tanous3174e4d2020-10-07 11:41:22 -0700157 bool keepAlive() const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700158 {
159 return req.keep_alive();
160 }
Ed Tanousf79b7a52021-09-22 19:04:29 -0700161
162 private:
Ed Tanous67df0732021-10-26 11:23:56 -0700163 bool setUrlInfo()
164 {
Ed Tanous079360a2022-06-29 10:05:19 -0700165 auto result = boost::urls::parse_relative_ref(target());
Ed Tanous67df0732021-10-26 11:23:56 -0700166
167 if (!result)
168 {
169 return false;
170 }
Ed Tanous39662a32023-02-06 15:09:46 -0800171 urlBase = *result;
Ed Tanous67df0732021-10-26 11:23:56 -0700172 return true;
173 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700174};
Ed Tanouse0d918b2018-03-27 17:41:04 -0700175
Ed Tanous1abe55e2018-09-05 08:30:59 -0700176} // namespace crow