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
66 }
67
68 void Response::json(const nlohmann::json& json) {
69 set("Content-Type", "application/json");
70 send(json.dump());
71 }
72
73 void Response::download(const std::string& file, const std::function<void(int)>& onError) {
74 download(file, std::filesystem::path(file).filename(), onError);
75 }
76
77 void Response::download(const std::string& file, const std::string& fileName, const std::function<void(int)>& onError) {
78 attachment(fileName);
79 sendFile(file, onError);
80 }
81
82 void Response::redirect(const std::string& loc) {
83 redirect(302, loc);
84 }
85
86 void Response::redirect(int state, const std::string& loc) {
87 location(loc);
88 sendStatus(state);
89 }
90
91 void Response::sendStatus(int state) {
92 this->status(state).send(web::http::StatusCode::reason(state) + "\r\n");
93 }
94
95 Response& Response::attachment(const std::string& fileName) {
96 set("Content-Disposition", "attachment" + ((!fileName.empty()) ? ("; filename=\"" + fileName + "\"") : ""));
97
98 return *this;
99 }
100
101 Response& Response::location(const std::string& loc) {
102 set("Location", loc);
103
104 return *this;
105 }
106
107 Response& Response::vary(const std::string& field) {
108 append("Vary", field);
109
110 return *this;
111 }
112
113 Response& Response::status(int status) {
114 responseBase->status(status);
115
116 return *this;
117 }
118
119 Response& Response::append(const std::string& field, const std::string& value) {
120 responseBase->append(field, value);
121
122 return *this;
123 }
124
125 Response& Response::set(const std::string& field, const std::string& value, bool overwrite) {
126 responseBase->set(field, value, overwrite);
127
128 return *this;
129 }
130
131 Response& Response::set(const std::map<std::string, std::string>& headers, bool overwrite) {
132 responseBase->set(headers, overwrite);
133
134 return *this;
135 }
136
137 Response& Response::type(const std::string& type) {
138 responseBase->type(type);
139
140 return *this;
141 }
142
143 Response& Response::cookie(const std::string& name, const std::string& value, const std::map<std::string, std::string>& options) {
144 responseBase->cookie(name, value, options);
145
146 return *this;
147 }
148
149 Response& Response::clearCookie(const std::string& name, const std::map<std::string, std::string>& options) {
150 responseBase->clearCookie(name, options);
151
152 return *this;
153 }
154
155 Response& Response::setTrailer(const std::string& field, const std::string& value, bool overwrite) {
156 responseBase->setTrailer(field, value, overwrite);
157
158 return *this;
159 }
160
161 void Response::send(const char* chunk, std::size_t chunkLen) {
162 responseBase->send(chunk, chunkLen);
163 }
164
165 void Response::send(const std::string& chunk) {
166 responseBase->send(chunk);
167 }
168
169 void Response::upgrade(const std::shared_ptr<Request>& request, const std::function<void(const std::string)>& status) {
170 responseBase->upgrade(request->requestBase, status);
171 }
172
173 void Response::end() {
175 }
176
177 void Response::sendFile(const std::string& file, const std::function<void(int)>& callback) {
178 responseBase->sendFile(file, callback);
179 }
180
183
184 return *this;
185 }
186
187 Response& Response::sendFragment(const char* chunk, std::size_t chunkLen) {
188 responseBase->sendFragment(chunk, chunkLen);
189
190 return *this;
191 }
192
193 Response& Response::sendFragment(const std::string& chunk) {
195
196 return *this;
197 }
198
199 const std::string& Response::header(const std::string& field) {
200 return responseBase->header(field);
201 }
202
203} // namespace express
std::shared_ptr< web::http::server::Request > requestBase
Definition Request.h:84
void download(const std::string &file, const std::string &fileName, const std::function< void(int)> &onError)
Definition Response.cpp:77
std::shared_ptr< web::http::server::Response > responseBase
Definition Response.h:96
Response & sendHeader()
Definition Response.cpp:181
Response & attachment(const std::string &fileName="")
Definition Response.cpp:95
Response & cookie(const std::string &name, const std::string &value, const std::map< std::string, std::string > &options={})
Definition Response.cpp:143
Response & clearCookie(const std::string &name, const std::map< std::string, std::string > &options={})
Definition Response.cpp:149
void sendFile(const std::string &file, const std::function< void(int)> &callback)
Definition Response.cpp:177
void redirect(const std::string &loc)
Definition Response.cpp:82
void download(const std::string &file, const std::function< void(int)> &onError)
Definition Response.cpp:73
Response & set(const std::string &field, const std::string &value, bool overwrite=true)
Definition Response.cpp:125
Response & setTrailer(const std::string &field, const std::string &value, bool overwrite=true)
Definition Response.cpp:155
Response & sendFragment(const std::string &chunk)
Definition Response.cpp:193
Response & sendFragment(const char *chunk, std::size_t chunkLen)
Definition Response.cpp:187
Response & append(const std::string &field, const std::string &value)
Definition Response.cpp:119
void send(const std::string &chunk)
Definition Response.cpp:165
Response(const std::shared_ptr< web::http::server::Response > &response) noexcept
Definition Response.cpp:57
void redirect(int state, const std::string &loc)
Definition Response.cpp:86
Response & status(int status)
Definition Response.cpp:113
void send(const char *chunk, std::size_t chunkLen)
Definition Response.cpp:161
Response & vary(const std::string &field)
Definition Response.cpp:107
Response & type(const std::string &type)
Definition Response.cpp:137
Response & set(const std::map< std::string, std::string > &headers, bool overwrite=true)
Definition Response.cpp:131
web::http::server::SocketContext * getSocketContext() const
Definition Response.cpp:64
const std::string & header(const std::string &field)
Definition Response.cpp:199
void json(const nlohmann::json &json)
Definition Response.cpp:68
void sendStatus(int state)
Definition Response.cpp:91
void upgrade(const std::shared_ptr< Request > &request, const std::function< void(const std::string)> &status)
Definition Response.cpp:169
Response & location(const std::string &loc)
Definition Response.cpp:101
static std::string reason(int status)
SocketContext * getSocketContext() const
Definition Response.cpp:457
Response & status(int statusCode)
Definition Response.cpp:102
void send(const std::string &chunk)
Definition Response.cpp:224
void send(const char *chunk, std::size_t chunkLen)
Definition Response.cpp:213
Response & setTrailer(const std::string &field, const std::string &value, bool overwrite=true)
Definition Response.cpp:194
Response & set(const std::map< std::string, std::string > &headers, bool overwrite=true)
Definition Response.cpp:120
Response & set(const std::string &field, const std::string &value, bool overwrite=true)
Definition Response.cpp:128
void upgrade(const std::shared_ptr< Request > &request, const std::function< void(const std::string &)> &status)
Definition Response.cpp:244
Response & type(const std::string &type)
Definition Response.cpp:174
Response & sendFragment(const std::string &chunk)
Definition Response.cpp:389
Response & cookie(const std::string &name, const std::string &value, const std::map< std::string, std::string > &options={})
Definition Response.cpp:178
Response & append(const std::string &field, const std::string &value)
Definition Response.cpp:108
Response & sendFragment(const char *chunk, std::size_t chunkLen)
Definition Response.cpp:371
void sendFile(const std::string &file, const std::function< void(int)> &callback)
Definition Response.cpp:292
Response & clearCookie(const std::string &name, const std::map< std::string, std::string > &options={})
Definition Response.cpp:184
const std::string & header(const std::string &field)
Definition Response.cpp:453