Webdar 1.0.0
Web user interface to libdar
answer.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 ANSWER_HPP
25#define ANSWER_HPP
26
27 // C system header files
28#include "my_config.h"
29extern "C"
30{
31
32}
33
34 // C++ system header files
35#include <string>
36#include <map>
37
38 // webdar headers
39#include "uri.hpp"
40#include "webdar_tools.hpp"
41#include "exceptions.hpp"
42#include "proto_connexion.hpp"
43
45
47
48class answer
49{
50public:
52 answer() { clear(); };
53
55 answer(const answer & ref) { copy_from(ref); };
56
58 answer(answer && ref) noexcept = default;
59
61 answer & operator = (const answer & ref) { copy_from(ref); return *this; };
62
64 answer & operator = (answer && ref) noexcept = default;
65
67 ~answer() = default;
68
69
71
73 void clear();
74
76 void set_status(unsigned int status_code) { status = status_code; };
77
79 void set_reason(const std::string & reason_phrase) { reason = reason_phrase; };
80
82 void set_version(unsigned int maj, unsigned int min) { maj_vers = maj; min_vers = min; };
83
85 void add_cookie(const std::string & key, const std::string & value);
86
88
90 void add_body(const std::string & key);
91
93 void drop_body_keep_header() { body = ""; };
94
96 void set_attribute(const std::string & key, const std::string & value) { attributes[webdar_tools_to_canonical_case(key)] = value; };
97
117 void add_attribute_member(const std::string & key, const std::string & value);
118
119
121
123 bool is_valid() const;
124
125
126
128
130 unsigned int get_status_code() const { return status; };
131
133 const std::string get_reason() const { return reason; };
134
136 unsigned int get_maj_version() const { return maj_vers; };
137
139 unsigned int get_min_version() const { return min_vers; };
140
142 const std::string get_body() const { return body; };
143
149 bool find_attribute(const std::string & key, std::string & value) const;
150
151
153
155 void write(proto_connexion & output);
156
157private:
158 unsigned int status;
159 std::string reason;
160 unsigned int maj_vers;
161 unsigned int min_vers;
162 std::map<std::string, std::string> attributes;
163 std::string body;
164
166 mutable std::map<std::string, std::string>::const_iterator next_read;
167
169 void reset_read_next_attribute() const;
170
176 bool read_next_attribute(std::string & key, std::string & value) const;
177
179 void copy_from(const answer & ref);
180
181};
182
183#endif
class answer provides easy means to set an HTTP answer and means to sent it back to a proto_connexion...
Definition: answer.hpp:49
void drop_body_keep_header()
removes the body keeping header untouched (Content-Length in particular)
Definition: answer.hpp:93
answer & operator=(const answer &ref)
assignment copy operator
Definition: answer.hpp:61
bool find_attribute(const std::string &key, std::string &value) const
Definition: answer.cpp:88
void clear()
clear all information from the object
Definition: answer.cpp:44
unsigned int get_min_version() const
get decimal part of the version info
Definition: answer.hpp:139
void set_reason(const std::string &reason_phrase)
set reason [optional]
Definition: answer.hpp:79
unsigned int get_status_code() const
get answer status code and reason
Definition: answer.hpp:130
const std::string get_body() const
get the current body of the answer
Definition: answer.hpp:142
void add_cookie(const std::string &key, const std::string &value)
add cookie to the answer [optional]
Definition: answer.cpp:54
answer(answer &&ref) noexcept=default
move constructor
void set_status(unsigned int status_code)
set answer status code and reason [MANDATORY]
Definition: answer.hpp:76
answer(const answer &ref)
copy constructor
Definition: answer.hpp:55
void add_attribute_member(const std::string &key, const std::string &value)
Definition: answer.cpp:74
answer()
class constructor
Definition: answer.hpp:52
bool is_valid() const
whether the minimal parameters have been set
Definition: answer.cpp:83
void set_attribute(const std::string &key, const std::string &value)
set a given attribute to the HTTP header
Definition: answer.hpp:96
~answer()=default
destructor
void add_body(const std::string &key)
adds the body to the answer [optional]
Definition: answer.cpp:68
void write(proto_connexion &output)
send the answer
Definition: answer.cpp:102
unsigned int get_maj_version() const
get integer part of the version info
Definition: answer.hpp:136
const std::string get_reason() const
get reason
Definition: answer.hpp:133
void set_version(unsigned int maj, unsigned int min)
set version info [MANDATORY]
Definition: answer.hpp:82
buffers data from a TCP connexion, this is a pure virtual class
Definition: proto_connexion.hpp:41