xref: /openbmc/bmcweb/http/http_request.hpp (revision 597d2b142362bafa90f24fc8c30750afab91f78f)
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_view.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     boost::beast::http::fields& fields;
24     std::string_view url{};
25     boost::urls::url_view urlView{};
26     boost::urls::query_params_view urlParams{};
27     bool isSecure{false};
28 
29     const std::string& body;
30 
31     boost::asio::io_context* ioService{};
32     boost::asio::ip::address ipAddress{};
33 
34     std::shared_ptr<persistent_data::UserSession> session;
35 
36     std::string userRole{};
37     Request(boost::beast::http::request<boost::beast::http::string_body> reqIn,
38             std::error_code& ec) :
39         req(std::move(reqIn)),
40         fields(req.base()), body(req.body())
41     {
42         if (!setUrlInfo())
43         {
44             ec = std::make_error_code(std::errc::invalid_argument);
45         }
46     }
47 
48     Request(const Request&) = delete;
49     Request& operator=(const Request&) = delete;
50 
51     boost::beast::http::verb method() const
52     {
53         return req.method();
54     }
55 
56     std::string_view getHeaderValue(std::string_view key) const
57     {
58         return req[key];
59     }
60 
61     std::string_view getHeaderValue(boost::beast::http::field key) const
62     {
63         return req[key];
64     }
65 
66     std::string_view methodString() const
67     {
68         return req.method_string();
69     }
70 
71     std::string_view target() const
72     {
73         return req.target();
74     }
75 
76     bool target(const std::string_view target)
77     {
78         req.target(target);
79         return setUrlInfo();
80     }
81 
82     unsigned version() const
83     {
84         return req.version();
85     }
86 
87     bool isUpgrade() const
88     {
89         return boost::beast::websocket::is_upgrade(req);
90     }
91 
92     bool keepAlive() const
93     {
94         return req.keep_alive();
95     }
96 
97   private:
98     bool setUrlInfo()
99     {
100         boost::urls::error_code ec;
101         urlView = boost::urls::parse_relative_ref(
102             boost::urls::string_view(target().data(), target().size()), ec);
103         if (ec)
104         {
105             return false;
106         }
107         url = std::string_view(urlView.encoded_path().data(),
108                                urlView.encoded_path().size());
109         urlParams = urlView.query_params();
110         return true;
111     }
112 };
113 
114 } // namespace crow
115