blob: be84f1847ca6b2387d258b34af23fdcab1545fec [file] [log] [blame]
Ed Tanous7045c8d2017-04-03 10:04:37 -07001#pragma once
2
Ed Tanous04e438c2020-10-03 08:06:26 -07003#include "common.hpp"
Ratan Gupta6f359562019-04-03 10:39:08 +05304#include "sessions.hpp"
5
Ed Tanous8f626352018-12-19 14:51:54 -08006#include <boost/asio/io_context.hpp>
Sunitha Harishc0ea7ae2020-10-30 02:37:30 -05007#include <boost/asio/ip/address.hpp>
Ed Tanousd43cd0c2020-09-30 20:46:53 -07008#include <boost/beast/http/message.hpp>
9#include <boost/beast/http/string_body.hpp>
Manojkiran Eda44250442020-06-16 12:51:38 +053010#include <boost/beast/websocket.hpp>
James Feist5a7e8772020-07-22 09:08:38 -070011#include <boost/url/url_view.hpp>
raviteja-b4722efe2020-02-03 12:23:18 -060012
Ed Tanous8a9a25c2021-05-11 14:50:58 -070013#include <string>
14#include <string_view>
Ed Tanousf79b7a52021-09-22 19:04:29 -070015#include <system_error>
Ed Tanous8a9a25c2021-05-11 14:50:58 -070016
Ed Tanous1abe55e2018-09-05 08:30:59 -070017namespace crow
18{
Ed Tanous7045c8d2017-04-03 10:04:37 -070019
Ed Tanous1abe55e2018-09-05 08:30:59 -070020struct Request
21{
John Edward Broadbent59b98b22021-07-13 15:36:32 -070022 boost::beast::http::request<boost::beast::http::string_body> req;
James Feistfe306722020-03-12 16:32:08 -070023 boost::beast::http::fields& fields;
Ed Tanous39e77502019-03-04 17:35:53 -080024 std::string_view url{};
James Feist5a7e8772020-07-22 09:08:38 -070025 boost::urls::url_view urlView{};
Ed Tanousd32c4fa2021-09-14 13:16:51 -070026 boost::urls::query_params_view urlParams{};
Ed Tanous1abe55e2018-09-05 08:30:59 -070027 bool isSecure{false};
Ed Tanous7045c8d2017-04-03 10:04:37 -070028
Ed Tanous1abe55e2018-09-05 08:30:59 -070029 const std::string& body;
Ed Tanouse0d918b2018-03-27 17:41:04 -070030
Ed Tanous8f626352018-12-19 14:51:54 -080031 boost::asio::io_context* ioService{};
Sunitha Harishc0ea7ae2020-10-30 02:37:30 -050032 boost::asio::ip::address ipAddress{};
Ed Tanous7045c8d2017-04-03 10:04:37 -070033
Ed Tanous52cc1122020-07-18 13:51:21 -070034 std::shared_ptr<persistent_data::UserSession> session;
Ratan Gupta6f359562019-04-03 10:39:08 +053035
RAJESWARAN THILLAIGOVINDAN61dbeef2019-12-13 04:26:54 -060036 std::string userRole{};
Ed Tanousf79b7a52021-09-22 19:04:29 -070037 Request(boost::beast::http::request<boost::beast::http::string_body> reqIn,
38 std::error_code& ec) :
John Edward Broadbent59b98b22021-07-13 15:36:32 -070039 req(std::move(reqIn)),
40 fields(req.base()), body(req.body())
Ed Tanousf79b7a52021-09-22 19:04:29 -070041 {
42 if (!setUrlInfo())
43 {
44 ec = std::make_error_code(std::errc::invalid_argument);
45 }
46 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070047
Ed Tanous271584a2019-07-09 16:24:22 -070048 boost::beast::http::verb method() const
Ed Tanous1abe55e2018-09-05 08:30:59 -070049 {
50 return req.method();
51 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070052
Ed Tanous3174e4d2020-10-07 11:41:22 -070053 std::string_view getHeaderValue(std::string_view key) const
Ed Tanous1abe55e2018-09-05 08:30:59 -070054 {
55 return req[key];
56 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070057
Ed Tanous3174e4d2020-10-07 11:41:22 -070058 std::string_view getHeaderValue(boost::beast::http::field key) const
Ed Tanous1abe55e2018-09-05 08:30:59 -070059 {
60 return req[key];
61 }
Ed Tanouse0d918b2018-03-27 17:41:04 -070062
Ed Tanous3174e4d2020-10-07 11:41:22 -070063 std::string_view methodString() const
Ed Tanous1abe55e2018-09-05 08:30:59 -070064 {
65 return req.method_string();
66 }
Ed Tanouse0d918b2018-03-27 17:41:04 -070067
Ed Tanous3174e4d2020-10-07 11:41:22 -070068 std::string_view target() const
Ed Tanous1abe55e2018-09-05 08:30:59 -070069 {
70 return req.target();
71 }
Ed Tanouse0d918b2018-03-27 17:41:04 -070072
Ed Tanousf79b7a52021-09-22 19:04:29 -070073 bool target(const std::string_view target)
74 {
75 req.target(target);
76 return setUrlInfo();
77 }
78
Ed Tanous3174e4d2020-10-07 11:41:22 -070079 unsigned version() const
Ed Tanous1abe55e2018-09-05 08:30:59 -070080 {
81 return req.version();
82 }
Ed Tanouse0d918b2018-03-27 17:41:04 -070083
Ed Tanous3174e4d2020-10-07 11:41:22 -070084 bool isUpgrade() const
Ed Tanous1abe55e2018-09-05 08:30:59 -070085 {
86 return boost::beast::websocket::is_upgrade(req);
87 }
Ed Tanouse0d918b2018-03-27 17:41:04 -070088
Ed Tanous3174e4d2020-10-07 11:41:22 -070089 bool keepAlive() const
Ed Tanous1abe55e2018-09-05 08:30:59 -070090 {
91 return req.keep_alive();
92 }
Ed Tanousf79b7a52021-09-22 19:04:29 -070093
94 private:
95 bool setUrlInfo()
96 {
97 boost::urls::error_code ec;
98 urlView = boost::urls::parse_relative_ref(
99 boost::urls::string_view(target().data(), target().size()), ec);
100 if (ec)
101 {
102 return false;
103 }
104 url = std::string_view(urlView.encoded_path().data(),
105 urlView.encoded_path().size());
106 urlParams = urlView.query_params();
107 return true;
108 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700109};
Ed Tanouse0d918b2018-03-27 17:41:04 -0700110
Ed Tanous1abe55e2018-09-05 08:30:59 -0700111} // namespace crow