source: trunk/src/network/access/qhttpnetworkconnection.cpp@ 428

Last change on this file since 428 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 82.7 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtNetwork module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qhttpnetworkconnection_p.h"
43#include <private/qnetworkrequest_p.h>
44#include <private/qobject_p.h>
45#include <private/qauthenticator_p.h>
46#include <qnetworkproxy.h>
47#include <qauthenticator.h>
48#include <qbytearraymatcher.h>
49#include <qbuffer.h>
50#include <qpair.h>
51#include <qhttp.h>
52#include <qdebug.h>
53
54#ifndef QT_NO_HTTP
55
56#ifndef QT_NO_OPENSSL
57# include <QtNetwork/qsslkey.h>
58# include <QtNetwork/qsslcipher.h>
59# include <QtNetwork/qsslconfiguration.h>
60#endif
61
62#ifndef QT_NO_COMPRESS
63# include <zlib.h>
64static const unsigned char gz_magic[2] = {0x1f, 0x8b}; // gzip magic header
65// gzip flag byte
66#define HEAD_CRC 0x02 // bit 1 set: header CRC present
67#define EXTRA_FIELD 0x04 // bit 2 set: extra field present
68#define ORIG_NAME 0x08 // bit 3 set: original file name present
69#define COMMENT 0x10 // bit 4 set: file comment present
70#define RESERVED 0xE0 // bits 5..7: reserved
71#define CHUNK 16384
72#endif
73
74QT_BEGIN_NAMESPACE
75
76class QHttpNetworkHeaderPrivate : public QSharedData
77{
78public:
79 QUrl url;
80 QList<QPair<QByteArray, QByteArray> > fields;
81
82 QHttpNetworkHeaderPrivate(const QUrl &newUrl = QUrl());
83 QHttpNetworkHeaderPrivate(const QHttpNetworkHeaderPrivate &other);
84 inline qint64 contentLength() const;
85 inline void setContentLength(qint64 length);
86
87 inline QByteArray headerField(const QByteArray &name, const QByteArray &defaultValue = QByteArray()) const;
88 inline QList<QByteArray> headerFieldValues(const QByteArray &name) const;
89 inline void setHeaderField(const QByteArray &name, const QByteArray &data);
90 bool operator==(const QHttpNetworkHeaderPrivate &other) const;
91
92};
93
94QHttpNetworkHeaderPrivate::QHttpNetworkHeaderPrivate(const QUrl &newUrl)
95 :url(newUrl)
96{
97}
98
99QHttpNetworkHeaderPrivate::QHttpNetworkHeaderPrivate(const QHttpNetworkHeaderPrivate &other)
100 :QSharedData(other)
101{
102 url = other.url;
103 fields = other.fields;
104}
105
106qint64 QHttpNetworkHeaderPrivate::contentLength() const
107{
108 bool ok = false;
109 QByteArray value = headerField("content-length");
110 qint64 length = value.toULongLong(&ok);
111 if (ok)
112 return length;
113 return -1; // the header field is not set
114}
115
116void QHttpNetworkHeaderPrivate::setContentLength(qint64 length)
117{
118 setHeaderField("Content-Length", QByteArray::number(length));
119}
120
121QByteArray QHttpNetworkHeaderPrivate::headerField(const QByteArray &name, const QByteArray &defaultValue) const
122{
123 QList<QByteArray> allValues = headerFieldValues(name);
124 if (allValues.isEmpty())
125 return defaultValue;
126
127 QByteArray result;
128 bool first = true;
129 foreach (QByteArray value, allValues) {
130 if (!first)
131 result += ", ";
132 first = false;
133 result += value;
134 }
135 return result;
136}
137
138QList<QByteArray> QHttpNetworkHeaderPrivate::headerFieldValues(const QByteArray &name) const
139{
140 QList<QByteArray> result;
141 QByteArray lowerName = name.toLower();
142 QList<QPair<QByteArray, QByteArray> >::ConstIterator it = fields.constBegin(),
143 end = fields.constEnd();
144 for ( ; it != end; ++it)
145 if (lowerName == it->first.toLower())
146 result += it->second;
147
148 return result;
149}
150
151void QHttpNetworkHeaderPrivate::setHeaderField(const QByteArray &name, const QByteArray &data)
152{
153 QByteArray lowerName = name.toLower();
154 QList<QPair<QByteArray, QByteArray> >::Iterator it = fields.begin();
155 while (it != fields.end()) {
156 if (lowerName == it->first.toLower())
157 it = fields.erase(it);
158 else