~ubuntu-branches/ubuntu/wily/qtbase-opensource-src/wily

« back to all changes in this revision

Viewing changes to src/network/access/qhttpnetworkconnection_p.h

  • Committer: Package Import Robot
  • Author(s): Timo Jyrinki
  • Date: 2013-02-05 12:46:17 UTC
  • Revision ID: package-import@ubuntu.com-20130205124617-c8jouts182j002fx
Tags: upstream-5.0.1+dfsg
ImportĀ upstreamĀ versionĀ 5.0.1+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
 
4
** Contact: http://www.qt-project.org/legal
 
5
**
 
6
** This file is part of the QtNetwork module of the Qt Toolkit.
 
7
**
 
8
** $QT_BEGIN_LICENSE:LGPL$
 
9
** Commercial License Usage
 
10
** Licensees holding valid commercial Qt licenses may use this file in
 
11
** accordance with the commercial license agreement provided with the
 
12
** Software or, alternatively, in accordance with the terms contained in
 
13
** a written agreement between you and Digia.  For licensing terms and
 
14
** conditions see http://qt.digia.com/licensing.  For further information
 
15
** use the contact form at http://qt.digia.com/contact-us.
 
16
**
 
17
** GNU Lesser General Public License Usage
 
18
** Alternatively, this file may be used under the terms of the GNU Lesser
 
19
** General Public License version 2.1 as published by the Free Software
 
20
** Foundation and appearing in the file LICENSE.LGPL included in the
 
21
** packaging of this file.  Please review the following information to
 
22
** ensure the GNU Lesser General Public License version 2.1 requirements
 
23
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 
24
**
 
25
** In addition, as a special exception, Digia gives you certain additional
 
26
** rights.  These rights are described in the Digia Qt LGPL Exception
 
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
 
28
**
 
29
** GNU General Public License Usage
 
30
** Alternatively, this file may be used under the terms of the GNU
 
31
** General Public License version 3.0 as published by the Free Software
 
32
** Foundation and appearing in the file LICENSE.GPL included in the
 
33
** packaging of this file.  Please review the following information to
 
34
** ensure the GNU General Public License version 3.0 requirements will be
 
35
** met: http://www.gnu.org/copyleft/gpl.html.
 
36
**
 
37
**
 
38
** $QT_END_LICENSE$
 
39
**
 
40
****************************************************************************/
 
41
 
 
42
#ifndef QHTTPNETWORKCONNECTION_H
 
43
#define QHTTPNETWORKCONNECTION_H
 
44
 
 
45
//
 
46
//  W A R N I N G
 
47
//  -------------
 
48
//
 
49
// This file is not part of the Qt API.  It exists for the convenience
 
50
// of the Network Access API.  This header file may change from
 
51
// version to version without notice, or even be removed.
 
52
//
 
53
// We mean it.
 
54
//
 
55
#include <QtNetwork/qnetworkrequest.h>
 
56
#include <QtNetwork/qnetworkreply.h>
 
57
#include <QtNetwork/qabstractsocket.h>
 
58
#include <QtNetwork/qnetworksession.h>
 
59
 
 
60
#include <private/qobject_p.h>
 
61
#include <qauthenticator.h>
 
62
#include <qnetworkproxy.h>
 
63
#include <qbuffer.h>
 
64
#include <qtimer.h>
 
65
 
 
66
#include <private/qhttpnetworkheader_p.h>
 
67
#include <private/qhttpnetworkrequest_p.h>
 
68
#include <private/qhttpnetworkreply_p.h>
 
69
 
 
70
#include <private/qhttpnetworkconnectionchannel_p.h>
 
71
 
 
72
#ifndef QT_NO_HTTP
 
73
 
 
74
#ifndef QT_NO_SSL
 
75
#    include <QtNetwork/qsslsocket.h>
 
76
#    include <QtNetwork/qsslerror.h>
 
77
#else
 
78
#   include <QtNetwork/qtcpsocket.h>
 
79
#endif
 
80
 
 
81
QT_BEGIN_NAMESPACE
 
82
 
 
83
class QHttpNetworkRequest;
 
84
class QHttpNetworkReply;
 
85
class QByteArray;
 
