Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
Request.hpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2024, the clio developers.
5
6 Permission to use, copy, modify, and distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#pragma once
21
22#include <boost/beast/http/field.hpp>
23#include <boost/beast/http/message.hpp>
24#include <boost/beast/http/string_body.hpp>
25
26#include <functional>
27#include <optional>
28#include <string>
29#include <string_view>
30#include <variant>
31
32namespace web::ng {
33
37class Request {
38public:
42 using HttpHeaders = boost::beast::http::request<boost::beast::http::string_body>::header_type;
43
44private:
45 struct WsData {
46 std::string request;
47 std::reference_wrapper<HttpHeaders const> headers;
48 };
49
50 using HttpRequest = boost::beast::http::request<boost::beast::http::string_body>;
51 std::variant<HttpRequest, WsData> data_;
52
53public:
59 explicit Request(boost::beast::http::request<boost::beast::http::string_body> request);
60
67 Request(std::string request, HttpHeaders const& headers);
68
74 enum class Method { Get, Post, Websocket, Unsupported };
75
81 Method
82 method() const;
83
89 bool
90 isHttp() const;
91
97 std::optional<
98 std::reference_wrapper<boost::beast::http::request<boost::beast::http::string_body> const>>
99 asHttpRequest() const;
100
107 std::string_view
108 message() const;
109
115 std::optional<std::string_view>
116 target() const;
117
123 HttpHeaders const&
124 httpHeaders() const;
125
132 std::optional<std::string_view>
133 headerValue(boost::beast::http::field headerName) const;
134
141 std::optional<std::string_view>
142 headerValue(std::string const& headerName) const;
143
144private:
152 HttpRequest const&
153 httpRequest() const;
154};
155
156} // namespace web::ng
HttpHeaders const & httpHeaders() const
Get the headers of the request.
Definition Request.cpp:112
std::optional< std::reference_wrapper< boost::beast::http::request< boost::beast::http::string_body > const > > asHttpRequest() const
Get the HTTP request.
Definition Request.cpp:86
Method method() const
Get the method of the request.
Definition Request.cpp:63
std::optional< std::string_view > headerValue(boost::beast::http::field headerName) const
Get the value of a header.
Definition Request.cpp:124
Request(boost::beast::http::request< boost::beast::http::string_body > request)
Construct from an HTTP request.
Definition Request.cpp:52
std::string_view message() const
Get the body (in case of an HTTP request) or the message (in case of a WebSocket request).
Definition Request.cpp:95
Method
Method of the request.
Definition Request.hpp:74
std::optional< std::string_view > target() const
Get the target of the request.
Definition Request.cpp:103
boost::beast::http::request< boost::beast::http::string_body >::header_type HttpHeaders
The headers of an HTTP request.
Definition Request.hpp:42
bool isHttp() const
Check if the request is an HTTP request.
Definition Request.cpp:79