/src/bmcweb/http/http_request.hpp
Line | Count | Source |
1 | | // SPDX-License-Identifier: Apache-2.0 |
2 | | // SPDX-FileCopyrightText: Copyright OpenBMC Authors |
3 | | #pragma once |
4 | | |
5 | | #include "http_body.hpp" |
6 | | #include "sessions.hpp" |
7 | | |
8 | | #include <boost/asio/ip/address.hpp> |
9 | | #include <boost/beast/http/field.hpp> |
10 | | #include <boost/beast/http/fields.hpp> |
11 | | #include <boost/beast/http/message.hpp> |
12 | | #include <boost/beast/http/verb.hpp> |
13 | | #include <boost/beast/websocket/rfc6455.hpp> |
14 | | #include <boost/url/parse.hpp> |
15 | | #include <boost/url/url.hpp> |
16 | | #include <boost/url/url_view.hpp> |
17 | | |
18 | | #include <memory> |
19 | | #include <string> |
20 | | #include <string_view> |
21 | | #include <system_error> |
22 | | #include <utility> |
23 | | |
24 | | namespace crow |
25 | | { |
26 | | |
27 | | struct Request |
28 | | { |
29 | | using Body = boost::beast::http::request<bmcweb::HttpBody>; |
30 | | Body req; |
31 | | |
32 | | private: |
33 | | boost::urls::url urlBase; |
34 | | |
35 | | Request(const Request& other) = default; |
36 | | |
37 | | public: |
38 | | boost::asio::ip::address ipAddress; |
39 | | |
40 | | std::shared_ptr<persistent_data::UserSession> session; |
41 | | |
42 | | std::string userRole; |
43 | | Request(Body&& reqIn, std::error_code& ec) : req(std::move(reqIn)) |
44 | 0 | { |
45 | 0 | if (!setUrlInfo()) |
46 | 0 | { |
47 | 0 | ec = std::make_error_code(std::errc::invalid_argument); |
48 | 0 | } |
49 | 0 | } |
50 | | |
51 | 1.86k | Request(std::string_view bodyIn, std::error_code& /*ec*/) : req({}, bodyIn) |
52 | 1.86k | {} |
53 | | |
54 | | Request() = default; |
55 | | |
56 | | Request(Request&& other) = default; |
57 | | |
58 | | Request& operator=(const Request&) = delete; |
59 | | Request& operator=(Request&&) = default; |
60 | 1.86k | ~Request() = default; |
61 | | |
62 | | Request copy() const |
63 | 0 | { |
64 | 0 | return {*this}; |
65 | 0 | } |
66 | | |
67 | | void addHeader(std::string_view key, std::string_view value) |
68 | 1.86k | { |
69 | 1.86k | req.insert(key, value); |
70 | 1.86k | } |
71 | | |
72 | | void addHeader(boost::beast::http::field key, std::string_view value) |
73 | 0 | { |
74 | 0 | req.insert(key, value); |
75 | 0 | } |
76 | | |
77 | | void clear() |
78 | 0 | { |
79 | 0 | req.clear(); |
80 | 0 | urlBase.clear(); |
81 | 0 | ipAddress = boost::asio::ip::address(); |
82 | 0 | session = nullptr; |
83 | 0 | userRole = ""; |
84 | 0 | } |
85 | | |
86 | | boost::beast::http::verb method() const |
87 | 0 | { |
88 | 0 | return req.method(); |
89 | 0 | } |
90 | | |
91 | | void method(boost::beast::http::verb verb) |
92 | 0 | { |
93 | 0 | req.method(verb); |
94 | 0 | } |
95 | | |
96 | | std::string_view methodString() |
97 | 0 | { |
98 | 0 | return req.method_string(); |
99 | 0 | } |
100 | | |
101 | | std::string_view getHeaderValue(std::string_view key) const |
102 | 1.86k | { |
103 | 1.86k | return req[key]; |
104 | 1.86k | } |
105 | | |
106 | | std::string_view getHeaderValue(boost::beast::http::field key) const |
107 | 0 | { |
108 | 0 | return req[key]; |
109 | 0 | } |
110 | | |
111 | | void clearHeader(boost::beast::http::field key) |
112 | 0 | { |
113 | 0 | req.erase(key); |
114 | 0 | } |
115 | | |
116 | | std::string_view methodString() const |
117 | 0 | { |
118 | 0 | return req.method_string(); |
119 | 0 | } |
120 | | |
121 | | std::string_view target() const |
122 | 0 | { |
123 | 0 | return req.target(); |
124 | 0 | } |
125 | | |
126 | | boost::urls::url& url() |
127 | 0 | { |
128 | 0 | return urlBase; |
129 | 0 | } |
130 | | |
131 | | boost::urls::url_view url() const |
132 | 0 | { |
133 | 0 | return {urlBase}; |
134 | 0 | } |
135 | | |
136 | | const boost::beast::http::fields& fields() const |
137 | 0 | { |
138 | 0 | return req.base(); |
139 | 0 | } |
140 | | |
141 | | const std::string& body() const |
142 | 1.86k | { |
143 | 1.86k | return req.body().str(); |
144 | 1.86k | } |
145 | | |
146 | | bool target(std::string_view target) |
147 | 0 | { |
148 | 0 | req.target(target); |
149 | 0 | return setUrlInfo(); |
150 | 0 | } |
151 | | |
152 | | unsigned version() const |
153 | 0 | { |
154 | 0 | return req.version(); |
155 | 0 | } |
156 | | |
157 | | bool isUpgrade() const |
158 | 0 | { |
159 | 0 | // NOLINTNEXTLINE(misc-include-cleaner) |
160 | 0 | return boost::beast::websocket::is_upgrade(req); |
161 | 0 | } |
162 | | |
163 | | bool keepAlive() const |
164 | 0 | { |
165 | 0 | return req.keep_alive(); |
166 | 0 | } |
167 | | |
168 | | private: |
169 | | bool setUrlInfo() |
170 | 0 | { |
171 | 0 | auto result = boost::urls::parse_relative_ref(target()); |
172 | 0 |
|
173 | 0 | if (!result) |
174 | 0 | { |
175 | 0 | return false; |
176 | 0 | } |
177 | 0 | urlBase = *result; |
178 | 0 | return true; |
179 | 0 | } |
180 | | }; |
181 | | |
182 | | } // namespace crow |