src/network/access/qhttpnetworkconnection_p.h
changeset 0 1918ee327afb
child 4 3b1da2848fc7
child 7 f7bc934e204c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtNetwork module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    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 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    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 
       
    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 #include <private/qhttpnetworkconnectionchannel_p.h>
       
    69 
       
    70 #ifndef QT_NO_HTTP
       
    71 
       
    72 #ifndef QT_NO_OPENSSL
       
    73 #    include <QtNetwork/qsslsocket.h>
       
    74 #    include <QtNetwork/qsslerror.h>
       
    75 #else
       
    76 #   include <QtNetwork/qtcpsocket.h>
       
    77 #endif
       
    78 
       
    79 QT_BEGIN_NAMESPACE
       
    80 
       
    81 class QHttpNetworkRequest;
       
    82 class QHttpNetworkReply;
       
    83 class QByteArray;
       
    84 
       
    85 class QHttpNetworkConnectionPrivate;
       
    86 class Q_AUTOTEST_EXPORT QHttpNetworkConnection : public QObject
       
    87 {
       
    88     Q_OBJECT
       
    89 public:
       
    90 
       
    91     QHttpNetworkConnection(const QString &hostName, quint16 port = 80, bool encrypt = false, QObject *parent = 0);
       
    92     QHttpNetworkConnection(quint16 channelCount, const QString &hostName, quint16 port = 80, bool encrypt = false, QObject *parent = 0);
       
    93     ~QHttpNetworkConnection();
       
    94 
       
    95     //The hostname to which this is connected to.
       
    96     QString hostName() const;
       
    97     //The HTTP port in use.
       
    98     quint16 port() const;
       
    99 
       
   100     //add a new HTTP request through this connection
       
   101     QHttpNetworkReply* sendRequest(const QHttpNetworkRequest &request);
       
   102 
       
   103 #ifndef QT_NO_NETWORKPROXY
       
   104     //set the proxy for this connection
       
   105     void setCacheProxy(const QNetworkProxy &networkProxy);
       
   106     QNetworkProxy cacheProxy() const;
       
   107     void setTransparentProxy(const QNetworkProxy &networkProxy);
       
   108     QNetworkProxy transparentProxy() const;
       
   109 #endif
       
   110 
       
   111     //enable encryption
       
   112     void enableEncryption();
       
   113     bool isEncrypted() const;
       
   114 
       
   115     //authentication parameters
       
   116     void setProxyAuthentication(QAuthenticator *authenticator);
       
   117     void setAuthentication(const QString &domain, QAuthenticator *authenticator);
       
   118 
       
   119 #ifndef QT_NO_OPENSSL
       
   120     void setSslConfiguration(const QSslConfiguration &config);
       
   121     void ignoreSslErrors(int channel = -1);
       
   122     void ignoreSslErrors(const QList<QSslError> &errors, int channel = -1);
       
   123 
       
   124 Q_SIGNALS:
       
   125     void sslErrors(const QList<QSslError> &errors);
       
   126 #endif
       
   127 
       
   128 Q_SIGNALS:
       
   129 #ifndef QT_NO_NETWORKPROXY
       
   130     //cannot be used with queued connection.
       
   131     void proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *authenticator,
       
   132                                      const QHttpNetworkConnection *connection = 0);
       
   133 #endif
       
   134     void authenticationRequired(const QHttpNetworkRequest &request, QAuthenticator *authenticator,
       
   135                                 const QHttpNetworkConnection *connection = 0);
       
   136     void error(QNetworkReply::NetworkError errorCode, const QString &detail = QString());
       
   137 
       
   138 private:
       
   139     Q_DECLARE_PRIVATE(QHttpNetworkConnection)
       
   140     Q_DISABLE_COPY(QHttpNetworkConnection)
       
   141     friend class QHttpNetworkReply;
       
   142     friend class QHttpNetworkConnectionChannel;
       
   143 
       
   144     Q_PRIVATE_SLOT(d_func(), void _q_startNextRequest())
       
   145     Q_PRIVATE_SLOT(d_func(), void _q_restartAuthPendingRequests())
       
   146 };
       
   147 
       
   148 
       
   149 // private classes
       
   150 typedef QPair<QHttpNetworkRequest, QHttpNetworkReply*> HttpMessagePair;
       
   151 
       
   152 
       
   153 class QHttpNetworkConnectionPrivate : public QObjectPrivate
       
   154 {
       
   155     Q_DECLARE_PUBLIC(QHttpNetworkConnection)
       
   156 public:
       
   157     static const int defaultChannelCount;
       
   158     static const int defaultPipelineLength;
       
   159 
       
   160     QHttpNetworkConnectionPrivate(const QString &hostName, quint16 port, bool encrypt);
       
   161     QHttpNetworkConnectionPrivate(quint16 channelCount, const QString &hostName, quint16 port, bool encrypt);
       
   162     ~QHttpNetworkConnectionPrivate();
       
   163     void init();
       
   164 
       
   165     enum { ChunkSize = 4096 };
       
   166 
       
   167     int indexOf(QAbstractSocket *socket) const;
       
   168 
       
   169     QHttpNetworkReply *queueRequest(const QHttpNetworkRequest &request);
       
   170     void requeueRequest(const HttpMessagePair &pair); // e.g. after pipeline broke
       
   171     void dequeueAndSendRequest(QAbstractSocket *socket);
       
   172     void prepareRequest(HttpMessagePair &request);
       
   173 
       
   174     void fillPipeline(QAbstractSocket *socket);
       
   175     bool fillPipeline(QList<HttpMessagePair> &queue, QHttpNetworkConnectionChannel &channel);
       
   176 
       
   177     void copyCredentials(int fromChannel, QAuthenticator *auth, bool isProxy);
       
   178 
       
   179     // private slots
       
   180     void _q_startNextRequest(); // send the next request from the queue
       
   181     void _q_restartAuthPendingRequests(); // send the currently blocked request
       
   182 
       
   183     void createAuthorization(QAbstractSocket *socket, QHttpNetworkRequest &request);
       
   184 
       
   185     QString errorDetail(QNetworkReply::NetworkError errorCode, QAbstractSocket *socket);
       
   186 
       
   187 #ifndef QT_NO_COMPRESS
       
   188     bool expand(QAbstractSocket *socket, QHttpNetworkReply *reply, bool dataComplete);
       
   189 #endif
       
   190     void removeReply(QHttpNetworkReply *reply);
       
   191 
       
   192     QString hostName;
       
   193     quint16 port;
       
   194     bool encrypt;
       
   195 
       
   196     const int channelCount;
       
   197     QHttpNetworkConnectionChannel *channels; // parallel connections to the server
       
   198 
       
   199     bool pendingAuthSignal; // there is an incomplete authentication signal
       
   200     bool pendingProxyAuthSignal; // there is an incomplete proxy authentication signal
       
   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 
       
   210 #ifndef QT_NO_OPENSSL
       
   211     QSslConfiguration sslConfiguration(const QHttpNetworkReply &reply) const;
       
   212 #endif
       
   213 
       
   214 #ifndef QT_NO_NETWORKPROXY
       
   215     QNetworkProxy networkProxy;
       
   216     void emitProxyAuthenticationRequired(const QHttpNetworkConnectionChannel *chan, const QNetworkProxy &proxy, QAuthenticator* auth);
       
   217 #endif
       
   218 
       
   219     //The request queues
       
   220     QList<HttpMessagePair> highPriorityQueue;
       
   221     QList<HttpMessagePair> lowPriorityQueue;
       
   222 
       
   223     friend class QHttpNetworkConnectionChannel;
       
   224 };
       
   225 
       
   226 
       
   227 
       
   228 QT_END_NAMESPACE
       
   229 
       
   230 #endif // QT_NO_HTTP
       
   231 
       
   232 #endif