xref: /openbmc/bmcweb/http/http_request.hpp (revision 1b8b02a4)
1 #pragma once
2 
3 #include "common.hpp"
4 #include "sessions.hpp"
5 
6 #include <boost/asio/io_context.hpp>
7 #include <boost/asio/ip/address.hpp>
8 #include <boost/beast/http/message.hpp>
9 #include <boost/beast/http/string_body.hpp>
10 #include <boost/beast/websocket.hpp>
11 #include <boost/url/url.hpp>
12 
13 #include <string>
14 #include <string_view>
15 #include <system_error>
16 
17 namespace crow
18 {
19 
20 struct Request
21 {
22     boost::beast::http::request<boost::beast::http::string_body> req;
23 
24   private:
25     boost::urls::url urlBase{};
26 
27   public:
28     bool isSecure{false};
29 
30     boost::asio::io_context* ioService{};
31     boost::asio::ip::address ipAddress{};
32 
33     std::shared_ptr<persistent_data::UserSession> session;
34 
35     std::string userRole{};
36     Request(boost::beast::http::request<boost::beast::http::string_body> reqIn,
37             std::error_code& ec) :
38         req(std::move(reqIn))
39     {
40         if (!setUrlInfo())
41         {
42             ec = std::make_error_code(std::errc::invalid_argument);
43         }
44     }
45 
46     Request(std::string_view bodyIn, std::error_code& ec) : req({}, bodyIn)
47     {
48         if (!setUrlInfo())
49         {
50             ec = std::make_error_code(std::errc::invalid_argument);
51         }
52     }
53 
54     Request() = default;
55 
56     Request(const Request& other) = default;
57     Request(Request&& other) = default;
58 
59     Request& operator=(const Request&) = delete;
60     Request& operator=(const Request&&) = delete;
61     ~Request() = default;
62 
63     boost::beast::http::verb method() const
64     {
65         return req.method();
66     }
67 
68     std::string_view getHeaderValue(std::string_view key) const
69     {
70         return req[key];
71     }
72 
73     std::string_view getHeaderValue(boost::beast::http::field key) const
74     {
75         return req[key];
76     }
77 
78     std::string_view methodString() const
79     {
80         return req.method_string();
81     }
82 
83     std::string_view target() const
84     {
85         return req.target();
86     }
87 
88     boost::urls::url_view url() const
89     {
90         return {urlBase};
91     }
92 
93     const boost::beast::http::fields& fields() const
94     {
95         return req.base();
96     }
97 
98     const std::string& body() const
99     {
100         return req.body();
101     }
102 
103     bool target(std::string_view target)
104     {
105         req.target(target);
106         return setUrlInfo();
107     }
108 
109     unsigned version() const
110     {
111         return req.version();
112     }
113 
114     bool isUpgrade() const
115     {
116         return boost::beast::websocket::is_upgrade(req);
117     }
118 
119     bool keepAlive() const
120     {
121         return req.keep_alive();
122     }
123 
124   private:
125     bool setUrlInfo()
126     {
127         auto result = boost::urls::parse_relative_ref(target());
128 
129         if (!result)
130         {
131             return false;
132         }
133         urlBase = *result;
134         return true;
135     }
136 };
137 
138 } // namespace crow
139