86
class QHostInfo;
 
87
 
 
88
class QHttpNetworkConnectionPrivate;
 
89
class Q_AUTOTEST_EXPORT QHttpNetworkConnection : public QObject
 
90
{
 
91
    Q_OBJECT
 
92
public:
 
93
 
 
94
#ifndef QT_NO_BEARERMANAGEMENT
 
95
    explicit QHttpNetworkConnection(const QString &hostName, quint16 port = 80, bool encrypt = false, QObject *parent = 0, QSharedPointer<QNetworkSession> networkSession = QSharedPointer<QNetworkSession>());
 
96
    QHttpNetworkConnection(quint16 channelCount, const QString &hostName, quint16 port = 80, bool encrypt = false, QObject *parent = 0, QSharedPointer<QNetworkSession> networkSession = QSharedPointer<QNetworkSession>());
 
97
#else
 
98
    explicit QHttpNetworkConnection(const QString &hostName, quint16 port = 80, bool encrypt = false, QObject *parent = 0);
 
99
    QHttpNetworkConnection(quint16 channelCount, const QString &hostName, quint16 port = 80, bool encrypt = false, QObject *parent = 0);
 
100
#endif
 
101
    ~QHttpNetworkConnection();
 
102
 
 
103
    //The hostname to which this is connected to.
 
104
    QString hostName() const;
 
105
    //The HTTP port in use.
 
106
    quint16 port() const;
 
107
 
 
108
    //add a new HTTP request through this connection
 
109
    QHttpNetworkReply* sendRequest(const QHttpNetworkRequest &request);
 
110
 
 
111
#ifndef QT_NO_NETWORKPROXY
 
112
    //set the proxy for this connection
 
113
    void setCacheProxy(const QNetworkProxy &networkProxy);
 
114
    QNetworkProxy cacheProxy() const;
 
115
    void setTransparentProxy(const QNetworkProxy &networkProxy);
 
116
    QNetworkProxy transparentProxy() const;
 
117
#endif
 
118
 
 
119
    bool isSsl() const;
 
120
 
 
121
    QHttpNetworkConnectionChannel *channels() const;
 
122
 
 
123
#ifndef QT_NO_SSL
 
124
    void setSslConfiguration(const QSslConfiguration &config);
 
125
    void ignoreSslErrors(int channel = -1);
 
126
    void ignoreSslErrors(const QList<QSslError> &errors, int channel = -1);
 
127
#endif
 
128
 
 
129
private:
 
130
    Q_DECLARE_PRIVATE(QHttpNetworkConnection)
 
131
    Q_DISABLE_COPY(QHttpNetworkConnection)
 
132
    friend class QHttpNetworkReply;
 
133
    friend class QHttpNetworkReplyPrivate;
 
134
    friend class QHttpNetworkConnectionChannel;
 
135
 
 
136
    Q_PRIVATE_SLOT(d_func(), void _q_startNextRequest())
 
137
    Q_PRIVATE_SLOT(d_func(), void _q_hostLookupFinished(QHostInfo))
 
138
    Q_PRIVATE_SLOT(d_func(), void _q_connectDelayedChannel())
 
139
};
 
140
 
 
141
 
 
142
// private classes
 
143
typedef QPair<QHttpNetworkRequest, QHttpNetworkReply*> HttpMessagePair;
 
144
 
 
145
 
 
146
class QHttpNetworkConnectionPrivate : public QObjectPrivate
 
