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
73 enum class Method { Get, Post, Websocket, Unsupported };
74
80 Method
81 method() const;
82
88 bool
89 isHttp() const;
90
96 std::optional<std::reference_wrapper<boost::beast::http::request<boost::beast::http::string_body> const>>
97 asHttpRequest() const;
98
104 std::string_view
105 message() const;
106
112 std::optional<std::string_view>
113 target() const;
114
120 HttpHeaders const&
121 httpHeaders() const;
122
129 std::optional<std::string_view>
130 headerValue(boost::beast::http::field headerName) const;
131
138 std::optional<std::string_view>
139 headerValue(std::string const& headerName) const;
140
141private:
149 HttpRequest const&
150 httpRequest() const;
151};
152
153} // namespace web::ng
Represents an HTTP or WebSocket request.
Definition Request.hpp:37
HttpHeaders const & httpHeaders() const
Get the headers of the request.
Definition Request.cpp:110
std::optional< std::reference_wrapper< boost::beast::http::request< boost::beast::http::string_body > const > > asHttpRequest() const
Get the HTTP request.
Definition Request.cpp:84
Method method() const
Get the method of the request.
Definition Request.cpp:62
std::optional< std::string_view > headerValue(boost::beast::http::field headerName) const
Get the value of a header.
Definition Request.cpp:122
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:93
Method
Method of the request.
Definition Request.hpp:73
std::optional< std::string_view > target() const
Get the target of the request.
Definition Request.cpp:101
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:78