forked from cutelyst/cutelyst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse.h
370 lines (318 loc) · 11.8 KB
/
response.h
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
365
366
367
368
369
370
/*
* 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
*/
#ifndef CUTELYST_RESPONSE_H
#define CUTELYST_RESPONSE_H
#include <QtCore/QIODevice>
#include <Cutelyst/cutelyst_global.h>
#include <Cutelyst/headers.h>
class QNetworkCookie;
namespace Cutelyst {
class Context;
class Engine;
class EngineRequest;
class ResponsePrivate;
class CUTELYST_LIBRARY Response : public QIODevice
{
Q_OBJECT
Q_DECLARE_PRIVATE(Response)
public:
/** This enum type specifies the status response to be sent to the client */
enum HttpStatus {
Continue = 100,
SwitchingProtocols = 101,
OK = 200,
Created = 201,
Accepted = 202,
NonAuthoritativeInformation = 203,
NoContent = 204,
ResetContent = 205,
PartialContent = 206,
MultiStatus = 207,
MultipleChoices = 300,
MovedPermanently = 301,
Found = 302,
SeeOther = 303, // Since HTTP/1.1
NotModified = 304,
UseProxy = 305, // Since HTTP/1.1
TemporaryRedirect = 307, // Since HTTP/1.1
PermanentRedirect = 308, // Since HTTP/1.1
BadRequest = 400,
Unauthorized = 401,
PaymentRequired = 402,
Forbidden = 403,
NotFound = 404,
MethodNotAllowed = 405,
NotAcceptable = 406,
ProxyAuthenticationRequired = 407,
RequestTimeout = 408,
Conflict = 409,
Gone = 410,
LengthRequired = 411,
PreconditionFailed = 412,
RequestEntityTooLarge = 413,
RequestURITooLong = 414,
UnsupportedMediaType = 415,
RequestedRangeNotSatisfiable = 416,
ExpectationFailed = 417,
InternalServerError = 500,
NotImplemented = 501,
BadGateway = 502,
ServiceUnavailable = 503,
GatewayTimeout = 504,
HTTPVersionNotSupported = 505,
BandwidthLimitExceeded = 509
};
Q_ENUM(HttpStatus)
/** This enum type specifies the status response to be sent to the client */
enum CloseCode {
CloseCodeNormal = 1000,
CloseCodeGoingAway = 1001,
CloseCodeProtocolError = 1002,
CloseCodeDatatypeNotSupported = 1003,
CloseCodeReserved1004 = 1004,
CloseCodeMissingStatusCode = 1005,
CloseCodeAbnormalDisconnection = 1006,
CloseCodeWrongDatatype = 1007,
CloseCodePolicyViolated = 1008,
CloseCodeTooMuchData = 1009,
CloseCodeMissingExtension = 1010,
CloseCodeBadOperation = 1011,
CloseCodeTlsHandshakeFailed = 1015
};
Q_ENUM(CloseCode)
virtual ~Response() override;
/**
* The current response code status
*/
quint16 status() const;
/**
* Sets the response code status
*/
void setStatus(quint16 status);
/**
* Returns true if a body device has been defined
* as QByteArray or QIODevice or write() was called
* and it's on chunked mode
*/
bool hasBody() const;
/**
* This function returns a reference to a
* QByteArray which implicity sets the body
* device to a QBuffer, even if one was already
* set.
*/
Q_REQUIRED_RESULT QByteArray &body();
/**
* Returns the body IO device (if any) of this response.
*/
QIODevice *bodyDevice() const;
/**
* Sets an IO device as the response body,
* the open mode must be at least QIODevice::ReadOnly.
* This function takes ownership of your device
* deleting after the request has completed
*/
void setBody(QIODevice *body);
/**
* Sets a QByteArray as the response body,
* content length will be automatically set to it's size.
*/
void setBody(const QByteArray &body);
/**
* Sets a QString as the response body, the output will be UTF-8 and
* content length will be automatically set to it's size.
*/
inline void setBody(const QString &body);
/**
* Sets a QJsonDocument as the response body,
* using toJson(QJsonDocument::Compact) output and setting
* content-type to application/json.
*/
void setJsonBody(const QJsonDocument &documment);
/**
* Sets a JSON string as the response body,
* this method is provided for convenience as it sets the content-type to application/json.
*/
void setJsonBody(const QString &json);
/**
* Sets a QJsonObject on a QJsonDocument as the response body,
* using toJson(QJsonDocument::Compact) output and setting
* content-type to application/json.
*/
void setJsonObjectBody(const QJsonObject &object);
/**
* Sets a QJsonArray on a QJsonDocument as the response body,
* using toJson(QJsonDocument::Compact) output and setting
* content-type to application/json.
*/
void setJsonArrayBody(const QJsonArray &array);
/**
* Short for headers().contentEncoding();
*/
QString contentEncoding() const;
/**
* Short for headers().setContentEncoding(encoding);
*/
void setContentEncoding(const QString &encoding);
/**
* Short for headers().contentLength();
*/
qint64 contentLength() const;
/**
* Short for headers().setContentLength(length);
*/
void setContentLength(qint64 length);
/**
* Short for headers().contentType();
*/
QString contentType() const;
/**
* Short for headers().setContentType(type);
*/
void setContentType(const QString &type)
{ headers().setContentType(type); }
/**
* Short for headers().contentTypeCharset();
*/
QString contentTypeCharset() const;
/**
* Returns the first QNetworkCookie matching the name
* or a null QVariant if not found
*/
QVariant cookie(const QByteArray &name) const;
/**
* Returns a list of all cookies set
*/
QList<QNetworkCookie> cookies() const;
/**
* Defines a QNetworkCookie to be sent to the user-agent,
* if a previous cookie->name() was set it will be replaced
*/
void setCookie(const QNetworkCookie &cookie);
/**
* Defines a list of QNetworkCookie to be sent to the user-agent,
* all previous matches to cookie->name() will be preserved.
*/
void setCookies(const QList<QNetworkCookie> &cookies);
/**
* Removes all cookies that matches name, returning
* the number of cookies removed
*/
int removeCookies(const QByteArray &name);
/**
* Causes the response to redirect to the specified URL. The default status is 302.
* This is a convenience method that sets the Location header to the redirect
* destination, and then sets the response status. You will want to return false or
* c->detach() to interrupt the normal processing flow if you want the redirect to
* occur straight away.
*
* \note do not give a relative URL as $url, i.e: one that is not fully
* qualified ("http://...", etc.) or that starts with a slash "/path/here".
* While it may work, it is not guaranteed to do the right thing and is not a
* standard behaviour. You may opt to use uriFor() or uriForAction() instead.
*/
void redirect(const QUrl &url, quint16 status = Found);
/**
* Causes the response to redirect to the specified URL. The default status is 302.
* This is a convenience method that sets the Location header to the redirect
* destination, and then sets the response status. You will want to return false or
* c->detach() to interrupt the normal processing flow if you want the redirect to
* occur straight away.
*
* \note do not give a relative URL as $url, i.e: one that is not fully
* qualified ("http://...", etc.) or that starts with a slash "/path/here".
* While it may work, it is not guaranteed to do the right thing and is not a
* standard behaviour. You may opt to use uriFor() or uriForAction() instead.
*/
void redirect(const QString &url, quint16 status = Found);
/**
* Returns the HTTP location set by the redirect
*/
QUrl location() const;
/**
* Shortcut headers().header()
*/
QString header(const QString &field) const;
/**
* Shortcut headers().setHeader()
*/
void setHeader(const QString &field, const QString &value);
/**
* Returns a reference to the response headers class
*/
Headers &headers();
/**
* Writting to user-agent is always sequential
*/
virtual bool isSequential() const override;
/**
* Reimplemented from QIODevice::readData().
*/
virtual qint64 size() const override;
/*!
* Sends the websocket handshake, if no parameters are defined it will use header data.
* Returns true in case of success, false otherwise, which can be due missing support on
* the engine or missing the appropriate headers.
*/
bool webSocketHandshake(const QString &key = QString(), const QString &origin = QString(), const QString &protocol = QString());
/*!
* Sends a WebSocket text message
*/
bool webSocketTextMessage(const QString &message);
/*!
* Sends a WebSocket binary message
*/
bool webSocketBinaryMessage(const QByteArray &message);
/*!
* Sends a WebSocket ping with an optional payload limited to 125 bytes,
* which will be truncated if larger.
*/
bool webSocketPing(const QByteArray &payload = QByteArray());
/*!
* Sends a WebSocket close frame, with both optional close code and a string reason.
*/
bool webSocketClose(quint16 code = Response::CloseCodeNormal, const QString &reason = QString());
protected:
/**
* Constructs a Response object, for this engine request and defaultHeaders.
*/
explicit Response(const Headers &defaultHeaders, EngineRequest *conn = nullptr);
/**
* Writes data to the response body, this will flush
* all response headers first and will enter in chunked
* mode if Transfer-Encoding header is set to chunked
* or if no content length is set and status is
* not 1xx or 204 (NoContent) or 304 (NotModified)
*/
virtual qint64 writeData(const char *data, qint64 len) override;
/**
* Reimplemented from QIODevice::readData().
*/
virtual qint64 readData(char *data, qint64 maxlen) override;
ResponsePrivate *d_ptr;
friend class Application;
friend class Engine;
friend class EngineConnection;
friend class Context;
friend class ContextPrivate;
};
inline void Response::setBody(const QString &_body) {
setBody(_body.toUtf8());
}
}
#endif // CUTELYST_RESPONSE_H