-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqhttp.cpp
3141 lines (2642 loc) · 93.2 KB
/
qhttp.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
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
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
**
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the QtNetwork module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** 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, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
//#define QHTTP_DEBUG
#include <qplatformdefs.h>
#include "qhttp.h"
#ifndef QT_NO_HTTP
# include "qtcpsocket.h"
# include "qsslsocket.h"
# include "qtextstream.h"
# include "qmap.h"
# include "qlist.h"
# include "qstring.h"
# include "qstringlist.h"
# include "qbuffer.h"
# include "qringbuffer_p.h"
# include "qcoreevent.h"
# include "qurl.h"
# include "qnetworkproxy.h"
# include "qauthenticator.h"
# include "qhttpauthenticator_p.h"
# include "qdebug.h"
# include "qtimer.h"
#endif
#ifndef QT_NO_HTTP
QT_BEGIN_NAMESPACE
class QHttpNormalRequest;
class QHttpRequest
{
public:
QHttpRequest() : finished(false)
{ id = idCounter.fetchAndAddRelaxed(1); }
virtual ~QHttpRequest()
{ }
virtual void start(QHttp *) = 0;
virtual bool hasRequestHeader();
virtual QHttpRequestHeader requestHeader();
virtual QIODevice *sourceDevice() = 0;
virtual QIODevice *destinationDevice() = 0;
int id;
bool finished;
private:
static QBasicAtomicInt idCounter;
};
class QHttpPrivate
{
public:
Q_DECLARE_PUBLIC(QHttp)
inline QHttpPrivate(QHttp* parent)
: socket(0), reconnectAttempts(2),
deleteSocket(0), state(QHttp::Unconnected),
error(QHttp::NoError), port(0), mode(QHttp::ConnectionModeHttp),
toDevice(0), postDevice(0), bytesDone(0), chunkedSize(-1),
repost(false), pendingPost(false), q_ptr(parent)
{
}
inline ~QHttpPrivate()
{
while (!pending.isEmpty())
delete pending.takeFirst();
if (deleteSocket)
delete socket;
}
// private slots
void _q_startNextRequest();
void _q_slotReadyRead();
void _q_slotConnected();
void _q_slotError(QAbstractSocket::SocketError);
void _q_slotClosed();
void _q_slotBytesWritten(qint64 numBytes);
#ifndef QT_NO_OPENSSL
void _q_slotEncryptedBytesWritten(qint64 numBytes);
#endif
void _q_slotDoFinished();
void _q_slotSendRequest();
void _q_continuePost();
int addRequest(QHttpNormalRequest *);
int addRequest(QHttpRequest *);
void finishedWithSuccess();
void finishedWithError(const QString &detail, int errorCode);
void init();
void setState(int);
void closeConn();
void setSock(QTcpSocket *sock);
void postMoreData();
QTcpSocket *socket;
int reconnectAttempts;
bool deleteSocket;
QList<QHttpRequest *> pending;
QHttp::State state;
QHttp::Error error;
QString errorString;
QString hostName;
quint16 port;
QHttp::ConnectionMode mode;
QByteArray buffer;
QIODevice *toDevice;
QIODevice *postDevice;
qint64 bytesDone;
qint64 bytesTotal;
qint64 chunkedSize;
QHttpRequestHeader header;
bool readHeader;
QString headerStr;
QHttpResponseHeader response;
QRingBuffer rba;
#ifndef QT_NO_NETWORKPROXY
QNetworkProxy proxy;
QHttpAuthenticator proxyAuthenticator;
#endif
QHttpAuthenticator authenticator;
bool repost;
bool hasFinishedWithError;
bool pendingPost;
QTimer post100ContinueTimer;
QHttp *q_ptr;
};
QBasicAtomicInt QHttpRequest::idCounter = Q_BASIC_ATOMIC_INITIALIZER(1);
bool QHttpRequest::hasRequestHeader()
{
return false;
}
QHttpRequestHeader QHttpRequest::requestHeader()
{
return QHttpRequestHeader();
}
/****************************************************
*
* QHttpNormalRequest
*
****************************************************/
class QHttpNormalRequest : public QHttpRequest
{
public:
QHttpNormalRequest(const QHttpRequestHeader &h, QIODevice *d, QIODevice *t) :
header(h), to(t)
{
is_ba = false;
data.dev = d;
}
QHttpNormalRequest(const QHttpRequestHeader &h, QByteArray *d, QIODevice *t) :
header(h), to(t)
{
is_ba = true;
data.ba = d;
}
~QHttpNormalRequest()
{
if (is_ba)
delete data.ba;
}
void start(QHttp *);
bool hasRequestHeader();
QHttpRequestHeader requestHeader();
inline void setRequestHeader(const QHttpRequestHeader &h) { header = h; }
QIODevice *sourceDevice();
QIODevice *destinationDevice();
protected:
QHttpRequestHeader header;
private:
union {
QByteArray *ba;
QIODevice *dev;
} data;
bool is_ba;
QIODevice *to;
};
void QHttpNormalRequest::start(QHttp *http)
{
if (!http->d->socket)
http->d->setSock(0);
http->d->header = header;
if (is_ba) {
http->d->buffer = *data.ba;
if (http->d->buffer.size() >= 0)
http->d->header.setContentLength(http->d->buffer.size());
http->d->postDevice = 0;
} else {
http->d->buffer = QByteArray();
if (data.dev && (data.dev->isOpen() || data.dev->open(QIODevice::ReadOnly))) {
http->d->postDevice = data.dev;
if (http->d->postDevice->size() >= 0)
http->d->header.setContentLength(http->d->postDevice->size());
} else {
http->d->postDevice = 0;
}
}
if (to && (to->isOpen() || to->open(QIODevice::WriteOnly)))
http->d->toDevice = to;
else
http->d->toDevice = 0;
http->d->reconnectAttempts = 2;
http->d->_q_slotSendRequest();
}
bool QHttpNormalRequest::hasRequestHeader()
{
return true;
}
QHttpRequestHeader QHttpNormalRequest::requestHeader()
{
return header;
}
QIODevice *QHttpNormalRequest::sourceDevice()
{
if (is_ba)
return 0;
return data.dev;
}
QIODevice *QHttpNormalRequest::destinationDevice()
{
return to;
}
/****************************************************
*
* QHttpPGHRequest
* (like a QHttpNormalRequest, but for the convenience
* functions put(), get() and head() -- i.e. set the
* host header field correctly before sending the
* request)
*
****************************************************/
class QHttpPGHRequest : public QHttpNormalRequest
{
public:
QHttpPGHRequest(const QHttpRequestHeader &h, QIODevice *d, QIODevice *t) :
QHttpNormalRequest(h, d, t)
{ }
QHttpPGHRequest(const QHttpRequestHeader &h, QByteArray *d, QIODevice *t) :
QHttpNormalRequest(h, d, t)
{ }
~QHttpPGHRequest()
{ }
void start(QHttp *);
};
void QHttpPGHRequest::start(QHttp *http)
{
if (http->d->port && http->d->port != 80)
header.setValue(QLatin1String("Host"), http->d->hostName + QLatin1Char(':') + QString::number(http->d->port));
else
header.setValue(QLatin1String("Host"), http->d->hostName);
QHttpNormalRequest::start(http);
}
/****************************************************
*
* QHttpSetHostRequest
*
****************************************************/
class QHttpSetHostRequest : public QHttpRequest
{
public:
QHttpSetHostRequest(const QString &h, quint16 p, QHttp::ConnectionMode m)
: hostName(h), port(p), mode(m)
{ }
void start(QHttp *);
QIODevice *sourceDevice()
{ return 0; }
QIODevice *destinationDevice()
{ return 0; }
private:
QString hostName;
quint16 port;
QHttp::ConnectionMode mode;
};
void QHttpSetHostRequest::start(QHttp *http)
{
http->d->hostName = hostName;
http->d->port = port;
http->d->mode = mode;
#ifdef QT_NO_OPENSSL
if (mode == QHttp::ConnectionModeHttps) {
// SSL requested but no SSL support compiled in
http->d->finishedWithError(QLatin1String(QT_TRANSLATE_NOOP("QHttp", "HTTPS connection requested but SSL support not compiled in")),
QHttp::UnknownError);
return;
}
#endif
http->d->finishedWithSuccess();
}
/****************************************************
*
* QHttpSetUserRequest
*
****************************************************/
class QHttpSetUserRequest : public QHttpRequest
{
public:
QHttpSetUserRequest(const QString &userName, const QString &password) :
user(userName), pass(password)
{ }
void start(QHttp *);
QIODevice *sourceDevice()
{ return 0; }
QIODevice *destinationDevice()
{ return 0; }
private:
QString user;
QString pass;
};
void QHttpSetUserRequest::start(QHttp *http)
{
http->d->authenticator.setUser(user);
http->d->authenticator.setPassword(pass);
http->d->finishedWithSuccess();
}
#ifndef QT_NO_NETWORKPROXY
/****************************************************
*
* QHttpSetProxyRequest
*
****************************************************/
class QHttpSetProxyRequest : public QHttpRequest
{
public:
inline QHttpSetProxyRequest(const QNetworkProxy &proxy)
{
this->proxy = proxy;
}
inline void start(QHttp *http)
{
http->d->proxy = proxy;
QString user = proxy.user();
if (!user.isEmpty())
http->d->proxyAuthenticator.setUser(user);
QString password = proxy.password();
if (!password.isEmpty())
http->d->proxyAuthenticator.setPassword(password);
http->d->finishedWithSuccess();
}
inline QIODevice *sourceDevice()
{ return 0; }
inline QIODevice *destinationDevice()
{ return 0; }
private:
QNetworkProxy proxy;
};
#endif // QT_NO_NETWORKPROXY
/****************************************************
*
* QHttpSetSocketRequest
*
****************************************************/
class QHttpSetSocketRequest : public QHttpRequest
{
public:
QHttpSetSocketRequest(QTcpSocket *s) : socket(s)
{ }
void start(QHttp *);
QIODevice *sourceDevice()
{ return 0; }
QIODevice *destinationDevice()
{ return 0; }
private:
QTcpSocket *socket;
};
void QHttpSetSocketRequest::start(QHttp *http)
{
http->d->setSock(socket);
http->d->finishedWithSuccess();
}
/****************************************************
*
* QHttpCloseRequest
*
****************************************************/
class QHttpCloseRequest : public QHttpRequest
{
public:
QHttpCloseRequest()
{ }
void start(QHttp *);
QIODevice *sourceDevice()
{ return 0; }
QIODevice *destinationDevice()
{ return 0; }
};
void QHttpCloseRequest::start(QHttp *http)
{
http->d->closeConn();
}
class QHttpHeaderPrivate
{
Q_DECLARE_PUBLIC(QHttpHeader)
public:
inline virtual ~QHttpHeaderPrivate() {}
QList<QPair<QString, QString> > values;
bool valid;
QHttpHeader *q_ptr;
};
/****************************************************
*
* QHttpHeader
*
****************************************************/
/*!
\class QHttpHeader
\obsolete
\brief The QHttpHeader class contains header information for HTTP.
\ingroup network
\inmodule QtNetwork
In most cases you should use the more specialized derivatives of
this class, QHttpResponseHeader and QHttpRequestHeader, rather
than directly using QHttpHeader.
QHttpHeader provides the HTTP header fields. A HTTP header field
consists of a name followed by a colon, a single space, and the
field value. (See RFC 1945.) Field names are case-insensitive. A
typical header field looks like this:
\snippet doc/src/snippets/code/src_network_access_qhttp.cpp 0
In the API the header field name is called the "key" and the
content is called the "value". You can get and set a header
field's value by using its key with value() and setValue(), e.g.
\snippet doc/src/snippets/code/src_network_access_qhttp.cpp 1
Some fields are so common that getters and setters are provided
for them as a convenient alternative to using \l value() and
\l setValue(), e.g. contentLength() and contentType(),
setContentLength() and setContentType().
Each header key has a \e single value associated with it. If you
set the value for a key which already exists the previous value
will be discarded.
\sa QHttpRequestHeader QHttpResponseHeader
*/
/*!
\fn int QHttpHeader::majorVersion() const
Returns the major protocol-version of the HTTP header.
*/
/*!
\fn int QHttpHeader::minorVersion() const
Returns the minor protocol-version of the HTTP header.
*/
/*!
Constructs an empty HTTP header.
*/
QHttpHeader::QHttpHeader()
: d_ptr(new QHttpHeaderPrivate)
{
Q_D(QHttpHeader);
d->q_ptr = this;
d->valid = true;
}
/*!
Constructs a copy of \a header.
*/
QHttpHeader::QHttpHeader(const QHttpHeader &header)
: d_ptr(new QHttpHeaderPrivate)
{
Q_D(QHttpHeader);
d->q_ptr = this;
d->valid = header.d_func()->valid;
d->values = header.d_func()->values;
}
/*!
Constructs a HTTP header for \a str.
This constructor parses the string \a str for header fields and
adds this information. The \a str should consist of one or more
"\r\n" delimited lines; each of these lines should have the format
key, colon, space, value.
*/
QHttpHeader::QHttpHeader(const QString &str)
: d_ptr(new QHttpHeaderPrivate)
{
Q_D(QHttpHeader);
d->q_ptr = this;
d->valid = true;
parse(str);
}
/*! \internal
*/
QHttpHeader::QHttpHeader(QHttpHeaderPrivate &dd, const QString &str)
: d_ptr(&dd)
{
Q_D(QHttpHeader);
d->q_ptr = this;
d->valid = true;
if (!str.isEmpty())
parse(str);
}
/*! \internal
*/
QHttpHeader::QHttpHeader(QHttpHeaderPrivate &dd, const QHttpHeader &header)
: d_ptr(&dd)
{
Q_D(QHttpHeader);
d->q_ptr = this;
d->valid = header.d_func()->valid;
d->values = header.d_func()->values;
}
/*!
Destructor.
*/
QHttpHeader::~QHttpHeader()
{
}
/*!
Assigns \a h and returns a reference to this http header.
*/
QHttpHeader &QHttpHeader::operator=(const QHttpHeader &h)
{
Q_D(QHttpHeader);
d->values = h.d_func()->values;
d->valid = h.d_func()->valid;
return *this;
}
/*!
Returns true if the HTTP header is valid; otherwise returns false.
A QHttpHeader is invalid if it was created by parsing a malformed string.
*/
bool QHttpHeader::isValid() const
{
Q_D(const QHttpHeader);
return d->valid;
}
/*! \internal
Parses the HTTP header string \a str for header fields and adds
the keys/values it finds. If the string is not parsed successfully
the QHttpHeader becomes \link isValid() invalid\endlink.
Returns true if \a str was successfully parsed; otherwise returns false.
\sa toString()
*/
bool QHttpHeader::parse(const QString &str)
{
Q_D(QHttpHeader);
QStringList lst;
int pos = str.indexOf(QLatin1Char('\n'));
if (pos > 0 && str.at(pos - 1) == QLatin1Char('\r'))
lst = str.trimmed().split(QLatin1String("\r\n"));
else
lst = str.trimmed().split(QLatin1String("\n"));
lst.removeAll(QString()); // No empties
if (lst.isEmpty())
return true;
QStringList lines;
QStringList::Iterator it = lst.begin();
for (; it != lst.end(); ++it) {
if (!(*it).isEmpty()) {
if ((*it)[0].isSpace()) {
if (!lines.isEmpty()) {
lines.last() += QLatin1Char(' ');
lines.last() += (*it).trimmed();
}
} else {
lines.append((*it));
}
}
}
int number = 0;
it = lines.begin();
for (; it != lines.end(); ++it) {
if (!parseLine(*it, number++)) {
d->valid = false;
return false;
}
}
return true;
}
/*! \internal
*/
void QHttpHeader::setValid(bool v)
{
Q_D(QHttpHeader);
d->valid = v;
}
/*!
Returns the first value for the entry with the given \a key. If no entry
has this \a key, an empty string is returned.
\sa setValue() removeValue() hasKey() keys()
*/
QString QHttpHeader::value(const QString &key) const
{
Q_D(const QHttpHeader);
QString lowercaseKey = key.toLower();
QList<QPair<QString, QString> >::ConstIterator it = d->values.constBegin();
while (it != d->values.constEnd()) {
if ((*it).first.toLower() == lowercaseKey)
return (*it).second;
++it;
}
return QString();
}
/*!
Returns all the entries with the given \a key. If no entry
has this \a key, an empty string list is returned.
*/
QStringList QHttpHeader::allValues(const QString &key) const
{
Q_D(const QHttpHeader);
QString lowercaseKey = key.toLower();
QStringList valueList;
QList<QPair<QString, QString> >::ConstIterator it = d->values.constBegin();
while (it != d->values.constEnd()) {
if ((*it).first.toLower() == lowercaseKey)
valueList.append((*it).second);
++it;
}
return valueList;
}
/*!
Returns a list of the keys in the HTTP header.
\sa hasKey()
*/
QStringList QHttpHeader::keys() const
{
Q_D(const QHttpHeader);
QStringList keyList;
QSet<QString> seenKeys;
QList<QPair<QString, QString> >::ConstIterator it = d->values.constBegin();
while (it != d->values.constEnd()) {
const QString &key = (*it).first;
QString lowercaseKey = key.toLower();
if (!seenKeys.contains(lowercaseKey)) {
keyList.append(key);
seenKeys.insert(lowercaseKey);
}
++it;
}
return keyList;
}
/*!
Returns true if the HTTP header has an entry with the given \a
key; otherwise returns false.
\sa value() setValue() keys()
*/
bool QHttpHeader::hasKey(const QString &key) const
{
Q_D(const QHttpHeader);
QString lowercaseKey = key.toLower();
QList<QPair<QString, QString> >::ConstIterator it = d->values.constBegin();
while (it != d->values.constEnd()) {
if ((*it).first.toLower() == lowercaseKey)
return true;
++it;
}
return false;
}
/*!
Sets the value of the entry with the \a key to \a value.
If no entry with \a key exists, a new entry with the given \a key
and \a value is created. If an entry with the \a key already
exists, the first value is discarded and replaced with the given
\a value.
\sa value() hasKey() removeValue()
*/
void QHttpHeader::setValue(const QString &key, const QString &value)
{
Q_D(QHttpHeader);
QString lowercaseKey = key.toLower();
QList<QPair<QString, QString> >::Iterator it = d->values.begin();
while (it != d->values.end()) {
if ((*it).first.toLower() == lowercaseKey) {
(*it).second = value;
return;
}
++it;
}
// not found so add
addValue(key, value);
}
/*!
Sets the header entries to be the list of key value pairs in \a values.
*/
void QHttpHeader::setValues(const QList<QPair<QString, QString> > &values)
{
Q_D(QHttpHeader);
d->values = values;
}
/*!
Adds a new entry with the \a key and \a value.
*/
void QHttpHeader::addValue(const QString &key, const QString &value)
{
Q_D(QHttpHeader);
d->values.append(qMakePair(key, value));
}
/*!
Returns all the entries in the header.
*/
QList<QPair<QString, QString> > QHttpHeader::values() const
{
Q_D(const QHttpHeader);
return d->values;
}
/*!
Removes the entry with the key \a key from the HTTP header.
\sa value() setValue()
*/
void QHttpHeader::removeValue(const QString &key)
{
Q_D(QHttpHeader);
QString lowercaseKey = key.toLower();
QList<QPair<QString, QString> >::Iterator it = d->values.begin();
while (it != d->values.end()) {
if ((*it).first.toLower() == lowercaseKey) {
d->values.erase(it);
return;
}
++it;
}
}
/*!
Removes all the entries with the key \a key from the HTTP header.
*/
void QHttpHeader::removeAllValues(const QString &key)
{
Q_D(QHttpHeader);
QString lowercaseKey = key.toLower();
QList<QPair<QString, QString> >::Iterator it = d->values.begin();
while (it != d->values.end()) {
if ((*it).first.toLower() == lowercaseKey) {
it = d->values.erase(it);
continue;
}
++it;
}
}
/*! \internal
Parses the single HTTP header line \a line which has the format
key, colon, space, value, and adds key/value to the headers. The
linenumber is \a number. Returns true if the line was successfully
parsed and the key/value added; otherwise returns false.
\sa parse()
*/
bool QHttpHeader::parseLine(const QString &line, int)
{
int i = line.indexOf(QLatin1Char(':'));
if (i == -1)
return false;
addValue(line.left(i).trimmed(), line.mid(i + 1).trimmed());
return true;
}
/*!
Returns a string representation of the HTTP header.
The string is suitable for use by the constructor that takes a
QString. It consists of lines with the format: key, colon, space,
value, "\r\n".
*/
QString QHttpHeader::toString() const
{
Q_D(const QHttpHeader);
if (!isValid())
return QLatin1String("");
QString ret = QLatin1String("");
QList<QPair<QString, QString> >::ConstIterator it = d->values.constBegin();
while (it != d->values.constEnd()) {
ret += (*it).first + QLatin1String(": ") + (*it).second + QLatin1String("\r\n");
++it;
}
return ret;
}
/*!
Returns true if the header has an entry for the special HTTP
header field \c content-length; otherwise returns false.
\sa contentLength() setContentLength()
*/
bool QHttpHeader::hasContentLength() const
{
return hasKey(QLatin1String("content-length"));
}
/*!
Returns the value of the special HTTP header field \c
content-length.
\sa setContentLength() hasContentLength()
*/
uint QHttpHeader::contentLength() const
{
return value(QLatin1String("content-length")).toUInt();
}
/*!
Sets the value of the special HTTP header field \c content-length
to \a len.
\sa contentLength() hasContentLength()
*/
void QHttpHeader::setContentLength(int len)
{
setValue(QLatin1String("content-length"), QString::number(len));
}
/*!
Returns true if the header has an entry for the special HTTP
header field \c content-type; otherwise returns false.
\sa contentType() setContentType()
*/
bool QHttpHeader::hasContentType() const
{
return hasKey(QLatin1String("content-type"));
}
/*!
Returns the value of the special HTTP header field \c content-type.
\sa setContentType() hasContentType()
*/
QString QHttpHeader::contentType() const
{
QString type = value(QLatin1String("content-type"));
if (type.isEmpty())
return QString();
int pos = type.indexOf(QLatin1Char(';'));
if (pos == -1)
return type;
return type.left(pos).trimmed();
}
/*!
Sets the value of the special HTTP header field \c content-type to
\a type.
\sa contentType() hasContentType()
*/
void QHttpHeader::setContentType(const QString &type)
{
setValue(QLatin1String("content-type"), type);
}
class QHttpResponseHeaderPrivate : public QHttpHeaderPrivate
{
Q_DECLARE_PUBLIC(QHttpResponseHeader)
public: