forked from cutelyst/cutelyst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheaders.h
443 lines (370 loc) · 13.9 KB
/
headers.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/*
* Copyright (C) 2014-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 HEADERS_H
#define HEADERS_H
#include <QtCore/QVariant>
#include <QtCore/QDateTime>
#include <QtCore/QMetaType>
#include <Cutelyst/cutelyst_global.h>
namespace Cutelyst {
class CUTELYST_LIBRARY Headers
{
public:
/**
* Construct an empty header object.
*/
Headers();
/**
* Constructs a copy of \pa other.
*/
Headers(const Headers &other);
#ifdef Q_COMPILER_INITIALIZER_LISTS
/**
* Construct a header from a std::initializer_list given by list.
*/
inline Headers(std::initializer_list<std::pair<QString,QString> > list)
{
for (std::initializer_list<std::pair<QString,QString> >::const_iterator it = list.begin(); it != list.end(); ++it)
pushHeader(it->first, it->second);
}
#endif
/**
* The Content-Disposition header field indicates if the content is expected
* to be displayed inline in the browser, that is, as a Web page or as part
* of a Web page, or as an attachment, that is downloaded and saved locally
*/
QString contentDisposition() const;
/**
* Defines the Cache-Control header
*/
void setCacheControl(const QString &value);
/**
* Defines the Content-Disposition header
* \sa contentDisposition()
*/
void setContentDisposition(const QString &contentDisposition);
/**
* Defines the Content-Disposition header as type attachment and the
* optional filename parameter
* \sa contentDisposition()
*/
void setContentDispositionAttachment(const QString &filename = QString());
/**
* The Content-Encoding header field is used as a modifier to the media type.
* When present, its value indicates what additional encoding mechanism has been applied to the resource.
*/
QString contentEncoding() const;
/**
* Defines the Content-Encoding header
* \sa contentEncoding()
*/
void setContentEncoding(const QString &encoding);
/**
* The Content-Type header field indicates the media type of the message content.
* E.g.: "text/html"
* The value returned will be converted to lower case, and potential parameters
* will be ignored. If there is no such header field, then the empty string is returned.
*/
QString contentType() const;
/**
* Defines the Content-Encoding header
* \sa contentType()
*/
void setContentType(const QString &contentType);
/**
* Returns the upper-cased charset specified in the Content-Type header.
*/
QString contentTypeCharset() const;
/**
* The Content-Type header field indicates the media type of the message content.
* this defines the charset of the content-type
*/
void setContentTypeCharset(const QString &charset);
/**
* Returns TRUE if the Content-Type header field indicate that the content is textual.
*/
bool contentIsText() const;
/**
* Returns TRUE if the Content-Type header field indicate that the
* content is some kind of HTML (including XHTML).
*/
bool contentIsHtml() const;
/**
* Returns TRUE if the Content-Type header field indicate that the content is XHTML.
*/
bool contentIsXHtml() const;
/**
* Returns TRUE if the Content-Type header field indicate that the content is XML.
*/
bool contentIsXml() const;
/**
* Returns TRUE if the Content-Type header field indicate that the content is JSON.
*/
bool contentIsJson() const;
/**
* Returns the size in bytes of the message content
*/
qint64 contentLength() const;
/**
* Defines the size in bytes of the message content
*/
void setContentLength(qint64 value);
/**
* Defines the header that represents the date and time at which the message was originated
*/
QString setDateWithDateTime(const QDateTime &date);
/**
* Returns the date header as QDateTime
*/
QDateTime date() const;
/**
* This header fields is used to make a request conditional. If the requested resource has
* (or has not) been modified since the time specified in this field,
* then the server will return a 304 Not Modified response instead of the document itself.
*/
QString ifModifiedSince() const;
/**
* This header fields is used to make a request conditional. If the requested resource has
* (or has not) been modified since the time specified in this field,
* then the server will return a 304 Not Modified response instead of the document itself.
* This method parses the header and convert to QDateTime assuming the date is in GMT
* timezone.
*/
QDateTime ifModifiedSinceDateTime() const;
/**
* Checks if Last-Modified's content has changed in comparison to the
* If-Modified-Since header.
*/
bool ifModifiedSince(const QDateTime &lastModified) const;
/**
* Checks for If-Match header usually used on POST to avoid mid-air collisions, making sure
* the content has not changed while the client changes it.
* Returns true if the etag value matches the value between double quotes of the client header
* or if the client did not provide the If-Match header.
*
* In case of false client should usually discard posted data and return
* status code of 412 - Response::PreconditionFailed
*/
bool ifMatch(const QString &etag) const;
/**
* Checks for If-None-Match header to see if the client has the most recent
* version of a cached resource.
* Returns true if the etag value matches the value between double quotes of the client header.
*
* In case of true client should usually return an empty body along with a
* status code of 304 - Response::NotModified.
*/
bool ifNoneMatch(const QString &etag) const;
/**
* Sets the ETag header in strong form by not prepending a 'W/' (weak etag)
* This method will place the etag value between double quotes, like:
* ETag: "33a64df551425fcc55e4d42a148795d9f25f89d4"
*/
void setETag(const QString &etag);
/**
* This header indicates the date and time at which the resource was last modified.
*/
QString lastModified() const;
/**
* Defines the date and time at which the resource was last modified.
*/
void setLastModified(const QString &value);
/**
* Defines the date and time at which the resource was last modified.
* This method takes a QDateTime and write it in RFC 822 and GMT timezone.
*/
QString setLastModified(const QDateTime &lastModified);
/**
* Returns the server header field contains information about the software
* being used by the originating server program handling the request.
*/
QString server() const;
/**
* Defines the server header field contains information about the software
* being used by the originating server program handling the request.
*/
void setServer(const QString &value);
/**
* Returns the 'Connection' header field that indicates how it should be handled after a
* request has been processed, like 'close'.
*/
QString connection() const;
/**
* Returns the 'Host' header field used in request messages, containing information about which host the client
* would like to talk to, this is especially useful for building URIs and for VirtualHosts.
*/
QString host() const;
/**
* Returns the header field used in request messages, containing information about the user agent originating the request.
*/
QString userAgent() const;
/**
* Used to specify the address (URI) of the document from which the requested resource address was obtained.
*/
QString referer() const;
/**
* Sets the referrer (Referer) header.
* This method removes the fragment from the given URI if it is present,
* as mandated by RFC2616. Note that the removal does not happen automatically
* if using the setHeader() method to set the referrer
*/
void setReferer(const QString &value);
/**
* This header must be included as part of a 401 Unauthorized response.
* The field value consist of a challenge that indicates the authentication scheme
* and parameters applicable to the requested URI.
*/
void setWwwAuthenticate(const QString &value);
/**
* This header must be included in a 407 Proxy Authentication Required response.
*/
void setProxyAuthenticate(const QString &value);
/**
* This method is used to get an authorization header without any decoding.
*/
QString authorization() const;
/**
* This method is used to get an authorization header that use the
* "Basic Authentication Scheme".
* It will return "username:password" as a single string value.
*/
QString authorizationBasic() const;
/**
* This method is used to get an authorization header that use the
* "Basic Authentication Scheme".
* It will return a pair of username and password respectively.
*/
std::pair<QString, QString> authorizationBasicPair() const;
/**
* This method is used to set an authorization header that use the
* "Basic Authentication Scheme".
* It won't set the values if username contains a colon ':'.
*/
QString setAuthorizationBasic(const QString &username, const QString &password);
/**
* A user agent that wishes to authenticate itself with a server or a proxy, may do so by including these headers.
*/
QString proxyAuthorization() const;
/**
* This method is used to get an authorization header that use the
* "Basic Authentication Scheme" but using the "Proxy-Authorization" header instead.
* It will return "username:password" as a single string value.
*/
QString proxyAuthorizationBasic() const;
/**
* This method is used to get an authorization header that use the
* "Basic Authentication Scheme" but using the "Proxy-Authorization" header instead.
* It will return a pair of username and password respectively.
*/
std::pair<QString, QString> proxyAuthorizationBasicPair() const;
/**
* Returns the value associated with \p field
*/
QString header(const QString &field) const;
/**
* Returns the value associated with \p field, if field is not set \p defaultValue is returned
*/
QString header(const QString &field, const QString &defaultValue) const;
/**
* Sets the header field to value
*/
void setHeader(const QString &field, const QString &value);
/**
* Sets the header field to the list of values
*/
void setHeader(const QString &field, const QStringList &values);
/**
* Appends the header field to values
*/
void pushHeader(const QString &field, const QString &value);
/**
* This method directly inserts a Header in it's properly form
* which is field in upper case form with underscores instead of
* dashes, so 'Content-Type' becomes 'CONTENT_TYPE' like CGI does,
* this method should be used only by Engines to get faster performance
* and avoiding normalization.
*/
inline void pushRawHeader(const QString &field, const QString &value);
/**
* This method appends a header to internal data normalizing the key.
*/
void pushHeader(const QString &field, const QStringList &values);
/**
* This method removes a header identified by \p field.
*/
void removeHeader(const QString &field);
/**
* Clears all headers.
*/
inline void clear() {
m_data.clear();
}
/**
* Returns the internal structure of headers, to be used by Engine subclasses.
*/
inline QHash<QString, QString> data() const {
return m_data;
}
/**
* Returns true if the header field is defined.
*/
bool contains(const QString &field);
/**
* Returns the value reference associated with key.
*/
QString &operator[](const QString &key);
/**
* Returns the const value associated with key.
*/
const QString operator[](const QString &key) const;
/**
* Assigns \p other to this Header and returns a reference to this Header.
*/
inline Headers &operator=(const Headers &other)
{
m_data = other.m_data;
return *this;
}
/**
* Compares if another Header object has the same data as this.
*/
inline bool operator==(const Headers &other) const {
return m_data == other.m_data;
}
/**
* Compares if another Header object does not have the same data as this.
*/
inline bool operator!=(const Headers &other) const {
return m_data != other.m_data;
}
/**
* Returns this Header internal data as a QVariant for easiness with Q_PROPERTY.
*/
inline operator QVariant() const {
return QVariant::fromValue(m_data);
}
private:
QHash<QString, QString> m_data;
};
void Headers::pushRawHeader(const QString &field, const QString &value) {
m_data.insertMulti(field, value);
}
}
Q_DECLARE_METATYPE(Cutelyst::Headers)
QDebug CUTELYST_LIBRARY operator<<(QDebug dbg, const Cutelyst::Headers &headers);
#endif // HEADERS_H