SNode.C
Loading...
Searching...
No Matches
Response.cpp
Go to the documentation of this file.
1/*
2 * SNode.C - A Slim Toolkit for Network Communication
3 * Copyright (C) Volker Christian <me@vchrist.at>
4 * 2020, 2021, 2022, 2023, 2024, 2025
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published
8 * by the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20/*
21 * MIT License
22 *
23 * Permission is hereby granted, free of charge, to any person obtaining a copy
24 * of this software and associated documentation files (the "Software"), to deal
25 * in the Software without restriction, including without limitation the rights
26 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
27 * copies of the Software, and to permit persons to whom the Software is
28 * furnished to do so, subject to the following conditions:
29 *
30 * The above copyright notice and this permission notice shall be included in
31 * all copies or substantial portions of the Software.
32 *
33 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
39 * THE SOFTWARE.
40 */
41
42#include "express/Response.h"
43
44#include "express/Request.h"
45#include "web/http/StatusCodes.h"
46#include "web/http/server/Response.h"
47
48#ifndef DOXYGEN_SHOULD_SKIP_THIS
49
50#include <filesystem>
51#include <nlohmann/json.hpp>
52
53#endif /* DOXYGEN_SHOULD_SKIP_THIS */
54
55namespace express {
56
57 Response::Response(const std::shared_ptr<web::http::server::Response>& response) noexcept
58 : responseBase(response) {
59 }
60
62 }
63
64 bool Response::isConnected() const {
66 }
67
70 }
71
72 void Response::json(const nlohmann::json& json) {
73 set("Content-Type", "application/json");
74 send(json.dump());
75 }
76
77 void Response::download(const std::string& file, const std::function<void(int)>& onError) {
78 download(file, std::filesystem::path(file).filename(), onError);
79 }
80
81 void Response::download(const std::string& file, const std::string& fileName, const std::function<void(int)>& onError) {
82 attachment(fileName);
83 sendFile(file, onError);
84 }
85
86 void Response::redirect(const std::string& loc, const std::string& html) {
87 redirect(302, loc, html);
88 }
89
90 void Response::redirect(int state, const std::string& loc, const std::string& html) {
91 location(loc);
92 sendStatus(state, html);
93 }
94
95 void Response::sendStatus(int state, const std::string& html) {
96 this->status(state).send(!html.empty() ? html : web::http::StatusCode::reason(state) + "\r\n");
97 }
98
99 Response& Response::attachment(const std::string& fileName) {
100 set("Content-Disposition", "attachment" + ((!fileName.empty()) ? ("; filename=\"" + fileName + "\"") : ""));
101
102 return *this;
103 }
104
105 Response& Response::location(const std::string& loc) {
106 set("Location", loc);
107
108 return *this;
109 }
110
111 Response& Response::vary(const std::string& field) {
112 append("Vary", field);
113
114 return *this;
115 }
116
117 Response& Response::status(int status) {
118 responseBase->status(status);
119
120 return *this;
121 }
122
123 Response& Response::append(const std::string& field, const std::string& value) {
124 responseBase->append(field, value);
125
126 return *this;
127 }
128
129 Response& Response::set(const std::string& field, const std::string& value, bool overwrite) {
130 responseBase->set(field, value, overwrite);
131
132 return *this;
133 }
134
135 Response& Response::set(const std::map<std::string, std::string>& headers, bool overwrite) {
136 responseBase->set(headers, overwrite);
137
138 return *this;
139 }
140
141 Response& Response::type(const std::string& type) {
142 responseBase->type(type);
143
144 return *this;
145 }
146
147 Response& Response::cookie(const std::string& name, const std::string& value, const std::map<std::string, std::string>& options) {
148 responseBase->cookie(name, value, options);
149
150 return *this;
151 }
152
153 Response& Response::clearCookie(const std::string& name, const std::map<std::string, std::string>& options) {
154 responseBase->clearCookie(name, options);
155
156 return *this;
157 }
158
159 Response& Response::setTrailer(const std::string& field, const std::string& value, bool overwrite) {
160 responseBase->setTrailer(field, value, overwrite);
161
162 return *this;
163 }
164
165 void Response::send(const char* chunk, std::size_t chunkLen) {
166 responseBase->send(chunk, chunkLen);
167 }
168
169 void Response::send(const std::string& chunk) {
170 responseBase->send(chunk);
171 }
172
173 void Response::upgrade(const std::shared_ptr<Request>& request, const std::function<void(const std::string)>& status) {
174 responseBase->upgrade(request->requestBase, status);
175 }
176
177 void Response::end() {
179 }
180
181 void Response::sendFile(const std::string& file, const std::function<void(int)>& callback) {
182 responseBase->sendFile(file, callback);
183 }
184
187
188 return *this;
189 }
190
191 Response& Response::sendFragment(const char* chunk, std::size_t chunkLen) {
192 responseBase->sendFragment(chunk, chunkLen);
193
194 return *this;
195 }
196
197 Response& Response::sendFragment(const std::string& chunk) {
199
200 return *this;
201 }
202
203 const std::string& Response::header(const std::string& field) {
204 return responseBase->header(field);
205 }
206
207} // namespace express
std::shared_ptr< web::http::server::Request > requestBase
Definition Request.h:84
bool isConnected() const
Definition Response.cpp:64
void download(const std::string &file, const std::string &fileName, const std::function< void(int)> &onError)
Definition Response.cpp:81
void redirect(const std::string &loc, const std::string &html={})
Definition Response.cpp:86
std::shared_ptr< web::http::server::Response > responseBase
Definition Response.h:97
Response & sendHeader()
Definition Response.cpp:185
Response & attachment(const std::string &fileName="")
Definition Response.cpp:99
Response & cookie(const std::string &name, const std::string &value, const std::map< std::string, std::string > &options={})
Definition Response.cpp:147
void redirect(int state, const std::string &loc, const std::string &html={})
Definition Response.cpp:90
Response & clearCookie(const std::string &name, const std::map< std::string, std::string > &options={})
Definition Response.cpp:153
void sendFile(const std::string &file, const std::function< void(int)> &callback)
Definition Response.cpp:181
void sendStatus(int state, const std::string &html={})
Definition Response.cpp:95
void download(const std::string &file, const std::function< void(int)> &onError)
Definition Response.cpp:77
Response & set(const std::string &field, const std::string &value, bool overwrite=true)
Definition Response.cpp:129
Response & setTrailer(const std::string &field, const std::string &value, bool overwrite=true)
Definition Response.cpp:159
Response & sendFragment(const char *chunk, std::size_t chunkLen)
Definition Response.cpp:191
Response & append(const std::string &field, const std::string &value)
Definition Response.cpp:123
Response & sendFragment(const std::string &chunk="")
Definition Response.cpp:197
void send(const std::string &chunk)
Definition Response.cpp:169
Response(const std::shared_ptr< web::http::server::Response > &response) noexcept
Definition Response.cpp:57
Response & status(int status)
Definition Response.cpp:117
void send(const char *chunk, std::size_t chunkLen)
Definition Response.cpp:165
Response & vary(const std::string &field)
Definition Response.cpp:111
Response & type(const std::string &type)
Definition Response.cpp:141
Response & set(const std::map< std::string, std::string > &headers, bool overwrite=true)
Definition Response.cpp:135
web::http::server::SocketContext * getSocketContext() const
Definition Response.cpp:68
const std::string & header(const std::string &field)
Definition Response.cpp:203
void json(const nlohmann::json &json)
Definition Response.cpp:72
void upgrade(const std::shared_ptr< Request > &request, const std::function< void(const std::string)> &status)
Definition Response.cpp:173
Response & location(const std::string &loc)
Definition Response.cpp:105
static std::string reason(int status)
SocketContext * getSocketContext() const
Definition Response.cpp:485
Response & status(int statusCode)
Definition Response.cpp:104
void send(const std::string &chunk)
Definition Response.cpp:228
Response & sendFragment(const std::string &chunk="")
Definition Response.cpp:422
void send(const char *chunk, std::size_t chunkLen)
Definition Response.cpp:215
Response & setTrailer(const std::string &field, const std::string &value, bool overwrite=true)
Definition Response.cpp:196
Response & set(const std::map< std::string, std::string > &headers, bool overwrite=true)
Definition Response.cpp:122
Response & set(const std::string &field, const std::string &value, bool overwrite=true)
Definition Response.cpp:130
void upgrade(const std::shared_ptr< Request > &request, const std::function< void(const std::string &)> &status)
Definition Response.cpp:248
Response & type(const std::string &type)
Definition Response.cpp:176
Response & cookie(const std::string &name, const std::string &value, const std::map< std::string, std::string > &options={})
Definition Response.cpp:180
Response & append(const std::string &field, const std::string &value)
Definition Response.cpp:110
Response & sendFragment(const char *chunk, std::size_t chunkLen)
Definition Response.cpp:404
void sendFile(const std::string &file, const std::function< void(int)> &callback)
Definition Response.cpp:324
Response & clearCookie(const std::string &name, const std::map< std::string, std::string > &options={})
Definition Response.cpp:186
const std::string & header(const std::string &field)
Definition Response.cpp:481