Webdar 1.0.0
Web user interface to libdar
request.hpp
1/*********************************************************************/
2// webdar - a web server and interface program to libdar
3// Copyright (C) 2013-2025 Denis Corbin
4//
5// This file is part of Webdar
6//
7// Webdar is free software: you can redistribute it and/or modify
8// it under the terms of the GNU General Public License as published by
9// the Free Software Foundation, either version 3 of the License, or
10// (at your option) any later version.
11//
12// Webdar is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License
18// along with Webdar. If not, see <http://www.gnu.org/licenses/>
19//
20//----
21// to contact the author: dar.linux@free.fr
22/*********************************************************************/
23
24#ifndef REQUEST_HPP
25#define REQUEST_HPP
26
27#include "my_config.h"
28
29 // C++ system header files
30#include <string>
31#include <map>
32#include <memory>
33
34 // webdar headers
35#include "uri.hpp"
36#include "webdar_tools.hpp"
37#include "exceptions.hpp"
38#include "tokens.hpp"
39#include "central_report.hpp"
40#include "connexion.hpp"
41
43
45{
46public:
48 request(const std::shared_ptr<central_report> & log) { clear(); if(log) clog = log; else throw WEBDAR_BUG; };
49 request(const request & ref) = default;
50 request(request && ref) noexcept = default;
51 request & operator = (const request & ref) = default;
52 request & operator = (request && ref) noexcept = default;
53 ~request() = default;
54
56 void clear();
57
63 bool try_reading(proto_connexion & input);
64
74 void read(proto_connexion & input);
75
76
78 const std::string & get_method() const { if(status < method_read) throw WEBDAR_BUG; return cached_method; };
79
81 void change_method(const std::string & val) { if(status < method_read) throw WEBDAR_BUG; cached_method = val; };
82
84 void post_to_get() { if(status < method_read) throw WEBDAR_BUG; if(cached_method == "POST") cached_method = "GET"; };
85
87 const uri & get_uri() const { if(status < uri_read) throw WEBDAR_BUG; return coordinates; };
88
90 int get_maj_version() const { if(status != completed) throw WEBDAR_BUG; return maj_vers; };
91
93 int get_min_version() const { if(status != completed) throw WEBDAR_BUG; return min_vers; };
94
96 const std::string & get_body() const { if(status != completed) throw WEBDAR_BUG; return body; };
97
103 std::map<std::string,std::string> get_body_form() const;
104
110 void add_cookie(const std::string & key, const std::string & value) const;
111
113 bool find_cookie(const std::string & key, std::string & value) const;
114
116 bool find_attribute(const std::string & key, std::string & value) const;
117
118
120
124 unsigned int get_multipart_number() const;
125
127
131 std::map<troncon,troncon> get_header_of_multipart(unsigned int num) const;
132
134
137 troncon get_body_of_multipart(unsigned int num) const;
138
140
142 void fake_valid_request();
143
144private:
145 enum { init, method_read, uri_read, reading_all, completed } status;
146
147 std::string cached_method; //< method already read from the next request
148 uri coordinates; //< uri spit in fields
149 unsigned int maj_vers; //< HTTP major version of the last request received
150 unsigned int min_vers; //< HTTP minor version of the last request received
151 std::map<std::string, std::string> attributes; //< request headers
152 std::map<std::string, std::string> cookies; //< request cookies
153 std::string body; //< request body if any
154 std::shared_ptr<central_report> clog; //< central report logging
155
157 typedef std::map<troncon, troncon> mp_header_map;
158
159 mutable std::deque<mp_header_map> mp_headers;
160 mutable std::deque<troncon> mp_body;
161
162 void clear_multipart() { mp_headers.clear(); mp_body.clear(); };
163
165 bool read_method_uri(proto_connexion & input, bool blocking);
166
167 // feed cookies fields from attributes and remove cookies from attributes
168 void extract_cookies();
169
171 void set_version(const std::string & version);
172
173 void add_attribute(const std::string & key, const std::string & value) { attributes.insert(std::pair<std::string, std::string>(webdar_tools_to_canonical_case(key), value)); };
174
175 void drop_attribute(const std::string & key);
176
178 static bool is_empty_line(proto_connexion & input);
179
181 static std::string up_to_eol(proto_connexion & input);
182
184 static std::string up_to_eof(proto_connexion & input);
185
189 static void skip_over(proto_connexion & input, char a);
190
191 static std::string up_to_length(proto_connexion & input, unsigned int length);
192
194 static void skip_line(proto_connexion & input);
195
197
202 static std::string up_to_eol_with_LWS(proto_connexion & input);
203
215 static bool get_token(proto_connexion & input, bool initial, bool blocking, std::string & token);
216
217
219
229 static bool get_word(proto_connexion & input, bool initial, bool blocking, std::string & word);
230
231};
232
233#endif
buffers data from a TCP connexion, this is a pure virtual class
Definition: proto_connexion.hpp:41
class holding fields of an HTTP request (method, URI, header, cookies, and so on)
Definition: request.hpp:45
int get_min_version() const
obtains the MINOR version string of the read request
Definition: request.hpp:93
bool try_reading(proto_connexion &input)
Definition: request.cpp:44
request(const std::shared_ptr< central_report > &log)
The constructor.
Definition: request.hpp:48
std::map< troncon, troncon > get_header_of_multipart(unsigned int num) const
obtains the headers of multiparts once get_multipart_number() has been executed
Definition: request.cpp:423
void clear()
clear all fields of the request
Definition: request.cpp:34
troncon get_body_of_multipart(unsigned int num) const
obtains the body of multiparts once get_multipart_number() has been executed
Definition: request.cpp:431
bool find_cookie(const std::string &key, std::string &value) const
lookup for a cookie
Definition: request.cpp:201
unsigned int get_multipart_number() const
analyse body as a MIME multipart component (RFC 1521)
Definition: request.cpp:231
void fake_valid_request()
set the fields in consistent state to mimic a valid request
Definition: request.cpp:439
bool find_attribute(const std::string &key, std::string &value) const
raw request header header access
Definition: request.cpp:214
int get_maj_version() const
obtains the MAJOR version string of the read request
Definition: request.hpp:90
void read(proto_connexion &input)
Definition: request.cpp:50
const uri & get_uri() const
obtains the URI of the read request
Definition: request.hpp:87
std::map< std::string, std::string > get_body_form() const
Definition: request.cpp:172
const std::string & get_method() const
obtains the method of the read request
Definition: request.hpp:78
void change_method(const std::string &val)
manually change the method of the request
Definition: request.hpp:81
void add_cookie(const std::string &key, const std::string &value) const
Definition: request.cpp:196
void post_to_get()
change POST request to a GET request, No modification for others
Definition: request.hpp:84
const std::string & get_body() const
obtain the body of the read request
Definition: request.hpp:96
uri type holds the splitted list of the scheme / hostname / path # anchor
Definition: uri.hpp:44
void clear()
clear the uri (empty uri)
Definition: uri.hpp:62
defines a substring portion of a existing string (to avoid data copy)
Definition: webdar_tools.hpp:98