forked from cutelyst/cutelyst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse.cpp
364 lines (307 loc) · 9.39 KB
/
response.cpp
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*
* Copyright (C) 2013-2018 Daniel Nicoletti <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "response_p.h"
#include "context_p.h"
#include "engine.h"
#include "enginerequest.h"
#include "common.h"
#include <QtCore/QJsonDocument>
#include <QCryptographicHash>
#include <QEventLoop>
using namespace Cutelyst;
Response::Response(const Headers &defaultHeaders, EngineRequest *engineRequest)
: d_ptr(new ResponsePrivate(defaultHeaders, engineRequest))
{
open(QIODevice::WriteOnly);
}
qint64 Response::readData(char *data, qint64 maxlen)
{
Q_UNUSED(data)
Q_UNUSED(maxlen)
return -1;
}
qint64 Response::writeData(const char *data, qint64 len)
{
Q_D(Response);
if (len <= 0) {
return len;
}
// Finalize headers if someone manually writes output
if (!(d->engineRequest->status & EngineRequest::FinalizedHeaders)) {
if (d->headers.header(QStringLiteral("TRANSFER_ENCODING")) == QLatin1String("chunked")) {
d->engineRequest->status |= EngineRequest::IOWrite | EngineRequest::Chunked;
} else {
// When chunked encoding is not set the client can only know
// that data is finished if we close the connection
d->headers.setHeader(QStringLiteral("CONNECTION"), QStringLiteral("close"));
d->engineRequest->status |= EngineRequest::IOWrite;
}
delete d->bodyIODevice;
d->bodyIODevice = nullptr;
d->bodyData = QByteArray();
d->engineRequest->finalizeHeaders();
}
return d->engineRequest->write(data, len);
}
Response::~Response()
{
delete d_ptr->bodyIODevice;
delete d_ptr;
}
quint16 Response::status() const
{
Q_D(const Response);
return d->status;
}
void Response::setStatus(quint16 status)
{
Q_D(Response);
d->status = status;
}
bool Response::hasBody() const
{
Q_D(const Response);
return !d->bodyData.isEmpty() || d->bodyIODevice || d->engineRequest->status & EngineRequest::IOWrite;
}
QByteArray &Response::body()
{
Q_D(Response);
if (d->bodyIODevice) {
delete d->bodyIODevice;
d->bodyIODevice = nullptr;
}
return d->bodyData;
}
QIODevice *Response::bodyDevice() const
{
Q_D(const Response);
return d->bodyIODevice;
}
void Response::setBody(QIODevice *body)
{
Q_D(Response);
Q_ASSERT(body && body->isOpen() && body->isReadable());
if (!(d->engineRequest->status & EngineRequest::IOWrite)) {
d->bodyData = QByteArray();
if (d->bodyIODevice) {
delete d->bodyIODevice;
}
d->bodyIODevice = body;
}
}
void Response::setBody(const QByteArray &body)
{
Q_D(Response);
d->setBodyData(body);
}
void Response::setJsonBody(const QJsonDocument &documment)
{
Q_D(Response);
const QByteArray body = documment.toJson(QJsonDocument::Compact);
d->setBodyData(body);
d->headers.setContentType(QStringLiteral("application/json"));
}
void Response::setJsonBody(const QString &json)
{
Q_D(Response);
d->setBodyData(json.toUtf8());
d->headers.setContentType(QStringLiteral("application/json"));
}
void Response::setJsonObjectBody(const QJsonObject &object)
{
Q_D(Response);
const QByteArray body = QJsonDocument(object).toJson(QJsonDocument::Compact);
d->setBodyData(body);
d->headers.setContentType(QStringLiteral("application/json"));
}
void Response::setJsonArrayBody(const QJsonArray &array)
{
Q_D(Response);
const QByteArray body = QJsonDocument(array).toJson(QJsonDocument::Compact);
d->setBodyData(body);
d->headers.setContentType(QStringLiteral("application/json"));
}
QString Response::contentEncoding() const
{
Q_D(const Response);
return d->headers.contentEncoding();
}
void Cutelyst::Response::setContentEncoding(const QString &encoding)
{
Q_D(Response);
Q_ASSERT_X(!(d->engineRequest->status & EngineRequest::FinalizedHeaders),
"setContentEncoding",
"setting a header value after finalize_headers and the response callback has been called. Not what you want.");
d->headers.setContentEncoding(encoding);
}
qint64 Response::contentLength() const
{
Q_D(const Response);
return d->headers.contentLength();
}
void Response::setContentLength(qint64 length)
{
Q_D(Response);
Q_ASSERT_X(!(d->engineRequest->status & EngineRequest::FinalizedHeaders),
"setContentLength",
"setting a header value after finalize_headers and the response callback has been called. Not what you want.");
d->headers.setContentLength(length);
}
QString Response::contentType() const
{
Q_D(const Response);
return d->headers.contentType();
}
QString Response::contentTypeCharset() const
{
Q_D(const Response);
return d->headers.contentTypeCharset();
}
QVariant Response::cookie(const QByteArray &name) const
{
Q_D(const Response);
return QVariant::fromValue(d->cookies.value(name));
}
QList<QNetworkCookie> Response::cookies() const
{
Q_D(const Response);
return d->cookies.values();
}
void Response::setCookie(const QNetworkCookie &cookie)
{
Q_D(Response);
d->cookies.insert(cookie.name(), cookie);
}
void Response::setCookies(const QList<QNetworkCookie> &cookies)
{
Q_D(Response);
for (const QNetworkCookie &cookie : cookies) {
d->cookies.insert(cookie.name(), cookie);
}
}
int Response::removeCookies(const QByteArray &name)
{
Q_D(Response);
return d->cookies.remove(name);
}
void Response::redirect(const QUrl &url, quint16 status)
{
Q_D(Response);
d->location = url;
d->status = status;
if (url.isValid()) {
const QString location = QString::fromLatin1(url.toEncoded(QUrl::FullyEncoded));
qCDebug(CUTELYST_RESPONSE) << "Redirecting to" << location << status;
d->headers.setHeader(QStringLiteral("LOCATION"), location);
d->headers.setContentType(QStringLiteral("text/html; charset=utf-8"));
const QString buf = QStringLiteral(
"<!DOCTYPE html>\n"
"<html xmlns=\"http://www.w3.org/1999/xhtml\">\n"
" <head>\n"
" <title>Moved</title>\n"
" </head>\n"
" <body>\n"
" <p>This item has moved <a href=\"") + location +
QStringLiteral("\">here</a>.</p>\n"
" </body>\n"
"</html>\n");
setBody(buf);
} else {
d->headers.removeHeader(QStringLiteral("LOCATION"));
qCDebug(CUTELYST_ENGINE) << "Invalid redirect removing header" << url << status;
}
}
void Response::redirect(const QString &url, quint16 status)
{
redirect(QUrl(url), status);
}
QUrl Response::location() const
{
Q_D(const Response);
return d->location;
}
QString Response::header(const QString &field) const
{
Q_D(const Response);
return d->headers.header(field);
}
void Response::setHeader(const QString &field, const QString &value)
{
Q_D(Response);
Q_ASSERT_X(!(d->engineRequest->status & EngineRequest::FinalizedHeaders),
"setHeader",
"setting a header value after finalize_headers and the response callback has been called. Not what you want.");
d->headers.setHeader(field, value);
}
Headers &Response::headers()
{
Q_D(Response);
return d->headers;
}
bool Response::isSequential() const
{
return true;
}
qint64 Response::size() const
{
Q_D(const Response);
if (d->engineRequest->status & EngineRequest::IOWrite) {
return -1;
} else if (d->bodyIODevice) {
return d->bodyIODevice->size();
} else {
return d->bodyData.size();
}
}
bool Response::webSocketHandshake(const QString &key, const QString &origin, const QString &protocol)
{
Q_D(Response);
return d->engineRequest->webSocketHandshake(key, origin, protocol);
}
bool Response::webSocketTextMessage(const QString &message)
{
Q_D(Response);
return d->engineRequest->webSocketSendTextMessage(message);
}
bool Response::webSocketBinaryMessage(const QByteArray &message)
{
Q_D(Response);
return d->engineRequest->webSocketSendBinaryMessage(message);
}
bool Response::webSocketPing(const QByteArray &payload)
{
Q_D(Response);
return d->engineRequest->webSocketSendPing(payload);
}
bool Response::webSocketClose(quint16 code, const QString &reason)
{
Q_D(Response);
return d->engineRequest->webSocketClose(code, reason);
}
void ResponsePrivate::setBodyData(const QByteArray &body)
{
if (!(engineRequest->status & EngineRequest::IOWrite)) {
if (bodyIODevice) {
delete bodyIODevice;
bodyIODevice = nullptr;
}
bodyData = body;
headers.setContentLength(body.size());
}
}
#include "moc_response.cpp"