147
{
 
148
    Q_DECLARE_PUBLIC(QHttpNetworkConnection)
 
149
public:
 
150
    static const int defaultChannelCount;
 
151
    static const int defaultPipelineLength;
 
152
    static const int defaultRePipelineLength;
 
153
 
 
154
    enum ConnectionState {
 
155
        RunningState = 0,
 
156
        PausedState = 1
 
157
    };
 
158
 
 
159
    enum NetworkLayerPreferenceState {
 
160
        Unknown,
 
161
        InProgress,
 
162
        IPv4,
 
163
        IPv6
 
164
    };
 
165
 
 
166
    QHttpNetworkConnectionPrivate(const QString &hostName, quint16 port, bool encrypt);
 
167
    QHttpNetworkConnectionPrivate(quint16 channelCount, const QString &hostName, quint16 port, bool encrypt);
 
168
    ~QHttpNetworkConnectionPrivate();
 
169
    void init();
 
170
 
 
171
    void pauseConnection();
 
172
    void resumeConnection();
 
173
    ConnectionState state;
 
174
    NetworkLayerPreferenceState networkLayerState;
 
175
 
 
176
    enum { ChunkSize = 4096 };
 
177
 
 
178
    int indexOf(QAbstractSocket *socket) const;
 
179
 
 
180
    QHttpNetworkReply *queueRequest(const QHttpNetworkRequest &request);
 
181
    void requeueRequest(const HttpMessagePair &pair); // e.g. after pipeline broke
 
182
    bool dequeueRequest(QAbstractSocket *socket);
 
183
    void prepareRequest(HttpMessagePair &request);
 
184
    QHttpNetworkRequest predictNextRequest();
 
185
 
 
186
    void fillPipeline(QAbstractSocket *socket);
 
187
    bool fillPipeline(QList<HttpMessagePair> &queue, QHttpNetworkConnectionChannel &channel);
 
188
 
 
189
    // read more HTTP body after the next event loop spin
 
190
    void readMoreLater(QHttpNetworkReply *reply);
 
191
 
 
192
    void copyCredentials(int fromChannel, QAuthenticator *auth, bool isProxy);
 
193
 
 
194
    void startHostInfoLookup();
 
195
    void startNetworkLayerStateLookup();
 
196
    void networkLayerDetected(QAbstractSocket::NetworkLayerProtocol protocol);
 
197
 
 
198
    // private slots
 
199
    void _q_startNextRequest(); // send the next request from the queue
 
200
 
 
201
    void _q_hostLookupFinished(QHostInfo info);
 
202
    void _q_connectDelayedChannel();
 
203
 
 
204
    void createAuthorization(QAbstractSocket *socket, QHttpNetworkRequest &request);
 
205
 
 
206
    QString errorDetail(QNetworkReply::NetworkError errorCode, QAbstractSocket *socket,
 
207
                        const QString &extraDetail = QString());
 
208
 
 
209
    void removeReply(QHttpNetworkReply *reply);
 
210
 
 
211
    QString hostName;
 
212
    quint16 port;
 
213
    bool encrypt;
 
214
    bool delayIpv4;
 
215
 
 
216
    const int channelCount;
 
217
    QTimer delayedConnectionTimer;
 
218
    QHttpNetworkConnectionChannel *channels; // parallel connections to the server
 
219
    bool shouldEmitChannelError(QAbstractSocket *socket);
 
220
 
 
221
    qint64 uncompressedBytesAvailable(const QHttpNetworkReply &reply) const;
 
222
    qint64 uncompressedBytesAvailableNextBlock(const QHttpNetworkReply &reply) const;
 
223
 
 
224
 
 
225
    void emitReplyError(QAbstractSocket *socket, QHttpNetworkReply *reply, QNetworkReply::NetworkError errorCode);
 
226
    bool handleAuthenticateChallenge(QAbstractSocket *socket, QHttpNetworkReply *reply, bool isProxy, bool &resend);
 
227
 
 
228
#ifndef QT_NO_NETWORKPROXY
 
229
    QNetworkProxy networkProxy;
 
230
    void emitProxyAuthenticationRequired(const QHttpNetworkConnectionChannel *chan, const QNetworkProxy &proxy, QAuthenticator* auth);
 
231
#endif
 
232
 
 
233
    //The request queues
 
234
    QList<HttpMessagePair> highPriorityQueue;
 
235
    QList<HttpMessagePair> lowPriorityQueue;
 
236
 
 
237
#ifndef QT_NO_BEARERMANAGEMENT
 
238
    QSharedPointer<QNetworkSession> networkSession;
 
239
#endif
 
240
 
 
241
    friend class QHttpNetworkConnectionChannel;
 
242
};
 
243
 
 
244
 
 
245
 
 
246
QT_END_NAMESPACE
 
247
 
 
248
#endif // QT_NO_HTTP
 
249
 
 
250
#endif