Decentralised Art Server
High-performance C++ backend that exposes HTML interface and a secure REST API for managing Performative Transactions entities
 
Loading...
Searching...
No Matches
http.hpp
Go to the documentation of this file.
1#pragma once
2#include <format>
3#include <string>
4#include <vector>
5#include <sstream>
6#include <limits>
7#include <ios>
8#include <algorithm>
9
10#include "http_codes.hpp"
11#include "http_headers.hpp"
12#include "http_method.hpp"
13#include "url.hpp"
14
15namespace dcn::http
16{
18 {
19 public:
20 //ctor
22
23 //move
26
27 //copy
28 MessageBase(const MessageBase & other) = delete;
30
31 //dtor
32 virtual ~MessageBase() = default;
33
38 MessageBase& setVersion(const std::string & version);
39
48 MessageBase& setBody(const std::string & body);
49
58 MessageBase& setBodyWithContentLength(const std::string & body);
59
67 MessageBase& addHeader(Header header, const std::string & value);
68
76 MessageBase& setHeader(Header header, const std::string & value);
77
83 std::vector<std::string> getHeader(Header header) const;
84
89 const std::string & getVersion() const;
90
95 const HeadersList & getHeaders() const;
96
101 const std::string & getBody() const;
102
103 private:
104 std::string _version;
105 HeadersList _headers;
106 std::string _body;
107 };
108
109 class Request : public MessageBase
110 {
111 public:
112 // ctor
113 Request() = default;
114
115 //move
116 Request(Request && other) = default;
118
119 //copy
120 Request(const Request & other) = delete;
121 Request & operator=(const Request & other) = delete;
122
123 // dtor
124 ~Request() = default;
125
130 Request& setMethod(const Method & method);
131
136 Request& setPath(URL path);
137
142 const Method & getMethod() const;
143
148 const URL & getPath() const;
149
150 private:
151 Method _method;
152 URL _path;
153 };
154
155 class Response : public MessageBase
156 {
157 public:
158 // ctor
159 Response() = default;
160
161 //move
162 Response(Response && other) = default;
164
165 //copy
166 Response(const Response & other) = delete;
167 Response & operator=(const Response & other) = delete;
168
169 // dtor
170 ~Response() = default;
171
176 Response& setCode(Code code);
177
182 const Code & getCode() const;
183
184 private:
185 Code _code;
186 };
187}
188
189namespace dcn::parse
190{
198 http::Request parseRequestFromString(const std::string & request);
199}
200
201template <>
202struct std::formatter<dcn::http::Response> : std::formatter<std::string> {
203 auto format(const dcn::http::Response & res, format_context& ctx) const {
204 return formatter<string>::format(
205 std::format("{} {}\n{}\r\n{}", res.getVersion(), res.getCode(), res.getHeaders(), res.getBody()), ctx);
206 }
207};
208
209template <>
210struct std::formatter<dcn::http::Request> : std::formatter<std::string> {
211
212 auto format(const dcn::http::Request & req, format_context& ctx) const {
213 return formatter<string>::format(
214 std::format("{} {} {}\n{}\r\n{}", req.getVersion(), req.getMethod(), req.getPath(), req.getHeaders(), req.getBody()), ctx);
215 }
216};
Definition http.hpp:18
MessageBase & addHeader(Header header, const std::string &value)
Adds a header to the message.
Definition http.cpp:28
MessageBase & operator=(const MessageBase &other)=delete
MessageBase & setHeader(Header header, const std::string &value)
Set a header in the message.
Definition http.cpp:34
const std::string & getVersion() const
Gets the version of the message.
Definition http.cpp:61
MessageBase()
Definition http.cpp:5
const HeadersList & getHeaders() const
Gets the headers of the message.
Definition http.cpp:66
virtual ~MessageBase()=default
const std::string & getBody() const
Gets the body of the message.
Definition http.cpp:71
MessageBase & setBody(const std::string &body)
Sets the body of the message.
Definition http.cpp:15
MessageBase(const MessageBase &other)=delete
MessageBase & setVersion(const std::string &version)
Sets the HTTP version of the message.
Definition http.cpp:9
MessageBase & setBodyWithContentLength(const std::string &body)
Sets the body of the message.
Definition http.cpp:21
std::vector< std::string > getHeader(Header header) const
Gets the value of a header.
Definition http.cpp:46
MessageBase & operator=(MessageBase &&other)=default
MessageBase(MessageBase &&other)=default
Definition http.hpp:110
Request(Request &&other)=default
const Method & getMethod() const
Gets the HTTP method of the request.
Definition http.cpp:88
Request & operator=(Request &&other)=default
const URL & getPath() const
Gets the path of the request.
Definition http.cpp:93
Request(const Request &other)=delete
Request & operator=(const Request &other)=delete
Request & setMethod(const Method &method)
Sets the HTTP method of the request.
Definition http.cpp:76
Request & setPath(URL path)
Sets the path of the request.
Definition http.cpp:82
Definition http.hpp:156
Response & setCode(Code code)
Sets the HTTP response code of the message.
Definition http.cpp:98
Response(Response &&other)=default
Response & operator=(const Response &other)=delete
const Code & getCode() const
Gets the HTTP response code of the message.
Definition http.cpp:104
Response & operator=(Response &&other)=default
Response(const Response &other)=delete
Definition url.hpp:13
Definition http.hpp:16
Method
Enum to represent the request method.
Definition http_method.hpp:31
Code
Enum to represent HTTP response status codes.
Definition http_codes.hpp:39
Header
Enum of HTTP headers as defined by the standard This enum contains the most commonly used HTTP header...
Definition http_headers.hpp:15
std::vector< std::pair< Header, std::string > > HeadersList
A list of headers A HeadersList is a vector of pairs, where the first element of the pair is a Header...
Definition http_headers.hpp:42
Definition auth.hpp:34
Definition decentralised_art.hpp:30
auto format(const dcn::http::Request &req, format_context &ctx) const
Definition http.hpp:212
auto format(const dcn::http::Response &res, format_context &ctx) const
Definition http.hpp:203