| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** All rights reserved.
|
|---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
|---|
| 6 | **
|
|---|
| 7 | ** This file is part of the QtNetwork module of the Qt Toolkit.
|
|---|
| 8 | **
|
|---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 10 | ** Commercial Usage
|
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 14 | ** a written agreement between you and Nokia.
|
|---|
| 15 | **
|
|---|
| 16 | ** GNU Lesser General Public License Usage
|
|---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 18 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 20 | ** packaging of this file. Please review the following information to
|
|---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 23 | **
|
|---|
| 24 | ** In addition, as a special exception, Nokia gives you certain additional
|
|---|
| 25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
|---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this 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 have questions regarding the use of this file, please contact
|
|---|
| 37 | ** Nokia at [email protected].
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | #ifndef QHTTPNETWORKCONNECTIONCHANNEL_H
|
|---|
| 43 | #define QHTTPNETWORKCONNECTIONCHANNEL_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 |
|
|---|
| 59 | #include <private/qobject_p.h>
|
|---|
| 60 | #include <qauthenticator.h>
|
|---|
| 61 | #include <qnetworkproxy.h>
|
|---|
| 62 | #include <qbuffer.h>
|
|---|
| 63 |
|
|---|
| 64 | #include <private/qhttpnetworkheader_p.h>
|
|---|
| 65 | #include <private/qhttpnetworkrequest_p.h>
|
|---|
| 66 | #include <private/qhttpnetworkreply_p.h>
|
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 | #ifndef QT_NO_HTTP
|
|---|
| 70 |
|
|---|
| 71 | #ifndef QT_NO_OPENSSL
|
|---|
| 72 | # include <QtNetwork/qsslsocket.h>
|
|---|
| 73 | # include <QtNetwork/qsslerror.h>
|
|---|
| 74 | #else
|
|---|
| 75 | # include <QtNetwork/qtcpsocket.h>
|
|---|
| 76 | #endif
|
|---|
| 77 |
|
|---|
| 78 | QT_BEGIN_NAMESPACE
|
|---|
| 79 |
|
|---|
| 80 | class QHttpNetworkRequest;
|
|---|
| 81 | class QHttpNetworkReply;
|
|---|
| 82 | class QByteArray;
|
|---|
| 83 | class QHttpNetworkConnection;
|
|---|
| 84 |
|
|---|
| 85 | #ifndef HttpMessagePair
|
|---|
| 86 | typedef QPair<QHttpNetworkRequest, QHttpNetworkReply*> HttpMessagePair;
|
|---|
| 87 | #endif
|
|---|
| 88 |
|
|---|
| 89 | class QHttpNetworkConnectionChannel : public QObject {
|
|---|
| 90 | Q_OBJECT
|
|---|
| 91 | public:
|
|---|
| 92 | enum ChannelState {
|
|---|
| 93 | IdleState = 0, // ready to send request
|
|---|
| 94 | ConnectingState = 1, // connecting to host
|
|---|
| 95 | WritingState = 2, // writing the data
|
|---|
| 96 | WaitingState = 4, // waiting for reply
|
|---|
| 97 | ReadingState = 8, // reading the reply
|
|---|
| 98 | Wait4AuthState = 0x10, // blocked for send till the current authentication slot is done
|
|---|
| 99 | BusyState = (ConnectingState|WritingState|WaitingState|ReadingState|Wait4AuthState)
|
|---|
| 100 | };
|
|---|
| 101 | QAbstractSocket *socket;
|
|---|
| 102 | ChannelState state;
|
|---|
| 103 | QHttpNetworkRequest request; // current request
|
|---|
| 104 | QHttpNetworkReply *reply; // current reply for this request
|
|---|
| 105 | qint64 written;
|
|---|
| 106 | qint64 bytesTotal;
|
|---|
| 107 | bool resendCurrent;
|
|---|
| 108 | int lastStatus; // last status received on this channel
|
|---|
| 109 | bool pendingEncrypt; // for https (send after encrypted)
|
|---|
| 110 | int reconnectAttempts; // maximum 2 reconnection attempts
|
|---|
| 111 | QAuthenticatorPrivate::Method authMehtod;
|
|---|
| 112 | QAuthenticatorPrivate::Method proxyAuthMehtod;
|
|---|
| 113 | QAuthenticator authenticator;
|
|---|
| 114 | QAuthenticator proxyAuthenticator;
|
|---|
| 115 | #ifndef QT_NO_OPENSSL
|
|---|
| 116 | bool ignoreAllSslErrors;
|
|---|
| 117 | QList<QSslError> ignoreSslErrorsList;
|
|---|
| 118 | #endif
|
|---|
| 119 |
|
|---|
| 120 | // HTTP pipelining -> http://en.wikipedia.org/wiki/Http_pipelining
|
|---|
| 121 | enum PipeliningSupport {
|
|---|
| 122 | PipeliningSupportUnknown, // default for a new connection
|
|---|
| 123 | PipeliningProbablySupported, // after having received a server response that indicates support
|
|---|
| 124 | PipeliningNotSupported // currently not used
|
|---|
| 125 | };
|
|---|
| 126 | PipeliningSupport pipeliningSupported;
|
|---|
| 127 | QList<HttpMessagePair> alreadyPipelinedRequests;
|
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 | QHttpNetworkConnectionChannel() : socket(0), state(IdleState), reply(0), written(0), bytesTotal(0), resendCurrent(false),
|
|---|
| 131 | lastStatus(0), pendingEncrypt(false), reconnectAttempts(2),
|
|---|
| 132 | authMehtod(QAuthenticatorPrivate::None), proxyAuthMehtod(QAuthenticatorPrivate::None)
|
|---|
| 133 | #ifndef QT_NO_OPENSSL
|
|---|
| 134 | , ignoreAllSslErrors(false)
|
|---|
| 135 | #endif
|
|---|
| 136 | , pipeliningSupported(PipeliningSupportUnknown)
|
|---|
| 137 | , connection(0)
|
|---|
| 138 | {}
|
|---|
| 139 |
|
|---|
| 140 | void setConnection(QHttpNetworkConnection *c) {connection = c;}
|
|---|
| 141 | QHttpNetworkConnection *connection;
|
|---|
| 142 |
|
|---|
| 143 | void init();
|
|---|
| 144 | void close();
|
|---|
| 145 |
|
|---|
| 146 | bool sendRequest();
|
|---|
| 147 | void receiveReply();
|
|---|
| 148 |
|
|---|
| 149 | bool ensureConnection();
|
|---|
| 150 |
|
|---|
| 151 | bool expand(bool dataComplete);
|
|---|
| 152 | void allDone(); // reply header + body have been read
|
|---|
| 153 | void handleStatus(); // called from allDone()
|
|---|
| 154 |
|
|---|
| 155 | void pipelineInto(HttpMessagePair &pair);
|
|---|
| 156 | void requeueCurrentlyPipelinedRequests();
|
|---|
| 157 | void detectPipeliningSupport();
|
|---|
| 158 |
|
|---|
| 159 | void closeAndResendCurrentRequest();
|
|---|
| 160 |
|
|---|
| 161 | void eatWhitespace();
|
|---|
| 162 |
|
|---|
| 163 | bool isSocketBusy() const;
|
|---|
| 164 | bool isSocketWriting() const;
|
|---|
| 165 | bool isSocketWaiting() const;
|
|---|
| 166 | bool isSocketReading() const;
|
|---|
| 167 |
|
|---|
| 168 | protected slots:
|
|---|
| 169 | void _q_bytesWritten(qint64 bytes); // proceed sending
|
|---|
| 170 | void _q_readyRead(); // pending data to read
|
|---|
| 171 | void _q_disconnected(); // disconnected from host
|
|---|
| 172 | void _q_connected(); // start sending request
|
|---|
| 173 | void _q_error(QAbstractSocket::SocketError); // error from socket
|
|---|
| 174 | #ifndef QT_NO_NETWORKPROXY
|
|---|
| 175 | void _q_proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *auth); // from transparent proxy
|
|---|
| 176 | #endif
|
|---|
| 177 |
|
|---|
| 178 | void _q_uploadDataReadyRead();
|
|---|
| 179 |
|
|---|
| 180 | #ifndef QT_NO_OPENSSL
|
|---|
| 181 | void _q_encrypted(); // start sending request (https)
|
|---|
| 182 | void _q_sslErrors(const QList<QSslError> &errors); // ssl errors from the socket
|
|---|
| 183 | void _q_encryptedBytesWritten(qint64 bytes); // proceed sending
|
|---|
| 184 | #endif
|
|---|
| 185 | };
|
|---|
| 186 |
|
|---|
| 187 |
|
|---|
| 188 |
|
|---|
| 189 | QT_END_NAMESPACE
|
|---|
| 190 |
|
|---|
| 191 | #endif // QT_NO_HTTP
|
|---|
| 192 |
|
|---|
| 193 | #endif
|
|---|