Qt 4.8
qhttpthreaddelegate_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 QHTTPTHREADDELEGATE_H
43 #define QHTTPTHREADDELEGATE_H
44 
45 
46 //
47 // W A R N I N G
48 // -------------
49 //
50 // This file is not part of the Qt API. It exists for the convenience
51 // of the Network Access API. This header file may change from
52 // version to version without notice, or even be removed.
53 //
54 // We mean it.
55 //
56 
57 #include <QObject>
58 #include <QThreadStorage>
59 #include <QNetworkProxy>
60 #include <QSslConfiguration>
61 #include <QSslError>
62 #include <QList>
63 #include <QNetworkReply>
64 #include "qhttpnetworkrequest_p.h"
66 #include <QSharedPointer>
67 #include "qsslconfiguration.h"
68 #include "private/qnoncontiguousbytedevice_p.h"
70 
71 #ifndef QT_NO_HTTP
72 
74 
75 class QAuthenticator;
76 class QHttpNetworkReply;
77 class QEventLoop;
80 
82 {
83  Q_OBJECT
84 public:
85  explicit QHttpThreadDelegate(QObject *parent = 0);
86 
88 
89  // incoming
90  bool ssl;
91 #ifndef QT_NO_OPENSSL
93 #endif
98  // From backend, modified by us for signal compression
101 #ifndef QT_NO_NETWORKPROXY
104 #endif
107 
108  // outgoing, Retrieved in the synchronous HTTP case
117 #ifndef QT_NO_BEARERMANAGEMENT
119 #endif
120 
121 protected:
122  // The zerocopy download buffer, if used:
124  // The QHttpNetworkConnection that is used
128 
129  // Used for implementing the synchronous HTTP, see startRequestSynchronously()
131 
132 signals:
134 #ifndef QT_NO_NETWORKPROXY
136 #endif
137 #ifndef QT_NO_OPENSSL
138  void sslErrors(const QList<QSslError> &, bool *, QList<QSslError> *);
140 #endif
143  void downloadData(QByteArray);
145  void downloadFinished();
146 public slots:
147  // This are called via QueuedConnection from user thread
148  void startRequest();
149  void abortRequest();
150  void readBufferSizeChanged(qint64 size);
151  void readBufferFreed(qint64 size);
152  // This is called with a BlockingQueuedConnection from user thread
154 protected slots:
155  // From QHttp*
156  void readyReadSlot();
157  void finishedSlot();
158  void finishedWithErrorSlot(QNetworkReply::NetworkError errorCode, const QString &detail = QString());
161  void headerChangedSlot();
163  void dataReadProgressSlot(int done, int total);
164  void cacheCredentialsSlot(const QHttpNetworkRequest &request, QAuthenticator *authenticator);
165 #ifndef QT_NO_OPENSSL
166  void sslErrorsSlot(const QList<QSslError> &errors);
167 #endif
168 
170 #ifndef QT_NO_NETWORKPROXY
172 #endif
173 
174 protected:
175  // Cache for all the QHttpNetworkConnection objects.
176  // This is per thread.
178 
179 };
180 
181 // This QNonContiguousByteDevice is connected to the QNetworkAccessHttpBackend
182 // and represents the PUT/POST data.
184 {
185  Q_OBJECT
186 protected:
189  char *m_data;
191  bool m_atEnd;
193 public:
196  wantDataPending(false),
197  m_amount(0),
198  m_data(0),
199  m_atEnd(aE),
200  m_size(s)
201  {
202  }
203 
205  {
206  }
207 
208  const char* readPointer(qint64 maximumLength, qint64 &len)
209  {
210  if (m_amount > 0) {
211  len = m_amount;
212  return m_data;
213  }
214 
215  if (m_atEnd) {
216  len = -1;
217  } else if (!wantDataPending) {
218  len = 0;
219  wantDataPending = true;
220  emit wantData(maximumLength);
221  } else {
222  // Do nothing, we already sent a wantData signal and wait for results
223  len = 0;
224  }
225  return 0;
226  }
227 
229  {
230  if (m_data == 0)
231  return false;
232 
233  m_amount -= a;
234  m_data += a;
235 
236  // To main thread to inform about our state
237  emit processedData(a);
238 
239  // FIXME possible optimization, already ask user thread for some data
240 
241  return true;
242  }
243 
244  bool atEnd()
245  {
246  if (m_amount > 0)
247  return false;
248  else
249  return m_atEnd;
250  }
251 
252  bool reset()
253  {
254  m_amount = 0;
255  m_data = 0;
256 
257  // Communicate as BlockingQueuedConnection
258  bool b = false;
259  emit resetData(&b);
260  return b;
261  }
262 
264  {
265  return m_size;
266  }
267 
268 public slots:
269  // From user thread:
270  void haveDataSlot(QByteArray dataArray, bool dataAtEnd, qint64 dataSize)
271  {
272  wantDataPending = false;
273 
274  m_dataArray = dataArray;
275  m_data = const_cast<char*>(m_dataArray.constData());
276  m_amount = dataArray.size();
277 
278  m_atEnd = dataAtEnd;
279  m_size = dataSize;
280 
281  // This will tell the HTTP code (QHttpNetworkConnectionChannel) that we have data available now
282  emit readyRead();
283  }
284 
285 signals:
286  // void readyRead(); in parent class
287  // void readProgress(qint64 current, qint64 total); happens in the main thread with the real bytedevice
288 
289  // to main thread:
290  void wantData(qint64);
291  void processedData(qint64);
292  void resetData(bool *b);
293 };
294 
296 
297 #endif // QT_NO_HTTP
298 
299 #endif // QHTTPTHREADDELEGATE_H
QNetworkReply::NetworkError incomingErrorCode
QSharedPointer< char > downloadBuffer
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
static QThreadStorage< QNetworkAccessCache * > connections
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
void haveDataSlot(QByteArray dataArray, bool dataAtEnd, qint64 dataSize)
void synchronousFinishedWithErrorSlot(QNetworkReply::NetworkError errorCode, const QString &detail=QString())
QHttpNetworkRequest httpRequest
QSslConfiguration incomingSslConfiguration
NetworkError
Indicates all possible error conditions found during the processing of the request.
Definition: qnetworkreply.h:70
void downloadMetaData(QList< QPair< QByteArray, QByteArray > >, int, QString, bool, QSharedPointer< char >, qint64)
const char * readPointer(qint64 maximumLength, qint64 &len)
Return a byte pointer for at most maximumLength bytes of that device.
long ASN1_INTEGER_get ASN1_INTEGER * a
void dataReadProgressSlot(int done, int total)
void proxyAuthenticationRequired(const QNetworkProxy &, QAuthenticator *)
The QString class provides a Unicode character string.
Definition: qstring.h:83
void downloadProgress(qint64, qint64)
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
void sslErrors(const QList< QSslError > &, bool *, QList< QSslError > *)
bool advanceReadPointer(qint64 a)
The old readPointer is invalid after this call.
bool reset()
Moves the internal read pointer back to the beginning.
The QNetworkProxy class provides a network layer proxy.
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
The QEventLoop class provides a means of entering and leaving an event loop.
Definition: qeventloop.h:55
QEventLoop * synchronousRequestLoop
void error(QNetworkReply::NetworkError, const QString)
void sslConfigurationChanged(const QSslConfiguration)
#define emit
Definition: qobjectdefs.h:76
QHttpThreadDelegate(QObject *parent=0)
__int64 qint64
Definition: qglobal.h:942
QNetworkAccessCachedHttpConnection * httpConnection
#define Q_OBJECT
Definition: qobjectdefs.h:157
The QAuthenticator class provides an authentication object.
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
QSharedPointer< QNetworkSession > networkSession
void readBufferFreed(qint64 size)
void finishedWithErrorSlot(QNetworkReply::NetworkError errorCode, const QString &detail=QString())
void downloadData(QByteArray)
int size() const
Returns the number of bytes in this byte array.
Definition: qbytearray.h:402
void synchronousProxyAuthenticationRequiredSlot(const QNetworkProxy &, QAuthenticator *)
QSharedPointer< QAtomicInt > pendingDownloadProgress
QSharedPointer< QNetworkAccessAuthenticationManager > authenticationManager
qint64 size()
Returns the size of the complete device or -1 if unknown.
QList< QPair< QByteArray, QByteArray > > incomingHeaders
void readBufferSizeChanged(qint64 size)
A QNonContiguousByteDevice is a representation of a file, array or buffer that allows access with a r...
The QSslConfiguration class holds the configuration and state of an SSL connection.
void authenticationRequired(const QHttpNetworkRequest &request, QAuthenticator *)
#define slots
Definition: qobjectdefs.h:68
void synchronousAuthenticationRequiredSlot(const QHttpNetworkRequest &request, QAuthenticator *)
#define signals
Definition: qobjectdefs.h:69
void cacheCredentialsSlot(const QHttpNetworkRequest &request, QAuthenticator *authenticator)
QHttpNetworkReply * httpReply
The QThreadStorage class provides per-thread data storage.
bool atEnd()
Returns true if everything has been read and the read pointer cannot be advanced anymore.
void sslErrorsSlot(const QList< QSslError > &errors)
QSharedPointer< QAtomicInt > pendingDownloadData
The QList class is a template class that provides lists.
Definition: qdatastream.h:62