blob: 160216b93f22e41d5eded9e25526d01bec16b939 [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 Tanousd7857202025-01-28 15:32:26 -080010#include <boost/beast/http/field.hpp>
11#include <boost/beast/http/fields.hpp>
Ed Tanousd43cd0c2020-09-30 20:46:53 -070012#include <boost/beast/http/message.hpp>
Ed Tanousd7857202025-01-28 15:32:26 -080013#include <boost/beast/http/verb.hpp>
14#include <boost/beast/websocket/rfc6455.hpp>
15#include <boost/url/parse.hpp>
Ed Tanous39662a32023-02-06 15:09:46 -080016#include <boost/url/url.hpp>
Ed Tanousd7857202025-01-28 15:32:26 -080017#include <boost/url/url_view.hpp>
raviteja-b4722efe2020-02-03 12:23:18 -060018
Ed Tanousd7857202025-01-28 15:32:26 -080019#include <memory>
Ed Tanous8a9a25c2021-05-11 14:50:58 -070020#include <string>
21#include <string_view>
Ed Tanousf79b7a52021-09-22 19:04:29 -070022#include <system_error>
Ed Tanousd7857202025-01-28 15:32:26 -080023#include <utility>
Ed Tanous8a9a25c2021-05-11 14:50:58 -070024
Ed Tanous1abe55e2018-09-05 08:30:59 -070025namespace crow
26{
Ed Tanous7045c8d2017-04-03 10:04:37 -070027
Ed Tanous1abe55e2018-09-05 08:30:59 -070028struct Request
29{
Jonathan Doman102a4cd2024-04-15 16:56:23 -070030 using Body = boost::beast::http::request<bmcweb::HttpBody>;
31 Body req;
Ed Tanous50b8a432022-02-03 16:29:50 -080032
Ed Tanous39662a32023-02-06 15:09:46 -080033 private:
Ed Tanous47f29342024-03-19 12:18:06 -070034 boost::urls::url urlBase;
Ed Tanous39662a32023-02-06 15:09:46 -080035
36 public:
Ed Tanous47f29342024-03-19 12:18:06 -070037 boost::asio::io_context* ioService = nullptr;
38 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(const Request& other) = default;
57 Request(Request&& other) = default;
Ed Tanous2d6cb562022-07-07 20:44:54 -070058
Ed Tanousf42e8592023-08-25 10:47:44 -070059 Request& operator=(const Request&) = default;
60 Request& operator=(Request&&) = default;
Ed Tanousecd6a3a2022-01-07 09:18:40 -080061 ~Request() = default;
Ed Tanous597d2b12021-09-16 15:07:53 -070062
Ed Tanous6fb96ce2024-01-28 21:30:06 -080063 void addHeader(std::string_view key, std::string_view value)
64 {
65 req.insert(key, value);
66 }
67
68 void addHeader(boost::beast::http::field key, std::string_view value)
69 {
70 req.insert(key, value);
71 }
72
Ed Tanous52e31622024-01-23 16:31:11 -080073 void clear()
74 {
75 req.clear();
76 urlBase.clear();
Ed Tanous52e31622024-01-23 16:31:11 -080077 ioService = nullptr;
78 ipAddress = boost::asio::ip::address();
79 session = nullptr;
80 userRole = "";
81 }
82
Ed Tanous271584a2019-07-09 16:24:22 -070083 boost::beast::http::verb method() const
Ed Tanous1abe55e2018-09-05 08:30:59 -070084 {
85 return req.method();
86 }
Ed Tanous50bfc912024-07-29 14:20:50 -070087
Myung Bae1873a042024-04-01 09:27:39 -050088 void method(boost::beast::http::verb verb)
89 {
Ed Tanous1919a032024-04-03 15:56:36 -070090 req.method(verb);
Myung Bae1873a042024-04-01 09:27:39 -050091 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070092
Ed Tanous50bfc912024-07-29 14:20:50 -070093 std::string_view methodString()
94 {
95 return req.method_string();
96 }
97
Ed Tanous3174e4d2020-10-07 11:41:22 -070098 std::string_view getHeaderValue(std::string_view key) const
Ed Tanous1abe55e2018-09-05 08:30:59 -070099 {
100 return req[key];
101 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700102
Ed Tanous3174e4d2020-10-07 11:41:22 -0700103 std::string_view getHeaderValue(boost::beast::http::field key) const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700104 {
105 return req[key];
106 }
Ed Tanouse0d918b2018-03-27 17:41:04 -0700107
Myung Bae1873a042024-04-01 09:27:39 -0500108 void clearHeader(boost::beast::http::field key)
109 {
110 req.erase(key);
111 }
112
Ed Tanous3174e4d2020-10-07 11:41:22 -0700113 std::string_view methodString() const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700114 {
115 return req.method_string();
116 }
Ed Tanouse0d918b2018-03-27 17:41:04 -0700117
Ed Tanous3174e4d2020-10-07 11:41:22 -0700118 std::string_view target() const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700119 {
120 return req.target();
121 }
Ed Tanouse0d918b2018-03-27 17:41:04 -0700122
Ed Tanous6282bc72024-08-19 11:46:20 -0700123 boost::urls::url& url()
124 {
125 return urlBase;
126 }
127
Ed Tanous39662a32023-02-06 15:09:46 -0800128 boost::urls::url_view url() const
129 {
130 return {urlBase};
131 }
132
Ed Tanous98fe7402023-02-14 14:50:33 -0800133 const boost::beast::http::fields& fields() const
134 {
135 return req.base();
136 }
137
Ed Tanous33c6b582023-02-14 15:05:48 -0800138 const std::string& body() const
139 {
Ed Tanous52e31622024-01-23 16:31:11 -0800140 return req.body().str();
Ed Tanous33c6b582023-02-14 15:05:48 -0800141 }
142
Ed Tanous26ccae32023-02-16 10:28:44 -0800143 bool target(std::string_view target)
Ed Tanousf79b7a52021-09-22 19:04:29 -0700144 {
145 req.target(target);
146 return setUrlInfo();
147 }
148
Ed Tanous3174e4d2020-10-07 11:41:22 -0700149 unsigned version() const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700150 {
151 return req.version();
152 }
Ed Tanouse0d918b2018-03-27 17:41:04 -0700153
Ed Tanous3174e4d2020-10-07 11:41:22 -0700154 bool isUpgrade() const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700155 {
Ed Tanousd7857202025-01-28 15:32:26 -0800156 // NOLINTNEXTLINE(misc-include-cleaner)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700157 return boost::beast::websocket::is_upgrade(req);
158 }
Ed Tanouse0d918b2018-03-27 17:41:04 -0700159
Ed Tanous3174e4d2020-10-07 11:41:22 -0700160 bool keepAlive() const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700161 {
162 return req.keep_alive();
163 }
Ed Tanousf79b7a52021-09-22 19:04:29 -0700164
165 private:
Ed Tanous67df0732021-10-26 11:23:56 -0700166 bool setUrlInfo()
167 {
Ed Tanous079360a2022-06-29 10:05:19 -0700168 auto result = boost::urls::parse_relative_ref(target());
Ed Tanous67df0732021-10-26 11:23:56 -0700169
170 if (!result)
171 {
172 return false;
173 }
Ed Tanous39662a32023-02-06 15:09:46 -0800174 urlBase = *result;
Ed Tanous67df0732021-10-26 11:23:56 -0700175 return true;
176 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700177};
Ed Tanouse0d918b2018-03-27 17:41:04 -0700178
Ed Tanous1abe55e2018-09-05 08:30:59 -0700179} // namespace crow