forked from cutelyst/cutelyst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenginerequest.h
191 lines (150 loc) · 5.28 KB
/
enginerequest.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
/*
* Copyright (C) 2017 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 ENGINEREQUEST_H
#define ENGINEREQUEST_H
#include <QObject>
#include <QHostAddress>
#include <QElapsedTimer>
#include <Cutelyst/Headers>
namespace Cutelyst {
class Engine;
class Context;
class CUTELYST_LIBRARY EngineRequest
{
Q_GADGET
friend class Engine;
public:
enum StatusFlag {
InitialState = 0x00,
FinalizedHeaders = 0x01,
IOWrite = 0x02,
Chunked = 0x04,
ChunkedDone = 0x08,
Async = 0x10,
};
Q_DECLARE_FLAGS(Status, StatusFlag)
explicit EngineRequest();
virtual ~EngineRequest();
/*!
* Engines must reimplement this to write the
* response body back to the caller
*/
virtual void finalizeBody();
/*!
* Engines should overwrite this if they
* want to to make custom error messages.
* Default implementation render an html
* with errors.
*/
virtual void finalizeError();
/*!
* Called by Application to deal
* with finalizing cookies, headers and body
*/
void finalize();
/*!
* Reimplement if you need a custom way
* to Set-Cookie, the default implementation
* writes them to c->res()->headers()
*/
virtual void finalizeCookies();
/*!
* Finalize the headers, and call
* doWriteHeader(), reimplemententions
* must call this first
*/
virtual bool finalizeHeaders();
/*!
* Called by Response to manually write data
*/
qint64 write(const char *data, qint64 len);
bool webSocketHandshake(const QString &key, const QString &origin, const QString &protocol);
virtual bool webSocketSendTextMessage(const QString &message);
virtual bool webSocketSendBinaryMessage(const QByteArray &message);
virtual bool webSocketSendPing(const QByteArray &payload);
virtual bool webSocketClose(quint16 code, const QString &reason);
protected:
/*!
* Reimplement this to do the RAW writing to the client
*/
virtual qint64 doWrite(const char *data, qint64 len) = 0;
/*!
* This is called when the Application chain is finished
* processing this request, here the request can send final
* bytes to the client or do a clean up.
*
* Default implementation deletes both body and context.
*
* If a WebSocket upgrade was made then you will want to keep
* the context object around.
*/
virtual void processingFinished();
/*!
* Reimplement this to write the headers back to the client
*/
virtual bool writeHeaders(quint16 status, const Headers &headers) = 0;
virtual bool webSocketHandshakeDo(const QString &key, const QString &origin, const QString &protocol);
public:
/*!
* This method sets the path and already does the decoding so that it is
* done a single time.
*
* The path requested by the user agent '/index', MUST NOT have a leading slash
*/
void setPath(char *rawPath, const int len);
inline void setPath(const QString &path) {
QByteArray rawPath = path.toLatin1();
setPath(rawPath.data(), rawPath.size());
}
/*! The method used (GET, POST...) */
QString method;
/*! Call setPath() instead */
QString path;
/*! The query string requested by the user agent 'foo=bar&baz' */
QByteArray query;
/*! The protocol requested by the user agent 'HTTP1/1' */
QString protocol;
/*! The server address which the server is listening to,
* usually the 'Host' header but if that's not present should be filled with the server address */
QString serverAddress;
/*! The remote/client address */
QHostAddress remoteAddress;
/*! The remote user name set by a front web server */
QString remoteUser;
/*! The request headers */
Headers headers;
/*! The timestamp of the start of request, TODO remove in Cutelyst 3 */
quint64 startOfRequest = 0;
/*! Connection status */
Status status = InitialState;
/*! The QIODevice containing the body (if any) of the request
* \note It's deleted when Context gets deleted */
QIODevice *body = nullptr;
/*! The Cutelyst::Context of this request
* \note It's deleted on processingFinished() or destructor */
Context *context = nullptr;
/*! The remote/client port */
quint16 remotePort = 0;
/*! If the connection is secure HTTPS */
bool isSecure = false;
/*! The elapsed timer since the start of request */
QElapsedTimer elapsed;
};
}
Q_DECLARE_OPERATORS_FOR_FLAGS(Cutelyst::EngineRequest::Status)
#endif // ENGINEREQUEST_H