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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
|
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtNetwork module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qplatformdefs.h"
#include "qnetworkrequest.h"
#include "qnetworkcookie.h"
#include "qnetworkrequest_p.h"
#include "qsslconfiguration.h"
#include "QtCore/qshareddata.h"
#include "QtCore/qlocale.h"
#include "QtCore/qdatetime.h"
#include <ctype.h>
#ifndef QT_NO_DATESTRING
# include <stdio.h>
#endif
QT_BEGIN_NAMESPACE
/*!
\class QNetworkRequest
\brief The QNetworkRequest class holds a request to be sent with QNetworkAccessManager.
\since 4.4
\ingroup network
\inmodule QtNetwork
QNetworkRequest is part of the Network Access API and is the class
holding the information necessary to send a request over the
network. It contains a URL and some ancillary information that can
be used to modify the request.
\sa QNetworkReply, QNetworkAccessManager
*/
/*!
\enum QNetworkRequest::KnownHeaders
List of known header types that QNetworkRequest parses. Each known
header is also represented in raw form with its full HTTP name.
\value ContentTypeHeader corresponds to the HTTP Content-Type
header and contains a string containing the media (MIME) type and
any auxiliary data (for instance, charset)
\value ContentLengthHeader corresponds to the HTTP Content-Length
header and contains the length in bytes of the data transmitted.
\value LocationHeader corresponds to the HTTP Location
header and contains a URL representing the actual location of the
data, including the destination URL in case of redirections.
\value LastModifiedHeader corresponds to the HTTP Last-Modified
header and contains a QDateTime representing the last modification
date of the contents
\value CookieHeader corresponds to the HTTP Cookie header
and contains a QList<QNetworkCookie> representing the cookies to
be sent back to the server
\value SetCookieHeader corresponds to the HTTP Set-Cookie
header and contains a QList<QNetworkCookie> representing the
cookies sent by the server to be stored locally
\sa header(), setHeader(), rawHeader(), setRawHeader()
*/
/*!
\enum QNetworkRequest::Attribute
Attribute codes for the QNetworkRequest and QNetworkReply.
Attributes are extra meta-data that are used to control the
behavior of the request and to pass further information from the
reply back to the application. Attributes are also extensible,
allowing custom implementations to pass custom values.
The following table explains what the default attribute codes are,
the QVariant types associated, the default value if said attribute
is missing and whether it's used in requests or replies.
\value HttpStatusCodeAttribute
Replies only, type: QVariant::Int (no default)
Indicates the HTTP status code received from the HTTP server
(like 200, 304, 404, 401, etc.). If the connection was not
HTTP-based, this attribute will not be present.
\value HttpReasonPhraseAttribute
Replies only, type: QVariant::ByteArray (no default)
Indicates the HTTP reason phrase as received from the HTTP
server (like "Ok", "Found", "Not Found", "Access Denied",
etc.) This is the human-readable representation of the status
code (see above). If the connection was not HTTP-based, this
attribute will not be present.
\value RedirectionTargetAttribute
Replies only, type: QVariant::Url (no default)
If present, it indicates that the server is redirecting the
request to a different URL. The Network Access API does not by
default follow redirections: it's up to the application to
determine if the requested redirection should be allowed,
according to its security policies.
\value ConnectionEncryptedAttribute
Replies only, type: QVariant::Bool (default: false)
Indicates whether the data was obtained through an encrypted
(secure) connection.
\value CacheLoadControlAttribute
Requests only, type: QVariant::Int (default: QNetworkRequest::PreferNetwork)
Controls how the cache should be accessed. The possible values
are those of QNetworkRequest::CacheLoadControl. Note that the
default QNetworkAccessManager implementation does not support
caching. However, this attribute may be used by certain
backends to modify their requests (for example, for caching proxies).
\value CacheSaveControlAttribute
Requests only, type: QVariant::Bool (default: true)
Controls if the data obtained should be saved to cache for
future uses. If the value is false, the data obtained will not
be automatically cached. If true, data may be cached, provided
it is cacheable (what is cacheable depends on the protocol
being used).
\value SourceIsFromCacheAttribute
Replies only, type: QVariant::Bool (default: false)
Indicates whether the data was obtained from cache
or not.
\value DoNotBufferUploadDataAttribute
Requests only, type: QVariant::Bool (default: false)
Indicates whether the QNetworkAccessManager code is
allowed to buffer the upload data, e.g. when doing a HTTP POST.
When using this flag with sequential upload data, the ContentLengthHeader
header must be set.
\value HttpPipeliningAllowedAttribute
Requests only, type: QVariant::Bool (default: false)
Indicates whether the QNetworkAccessManager code is
allowed to use HTTP pipelining with this request.
\value HttpPipeliningWasUsedAttribute
Replies only, type: QVariant::Bool
Indicates whether the HTTP pipelining was used for receiving
this reply.
\value User
Special type. Additional information can be passed in
QVariants with types ranging from User to UserMax. The default
implementation of Network Access will ignore any request
attributes in this range and it will not produce any
attributes in this range in replies. The range is reserved for
extensions of QNetworkAccessManager.
\value UserMax
Special type. See User.
*/
/*!
\enum QNetworkRequest::CacheLoadControl
Controls the caching mechanism of QNetworkAccessManager.
\value AlwaysNetwork always load from network and do not
check if the cache has a valid entry (similar to the
"Reload" feature in browsers)
\value PreferNetwork default value; load from the network
if the cached entry is older than the network entry
\value PreferCache load from cache if available,
otherwise load from network. Note that this can return possibly
stale (but not expired) items from cache.
\value AlwaysCache only load from cache, indicating error
if the item was not cached (i.e., off-line mode)
*/
class QNetworkRequestPrivate: public QSharedData, public QNetworkHeadersPrivate
{
public:
inline QNetworkRequestPrivate()
#ifndef QT_NO_OPENSSL
: sslConfiguration(0)
#endif
{ qRegisterMetaType<QNetworkRequest>(); }
~QNetworkRequestPrivate()
{
#ifndef QT_NO_OPENSSL
delete sslConfiguration;
#endif
}
QNetworkRequestPrivate(const QNetworkRequestPrivate &other)
: QSharedData(other), QNetworkHeadersPrivate(other)
{
url = other.url;
#ifndef QT_NO_OPENSSL
sslConfiguration = 0;
if (other.sslConfiguration)
sslConfiguration = new QSslConfiguration(*other.sslConfiguration);
#endif
}
inline bool operator==(const QNetworkRequestPrivate &other) const
{
return url == other.url &&
rawHeaders == other.rawHeaders &&
attributes == other.attributes;
// don't compare cookedHeaders
}
QUrl url;
#ifndef QT_NO_OPENSSL
mutable QSslConfiguration *sslConfiguration;
#endif
};
/*!
Constructs a QNetworkRequest object with \a url as the URL to be
requested.
\sa url(), setUrl()
*/
QNetworkRequest::QNetworkRequest(const QUrl &url)
: d(new QNetworkRequestPrivate)
{
d->url = url;
}
/*!
Creates a copy of \a other.
*/
QNetworkRequest::QNetworkRequest(const QNetworkRequest &other)
: d(other.d)
{
}
/*!
Disposes of the QNetworkRequest object.
*/
QNetworkRequest::~QNetworkRequest()
{
// QSharedDataPointer auto deletes
d = 0;
}
/*!
Returns true if this object is the same as \a other (i.e., if they
have the same URL, same headers and same meta-data settings).
\sa operator!=()
*/
bool QNetworkRequest::operator==(const QNetworkRequest &other) const
{
return d == other.d || *d == *other.d;
}
/*!
\fn bool QNetworkRequest::operator!=(const QNetworkRequest &other) const
Returns false if this object is not the same as \a other.
\sa operator==()
*/
/*!
Creates a copy of \a other
*/
QNetworkRequest &QNetworkRequest::operator=(const QNetworkRequest &other)
{
d = other.d;
return *this;
}
/*!
Returns the URL this network request is referring to.
\sa setUrl()
*/
QUrl QNetworkRequest::url() const
{
return d->url;
}
/*!
Sets the URL this network request is referring to to be \a url.
\sa url()
*/
void QNetworkRequest::setUrl(const QUrl &url)
{
d->url = url;
}
/*!
Returns the value of the known network header \a header if it is
present in this request. If it is not present, returns QVariant()
(i.e., an invalid variant).
\sa KnownHeaders, rawHeader(), setHeader()
*/
QVariant QNetworkRequest::header(KnownHeaders header) const
{
return d->cookedHeaders.value(header);
}
/*!
Sets the value of the known header \a header to be \a value,
overriding any previously set headers. This operation also sets
the equivalent raw HTTP header.
\sa KnownHeaders, setRawHeader(), header()
*/
void QNetworkRequest::setHeader(KnownHeaders header, const QVariant &value)
{
d->setCookedHeader(header, value);
}
/*!
Returns true if the raw header \a headerName is present in this
network request.
\sa rawHeader(), setRawHeader()
*/
bool QNetworkRequest::hasRawHeader(const QByteArray &headerName) const
{
return d->findRawHeader(headerName) != d->rawHeaders.constEnd();
}
/*!
Returns the raw form of header \a headerName. If no such header is
present, an empty QByteArray is returned, which may be
indistinguishable from a header that is present but has no content
(use hasRawHeader() to find out if the header exists or not).
Raw headers can be set with setRawHeader() or with setHeader().
\sa header(), setRawHeader()
*/
QByteArray QNetworkRequest::rawHeader(const QByteArray &headerName) const
{
QNetworkHeadersPrivate::RawHeadersList::ConstIterator it =
d->findRawHeader(headerName);
if (it != d->rawHeaders.constEnd())
return it->second;
return QByteArray();
}
/*!
Returns a list of all raw headers that are set in this network
request. The list is in the order that the headers were set.
\sa hasRawHeader(), rawHeader()
*/
QList<QByteArray> QNetworkRequest::rawHeaderList() const
{
return d->rawHeadersKeys();
}
/*!
Sets the header \a headerName to be of value \a headerValue. If \a
headerName corresponds to a known header (see
QNetworkRequest::KnownHeaders), the raw format will be parsed and
the corresponding "cooked" header will be set as well.
For example:
\snippet doc/src/snippets/code/src_network_access_qnetworkrequest.cpp 0
will also set the known header LastModifiedHeader to be the
QDateTime object of the parsed date.
Note: setting the same header twice overrides the previous
setting. To accomplish the behaviour of multiple HTTP headers of
the same name, you should concatenate the two values, separating
them with a comma (",") and set one single raw header.
\sa KnownHeaders, setHeader(), hasRawHeader(), rawHeader()
*/
void QNetworkRequest::setRawHeader(const QByteArray &headerName, const QByteArray &headerValue)
{
d->setRawHeader(headerName, headerValue);
}
/*!
Returns the attribute associated with the code \a code. If the
attribute has not been set, it returns \a defaultValue.
Note: this function does not apply the defaults listed in
QNetworkRequest::Attribute.
\sa setAttribute(), QNetworkRequest::Attribute
*/
QVariant QNetworkRequest::attribute(Attribute code, const QVariant &defaultValue) const
{
return d->attributes.value(code, defaultValue);
}
/*!
Sets the attribute associated with code \a code to be value \a
value. If the attribute is already set, the previous value is
discarded. In special, if \a value is an invalid QVariant, the
attribute is unset.
\sa attribute(), QNetworkRequest::Attribute
*/
void QNetworkRequest::setAttribute(Attribute code, const QVariant &value)
{
if (value.isValid())
d->attributes.insert(code, value);
else
d->attributes.remove(code);
}
#ifndef QT_NO_OPENSSL
/*!
Returns this network request's SSL configuration. By default, no
SSL settings are specified.
\sa setSslConfiguration()
*/
QSslConfiguration QNetworkRequest::sslConfiguration() const
{
if (!d->sslConfiguration)
d->sslConfiguration = new QSslConfiguration;
return *d->sslConfiguration;
}
/*!
Sets this network request's SSL configuration to be \a config. The
settings that apply are the private key, the local certificate,
the SSL protocol (SSLv2, SSLv3, TLSv1 where applicable) and the
ciphers that the SSL backend is allowed to use.
By default, no SSL configuration is set, which allows the backends
to choose freely what configuration is best for them.
\sa sslConfiguration(), QSslConfiguration::defaultConfiguration()
*/
void QNetworkRequest::setSslConfiguration(const QSslConfiguration &config)
{
if (!d->sslConfiguration)
d->sslConfiguration = new QSslConfiguration(config);
else
*d->sslConfiguration = config;
}
#endif
/*!
\since 4.6
Allows setting a reference to the \a object initiating
the request.
For example QtWebKit sets the originating object to the
QWebFrame that initiated the request.
\sa originatingObject()
*/
void QNetworkRequest::setOriginatingObject(QObject *object)
{
d->originatingObject = object;
}
/*!
\since 4.6
Returns a reference to the object that initiated this
network request; returns 0 if not set or the object has
been destroyed.
\sa setOriginatingObject()
*/
QObject *QNetworkRequest::originatingObject() const
{
return d->originatingObject.data();
}
static QByteArray headerName(QNetworkRequest::KnownHeaders header)
{
switch (header) {
case QNetworkRequest::ContentTypeHeader:
return "Content-Type";
case QNetworkRequest::ContentLengthHeader:
return "Content-Length";
case QNetworkRequest::LocationHeader:
return "Location";
case QNetworkRequest::LastModifiedHeader:
return "Last-Modified";
case QNetworkRequest::CookieHeader:
return "Cookie";
case QNetworkRequest::SetCookieHeader:
return "Set-Cookie";
// no default:
// if new values are added, this will generate a compiler warning
}
return QByteArray();
}
static QByteArray headerValue(QNetworkRequest::KnownHeaders header, const QVariant &value)
{
switch (header) {
case QNetworkRequest::ContentTypeHeader:
case QNetworkRequest::ContentLengthHeader:
return value.toByteArray();
case QNetworkRequest::LocationHeader:
switch (value.type()) {
case QVariant::Url:
return value.toUrl().toEncoded();
default:
return value.toByteArray();
}
case QNetworkRequest::LastModifiedHeader:
switch (value.type()) {
case QVariant::Date:
case QVariant::DateTime:
// generate RFC 1123/822 dates:
return QNetworkHeadersPrivate::toHttpDate(value.toDateTime());
default:
return value.toByteArray();
}
case QNetworkRequest::CookieHeader: {
QList<QNetworkCookie> cookies = qvariant_cast<QList<QNetworkCookie> >(value);
if (cookies.isEmpty() && value.userType() == qMetaTypeId<QNetworkCookie>())
cookies << qvariant_cast<QNetworkCookie>(value);
QByteArray result;
bool first = true;
foreach (const QNetworkCookie &cookie, cookies) {
if (!first)
result += "; ";
first = false;
result += cookie.toRawForm(QNetworkCookie::NameAndValueOnly);
}
return result;
}
case QNetworkRequest::SetCookieHeader: {
QList<QNetworkCookie> cookies = qvariant_cast<QList<QNetworkCookie> >(value);
if (cookies.isEmpty() && value.userType() == qMetaTypeId<QNetworkCookie>())
cookies << qvariant_cast<QNetworkCookie>(value);
QByteArray result;
bool first = true;
foreach (const QNetworkCookie &cookie, cookies) {
if (!first)
result += ", ";
first = false;
result += cookie.toRawForm(QNetworkCookie::Full);
}
return result;
}
}
return QByteArray();
}
static QNetworkRequest::KnownHeaders parseHeaderName(const QByteArray &headerName)
{
// headerName is not empty here
switch (tolower(headerName.at(0))) {
case 'c':
if (qstricmp(headerName.constData(), "content-type") == 0)
return QNetworkRequest::ContentTypeHeader;
else if (qstricmp(headerName.constData(), "content-length") == 0)
return QNetworkRequest::ContentLengthHeader;
else if (qstricmp(headerName.constData(), "cookie") == 0)
return QNetworkRequest::CookieHeader;
break;
case 'l':
if (qstricmp(headerName.constData(), "location") == 0)
return QNetworkRequest::LocationHeader;
else if (qstricmp(headerName.constData(), "last-modified") == 0)
return QNetworkRequest::LastModifiedHeader;
break;
case 's':
if (qstricmp(headerName.constData(), "set-cookie") == 0)
return QNetworkRequest::SetCookieHeader;
break;
}
return QNetworkRequest::KnownHeaders(-1); // nothing found
}
static QVariant parseHttpDate(const QByteArray &raw)
{
QDateTime dt = QNetworkHeadersPrivate::fromHttpDate(raw);
if (dt.isValid())
return dt;
return QVariant(); // transform an invalid QDateTime into a null QVariant
}
static QVariant parseCookieHeader(const QByteArray &raw)
{
QList<QNetworkCookie> result;
QList<QByteArray> cookieList = raw.split(';');
foreach (QByteArray cookie, cookieList) {
QList<QNetworkCookie> parsed = QNetworkCookie::parseCookies(cookie.trimmed());
if (parsed.count() != 1)
return QVariant(); // invalid Cookie: header
result += parsed;
}
return qVariantFromValue(result);
}
static QVariant parseHeaderValue(QNetworkRequest::KnownHeaders header, const QByteArray &value)
{
// header is always a valid value
switch (header) {
case QNetworkRequest::ContentTypeHeader:
// copy exactly, convert to QString
return QString::fromLatin1(value);
case QNetworkRequest::ContentLengthHeader: {
bool ok;
qint64 result = value.trimmed().toLongLong(&ok);
if (ok)
return result;
return QVariant();
}
case QNetworkRequest::LocationHeader: {
QUrl result = QUrl::fromEncoded(value, QUrl::StrictMode);
if (result.isValid() && !result.scheme().isEmpty())
return result;
return QVariant();
}
case QNetworkRequest::LastModifiedHeader:
return parseHttpDate(value);
case QNetworkRequest::CookieHeader:
return parseCookieHeader(value);
case QNetworkRequest::SetCookieHeader:
return qVariantFromValue(QNetworkCookie::parseCookies(value));
default:
Q_ASSERT(0);
}
return QVariant();
}
QNetworkHeadersPrivate::RawHeadersList::ConstIterator
QNetworkHeadersPrivate::findRawHeader(const QByteArray &key) const
{
RawHeadersList::ConstIterator it = rawHeaders.constBegin();
RawHeadersList::ConstIterator end = rawHeaders.constEnd();
for ( ; it != end; ++it)
if (qstricmp(it->first.constData(), key.constData()) == 0)
return it;
return end; // not found
}
QList<QByteArray> QNetworkHeadersPrivate::rawHeadersKeys() const
{
QList<QByteArray> result;
RawHeadersList::ConstIterator it = rawHeaders.constBegin(),
end = rawHeaders.constEnd();
for ( ; it != end; ++it)
result << it->first;
return result;
}
void QNetworkHeadersPrivate::setRawHeader(const QByteArray &key, const QByteArray &value)
{
if (key.isEmpty())
// refuse to accept an empty raw header
return;
setRawHeaderInternal(key, value);
parseAndSetHeader(key, value);
}
/*!
\internal
Sets the internal raw headers list to match \a list. The cooked headers
will also be updated.
If \a list contains duplicates, they will be stored, but only the first one
is usually accessed.
*/
void QNetworkHeadersPrivate::setAllRawHeaders(const RawHeadersList &list)
{
cookedHeaders.clear();
rawHeaders = list;
RawHeadersList::ConstIterator it = rawHeaders.constBegin();
RawHeadersList::ConstIterator end = rawHeaders.constEnd();
for ( ; it != end; ++it)
parseAndSetHeader(it->first, it->second);
}
void QNetworkHeadersPrivate::setCookedHeader(QNetworkRequest::KnownHeaders header,
const QVariant &value)
{
QByteArray name = headerName(header);
if (name.isEmpty()) {
// headerName verifies that \a header is a known value
qWarning("QNetworkRequest::setHeader: invalid header value KnownHeader(%d) received", header);
return;
}
if (value.isNull()) {
setRawHeaderInternal(name, QByteArray());
cookedHeaders.remove(header);
} else {
QByteArray rawValue = headerValue(header, value);
if (rawValue.isEmpty()) {
qWarning("QNetworkRequest::setHeader: QVariant of type %s cannot be used with header %s",
value.typeName(), name.constData());
return;
}
setRawHeaderInternal(name, rawValue);
cookedHeaders.insert(header, value);
}
}
void QNetworkHeadersPrivate::setRawHeaderInternal(const QByteArray &key, const QByteArray &value)
{
RawHeadersList::Iterator it = rawHeaders.begin();
while (it != rawHeaders.end()) {
if (qstricmp(it->first.constData(), key.constData()) == 0)
it = rawHeaders.erase(it);
else
++it;
}
if (value.isNull())
return; // only wanted to erase key
RawHeaderPair pair;
pair.first = key;
pair.second = value;
rawHeaders.append(pair);
}
void QNetworkHeadersPrivate::parseAndSetHeader(const QByteArray &key, const QByteArray &value)
{
// is it a known header?
QNetworkRequest::KnownHeaders parsedKey = parseHeaderName(key);
if (parsedKey != QNetworkRequest::KnownHeaders(-1)) {
if (value.isNull())
cookedHeaders.remove(parsedKey);
else
cookedHeaders.insert(parsedKey, parseHeaderValue(parsedKey, value));
}
}
// Fast month string to int conversion. This code
// assumes that the Month name is correct and that
// the string is at least three chars long.
static int name_to_month(const char* month_str)
{
switch (month_str[0]) {
case 'J':
switch (month_str[1]) {
case 'a':
return 1;
break;
case 'u':
switch (month_str[2] ) {
case 'n':
return 6;
break;
case 'l':
return 7;
break;
}
}
break;
case 'F':
return 2;
break;
case 'M':
switch (month_str[2] ) {
case 'r':
return 3;
break;
case 'y':
return 5;
break;
}
break;
case 'A':
switch (month_str[1]) {
case 'p':
return 4;
break;
case 'u':
return 8;
break;
}
break;
case 'O':
return 10;
break;
case 'S':
return 9;
break;
case 'N':
return 11;
break;
case 'D':
return 12;
break;
}
return 0;
}
QDateTime QNetworkHeadersPrivate::fromHttpDate(const QByteArray &value)
{
// HTTP dates have three possible formats:
// RFC 1123/822 - ddd, dd MMM yyyy hh:mm:ss "GMT"
// RFC 850 - dddd, dd-MMM-yy hh:mm:ss "GMT"
// ANSI C's asctime - ddd MMM d hh:mm:ss yyyy
// We only handle them exactly. If they deviate, we bail out.
int pos = value.indexOf(',');
QDateTime dt;
#ifndef QT_NO_DATESTRING
if (pos == -1) {
// no comma -> asctime(3) format
dt = QDateTime::fromString(QString::fromLatin1(value), Qt::TextDate);
} else {
// Use sscanf over QLocal/QDateTimeParser for speed reasons. See the
// QtWebKit performance benchmarks to get an idea.
if (pos == 3) {
char month_name[4];
int day, year, hour, minute, second;
if (sscanf(value.constData(), "%*3s, %d %3s %d %d:%d:%d 'GMT'", &day, month_name, &year, &hour, &minute, &second) == 6)
dt = QDateTime(QDate(year, name_to_month(month_name), day), QTime(hour, minute, second));
} else {
QLocale c = QLocale::c();
// eat the weekday, the comma and the space following it
QString sansWeekday = QString::fromLatin1(value.constData() + pos + 2);
// must be RFC 850 date
dt = c.toDateTime(sansWeekday, QLatin1String("dd-MMM-yy hh:mm:ss 'GMT'"));
}
}
#endif // QT_NO_DATESTRING
if (dt.isValid())
dt.setTimeSpec(Qt::UTC);
return dt;
}
QByteArray QNetworkHeadersPrivate::toHttpDate(const QDateTime &dt)
{
return QLocale::c().toString(dt, QLatin1String("ddd, dd MMM yyyy hh:mm:ss 'GMT'"))
.toLatin1();
}
QT_END_NAMESPACE
|