Toolbox snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
Request.hpp
Go to the documentation of this file.
1// The Reactive C++ Toolbox.
2// Copyright (C) 2013-2019 Swirly Cloud Limited
3// Copyright (C) 2022 Reactive Markets Limited
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17#ifndef TOOLBOX_HTTP_REQUEST_HPP
18#define TOOLBOX_HTTP_REQUEST_HPP
19
20#include <toolbox/http/Url.hpp>
21
22#include <vector>
23
24namespace toolbox {
25inline namespace http {
26
27using Headers = std::vector<std::pair<std::string, std::string>>;
28
29class TOOLBOX_API Request : public BasicUrl<Request> {
30 public:
31 Request() = default;
33
34 // Copy.
35 Request(const Request&) = delete;
36 Request& operator=(const Request&) = delete;
37
38 // Move.
39 Request(Request&&) = delete;
41
42 Method method() const noexcept { return method_; }
43 const std::string& url() const noexcept { return url_; }
44 const Headers& headers() const noexcept { return headers_; }
45 const std::string& body() const noexcept { return body_; }
46
47 void clear() noexcept
48 {
49 method_ = Method::Get;
50 url_.clear();
51 headers_.clear();
52 body_.clear();
53 }
54 void flush() { parse(); }
55 void set_method(Method method) noexcept { method_ = method; }
56 void append_url(std::string_view sv) { url_.append(sv.data(), sv.size()); }
57 void append_header_field(std::string_view sv, First first)
58 {
59 if (first == First::Yes) {
60 headers_.emplace_back(std::string{sv.data(), sv.size()}, "");
61 } else {
62 headers_.back().first.append(sv.data(), sv.size());
63 }
64 }
65 void append_header_value(std::string_view sv, First /*first*/)
66 {
67 headers_.back().second.append(sv.data(), sv.size());
68 }
69 void append_body(std::string_view sv) { body_.append(sv.data(), sv.size()); }
70
71 private:
72 Method method_{Method::Get};
73 std::string url_;
74 Headers headers_;
75 std::string body_;
76};
77
78} // namespace http
79} // namespace toolbox
80
81#endif // TOOLBOX_HTTP_REQUEST_HPP
#define TOOLBOX_API
Definition Config.h:39
Request & operator=(Request &&)=delete
Method method() const noexcept
Definition Request.hpp:42
void set_method(Method method) noexcept
Definition Request.hpp:55
const std::string & url() const noexcept
Definition Request.hpp:43
void append_header_value(std::string_view sv, First)
Definition Request.hpp:65
void append_header_field(std::string_view sv, First first)
Definition Request.hpp:57
void clear() noexcept
Definition Request.hpp:47
Request(const Request &)=delete
const std::string & body() const noexcept
Definition Request.hpp:45
void append_url(std::string_view sv)
Definition Request.hpp:56
const Headers & headers() const noexcept
Definition Request.hpp:44
void append_body(std::string_view sv)
Definition Request.hpp:69
Request(Request &&)=delete
Request & operator=(const Request &)=delete
std::vector< std::pair< std::string, std::string > > Headers
Definition Request.hpp:27
std::string_view sv
Definition Tokeniser.hpp:26