-
Notifications
You must be signed in to change notification settings - Fork 2
/
response.hpp
319 lines (282 loc) · 7.4 KB
/
response.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#ifndef RECURSE_RESPONSE_HPP
#define RECURSE_RESPONSE_HPP
#include <QHash>
#include <QJsonDocument>
#include <functional>
class Response
{
public:
//!
//! \brief get
//! Returns the HTTP response header specified by key
//!
//! \param QString case-insensitive key of the header
//! \return QString header
//!
QString getHeader(const QString &key)
{
return m_headers[key.toLower()];
}
//!
//! \brief set
//! Sets the response HTTP header to value.
//!
//! \param QString key of the header
//! \param QString value for the header
//! \return Response chainable
//!
Response &setHeader(const QString &key, const QString &value)
{
m_headers[key] = value;
return *this;
}
//!
//! \brief status
//! Get HTTP response status
//!
//! \return quint16 status
//!
quint16 status() const
{
return m_status;
}
//!
//! \brief status
//! Set HTTP response status
//!
//! \param quint16 status
//! \return Response chainable
//!
Response &status(quint16 status)
{
m_status = status;
return *this;
}
//!
//! \brief type
//! Get the Content-Type HTTP header
//!
//! \return QString MIME content-type
//!
QString type() const
{
return m_headers["content-type"];
}
//!
//! \brief type
//! Sets Content-Type HTTP header
//!
//! \param QString MIME type
//! \return Response chainable
//!
Response &type(const QString &type)
{
m_headers["content-type"] = type;
return *this;
}
//!
//! \brief body
//! Get current response body data content, useful for upstream middleware
//!
//! \return QString response content
//!
QString body() const
{
return m_body;
}
//!
//! \brief body
//! Set response content, overrides existing
//!
//! \param QString body
//! \return Response chainable
//!
Response &body(const QString &body)
{
m_body = body;
return *this;
}
//!
//! \brief write
//! Appends data to existing content (set by write() or body())
//!
//! \param QString data to be added
//! \return Response chainable
//!
Response &write(const QString &data)
{
m_body += data;
return *this;
}
//!
//! \brief send
//! Sends actual data to client
//!
//! \param QString body optional, if provided this is sent instead of current data in buffer
//!
void send(const QString &body = "")
{
if (body.size())
m_body = body;
end();
}
//!
//! \brief send
//! Overloaded function, allows sending QJsonDocument
//!
//! \param body
//!
void send(const QJsonDocument &body)
{
type("application/json");
m_body = body.toJson(QJsonDocument::Compact);
end();
}
//! \brief redirect
//! Perform a 302 redirect to `url`.
//!
//! the string "back" is used to provide Referrer support
//! when Referrer is not present `alt` is used
//!
//! Examples:
//!
//! redirect('back');
//! redirect('back', '/index.html');
//! redirect('/login');
//! redirect('http://google.com');
//!
//! To override status or body set them before calling redirect
//!
//! status(301).body("Redirecting...").redirect("http://www.google.com")
//!
//! \param url to redirect to
//! \param alt used when referrer is not present, "/" by default
void redirect(const QString &url, const QString &alt = "/")
{
// set location
if (url == "back")
{
const QString &referrer = getHeader("referrer");
if (!referrer.isEmpty())
setHeader("Location", referrer);
else
setHeader("Location", alt);
}
else
{
setHeader("Location", url);
}
// set redirect status if not set
// https://tools.ietf.org/html/rfc7231#section-6.4
if (status() < 300 || status() > 308)
status(302);
// set body if not set
if (body().isEmpty())
body("This page has moved to " % url);
end();
}
//!
//! \brief end
//! final function responsible for sending data
//! this is bound to Recurse::end function, from recurse.hpp
//! ctx->response.end = std::bind(&Recurse::end, this, ctx);
//!
std::function<void()> end;
//!
//! \brief method
//! Response method, eg: GET
//!
QString method;
//!
//! \brief protocol
//! Response protocol, eg: HTTP
//!
QString protocol;
//!
//! \brief http codes
//!
QHash<quint16, QString> http_codes{
{ 100, "Continue" },
{ 101, "Switching Protocols" },
{ 200, "OK" },
{ 201, "Created" },
{ 202, "Accepted" },
{ 203, "Non-Authoritative Information" },
{ 204, "No Content" },
{ 205, "Reset Content" },
{ 206, "Partial Content" },
{ 300, "Multiple Choices" },
{ 301, "Moved Permanently" },
{ 302, "Found" },
{ 303, "See Other" },
{ 304, "Not Modified" },
{ 305, "Use Proxy" },
{ 307, "Temporary Redirect" },
{ 400, "Bad Request" },
{ 401, "Unauthorized" },
{ 402, "Payment Required" },
{ 403, "Forbidden" },
{ 404, "Not Found" },
{ 405, "Method Not Allowed" },
{ 406, "Not Acceptable" },
{ 407, "Proxy Authentication Required" },
{ 408, "Request Time-out" },
{ 409, "Conflict" },
{ 410, "Gone" },
{ 411, "Length Required" },
{ 412, "Precondition Failed" },
{ 413, "Request Entity Too Large" },
{ 414, "Request-URI Too Large" },
{ 415, "Unsupported Media Type" },
{ 416, "Requested range not satisfiable" },
{ 417, "Expectation Failed" },
{ 500, "Internal Server Error" },
{ 501, "Not Implemented" },
{ 502, "Bad Gateway" },
{ 503, "Service Unavailable" },
{ 504, "Gateway Time-out" },
{ 505, "HTTP Version not supported" }
};
//!
//! \brief create_reply
//! create reply for sending to client
//!
//! \return QString reply to be sent
//!
QString create_reply();
private:
//!
//! \brief m_status
//! HTTP response status
//!
quint16 m_status = 200;
//!
//! \brief header
//! holds all header data as key/value
//!
QHash<QString, QString> m_headers;
//!
//! \brief m_body
//! HTTP response content
//!
QString m_body;
};
// https://tools.ietf.org/html/rfc7230#page-19
inline QString Response::create_reply()
{
QString reply = this->protocol % " " % QString::number(this->status()) % " "
% this->http_codes[this->status()] % "\r\n";
// set content length
m_headers["content-length"] = QString::number(this->body().size());
// set content type if not set
if (!m_headers.contains("content-type"))
m_headers["content-type"] = "text/plain";
// set custom header fields
for (auto i = m_headers.constBegin(); i != m_headers.constEnd(); ++i)
reply = reply % i.key() % ": " % i.value() % "\r\n";
reply += "\r\n";
if (this->body().size())
reply += this->body();
return reply;
}
#endif