Qt 4.8
qhttpnetworkconnection_p.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 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 
65 #include <private/qhttpnetworkheader_p.h>
66 #include <private/qhttpnetworkrequest_p.h>
67 #include <private/qhttpnetworkreply_p.h>
68 
69 #include <private/qhttpnetworkconnectionchannel_p.h>
70 
71 #ifndef QT_NO_HTTP
72 
73 #ifndef QT_NO_OPENSSL
74 # include <QtNetwork/qsslsocket.h>
75 # include <QtNetwork/qsslerror.h>
76 #else
77 # include <QtNetwork/qtcpsocket.h>
78 #endif
79 
81 
83 class QHttpNetworkReply;
84 class QByteArray;
85 
88 {
89  Q_OBJECT
90 public:
91 
92 #ifndef QT_NO_BEARERMANAGEMENT
93  QHttpNetworkConnection(const QString &hostName, quint16 port = 80, bool encrypt = false, QObject *parent = 0, QSharedPointer<QNetworkSession> networkSession = QSharedPointer<QNetworkSession>());
94  QHttpNetworkConnection(quint16 channelCount, const QString &hostName, quint16 port = 80, bool encrypt = false, QObject *parent = 0, QSharedPointer<QNetworkSession> networkSession = QSharedPointer<QNetworkSession>());
95 #else
96  QHttpNetworkConnection(const QString &hostName, quint16 port = 80, bool encrypt = false, QObject *parent = 0);
97  QHttpNetworkConnection(quint16 channelCount, const QString &hostName, quint16 port = 80, bool encrypt = false, QObject *parent = 0);
98 #endif
100 
101  //The hostname to which this is connected to.
102  QString hostName() const;
103  //The HTTP port in use.
104  quint16 port() const;
105 
106  //add a new HTTP request through this connection
107  QHttpNetworkReply* sendRequest(const QHttpNetworkRequest &request);
108 
109 #ifndef QT_NO_NETWORKPROXY
110  //set the proxy for this connection
111  void setCacheProxy(const QNetworkProxy &networkProxy);
112  QNetworkProxy cacheProxy() const;
113  void setTransparentProxy(const QNetworkProxy &networkProxy);
114  QNetworkProxy transparentProxy() const;
115 #endif
116 
117  bool isSsl() const;
118 
119  QHttpNetworkConnectionChannel *channels() const;
120 
121 #ifndef QT_NO_OPENSSL
122  void setSslConfiguration(const QSslConfiguration &config);
123  void ignoreSslErrors(int channel = -1);
124  void ignoreSslErrors(const QList<QSslError> &errors, int channel = -1);
125 #endif
126 
127 private:
133 
134  Q_PRIVATE_SLOT(d_func(), void _q_startNextRequest())
135 };
136 
137 
138 // private classes
139 typedef QPair<QHttpNetworkRequest, QHttpNetworkReply*> HttpMessagePair;
140 
141 
143 {
145 public:
146  static const int defaultChannelCount;
147  static const int defaultPipelineLength;
148  static const int defaultRePipelineLength;
149 
151  RunningState = 0,
152  PausedState = 1,
153  };
154 
155  QHttpNetworkConnectionPrivate(const QString &hostName, quint16 port, bool encrypt);
156  QHttpNetworkConnectionPrivate(quint16 channelCount, const QString &hostName, quint16 port, bool encrypt);
158  void init();
159 
160  void pauseConnection();
161  void resumeConnection();
163 
164  enum { ChunkSize = 4096 };
165 
166  int indexOf(QAbstractSocket *socket) const;
167 
168  QHttpNetworkReply *queueRequest(const QHttpNetworkRequest &request);
169  void requeueRequest(const HttpMessagePair &pair); // e.g. after pipeline broke
170  bool dequeueRequest(QAbstractSocket *socket);
171  void prepareRequest(HttpMessagePair &request);
172  QHttpNetworkRequest predictNextRequest();
173 
174  void fillPipeline(QAbstractSocket *socket);
175  bool fillPipeline(QList<HttpMessagePair> &queue, QHttpNetworkConnectionChannel &channel);
176 
177  // read more HTTP body after the next event loop spin
178  void readMoreLater(QHttpNetworkReply *reply);
179 
180  void copyCredentials(int fromChannel, QAuthenticator *auth, bool isProxy);
181 
182  // private slots
183  void _q_startNextRequest(); // send the next request from the queue
184 
185  void createAuthorization(QAbstractSocket *socket, QHttpNetworkRequest &request);
186 
187  QString errorDetail(QNetworkReply::NetworkError errorCode, QAbstractSocket *socket,
188  const QString &extraDetail = QString());
189 
190 #ifndef QT_NO_COMPRESS
191  bool expand(QAbstractSocket *socket, QHttpNetworkReply *reply, bool dataComplete);
192 #endif
193  void removeReply(QHttpNetworkReply *reply);
194 
197  bool encrypt;
198 
199  const int channelCount;
200  QHttpNetworkConnectionChannel *channels; // parallel connections to the server
201 
202  qint64 uncompressedBytesAvailable(const QHttpNetworkReply &reply) const;
203  qint64 uncompressedBytesAvailableNextBlock(const QHttpNetworkReply &reply) const;
204 
205 
206  void emitReplyError(QAbstractSocket *socket, QHttpNetworkReply *reply, QNetworkReply::NetworkError errorCode);
207  bool handleAuthenticateChallenge(QAbstractSocket *socket, QHttpNetworkReply *reply, bool isProxy, bool &resend);
208 
209 #ifndef QT_NO_NETWORKPROXY
211  void emitProxyAuthenticationRequired(const QHttpNetworkConnectionChannel *chan, const QNetworkProxy &proxy, QAuthenticator* auth);
212 #endif
213 
214  //The request queues
217 
218 #ifndef QT_NO_BEARERMANAGEMENT
220 #endif
221 
222  friend class QHttpNetworkConnectionChannel;
223 };
224 
225 
226 
228 
229 #endif // QT_NO_HTTP
230 
231 #endif
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
NetworkError
Indicates all possible error conditions found during the processing of the request.
Definition: qnetworkreply.h:70
QList< HttpMessagePair > highPriorityQueue
#define Q_DISABLE_COPY(Class)
Disables the use of copy constructors and assignment operators for the given Class.
Definition: qglobal.h:2523
#define Q_PRIVATE_SLOT(d, signature)
Definition: qobjectdefs.h:73
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
QHttpNetworkConnectionChannel * channels
The QNetworkProxy class provides a network layer proxy.
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
static bool init
QList< HttpMessagePair > lowPriorityQueue
unsigned short quint16
Definition: qglobal.h:936
__int64 qint64
Definition: qglobal.h:942
#define Q_OBJECT
Definition: qobjectdefs.h:157
The QAuthenticator class provides an authentication object.
#define Q_DECLARE_PUBLIC(Class)
Definition: qglobal.h:2477
QSharedPointer< QNetworkSession > networkSession
#define Q_AUTOTEST_EXPORT
Definition: qglobal.h:1510
The QSslConfiguration class holds the configuration and state of an SSL connection.
#define Q_DECLARE_PRIVATE(Class)
Definition: qglobal.h:2467
#define class
The QAbstractSocket class provides the base functionality common to all socket types.