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#include "express/Response.h"
21
22#include "express/Request.h"
23#include "web/http/StatusCodes.h"
24#include "web/http/server/Response.h"
25
26#ifndef DOXYGEN_SHOULD_SKIP_THIS
27
28#include <filesystem>
29#include <nlohmann/json.hpp>
30
31#endif /* DOXYGEN_SHOULD_SKIP_THIS */
32
33namespace express {
34
35 Response::Response(const std::shared_ptr<web::http::server::Response>& response) noexcept
37 }
38
40 }
41
42 web::http::server::SocketContext* Response::getSocketContext() const {
43 return responseBase->getSocketContext();
44 }
45
46 void Response::json(const nlohmann::json& json) {
47 set("Content-Type", "application/json");
48 send(json.dump());
49 }
50
51 void Response::download(const std::string& file, const std::function<void(int)>& onError) {
52 download(file, std::filesystem::path(file).filename(), onError);
53 }
54
55 void Response::download(const std::string& file, const std::string& fileName, const std::function<void(int)>& onError) {
56 attachment(fileName);
57 sendFile(file, onError);
58 }
59
60 void Response::redirect(const std::string& loc) {
61 redirect(302, loc);
62 }
63
64 void Response::redirect(int state, const std::string& loc) {
65 location(loc);
66 sendStatus(state);
67 }
68
69 void Response::sendStatus(int state) {
70 this->status(state).send(web::http::StatusCode::reason(state) + "\r\n");
71 }
72
73 Response& Response::attachment(const std::string& fileName) {
74 set("Content-Disposition", "attachment" + ((!fileName.empty()) ? ("; filename=\"" + fileName + "\"") : ""));
75
76 return *this;
77 }
78
79 Response& Response::location(const std::string& loc) {
80 set("Location", loc);
81
82 return *this;
83 }
84
85 Response& Response::vary(const std::string& field) {
86 append("Vary", field);
87
88 return *this;
89 }
90
91 Response& Response::status(int status) {
92 responseBase->status(status);
93
94 return *this;
95 }
96
97 Response& Response::append(const std::string& field, const std::string& value) {
98 responseBase->append(field, value);
99
100 return *this;
101 }
102
103 Response& Response::set(const std::string& field, const std::string& value, bool overwrite) {
104 responseBase->set(field, value, overwrite);
105
106 return *this;
107 }
108
109 Response& Response::set(const std::map<std::string, std::string>& headers, bool overwrite) {
110 responseBase->set(headers, overwrite);
111
112 return *this;
113 }
114
115 Response& Response::type(const std::string& type) {
116 responseBase->type(type);
117
118 return *this;
119 }
120
121 Response& Response::cookie(const std::string& name, const std::string& value, const std::map<std::string, std::string>& options) {
122 responseBase->cookie(name, value, options);
123
124 return *this;
125 }
126
127 Response& Response::clearCookie(const std::string& name, const std::map<std::string, std::string>& options) {
128 responseBase->clearCookie(name, options);
129
130 return *this;
131 }
132
133 Response& Response::setTrailer(const std::string& field, const std::string& value, bool overwrite) {
134 responseBase->setTrailer(field, value, overwrite);
135
136 return *this;
137 }
138
139 void Response::send(const char* chunk, std::size_t chunkLen) {
140 responseBase->send(chunk, chunkLen);
141 }
142
143 void Response::send(const std::string& chunk) {
144 responseBase->send(chunk);
145 }
146
147 void Response::upgrade(const std::shared_ptr<Request>& request, const std::function<void(const std::string)>& status) {
148 responseBase->upgrade(request->requestBase, status);
149 }
150
151 void Response::end() {
152 responseBase->end();
153 }
154
155 void Response::sendFile(const std::string& file, const std::function<void(int)>& callback) {
156 responseBase->sendFile(file, callback);
157 }
158
160 responseBase->sendHeader();
161
162 return *this;
163 }
164
165 Response& Response::sendFragment(const char* chunk, std::size_t chunkLen) {
166 responseBase->sendFragment(chunk, chunkLen);
167
168 return *this;
169 }
170
171 Response& Response::sendFragment(const std::string& chunk) {
172 responseBase->sendFragment(chunk);
173
174 return *this;
175 }
176
178 return responseBase->header(field);
179 }
180
181} // namespace express
Response & sendHeader()
Definition Response.cpp:159
Response & attachment(const std::string &fileName="")
Definition Response.cpp:73
void redirect(const std::string &loc)
Definition Response.cpp:60
Response & set(const std::string &field, const std::string &value, bool overwrite=true)
Definition Response.cpp:103
Response & setTrailer(const std::string &field, const std::string &value, bool overwrite=true)
Definition Response.cpp:133
Response & sendFragment(const std::string &chunk)
Definition Response.cpp:171
Response & sendFragment(const char *chunk, std::size_t chunkLen)
Definition Response.cpp:165
Response & append(const std::string &field, const std::string &value)
Definition Response.cpp:97
void send(const std::string &chunk)
Definition Response.cpp:143
Response(const std::shared_ptr< web::http::server::Response > &response) noexcept
Definition Response.cpp:35
void redirect(int state, const std::string &loc)
Definition Response.cpp:64
Response & status(int status)
Definition Response.cpp:91
void send(const char *chunk, std::size_t chunkLen)
Definition Response.cpp:139
Response & vary(const std::string &field)
Definition Response.cpp:85
Response & type(const std::string &type)
Definition Response.cpp:115
web::http::server::SocketContext * getSocketContext() const
Definition Response.cpp:42
const std::string & header(const std::string &field)
Definition Response.cpp:177
void json(const nlohmann::json &json)
Definition Response.cpp:46
void sendStatus(int state)
Definition Response.cpp:69
Response & location(const std::string &loc)
Definition Response.cpp:79