blob: c2e7d8db2f942c636c291a533b0f75e853e49c67 [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
Ed Tanous8f626352018-12-19 14:51:54 -08008#include <boost/asio/io_context.hpp>
Sunitha Harishc0ea7ae2020-10-30 02:37:30 -05009#include <boost/asio/ip/address.hpp>
Ed Tanousd43cd0c2020-09-30 20:46:53 -070010#include <boost/beast/http/message.hpp>
Manojkiran Eda44250442020-06-16 12:51:38 +053011#include <boost/beast/websocket.hpp>
Ed Tanous39662a32023-02-06 15:09:46 -080012#include <boost/url/url.hpp>
raviteja-b4722efe2020-02-03 12:23:18 -060013
Ed Tanous8a9a25c2021-05-11 14:50:58 -070014#include <string>
15#include <string_view>
Ed Tanousf79b7a52021-09-22 19:04:29 -070016#include <system_error>
Ed Tanous8a9a25c2021-05-11 14:50:58 -070017
Ed Tanous1abe55e2018-09-05 08:30:59 -070018namespace crow
19{
Ed Tanous7045c8d2017-04-03 10:04:37 -070020
Ed Tanous1abe55e2018-09-05 08:30:59 -070021struct Request
22{
Jonathan Doman102a4cd2024-04-15 16:56:23 -070023 using Body = boost::beast::http::request<bmcweb::HttpBody>;
24 Body req;
Ed Tanous50b8a432022-02-03 16:29:50 -080025
Ed Tanous39662a32023-02-06 15:09:46 -080026 private:
Ed Tanous47f29342024-03-19 12:18:06 -070027 boost::urls::url urlBase;
Ed Tanous39662a32023-02-06 15:09:46 -080028
29 public:
Ed Tanous47f29342024-03-19 12:18:06 -070030 boost::asio::io_context* ioService = nullptr;
31 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
Ed Tanous47f29342024-03-19 12:18:06 -070035 std::string userRole;
Jonathan Doman102a4cd2024-04-15 16:56:23 -070036 Request(Body reqIn, std::error_code& ec) : req(std::move(reqIn))
Ed Tanous33c6b582023-02-14 15:05:48 -080037 {
38 if (!setUrlInfo())
39 {
40 ec = std::make_error_code(std::errc::invalid_argument);
41 }
42 }
43
Ed Tanous818db202023-09-11 09:58:39 -070044 Request(std::string_view bodyIn, std::error_code& /*ec*/) : req({}, bodyIn)
45 {}
Ed Tanous7045c8d2017-04-03 10:04:37 -070046
Ed Tanousfca2cbe2021-01-28 14:49:59 -080047 Request() = default;
48
Ed Tanous39662a32023-02-06 15:09:46 -080049 Request(const Request& other) = default;
50 Request(Request&& other) = default;
Ed Tanous2d6cb562022-07-07 20:44:54 -070051
Ed Tanousf42e8592023-08-25 10:47:44 -070052 Request& operator=(const Request&) = default;
53 Request& operator=(Request&&) = default;
Ed Tanousecd6a3a2022-01-07 09:18:40 -080054 ~Request() = default;
Ed Tanous597d2b12021-09-16 15:07:53 -070055
Ed Tanous6fb96ce2024-01-28 21:30:06 -080056 void addHeader(std::string_view key, std::string_view value)
57 {
58 req.insert(key, value);
59 }
60
61 void addHeader(boost::beast::http::field key, std::string_view value)
62 {
63 req.insert(key, value);
64 }
65
Ed Tanous52e31622024-01-23 16:31:11 -080066 void clear()
67 {
68 req.clear();
69 urlBase.clear();
Ed Tanous52e31622024-01-23 16:31:11 -080070 ioService = nullptr;
71 ipAddress = boost::asio::ip::address();
72 session = nullptr;
73 userRole = "";
74 }
75
Ed Tanous271584a2019-07-09 16:24:22 -070076 boost::beast::http::verb method() const
Ed Tanous1abe55e2018-09-05 08:30:59 -070077 {
78 return req.method();
79 }
Ed Tanous50bfc912024-07-29 14:20:50 -070080
Myung Bae1873a042024-04-01 09:27:39 -050081 void method(boost::beast::http::verb verb)
82 {
Ed Tanous1919a032024-04-03 15:56:36 -070083 req.method(verb);
Myung Bae1873a042024-04-01 09:27:39 -050084 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070085
Ed Tanous50bfc912024-07-29 14:20:50 -070086 std::string_view methodString()
87 {
88 return req.method_string();
89 }
90
Ed Tanous3174e4d2020-10-07 11:41:22 -070091 std::string_view getHeaderValue(std::string_view key) const
Ed Tanous1abe55e2018-09-05 08:30:59 -070092 {
93 return req[key];
94 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070095
Ed Tanous3174e4d2020-10-07 11:41:22 -070096 std::string_view getHeaderValue(boost::beast::http::field key) const
Ed Tanous1abe55e2018-09-05 08:30:59 -070097 {
98 return req[key];
99 }
Ed Tanouse0d918b2018-03-27 17:41:04 -0700100
Myung Bae1873a042024-04-01 09:27:39 -0500101 void clearHeader(boost::beast::http::field key)
102 {
103 req.erase(key);
104 }
105
Ed Tanous3174e4d2020-10-07 11:41:22 -0700106 std::string_view methodString() const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700107 {
108 return req.method_string();
109 }
Ed Tanouse0d918b2018-03-27 17:41:04 -0700110
Ed Tanous3174e4d2020-10-07 11:41:22 -0700111 std::string_view target() const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700112 {
113 return req.target();
114 }
Ed Tanouse0d918b2018-03-27 17:41:04 -0700115
Ed Tanous6282bc72024-08-19 11:46:20 -0700116 boost::urls::url& url()
117 {
118 return urlBase;
119 }
120
Ed Tanous39662a32023-02-06 15:09:46 -0800121 boost::urls::url_view url() const
122 {
123 return {urlBase};
124 }
125
Ed Tanous98fe7402023-02-14 14:50:33 -0800126 const boost::beast::http::fields& fields() const
127 {
128 return req.base();
129 }
130
Ed Tanous33c6b582023-02-14 15:05:48 -0800131 const std::string& body() const
132 {
Ed Tanous52e31622024-01-23 16:31:11 -0800133 return req.body().str();
Ed Tanous33c6b582023-02-14 15:05:48 -0800134 }
135
Ed Tanous26ccae32023-02-16 10:28:44 -0800136 bool target(std::string_view target)
Ed Tanousf79b7a52021-09-22 19:04:29 -0700137 {
138 req.target(target);
139 return setUrlInfo();
140 }
141
Ed Tanous3174e4d2020-10-07 11:41:22 -0700142 unsigned version() const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700143 {
144 return req.version();
145 }
Ed Tanouse0d918b2018-03-27 17:41:04 -0700146
Ed Tanous3174e4d2020-10-07 11:41:22 -0700147 bool isUpgrade() const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700148 {
149 return boost::beast::websocket::is_upgrade(req);
150 }
Ed Tanouse0d918b2018-03-27 17:41:04 -0700151
Ed Tanous3174e4d2020-10-07 11:41:22 -0700152 bool keepAlive() const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700153 {
154 return req.keep_alive();
155 }
Ed Tanousf79b7a52021-09-22 19:04:29 -0700156
157 private:
Ed Tanous67df0732021-10-26 11:23:56 -0700158 bool setUrlInfo()
159 {
Ed Tanous079360a2022-06-29 10:05:19 -0700160 auto result = boost::urls::parse_relative_ref(target());
Ed Tanous67df0732021-10-26 11:23:56 -0700161
162 if (!result)
163 {
164 return false;
165 }
Ed Tanous39662a32023-02-06 15:09:46 -0800166 urlBase = *result;
Ed Tanous67df0732021-10-26 11:23:56 -0700167 return true;
168 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700169};
Ed Tanouse0d918b2018-03-27 17:41:04 -0700170
Ed Tanous1abe55e2018-09-05 08:30:59 -0700171} // namespace crow