diff options
Diffstat (limited to 'src/modules/objects/qthttp/qhttp.cpp')
| -rw-r--r-- | src/modules/objects/qthttp/qhttp.cpp | 2714 |
1 files changed, 1440 insertions, 1274 deletions
diff --git a/src/modules/objects/qthttp/qhttp.cpp b/src/modules/objects/qthttp/qhttp.cpp index 33b885580..8dd437451 100644 --- a/src/modules/objects/qthttp/qhttp.cpp +++ b/src/modules/objects/qthttp/qhttp.cpp @@ -45,22 +45,22 @@ #include "qhttp.h" #ifndef QT_NO_HTTP -# include "qtcpsocket.h" -# include "qsslsocket.h" -# include "qtextstream.h" -# include "qmap.h" -# include "qlist.h" -# include "qstring.h" -# include "qstringlist.h" -# include "qbuffer.h" -# include "qringbuffer_p.h" -# include "qcoreevent.h" -# include "qurl.h" -# include "qnetworkproxy.h" -# include "qauthenticator.h" -# include "qhttpauthenticator_p.h" -# include "qdebug.h" -# include "qtimer.h" +#include "qtcpsocket.h" +#include "qsslsocket.h" +#include "qtextstream.h" +#include "qmap.h" +#include "qlist.h" +#include "qstring.h" +#include "qstringlist.h" +#include "qbuffer.h" +#include "qringbuffer_p.h" +#include "qcoreevent.h" +#include "qurl.h" +#include "qnetworkproxy.h" +#include "qauthenticator.h" +#include "qhttpauthenticator_p.h" +#include "qdebug.h" +#include "qtimer.h" #endif #ifndef QT_NO_HTTP @@ -71,125 +71,128 @@ class QHttpNormalRequest; class QHttpRequest { public: - QHttpRequest() : finished(false) - { id = idCounter.fetchAndAddRelaxed(1); } - virtual ~QHttpRequest() - { } + QHttpRequest() : finished(false) + { + id = idCounter.fetchAndAddRelaxed(1); + } + virtual ~QHttpRequest() + { + } - virtual void start(QHttp *) = 0; - virtual bool hasRequestHeader(); - virtual QHttpRequestHeader requestHeader(); + virtual void start(QHttp *) = 0; + virtual bool hasRequestHeader(); + virtual QHttpRequestHeader requestHeader(); - virtual QIODevice *sourceDevice() = 0; - virtual QIODevice *destinationDevice() = 0; + virtual QIODevice * sourceDevice() = 0; + virtual QIODevice * destinationDevice() = 0; - int id; - bool finished; + int id; + bool finished; private: - static QBasicAtomicInt idCounter; + static QBasicAtomicInt idCounter; }; class QHttpPrivate { public: - Q_DECLARE_PUBLIC(QHttp) + Q_DECLARE_PUBLIC(QHttp) - inline QHttpPrivate(QHttp* parent) - : socket(0), reconnectAttempts(2), - deleteSocket(0), state(QHttp::Unconnected), - error(QHttp::NoError), port(0), mode(QHttp::ConnectionModeHttp), - toDevice(0), postDevice(0), bytesDone(0), chunkedSize(-1), - repost(false), pendingPost(false), q_ptr(parent) - { - } + inline QHttpPrivate(QHttp * parent) + : socket(0), reconnectAttempts(2), + deleteSocket(0), state(QHttp::Unconnected), + error(QHttp::NoError), port(0), mode(QHttp::ConnectionModeHttp), + toDevice(0), postDevice(0), bytesDone(0), chunkedSize(-1), + repost(false), pendingPost(false), q_ptr(parent) + { + } - inline ~QHttpPrivate() - { - while (!pending.isEmpty()) - delete pending.takeFirst(); + inline ~QHttpPrivate() + { + while(!pending.isEmpty()) + delete pending.takeFirst(); - if (deleteSocket) - delete socket; - } + if(deleteSocket) + delete socket; + } - // private slots - void _q_startNextRequest(); - void _q_slotReadyRead(); - void _q_slotConnected(); - void _q_slotError(QAbstractSocket::SocketError); - void _q_slotClosed(); - void _q_slotBytesWritten(qint64 numBytes); + // private slots + void _q_startNextRequest(); + void _q_slotReadyRead(); + void _q_slotConnected(); + void _q_slotError(QAbstractSocket::SocketError); + void _q_slotClosed(); + void _q_slotBytesWritten(qint64 numBytes); #ifndef QT_NO_OPENSSL - void _q_slotEncryptedBytesWritten(qint64 numBytes); + void _q_slotEncryptedBytesWritten(qint64 numBytes); #endif - void _q_slotDoFinished(); - void _q_slotSendRequest(); - void _q_continuePost(); + void _q_slotDoFinished(); + void _q_slotSendRequest(); + void _q_continuePost(); - int addRequest(QHttpNormalRequest *); - int addRequest(QHttpRequest *); - void finishedWithSuccess(); - void finishedWithError(const QString &detail, int errorCode); + int addRequest(QHttpNormalRequest *); + int addRequest(QHttpRequest *); + void finishedWithSuccess(); + void finishedWithError(const QString & detail, int errorCode); - void init(); - void setState(int); - void closeConn(); - void setSock(QTcpSocket *sock); + void init(); + void setState(int); + void closeConn(); + void setSock(QTcpSocket * sock); - void postMoreData(); + void postMoreData(); - QTcpSocket *socket; - int reconnectAttempts; - bool deleteSocket; - QList<QHttpRequest *> pending; + QTcpSocket * socket; + int reconnectAttempts; + bool deleteSocket; + QList<QHttpRequest *> pending; - QHttp::State state; - QHttp::Error error; - QString errorString; + QHttp::State state; + QHttp::Error error; + QString errorString; - QString hostName; - quint16 port; - QHttp::ConnectionMode mode; + QString hostName; + quint16 port; + QHttp::ConnectionMode mode; - QByteArray buffer; - QIODevice *toDevice; - QIODevice *postDevice; + QByteArray buffer; + QIODevice * toDevice; + QIODevice * postDevice; - qint64 bytesDone; - qint64 bytesTotal; - qint64 chunkedSize; + qint64 bytesDone; + qint64 bytesTotal; + qint64 chunkedSize; - QHttpRequestHeader header; + QHttpRequestHeader header; - bool readHeader; - QString headerStr; - QHttpResponseHeader response; + bool readHeader; + QString headerStr; + QHttpResponseHeader response; - QRingBuffer rba; + QRingBuffer rba; #ifndef QT_NO_NETWORKPROXY - QNetworkProxy proxy; - QHttpAuthenticator proxyAuthenticator; + QNetworkProxy proxy; + QHttpAuthenticator proxyAuthenticator; #endif - QHttpAuthenticator authenticator; - bool repost; - bool hasFinishedWithError; - bool pendingPost; - QTimer post100ContinueTimer; - QHttp *q_ptr; + QHttpAuthenticator authenticator; + bool repost; + bool hasFinishedWithError; + bool pendingPost; + QTimer post100ContinueTimer; + QHttp * q_ptr; }; QBasicAtomicInt QHttpRequest::idCounter = Q_BASIC_ATOMIC_INITIALIZER(1); bool QHttpRequest::hasRequestHeader() { - return false; + return false; } QHttpRequestHeader QHttpRequest::requestHeader() { - return QHttpRequestHeader(); + return QHttpRequestHeader(); } /**************************************************** @@ -201,99 +204,104 @@ QHttpRequestHeader QHttpRequest::requestHeader() class QHttpNormalRequest : public QHttpRequest { public: - QHttpNormalRequest(const QHttpRequestHeader &h, QIODevice *d, QIODevice *t) : - header(h), to(t) - { - is_ba = false; - data.dev = d; - } + QHttpNormalRequest(const QHttpRequestHeader & h, QIODevice * d, QIODevice * t) : header(h), to(t) + { + is_ba = false; + data.dev = d; + } - QHttpNormalRequest(const QHttpRequestHeader &h, QByteArray *d, QIODevice *t) : - header(h), to(t) - { - is_ba = true; - data.ba = d; - } + QHttpNormalRequest(const QHttpRequestHeader & h, QByteArray * d, QIODevice * t) : header(h), to(t) + { + is_ba = true; + data.ba = d; + } - ~QHttpNormalRequest() - { - if (is_ba) - delete data.ba; - } + ~QHttpNormalRequest() + { + if(is_ba) + delete data.ba; + } - void start(QHttp *); - bool hasRequestHeader(); - QHttpRequestHeader requestHeader(); - inline void setRequestHeader(const QHttpRequestHeader &h) { header = h; } + void start(QHttp *); + bool hasRequestHeader(); + QHttpRequestHeader requestHeader(); + inline void setRequestHeader(const QHttpRequestHeader & h) { header = h; } - QIODevice *sourceDevice(); - QIODevice *destinationDevice(); + QIODevice * sourceDevice(); + QIODevice * destinationDevice(); protected: - QHttpRequestHeader header; + QHttpRequestHeader header; private: - union { - QByteArray *ba; - QIODevice *dev; - } data; - bool is_ba; - QIODevice *to; + union + { + QByteArray * ba; + QIODevice * dev; + } data; + bool is_ba; + QIODevice * to; }; -void QHttpNormalRequest::start(QHttp *http) +void QHttpNormalRequest::start(QHttp * http) { - if (!http->d->socket) - http->d->setSock(0); - http->d->header = header; + if(!http->d->socket) + http->d->setSock(0); + http->d->header = header; - if (is_ba) { - http->d->buffer = *data.ba; - if (http->d->buffer.size() >= 0) - http->d->header.setContentLength(http->d->buffer.size()); + if(is_ba) + { + http->d->buffer = *data.ba; + if(http->d->buffer.size() >= 0) + http->d->header.setContentLength(http->d->buffer.size()); - http->d->postDevice = 0; - } else { - http->d->buffer = QByteArray(); + http->d->postDevice = 0; + } + else + { + http->d->buffer = QByteArray(); - if (data.dev && (data.dev->isOpen() || data.dev->open(QIODevice::ReadOnly))) { - http->d->postDevice = data.dev; - if (http->d->postDevice->size() >= 0) - http->d->header.setContentLength(http->d->postDevice->size()); - } else { - http->d->postDevice = 0; - } - } + if(data.dev && (data.dev->isOpen() || data.dev->open(QIODevice::ReadOnly))) + { + http->d->postDevice = data.dev; + if(http->d->postDevice->size() >= 0) + http->d->header.setContentLength(http->d->postDevice->size()); + } + else + { + http->d->postDevice = 0; + } + } - if (to && (to->isOpen() || to->open(QIODevice::WriteOnly))) - http->d->toDevice = to; - else - http->d->toDevice = 0; + if(to && (to->isOpen() || to->open(QIODevice::WriteOnly))) + http->d->toDevice = to; + else + http->d->toDevice = 0; - http->d->reconnectAttempts = 2; - http->d->_q_slotSendRequest(); + http->d->reconnectAttempts = 2; + http->d->_q_slotSendRequest(); } bool QHttpNormalRequest::hasRequestHeader() { - return true; + return true; } QHttpRequestHeader QHttpNormalRequest::requestHeader() { - return header; + return header; } -QIODevice *QHttpNormalRequest::sourceDevice() +QIODevice * QHttpNormalRequest::sourceDevice() { - if (is_ba) - return 0; - return data.dev; + if(is_ba) + return 0; + return data.dev; } -QIODevice *QHttpNormalRequest::destinationDevice() +QIODevice * QHttpNormalRequest::destinationDevice() { - return to; + return to; } /**************************************************** @@ -309,27 +317,28 @@ QIODevice *QHttpNormalRequest::destinationDevice() class QHttpPGHRequest : public QHttpNormalRequest { public: - QHttpPGHRequest(const QHttpRequestHeader &h, QIODevice *d, QIODevice *t) : - QHttpNormalRequest(h, d, t) - { } + QHttpPGHRequest(const QHttpRequestHeader & h, QIODevice * d, QIODevice * t) : QHttpNormalRequest(h, d, t) + { + } - QHttpPGHRequest(const QHttpRequestHeader &h, QByteArray *d, QIODevice *t) : - QHttpNormalRequest(h, d, t) - { } + QHttpPGHRequest(const QHttpRequestHeader & h, QByteArray * d, QIODevice * t) : QHttpNormalRequest(h, d, t) + { + } - ~QHttpPGHRequest() - { } + ~QHttpPGHRequest() + { + } - void start(QHttp *); + void start(QHttp *); }; -void QHttpPGHRequest::start(QHttp *http) +void QHttpPGHRequest::start(QHttp * http) { - if (http->d->port && http->d->port != 80) - header.setValue(QLatin1String("Host"), http->d->hostName + QLatin1Char(':') + QString::number(http->d->port)); - else - header.setValue(QLatin1String("Host"), http->d->hostName); - QHttpNormalRequest::start(http); + if(http->d->port && http->d->port != 80) + header.setValue(QLatin1String("Host"), http->d->hostName + QLatin1Char(':') + QString::number(http->d->port)); + else + header.setValue(QLatin1String("Host"), http->d->hostName); + QHttpNormalRequest::start(http); } /**************************************************** @@ -341,39 +350,45 @@ void QHttpPGHRequest::start(QHttp *http) class QHttpSetHostRequest : public QHttpRequest { public: - QHttpSetHostRequest(const QString &h, quint16 p, QHttp::ConnectionMode m) - : hostName(h), port(p), mode(m) - { } + QHttpSetHostRequest(const QString & h, quint16 p, QHttp::ConnectionMode m) + : hostName(h), port(p), mode(m) + { + } - void start(QHttp *); + void start(QHttp *); - QIODevice *sourceDevice() - { return 0; } - QIODevice *destinationDevice() - { return 0; } + QIODevice * sourceDevice() + { + return 0; + } + QIODevice * destinationDevice() + { + return 0; + } private: - QString hostName; - quint16 port; - QHttp::ConnectionMode mode; + QString hostName; + quint16 port; + QHttp::ConnectionMode mode; }; -void QHttpSetHostRequest::start(QHttp *http) +void QHttpSetHostRequest::start(QHttp * http) { - http->d->hostName = hostName; - http->d->port = port; - http->d->mode = mode; + http->d->hostName = hostName; + http->d->port = port; + http->d->mode = mode; #ifdef QT_NO_OPENSSL - if (mode == QHttp::ConnectionModeHttps) { - // SSL requested but no SSL support compiled in - http->d->finishedWithError(QLatin1String(QT_TRANSLATE_NOOP("QHttp", "HTTPS connection requested but SSL support not compiled in")), - QHttp::UnknownError); - return; - } + if(mode == QHttp::ConnectionModeHttps) + { + // SSL requested but no SSL support compiled in + http->d->finishedWithError(QLatin1String(QT_TRANSLATE_NOOP("QHttp", "HTTPS connection requested but SSL support not compiled in")), + QHttp::UnknownError); + return; + } #endif - http->d->finishedWithSuccess(); + http->d->finishedWithSuccess(); } /**************************************************** @@ -385,27 +400,31 @@ void QHttpSetHostRequest::start(QHttp *http) class QHttpSetUserRequest : public QHttpRequest { public: - QHttpSetUserRequest(const QString &userName, const QString &password) : - user(userName), pass(password) - { } + QHttpSetUserRequest(const QString & userName, const QString & password) : user(userName), pass(password) + { + } - void start(QHttp *); + void start(QHttp *); - QIODevice *sourceDevice() - { return 0; } - QIODevice *destinationDevice() - { return 0; } + QIODevice * sourceDevice() + { + return 0; + } + QIODevice * destinationDevice() + { + return 0; + } private: - QString user; - QString pass; + QString user; + QString pass; }; -void QHttpSetUserRequest::start(QHttp *http) +void QHttpSetUserRequest::start(QHttp * http) { - http->d->authenticator.setUser(user); - http->d->authenticator.setPassword(pass); - http->d->finishedWithSuccess(); + http->d->authenticator.setUser(user); + http->d->authenticator.setPassword(pass); + http->d->finishedWithSuccess(); } #ifndef QT_NO_NETWORKPROXY @@ -419,29 +438,34 @@ void QHttpSetUserRequest::start(QHttp *http) class QHttpSetProxyRequest : public QHttpRequest { public: - inline QHttpSetProxyRequest(const QNetworkProxy &proxy) - { - this->proxy = proxy; - } + inline QHttpSetProxyRequest(const QNetworkProxy & proxy) + { + this->proxy = proxy; + } - inline void start(QHttp *http) - { - http->d->proxy = proxy; - QString user = proxy.user(); - if (!user.isEmpty()) - http->d->proxyAuthenticator.setUser(user); - QString password = proxy.password(); - if (!password.isEmpty()) - http->d->proxyAuthenticator.setPassword(password); - http->d->finishedWithSuccess(); - } + inline void start(QHttp * http) + { + http->d->proxy = proxy; + QString user = proxy.user(); + if(!user.isEmpty()) + http->d->proxyAuthenticator.setUser(user); + QString password = proxy.password(); + if(!password.isEmpty()) + http->d->proxyAuthenticator.setPassword(password); + http->d->finishedWithSuccess(); + } + + inline QIODevice * sourceDevice() + { + return 0; + } + inline QIODevice * destinationDevice() + { + return 0; + } - inline QIODevice *sourceDevice() - { return 0; } - inline QIODevice *destinationDevice() - { return 0; } private: - QNetworkProxy proxy; + QNetworkProxy proxy; }; #endif // QT_NO_NETWORKPROXY @@ -455,24 +479,29 @@ private: class QHttpSetSocketRequest : public QHttpRequest { public: - QHttpSetSocketRequest(QTcpSocket *s) : socket(s) - { } + QHttpSetSocketRequest(QTcpSocket * s) : socket(s) + { + } - void start(QHttp *); + void start(QHttp *); - QIODevice *sourceDevice() - { return 0; } - QIODevice *destinationDevice() - { return 0; } + QIODevice * sourceDevice() + { + return 0; + } + QIODevice * destinationDevice() + { + return 0; + } private: - QTcpSocket *socket; + QTcpSocket * socket; }; -void QHttpSetSocketRequest::start(QHttp *http) +void QHttpSetSocketRequest::start(QHttp * http) { - http->d->setSock(socket); - http->d->finishedWithSuccess(); + http->d->setSock(socket); + http->d->finishedWithSuccess(); } /**************************************************** @@ -484,30 +513,35 @@ void QHttpSetSocketRequest::start(QHttp *http) class QHttpCloseRequest : public QHttpRequest { public: - QHttpCloseRequest() - { } - void start(QHttp *); + QHttpCloseRequest() + { + } + void start(QHttp *); - QIODevice *sourceDevice() - { return 0; } - QIODevice *destinationDevice() - { return 0; } + QIODevice * sourceDevice() + { + return 0; + } + QIODevice * destinationDevice() + { + return 0; + } }; -void QHttpCloseRequest::start(QHttp *http) +void QHttpCloseRequest::start(QHttp * http) { - http->d->closeConn(); + http->d->closeConn(); } class QHttpHeaderPrivate { - Q_DECLARE_PUBLIC(QHttpHeader) + Q_DECLARE_PUBLIC(QHttpHeader) public: - inline virtual ~QHttpHeaderPrivate() {} + inline virtual ~QHttpHeaderPrivate() {} - QList<QPair<QString, QString> > values; - bool valid; - QHttpHeader *q_ptr; + QList<QPair<QString, QString>> values; + bool valid; + QHttpHeader * q_ptr; }; /**************************************************** @@ -569,21 +603,21 @@ public: QHttpHeader::QHttpHeader() : d_ptr(new QHttpHeaderPrivate) { - Q_D(QHttpHeader); - d->q_ptr = this; - d->valid = true; + Q_D(QHttpHeader); + d->q_ptr = this; + d->valid = true; } /*! Constructs a copy of \a header. */ -QHttpHeader::QHttpHeader(const QHttpHeader &header) +QHttpHeader::QHttpHeader(const QHttpHeader & header) : d_ptr(new QHttpHeaderPrivate) { - Q_D(QHttpHeader); - d->q_ptr = this; - d->valid = header.d_func()->valid; - d->values = header.d_func()->values; + Q_D(QHttpHeader); + d->q_ptr = this; + d->valid = header.d_func()->valid; + d->values = header.d_func()->values; } /*! @@ -594,36 +628,36 @@ QHttpHeader::QHttpHeader(const QHttpHeader &header) "\r\n" delimited lines; each of these lines should have the format key, colon, space, value. */ -QHttpHeader::QHttpHeader(const QString &str) +QHttpHeader::QHttpHeader(const QString & str) : d_ptr(new QHttpHeaderPrivate) { - Q_D(QHttpHeader); - d->q_ptr = this; - d->valid = true; - parse(str); + Q_D(QHttpHeader); + d->q_ptr = this; + d->valid = true; + parse(str); } /*! \internal */ -QHttpHeader::QHttpHeader(QHttpHeaderPrivate &dd, const QString &str) +QHttpHeader::QHttpHeader(QHttpHeaderPrivate & dd, const QString & str) : d_ptr(&dd) { - Q_D(QHttpHeader); - d->q_ptr = this; - d->valid = true; - if (!str.isEmpty()) - parse(str); + Q_D(QHttpHeader); + d->q_ptr = this; + d->valid = true; + if(!str.isEmpty()) + parse(str); } /*! \internal */ -QHttpHeader::QHttpHeader(QHttpHeaderPrivate &dd, const QHttpHeader &header) +QHttpHeader::QHttpHeader(QHttpHeaderPrivate & dd, const QHttpHeader & header) : d_ptr(&dd) { - Q_D(QHttpHeader); - d->q_ptr = this; - d->valid = header.d_func()->valid; - d->values = header.d_func()->values; + Q_D(QHttpHeader); + d->q_ptr = this; + d->valid = header.d_func()->valid; + d->values = header.d_func()->values; } /*! Destructor. @@ -635,12 +669,12 @@ QHttpHeader::~QHttpHeader() /*! Assigns \a h and returns a reference to this http header. */ -QHttpHeader &QHttpHeader::operator=(const QHttpHeader &h) +QHttpHeader & QHttpHeader::operator=(const QHttpHeader & h) { - Q_D(QHttpHeader); - d->values = h.d_func()->values; - d->valid = h.d_func()->valid; - return *this; + Q_D(QHttpHeader); + d->values = h.d_func()->values; + d->valid = h.d_func()->valid; + return *this; } /*! @@ -650,8 +684,8 @@ QHttpHeader &QHttpHeader::operator=(const QHttpHeader &h) */ bool QHttpHeader::isValid() const { - Q_D(const QHttpHeader); - return d->valid; + Q_D(const QHttpHeader); + return d->valid; } /*! \internal @@ -663,52 +697,60 @@ bool QHttpHeader::isValid() const \sa toString() */ -bool QHttpHeader::parse(const QString &str) +bool QHttpHeader::parse(const QString & str) { - Q_D(QHttpHeader); - QStringList lst; - int pos = str.indexOf(QLatin1Char('\n')); - if (pos > 0 && str.at(pos - 1) == QLatin1Char('\r')) - lst = str.trimmed().split(QLatin1String("\r\n")); - else - lst = str.trimmed().split(QLatin1String("\n")); - lst.removeAll(QString()); // No empties + Q_D(QHttpHeader); + QStringList lst; + int pos = str.indexOf(QLatin1Char('\n')); + if(pos > 0 && str.at(pos - 1) == QLatin1Char('\r')) + lst = str.trimmed().split(QLatin1String("\r\n")); + else + lst = str.trimmed().split(QLatin1String("\n")); + lst.removeAll(QString()); // No empties - if (lst.isEmpty()) - return true; + if(lst.isEmpty()) + return true; - QStringList lines; - QStringList::Iterator it = lst.begin(); - for (; it != lst.end(); ++it) { - if (!(*it).isEmpty()) { - if ((*it)[0].isSpace()) { - if (!lines.isEmpty()) { - lines.last() += QLatin1Char(' '); - lines.last() += (*it).trimmed(); - } - } else { - lines.append((*it)); - } - } - } + QStringList lines; + QStringList::Iterator it = lst.begin(); + for(; it != lst.end(); ++it) + { + if(!(*it).isEmpty()) + { + if((*it)[0].isSpace()) + { + if(!lines.isEmpty()) + { + lines.last() += QLatin1Char(' '); + lines.last() += (*it).trimmed(); + } + } + else + { + lines.append((*it)); + } + } + } - int number = 0; - it = lines.begin(); - for (; it != lines.end(); ++it) { - if (!parseLine(*it, number++)) { - d->valid = false; - return false; - } - } - return true; + int number = 0; + it = lines.begin(); + for(; it != lines.end(); ++it) + { + if(!parseLine(*it, number++)) + { + d->valid = false; + return false; + } + } + return true; } /*! \internal */ void QHttpHeader::setValid(bool v) { - Q_D(QHttpHeader); - d->valid = v; + Q_D(QHttpHeader); + d->valid = v; } /*! @@ -717,35 +759,37 @@ void QHttpHeader::setValid(bool v) \sa setValue() removeValue() hasKey() keys() */ -QString QHttpHeader::value(const QString &key) const +QString QHttpHeader::value(const QString & key) const { - Q_D(const QHttpHeader); - QString lowercaseKey = key.toLower(); - QList<QPair<QString, QString> >::ConstIterator it = d->values.constBegin(); - while (it != d->values.constEnd()) { - if ((*it).first.toLower() == lowercaseKey) - return (*it).second; - ++it; - } - return QString(); + Q_D(const QHttpHeader); + QString lowercaseKey = key.toLower(); + QList<QPair<QString, QString>>::ConstIterator it = d->values.constBegin(); + while(it != d->values.constEnd()) + { + if((*it).first.toLower() == lowercaseKey) + return (*it).second; + ++it; + } + return QString(); } /*! Returns all the entries with the given \a key. If no entry has this \a key, an empty string list is returned. */ -QStringList QHttpHeader::allValues(const QString &key) const +QStringList QHttpHeader::allValues(const QString & key) const { - Q_D(const QHttpHeader); - QString lowercaseKey = key.toLower(); - QStringList valueList; - QList<QPair<QString, QString> >::ConstIterator it = d->values.constBegin(); - while (it != d->values.constEnd()) { - if ((*it).first.toLower() == lowercaseKey) - valueList.append((*it).second); - ++it; - } - return valueList; + Q_D(const QHttpHeader); + QString lowercaseKey = key.toLower(); + QStringList valueList; + QList<QPair<QString, QString>>::ConstIterator it = d->values.constBegin(); + while(it != d->values.constEnd()) + { + if((*it).first.toLower() == lowercaseKey) + valueList.append((*it).second); + ++it; + } + return valueList; } /*! @@ -755,20 +799,22 @@ QStringList QHttpHeader::allValues(const QString &key) const */ QStringList QHttpHeader::keys() const { - Q_D(const QHttpHeader); - QStringList keyList; - QSet<QString> seenKeys; - QList<QPair<QString, QString> >::ConstIterator it = d->values.constBegin(); - while (it != d->values.constEnd()) { - const QString &key = (*it).first; - QString lowercaseKey = key.toLower(); - if (!seenKeys.contains(lowercaseKey)) { - keyList.append(key); - seenKeys.insert(lowercaseKey); - } - ++it; - } - return keyList; + Q_D(const QHttpHeader); + QStringList keyList; + QSet<QString> seenKeys; + QList<QPair<QString, QString>>::ConstIterator it = d->values.constBegin(); + while(it != d->values.constEnd()) + { + const QString & key = (*it).first; + QString lowercaseKey = key.toLower(); + if(!seenKeys.contains(lowercaseKey)) + { + keyList.append(key); + seenKeys.insert(lowercaseKey); + } + ++it; + } + return keyList; } /*! @@ -777,17 +823,18 @@ QStringList QHttpHeader::keys() const \sa value() setValue() keys() */ -bool QHttpHeader::hasKey(const QString &key) const +bool QHttpHeader::hasKey(const QString & key) const { - Q_D(const QHttpHeader); - QString lowercaseKey = key.toLower(); - QList<QPair<QString, QString> >::ConstIterator it = d->values.constBegin(); - while (it != d->values.constEnd()) { - if ((*it).first.toLower() == lowercaseKey) - return true; - ++it; - } - return false; + Q_D(const QHttpHeader); + QString lowercaseKey = key.toLower(); + QList<QPair<QString, QString>>::ConstIterator it = d->values.constBegin(); + while(it != d->values.constEnd()) + { + if((*it).first.toLower() == lowercaseKey) + return true; + ++it; + } + return false; } /*! @@ -800,47 +847,49 @@ bool QHttpHeader::hasKey(const QString &key) const \sa value() hasKey() removeValue() */ -void QHttpHeader::setValue(const QString &key, const QString &value) +void QHttpHeader::setValue(const QString & key, const QString & value) { - Q_D(QHttpHeader); - QString lowercaseKey = key.toLower(); - QList<QPair<QString, QString> >::Iterator it = d->values.begin(); - while (it != d->values.end()) { - if ((*it).first.toLower() == lowercaseKey) { - (*it).second = value; - return; - } - ++it; - } - // not found so add - addValue(key, value); + Q_D(QHttpHeader); + QString lowercaseKey = key.toLower(); + QList<QPair<QString, QString>>::Iterator it = d->values.begin(); + while(it != d->values.end()) + { + if((*it).first.toLower() == lowercaseKey) + { + (*it).second = value; + return; + } + ++it; + } + // not found so add + addValue(key, value); } /*! Sets the header entries to be the list of key value pairs in \a values. */ -void QHttpHeader::setValues(const QList<QPair<QString, QString> > &values) +void QHttpHeader::setValues(const QList<QPair<QString, QString>> & values) { - Q_D(QHttpHeader); - d->values = values; + Q_D(QHttpHeader); + d->values = values; } /*! Adds a new entry with the \a key and \a value. */ -void QHttpHeader::addValue(const QString &key, const QString &value) +void QHttpHeader::addValue(const QString & key, const QString & value) { - Q_D(QHttpHeader); - d->values.append(qMakePair(key, value)); + Q_D(QHttpHeader); + d->values.append(qMakePair(key, value)); } /*! Returns all the entries in the header. */ -QList<QPair<QString, QString> > QHttpHeader::values() const +QList<QPair<QString, QString>> QHttpHeader::values() const { - Q_D(const QHttpHeader); - return d->values; + Q_D(const QHttpHeader); + return d->values; } /*! @@ -848,35 +897,39 @@ QList<QPair<QString, QString> > QHttpHeader::values() const \sa value() setValue() */ -void QHttpHeader::removeValue(const QString &key) +void QHttpHeader::removeValue(const QString & key) { - Q_D(QHttpHeader); - QString lowercaseKey = key.toLower(); - QList<QPair<QString, QString> >::Iterator it = d->values.begin(); - while (it != d->values.end()) { - if ((*it).first.toLower() == lowercaseKey) { - d->values.erase(it); - return; - } - ++it; - } + Q_D(QHttpHeader); + QString lowercaseKey = key.toLower(); + QList<QPair<QString, QString>>::Iterator it = d->values.begin(); + while(it != d->values.end()) + { + if((*it).first.toLower() == lowercaseKey) + { + d->values.erase(it); + return; + } + ++it; + } } /*! Removes all the entries with the key \a key from the HTTP header. */ -void QHttpHeader::removeAllValues(const QString &key) +void QHttpHeader::removeAllValues(const QString & key) { - Q_D(QHttpHeader); - QString lowercaseKey = key.toLower(); - QList<QPair<QString, QString> >::Iterator it = d->values.begin(); - while (it != d->values.end()) { - if ((*it).first.toLower() == lowercaseKey) { - it = d->values.erase(it); - continue; - } - ++it; - } + Q_D(QHttpHeader); + QString lowercaseKey = key.toLower(); + QList<QPair<QString, QString>>::Iterator it = d->values.begin(); + while(it != d->values.end()) + { + if((*it).first.toLower() == lowercaseKey) + { + it = d->values.erase(it); + continue; + } + ++it; + } } /*! \internal @@ -887,15 +940,15 @@ void QHttpHeader::removeAllValues(const QString &key) \sa parse() */ -bool QHttpHeader::parseLine(const QString &line, int) +bool QHttpHeader::parseLine(const QString & line, int) { - int i = line.indexOf(QLatin1Char(':')); - if (i == -1) - return false; + int i = line.indexOf(QLatin1Char(':')); + if(i == -1) + return false; - addValue(line.left(i).trimmed(), line.mid(i + 1).trimmed()); + addValue(line.left(i).trimmed(), line.mid(i + 1).trimmed()); - return true; + return true; } /*! @@ -907,18 +960,19 @@ bool QHttpHeader::parseLine(const QString &line, int) */ QString QHttpHeader::toString() const { - Q_D(const QHttpHeader); - if (!isValid()) - return QLatin1String(""); + Q_D(const QHttpHeader); + if(!isValid()) + return QLatin1String(""); - QString ret = QLatin1String(""); + QString ret = QLatin1String(""); - QList<QPair<QString, QString> >::ConstIterator it = d->values.constBegin(); - while (it != d->values.constEnd()) { - ret += (*it).first + QLatin1String(": ") + (*it).second + QLatin1String("\r\n"); - ++it; - } - return ret; + QList<QPair<QString, QString>>::ConstIterator it = d->values.constBegin(); + while(it != d->values.constEnd()) + { + ret += (*it).first + QLatin1String(": ") + (*it).second + QLatin1String("\r\n"); + ++it; + } + return ret; } /*! @@ -929,7 +983,7 @@ QString QHttpHeader::toString() const */ bool QHttpHeader::hasContentLength() const { - return hasKey(QLatin1String("content-length")); + return hasKey(QLatin1String("content-length")); } /*! @@ -940,7 +994,7 @@ bool QHttpHeader::hasContentLength() const */ uint QHttpHeader::contentLength() const { - return value(QLatin1String("content-length")).toUInt(); + return value(QLatin1String("content-length")).toUInt(); } /*! @@ -951,7 +1005,7 @@ uint QHttpHeader::contentLength() const */ void QHttpHeader::setContentLength(int len) { - setValue(QLatin1String("content-length"), QString::number(len)); + setValue(QLatin1String("content-length"), QString::number(len)); } /*! @@ -962,7 +1016,7 @@ void QHttpHeader::setContentLength(int len) */ bool QHttpHeader::hasContentType() const { - return hasKey(QLatin1String("content-type")); + return hasKey(QLatin1String("content-type")); } /*! @@ -972,15 +1026,15 @@ bool QHttpHeader::hasContentType() const */ QString QHttpHeader::contentType() const { - QString type = value(QLatin1String("content-type")); - if (type.isEmpty()) - return QString(); + QString type = value(QLatin1String("content-type")); + if(type.isEmpty()) + return QString(); - int pos = type.indexOf(QLatin1Char(';')); - if (pos == -1) - return type; + int pos = type.indexOf(QLatin1Char(';')); + if(pos == -1) + return type; - return type.left(pos).trimmed(); + return type.left(pos).trimmed(); } /*! @@ -989,19 +1043,19 @@ QString QHttpHeader::contentType() const \sa contentType() hasContentType() */ -void QHttpHeader::setContentType(const QString &type) +void QHttpHeader::setContentType(const QString & type) { - setValue(QLatin1String("content-type"), type); + setValue(QLatin1String("content-type"), type); } class QHttpResponseHeaderPrivate : public QHttpHeaderPrivate { - Q_DECLARE_PUBLIC(QHttpResponseHeader) + Q_DECLARE_PUBLIC(QHttpResponseHeader) public: - int statCode; - QString reasonPhr; - int majVer; - int minVer; + int statCode; + QString reasonPhr; + int majVer; + int minVer; }; /**************************************************** @@ -1037,34 +1091,34 @@ public: QHttpResponseHeader::QHttpResponseHeader() : QHttpHeader(*new QHttpResponseHeaderPrivate) { - setValid(false); + setValid(false); } /*! Constructs a copy of \a header. */ -QHttpResponseHeader::QHttpResponseHeader(const QHttpResponseHeader &header) +QHttpResponseHeader::QHttpResponseHeader(const QHttpResponseHeader & header) : QHttpHeader(*new QHttpResponseHeaderPrivate, header) { - Q_D(QHttpResponseHeader); - d->statCode = header.d_func()->statCode; - d->reasonPhr = header.d_func()->reasonPhr; - d->majVer = header.d_func()->majVer; - d->minVer = header.d_func()->minVer; + Q_D(QHttpResponseHeader); + d->statCode = header.d_func()->statCode; + d->reasonPhr = header.d_func()->reasonPhr; + d->majVer = header.d_func()->majVer; + d->minVer = header.d_func()->minVer; } /*! Copies the contents of \a header into this QHttpResponseHeader. */ -QHttpResponseHeader &QHttpResponseHeader::operator=(const QHttpResponseHeader &header) +QHttpResponseHeader & QHttpResponseHeader::operator=(const QHttpResponseHeader & header) { - Q_D(QHttpResponseHeader); - QHttpHeader::operator=(header); - d->statCode = header.d_func()->statCode; - d->reasonPhr = header.d_func()->reasonPhr; - d->majVer = header.d_func()->majVer; - d->minVer = header.d_func()->minVer; - return *this; + Q_D(QHttpResponseHeader); + QHttpHeader::operator=(header); + d->statCode = header.d_func()->statCode; + d->reasonPhr = header.d_func()->reasonPhr; + d->majVer = header.d_func()->majVer; + d->minVer = header.d_func()->minVer; + return *this; } /*! @@ -1075,10 +1129,10 @@ QHttpResponseHeader &QHttpResponseHeader::operator=(const QHttpResponseHeader &h reason-phrase); each of remaining lines should have the format key, colon, space, value. */ -QHttpResponseHeader::QHttpResponseHeader(const QString &str) +QHttpResponseHeader::QHttpResponseHeader(const QString & str) : QHttpHeader(*new QHttpResponseHeaderPrivate) { - parse(str); + parse(str); } /*! @@ -1090,10 +1144,10 @@ QHttpResponseHeader::QHttpResponseHeader(const QString &str) \sa statusCode() reasonPhrase() majorVersion() minorVersion() */ -QHttpResponseHeader::QHttpResponseHeader(int code, const QString &text, int majorVer, int minorVer) +QHttpResponseHeader::QHttpResponseHeader(int code, const QString & text, int majorVer, int minorVer) : QHttpHeader(*new QHttpResponseHeaderPrivate) { - setStatusLine(code, text, majorVer, minorVer); + setStatusLine(code, text, majorVer, minorVer); } /*! @@ -1104,14 +1158,14 @@ QHttpResponseHeader::QHttpResponseHeader(int code, const QString &text, int majo \sa statusCode() reasonPhrase() majorVersion() minorVersion() */ -void QHttpResponseHeader::setStatusLine(int code, const QString &text, int majorVer, int minorVer) +void QHttpResponseHeader::setStatusLine(int code, const QString & text, int majorVer, int minorVer) { - Q_D(QHttpResponseHeader); - setValid(true); - d->statCode = code; - d->reasonPhr = text; - d->majVer = majorVer; - d->minVer = minorVer; + Q_D(QHttpResponseHeader); + setValid(true); + d->statCode = code; + d->reasonPhr = text; + d->majVer = majorVer; + d->minVer = minorVer; } /*! @@ -1121,8 +1175,8 @@ void QHttpResponseHeader::setStatusLine(int code, const QString &text, int major */ int QHttpResponseHeader::statusCode() const { - Q_D(const QHttpResponseHeader); - return d->statCode; + Q_D(const QHttpResponseHeader); + return d->statCode; } /*! @@ -1132,8 +1186,8 @@ int QHttpResponseHeader::statusCode() const */ QString QHttpResponseHeader::reasonPhrase() const { - Q_D(const QHttpResponseHeader); - return d->reasonPhr; + Q_D(const QHttpResponseHeader); + return d->reasonPhr; } /*! @@ -1143,8 +1197,8 @@ QString QHttpResponseHeader::reasonPhrase() const */ int QHttpResponseHeader::majorVersion() const { - Q_D(const QHttpResponseHeader); - return d->majVer; + Q_D(const QHttpResponseHeader); + return d->majVer; } /*! @@ -1154,59 +1208,64 @@ int QHttpResponseHeader::majorVersion() const */ int QHttpResponseHeader::minorVersion() const { - Q_D(const QHttpResponseHeader); - return d->minVer; + Q_D(const QHttpResponseHeader); + return d->minVer; } /*! \internal */ -bool QHttpResponseHeader::parseLine(const QString &line, int number) +bool QHttpResponseHeader::parseLine(const QString & line, int number) { - Q_D(QHttpResponseHeader); - if (number != 0) - return QHttpHeader::parseLine(line, number); + Q_D(QHttpResponseHeader); + if(number != 0) + return QHttpHeader::parseLine(line, number); - QString l = line.simplified(); - if (l.length() < 10) - return false; + QString l = line.simplified(); + if(l.length() < 10) + return false; - if (l.left(5) == QLatin1String("HTTP/") && l[5].isDigit() && l[6] == QLatin1Char('.') && - l[7].isDigit() && l[8] == QLatin1Char(' ') && l[9].isDigit()) { - d->majVer = l[5].toLatin1() - '0'; - d->minVer = l[7].toLatin1() - '0'; + if(l.left(5) == QLatin1String("HTTP/") && l[5].isDigit() && l[6] == QLatin1Char('.') && l[7].isDigit() && l[8] == QLatin1Char(' ') && l[9].isDigit()) + { + d->majVer = l[5].toLatin1() - '0'; + d->minVer = l[7].toLatin1() - '0'; - int pos = l.indexOf(QLatin1Char(' '), 9); - if (pos != -1) { - d->reasonPhr = l.mid(pos + 1); - d->statCode = l.mid(9, pos - 9).toInt(); - } else { - d->statCode = l.mid(9).toInt(); - d->reasonPhr.clear(); - } - } else { - return false; - } + int pos = l.indexOf(QLatin1Char(' '), 9); + if(pos != -1) + { + d->reasonPhr = l.mid(pos + 1); + d->statCode = l.mid(9, pos - 9).toInt(); + } + else + { + d->statCode = l.mid(9).toInt(); + d->reasonPhr.clear(); + } + } + else + { + return false; + } - return true; + return true; } /*! \reimp */ QString QHttpResponseHeader::toString() const { - Q_D(const QHttpResponseHeader); - QString ret(QLatin1String("HTTP/%1.%2 %3 %4\r\n%5\r\n")); - return ret.arg(d->majVer).arg(d->minVer).arg(d->statCode).arg(d->reasonPhr).arg(QHttpHeader::toString()); + Q_D(const QHttpResponseHeader); + QString ret(QLatin1String("HTTP/%1.%2 %3 %4\r\n%5\r\n")); + return ret.arg(d->majVer).arg(d->minVer).arg(d->statCode).arg(d->reasonPhr).arg(QHttpHeader::toString()); } class QHttpRequestHeaderPrivate : public QHttpHeaderPrivate { - Q_DECLARE_PUBLIC(QHttpRequestHeader) + Q_DECLARE_PUBLIC(QHttpRequestHeader) public: - QString m; - QString p; - int majVer; - int minVer; + QString m; + QString p; + int majVer; + int minVer; }; /**************************************************** @@ -1252,7 +1311,7 @@ public: QHttpRequestHeader::QHttpRequestHeader() : QHttpHeader(*new QHttpRequestHeaderPrivate) { - setValid(false); + setValid(false); } /*! @@ -1261,41 +1320,41 @@ QHttpRequestHeader::QHttpRequestHeader() minorVer. The \a path argument must be properly encoded for an HTTP request. */ -QHttpRequestHeader::QHttpRequestHeader(const QString &method, const QString &path, int majorVer, int minorVer) +QHttpRequestHeader::QHttpRequestHeader(const QString & method, const QString & path, int majorVer, int minorVer) : QHttpHeader(*new QHttpRequestHeaderPrivate) { - Q_D(QHttpRequestHeader); - d->m = method; - d->p = path; - d->majVer = majorVer; - d->minVer = minorVer; + Q_D(QHttpRequestHeader); + d->m = method; + d->p = path; + d->majVer = majorVer; + d->minVer = minorVer; } /*! Constructs a copy of \a header. */ -QHttpRequestHeader::QHttpRequestHeader(const QHttpRequestHeader &header) +QHttpRequestHeader::QHttpRequestHeader(const QHttpRequestHeader & header) : QHttpHeader(*new QHttpRequestHeaderPrivate, header) { - Q_D(QHttpRequestHeader); - d->m = header.d_func()->m; - d->p = header.d_func()->p; - d->majVer = header.d_func()->majVer; - d->minVer = header.d_func()->minVer; + Q_D(QHttpRequestHeader); + d->m = header.d_func()->m; + d->p = header.d_func()->p; + d->majVer = header.d_func()->majVer; + d->minVer = header.d_func()->minVer; } /*! Copies the content of \a header into this QHttpRequestHeader */ -QHttpRequestHeader &QHttpRequestHeader::operator=(const QHttpRequestHeader &header) +QHttpRequestHeader & QHttpRequestHeader::operator=(const QHttpRequestHeader & header) { - Q_D(QHttpRequestHeader); - QHttpHeader::operator=(header); - d->m = header.d_func()->m; - d->p = header.d_func()->p; - d->majVer = header.d_func()->majVer; - d->minVer = header.d_func()->minVer; - return *this; + Q_D(QHttpRequestHeader); + QHttpHeader::operator=(header); + d->m = header.d_func()->m; + d->p = header.d_func()->p; + d->majVer = header.d_func()->majVer; + d->minVer = header.d_func()->minVer; + return *this; } /*! @@ -1305,10 +1364,10 @@ QHttpRequestHeader &QHttpRequestHeader::operator=(const QHttpRequestHeader &head HTTP-version); each of the remaining lines should have the format key, colon, space, value. */ -QHttpRequestHeader::QHttpRequestHeader(const QString &str) +QHttpRequestHeader::QHttpRequestHeader(const QString & str) : QHttpHeader(*new QHttpRequestHeaderPrivate) { - parse(str); + parse(str); } /*! @@ -1319,14 +1378,14 @@ QHttpRequestHeader::QHttpRequestHeader(const QString &str) \sa method() path() majorVersion() minorVersion() */ -void QHttpRequestHeader::setRequest(const QString &method, const QString &path, int majorVer, int minorVer) +void QHttpRequestHeader::setRequest(const QString & method, const QString & path, int majorVer, int minorVer) { - Q_D(QHttpRequestHeader); - setValid(true); - d->m = method; - d->p = path; - d->majVer = majorVer; - d->minVer = minorVer; + Q_D(QHttpRequestHeader); + setValid(true); + d->m = method; + d->p = path; + d->majVer = majorVer; + d->minVer = minorVer; } /*! @@ -1336,8 +1395,8 @@ void QHttpRequestHeader::setRequest(const QString &method, const QString &path, */ QString QHttpRequestHeader::method() const { - Q_D(const QHttpRequestHeader); - return d->m; + Q_D(const QHttpRequestHeader); + return d->m; } /*! @@ -1347,8 +1406,8 @@ QString QHttpRequestHeader::method() const */ QString QHttpRequestHeader::path() const { - Q_D(const QHttpRequestHeader); - return d->p; + Q_D(const QHttpRequestHeader); + return d->p; } /*! @@ -1358,8 +1417,8 @@ QString QHttpRequestHeader::path() const */ int QHttpRequestHeader::majorVersion() const { - Q_D(const QHttpRequestHeader); - return d->majVer; + Q_D(const QHttpRequestHeader); + return d->majVer; } /*! @@ -1369,50 +1428,51 @@ int QHttpRequestHeader::majorVersion() const */ int QHttpRequestHeader::minorVersion() const { - Q_D(const QHttpRequestHeader); - return d->minVer; + Q_D(const QHttpRequestHeader); + return d->minVer; } /*! \internal */ -bool QHttpRequestHeader::parseLine(const QString &line, int number) +bool QHttpRequestHeader::parseLine(const QString & line, int number) { - Q_D(QHttpRequestHeader); - if (number != 0) - return QHttpHeader::parseLine(line, number); + Q_D(QHttpRequestHeader); + if(number != 0) + return QHttpHeader::parseLine(line, number); - QStringList lst = line.simplified().split(QLatin1String(" ")); - if (lst.count() > 0) { - d->m = lst[0]; - if (lst.count() > 1) { - d->p = lst[1]; - if (lst.count() > 2) { - QString v = lst[2]; - if (v.length() >= 8 && v.left(5) == QLatin1String("HTTP/") && - v[5].isDigit() && v[6] == QLatin1Char('.') && v[7].isDigit()) { - d->majVer = v[5].toLatin1() - '0'; - d->minVer = v[7].toLatin1() - '0'; - return true; - } - } - } - } + QStringList lst = line.simplified().split(QLatin1String(" ")); + if(lst.count() > 0) + { + d->m = lst[0]; + if(lst.count() > 1) + { + d->p = lst[1]; + if(lst.count() > 2) + { + QString v = lst[2]; + if(v.length() >= 8 && v.left(5) == QLatin1String("HTTP/") && v[5].isDigit() && v[6] == QLatin1Char('.') && v[7].isDigit()) + { + d->majVer = v[5].toLatin1() - '0'; + d->minVer = v[7].toLatin1() - '0'; + return true; + } + } + } + } - return false; + return false; } /*! \reimp */ QString QHttpRequestHeader::toString() const { - Q_D(const QHttpRequestHeader); - QString first(QLatin1String("%1 %2")); - QString last(QLatin1String(" HTTP/%3.%4\r\n%5\r\n")); - return first.arg(d->m).arg(d->p) + - last.arg(d->majVer).arg(d->minVer).arg(QHttpHeader::toString()); + Q_D(const QHttpRequestHeader); + QString first(QLatin1String("%1 %2")); + QString last(QLatin1String(" HTTP/%3.%4\r\n%5\r\n")); + return first.arg(d->m).arg(d->p) + last.arg(d->majVer).arg(d->minVer).arg(QHttpHeader::toString()); } - /**************************************************** * * QHttp @@ -1526,10 +1586,10 @@ QString QHttpRequestHeader::toString() const Constructs a QHttp object. The \a parent parameter is passed on to the QObject constructor. */ -QHttp::QHttp(QObject *parent) +QHttp::QHttp(QObject * parent) : QObject(parent), d(new QHttpPrivate(this)) { - d->init(); + d->init(); } /*! @@ -1540,13 +1600,13 @@ QHttp::QHttp(QObject *parent) \sa setHost() */ -QHttp::QHttp(const QString &hostName, quint16 port, QObject *parent) +QHttp::QHttp(const QString & hostName, quint16 port, QObject * parent) : QObject(parent), d(new QHttpPrivate(this)) { - d->init(); + d->init(); - d->hostName = hostName; - d->port = port; + d->hostName = hostName; + d->port = port; } /*! @@ -1561,25 +1621,25 @@ QHttp::QHttp(const QString &hostName, quint16 port, QObject *parent) \sa setHost() */ -QHttp::QHttp(const QString &hostName, ConnectionMode mode, quint16 port, QObject *parent) +QHttp::QHttp(const QString & hostName, ConnectionMode mode, quint16 port, QObject * parent) : QObject(parent), d(new QHttpPrivate(this)) { - d->init(); + d->init(); - d->hostName = hostName; - if (port == 0) - port = (mode == ConnectionModeHttp) ? 80 : 443; - d->port = port; - d->mode = mode; + d->hostName = hostName; + if(port == 0) + port = (mode == ConnectionModeHttp) ? 80 : 443; + d->port = port; + d->mode = mode; } void QHttpPrivate::init() { - Q_Q(QHttp); - errorString = QLatin1String(QT_TRANSLATE_NOOP("QHttp", "Unknown error")); - QMetaObject::invokeMethod(q, "_q_slotDoFinished", Qt::QueuedConnection); - post100ContinueTimer.setSingleShot(true); - QObject::connect(&post100ContinueTimer, SIGNAL(timeout()), q, SLOT(_q_continuePost())); + Q_Q(QHttp); + errorString = QLatin1String(QT_TRANSLATE_NOOP("QHttp", "Unknown error")); + QMetaObject::invokeMethod(q, "_q_slotDoFinished", Qt::QueuedConnection); + post100ContinueTimer.setSingleShot(true); + QObject::connect(&post100ContinueTimer, SIGNAL(timeout()), q, SLOT(_q_continuePost())); } /*! @@ -1588,7 +1648,7 @@ void QHttpPrivate::init() */ QHttp::~QHttp() { - abort(); + abort(); } void QHttp::_q_startNextRequest() @@ -1867,14 +1927,14 @@ void QHttp::_q_continuePost() */ void QHttp::abort() { - if (d->pending.isEmpty()) - return; + if(d->pending.isEmpty()) + return; - d->finishedWithError(tr("Request aborted"), Aborted); - clearPendingRequests(); - if (d->socket) - d->socket->abort(); - d->closeConn(); + d->finishedWithError(tr("Request aborted"), Aborted); + clearPendingRequests(); + if(d->socket) + d->socket->abort(); + d->closeConn(); } /*! @@ -1886,9 +1946,9 @@ void QHttp::abort() qint64 QHttp::bytesAvailable() const { #if defined(QHTTP_DEBUG) - qDebug("QHttp::bytesAvailable(): %d bytes", (int)d->rba.size()); + qDebug("QHttp::bytesAvailable(): %d bytes", (int)d->rba.size()); #endif - return qint64(d->rba.size()); + return qint64(d->rba.size()); } /*! \fn qint64 QHttp::readBlock(char *data, quint64 maxlen) @@ -1902,28 +1962,30 @@ qint64 QHttp::bytesAvailable() const \sa get() post() request() readyRead() bytesAvailable() readAll() */ -qint64 QHttp::read(char *data, qint64 maxlen) +qint64 QHttp::read(char * data, qint64 maxlen) { - if (data == 0 && maxlen != 0) { - qWarning("QHttp::read: Null pointer error"); - return -1; - } - if (maxlen >= d->rba.size()) - maxlen = d->rba.size(); - int readSoFar = 0; - while (!d->rba.isEmpty() && readSoFar < maxlen) { - int nextBlockSize = d->rba.nextDataBlockSize(); - int bytesToRead = qMin<qint64>(maxlen - readSoFar, nextBlockSize); - memcpy(data + readSoFar, d->rba.readPointer(), bytesToRead); - d->rba.free(bytesToRead); - readSoFar += bytesToRead; - } + if(data == 0 && maxlen != 0) + { + qWarning("QHttp::read: Null pointer error"); + return -1; + } + if(maxlen >= d->rba.size()) + maxlen = d->rba.size(); + int readSoFar = 0; + while(!d->rba.isEmpty() && readSoFar < maxlen) + { + int nextBlockSize = d->rba.nextDataBlockSize(); + int bytesToRead = qMin<qint64>(maxlen - readSoFar, nextBlockSize); + memcpy(data + readSoFar, d->rba.readPointer(), bytesToRead); + d->rba.free(bytesToRead); + readSoFar += bytesToRead; + } - d->bytesDone += maxlen; + d->bytesDone += maxlen; #if defined(QHTTP_DEBUG) - qDebug("QHttp::read(): read %lld bytes (%lld bytes done)", maxlen, d->bytesDone); + qDebug("QHttp::read(): read %lld bytes (%lld bytes done)", maxlen, d->bytesDone); #endif - return maxlen; + return maxlen; } /*! @@ -1933,12 +1995,12 @@ qint64 QHttp::read(char *data, qint64 maxlen) */ QByteArray QHttp::readAll() { - qint64 avail = bytesAvailable(); - QByteArray tmp; - tmp.resize(int(avail)); - qint64 got = read(tmp.data(), int(avail)); - tmp.resize(got); - return tmp; + qint64 avail = bytesAvailable(); + QByteArray tmp; + tmp.resize(int(avail)); + qint64 got = read(tmp.data(), int(avail)); + tmp.resize(got); + return tmp; } /*! @@ -1949,9 +2011,9 @@ QByteArray QHttp::readAll() */ int QHttp::currentId() const { - if (d->pending.isEmpty()) - return 0; - return d->pending.first()->id; + if(d->pending.isEmpty()) + return 0; + return d->pending.first()->id; } /*! @@ -1964,12 +2026,13 @@ int QHttp::currentId() const */ QHttpRequestHeader QHttp::currentRequest() const { - if (!d->pending.isEmpty()) { - QHttpRequest *r = d->pending.first(); - if (r->hasRequestHeader()) - return r->requestHeader(); - } - return QHttpRequestHeader(); + if(!d->pending.isEmpty()) + { + QHttpRequest * r = d->pending.first(); + if(r->hasRequestHeader()) + return r->requestHeader(); + } + return QHttpRequestHeader(); } /*! @@ -1981,7 +2044,7 @@ QHttpRequestHeader QHttp::currentRequest() const */ QHttpResponseHeader QHttp::lastResponse() const { - return d->response; + return d->response; } /*! @@ -1994,11 +2057,11 @@ QHttpResponseHeader QHttp::lastResponse() const \sa currentDestinationDevice() post() request() */ -QIODevice *QHttp::currentSourceDevice() const +QIODevice * QHttp::currentSourceDevice() const { - if (d->pending.isEmpty()) - return 0; - return d->pending.first()->sourceDevice(); + if(d->pending.isEmpty()) + return 0; + return d->pending.first()->sourceDevice(); } /*! @@ -2011,11 +2074,11 @@ QIODevice *QHttp::currentSourceDevice() const \sa currentSourceDevice() get() post() request() */ -QIODevice *QHttp::currentDestinationDevice() const +QIODevice * QHttp::currentDestinationDevice() const { - if (d->pending.isEmpty()) - return 0; - return d->pending.first()->destinationDevice(); + if(d->pending.isEmpty()) + return 0; + return d->pending.first()->destinationDevice(); } /*! @@ -2029,7 +2092,7 @@ QIODevice *QHttp::currentDestinationDevice() const */ bool QHttp::hasPendingRequests() const { - return d->pending.count() > 1; + return d->pending.count() > 1; } /*! @@ -2041,9 +2104,9 @@ bool QHttp::hasPendingRequests() const */ void QHttp::clearPendingRequests() { - // delete all entires except the first one - while (d->pending.count() > 1) - delete d->pending.takeLast(); + // delete all entires except the first one + while(d->pending.count() > 1) + delete d->pending.takeLast(); } /*! @@ -2061,9 +2124,9 @@ void QHttp::clearPendingRequests() \sa get() post() head() request() requestStarted() requestFinished() done() */ -int QHttp::setHost(const QString &hostName, quint16 port) +int QHttp::setHost(const QString & hostName, quint16 port) { - return d->addRequest(new QHttpSetHostRequest(hostName, port, ConnectionModeHttp)); + return d->addRequest(new QHttpSetHostRequest(hostName, port, ConnectionModeHttp)); } /*! @@ -2084,15 +2147,15 @@ int QHttp::setHost(const QString &hostName, quint16 port) \sa get() post() head() request() requestStarted() requestFinished() done() */ -int QHttp::setHost(const QString &hostName, ConnectionMode mode, quint16 port) +int QHttp::setHost(const QString & hostName, ConnectionMode mode, quint16 port) { #ifdef QT_NO_OPENSSL - if (mode == ConnectionModeHttps) - qWarning("QHttp::setHost: HTTPS connection requested but SSL support not compiled in"); + if(mode == ConnectionModeHttps) + qWarning("QHttp::setHost: HTTPS connection requested but SSL support not compiled in"); #endif - if (port == 0) - port = (mode == ConnectionModeHttp) ? 80 : 443; - return d->addRequest(new QHttpSetHostRequest(hostName, port, mode)); + if(port == 0) + port = (mode == ConnectionModeHttp) ? 80 : 443; + return d->addRequest(new QHttpSetHostRequest(hostName, port, mode)); } /*! @@ -2116,9 +2179,9 @@ int QHttp::setHost(const QString &hostName, ConnectionMode mode, quint16 port) \sa QObject::moveToThread(), {Thread Support in Qt} */ -int QHttp::setSocket(QTcpSocket *socket) +int QHttp::setSocket(QTcpSocket * socket) { - return d->addRequest(new QHttpSetSocketRequest(socket)); + return d->addRequest(new QHttpSetSocketRequest(socket)); } /*! @@ -2134,9 +2197,9 @@ int QHttp::setSocket(QTcpSocket *socket) emitted. When it is finished the requestFinished() signal is emitted. */ -int QHttp::setUser(const QString &userName, const QString &password) +int QHttp::setUser(const QString & userName, const QString & password) { - return d->addRequest(new QHttpSetUserRequest(userName, password)); + return d->addRequest(new QHttpSetUserRequest(userName, password)); } #ifndef QT_NO_NETWORKPROXY @@ -2160,11 +2223,11 @@ int QHttp::setUser(const QString &userName, const QString &password) \sa QFtp::setProxy() */ -int QHttp::setProxy(const QString &host, int port, - const QString &username, const QString &password) +int QHttp::setProxy(const QString & host, int port, + const QString & username, const QString & password) { - QNetworkProxy proxy(QNetworkProxy::HttpProxy, host, port, username, password); - return d->addRequest(new QHttpSetProxyRequest(proxy)); + QNetworkProxy proxy(QNetworkProxy::HttpProxy, host, port, username, password); + return d->addRequest(new QHttpSetProxyRequest(proxy)); } /*! @@ -2181,9 +2244,9 @@ int QHttp::setProxy(const QString &host, int port, is, ConnectionModeHttp), QHttp will treat the proxy as a caching proxy. */ -int QHttp::setProxy(const QNetworkProxy &proxy) +int QHttp::setProxy(const QNetworkProxy & proxy) { - return d->addRequest(new QHttpSetProxyRequest(proxy)); + return d->addRequest(new QHttpSetProxyRequest(proxy)); } #endif @@ -2219,11 +2282,11 @@ int QHttp::setProxy(const QNetworkProxy &proxy) \sa setHost(), post(), head(), request(), requestStarted(), requestFinished(), done() */ -int QHttp::get(const QString &path, QIODevice *to) +int QHttp::get(const QString & path, QIODevice * to) { - QHttpRequestHeader header(QLatin1String("GET"), path); - header.setValue(QLatin1String("Connection"), QLatin1String("Keep-Alive")); - return d->addRequest(new QHttpPGHRequest(header, (QIODevice *) 0, to)); + QHttpRequestHeader header(QLatin1String("GET"), path); + header.setValue(QLatin1String("Connection"), QLatin1String("Keep-Alive")); + return d->addRequest(new QHttpPGHRequest(header, (QIODevice *)0, to)); } /*! @@ -2256,11 +2319,11 @@ int QHttp::get(const QString &path, QIODevice *to) \sa setHost() get() head() request() requestStarted() requestFinished() done() */ -int QHttp::post(const QString &path, QIODevice *data, QIODevice *to ) +int QHttp::post(const QString & path, QIODevice * data, QIODevice * to) { - QHttpRequestHeader header(QLatin1String("POST"), path); - header.setValue(QLatin1String("Connection"), QLatin1String("Keep-Alive")); - return d->addRequest(new QHttpPGHRequest(header, data, to)); + QHttpRequestHeader header(QLatin1String("POST"), path); + header.setValue(QLatin1String("Connection"), QLatin1String("Keep-Alive")); + return d->addRequest(new QHttpPGHRequest(header, data, to)); } /*! @@ -2268,11 +2331,11 @@ int QHttp::post(const QString &path, QIODevice *data, QIODevice *to ) \a data is used as the content data of the HTTP request. */ -int QHttp::post(const QString &path, const QByteArray &data, QIODevice *to) +int QHttp::post(const QString & path, const QByteArray & data, QIODevice * to) { - QHttpRequestHeader header(QLatin1String("POST"), path); - header.setValue(QLatin1String("Connection"), QLatin1String("Keep-Alive")); - return d->addRequest(new QHttpPGHRequest(header, new QByteArray(data), to)); + QHttpRequestHeader header(QLatin1String("POST"), path); + header.setValue(QLatin1String("Connection"), QLatin1String("Keep-Alive")); + return d->addRequest(new QHttpPGHRequest(header, new QByteArray(data), to)); } /*! @@ -2293,11 +2356,11 @@ int QHttp::post(const QString &path, const QByteArray &data, QIODevice *to) \sa setHost() get() post() request() requestStarted() requestFinished() done() */ -int QHttp::head(const QString &path) +int QHttp::head(const QString & path) { - QHttpRequestHeader header(QLatin1String("HEAD"), path); - header.setValue(QLatin1String("Connection"), QLatin1String("Keep-Alive")); - return d->addRequest(new QHttpPGHRequest(header, (QIODevice*)0, 0)); + QHttpRequestHeader header(QLatin1String("HEAD"), path); + header.setValue(QLatin1String("Connection"), QLatin1String("Keep-Alive")); + return d->addRequest(new QHttpPGHRequest(header, (QIODevice *)0, 0)); } /*! @@ -2327,9 +2390,9 @@ int QHttp::head(const QString &path) \sa setHost() get() post() head() requestStarted() requestFinished() done() */ -int QHttp::request(const QHttpRequestHeader &header, QIODevice *data, QIODevice *to) +int QHttp::request(const QHttpRequestHeader & header, QIODevice * data, QIODevice * to) { - return d->addRequest(new QHttpNormalRequest(header, data, to)); + return d->addRequest(new QHttpNormalRequest(header, data, to)); } /*! @@ -2337,9 +2400,9 @@ int QHttp::request(const QHttpRequestHeader &header, QIODevice *data, QIODevice \a data is used as the content data of the HTTP request. */ -int QHttp::request(const QHttpRequestHeader &header, const QByteArray &data, QIODevice *to ) +int QHttp::request(const QHttpRequestHeader & header, const QByteArray & data, QIODevice * to) { - return d->addRequest(new QHttpNormalRequest(header, new QByteArray(data), to)); + return d->addRequest(new QHttpNormalRequest(header, new QByteArray(data), to)); } /*! @@ -2368,7 +2431,7 @@ int QHttp::request(const QHttpRequestHeader &header, const QByteArray &data, QIO */ int QHttp::close() { - return d->addRequest(new QHttpCloseRequest()); + return d->addRequest(new QHttpCloseRequest()); } /*! @@ -2378,679 +2441,777 @@ int QHttp::close() */ int QHttp::closeConnection() { - return d->addRequest(new QHttpCloseRequest()); + return d->addRequest(new QHttpCloseRequest()); } -int QHttpPrivate::addRequest(QHttpNormalRequest *req) +int QHttpPrivate::addRequest(QHttpNormalRequest * req) { - QHttpRequestHeader h = req->requestHeader(); - if (h.path().isEmpty()) { - // note: the following qWarning is autotested. If you change it, change the test too. - qWarning("QHttp: empty path requested is invalid -- using '/'"); - h.setRequest(h.method(), QLatin1String("/"), h.majorVersion(), h.minorVersion()); - req->setRequestHeader(h); - } + QHttpRequestHeader h = req->requestHeader(); + if(h.path().isEmpty()) + { + // note: the following qWarning is autotested. If you change it, change the test too. + qWarning("QHttp: empty path requested is invalid -- using '/'"); + h.setRequest(h.method(), QLatin1String("/"), h.majorVersion(), h.minorVersion()); + req->setRequestHeader(h); + } - // contine below - return addRequest(static_cast<QHttpRequest *>(req)); + // contine below + return addRequest(static_cast<QHttpRequest *>(req)); } -int QHttpPrivate::addRequest(QHttpRequest *req) +int QHttpPrivate::addRequest(QHttpRequest * req) { - Q_Q(QHttp); - pending.append(req); + Q_Q(QHttp); + pending.append(req); - if (pending.count() == 1) { - // don't emit the requestStarted() signal before the id is returned - QMetaObject::invokeMethod(q, "_q_startNextRequest", Qt::QueuedConnection); - } - return req->id; + if(pending.count() == 1) + { + // don't emit the requestStarted() signal before the id is returned + QMetaObject::invokeMethod(q, "_q_startNextRequest", Qt::QueuedConnection); + } + return req->id; } void QHttpPrivate::_q_startNextRequest() { - Q_Q(QHttp); - if (pending.isEmpty()) - return; - QHttpRequest *r = pending.first(); + Q_Q(QHttp); + if(pending.isEmpty()) + return; + QHttpRequest * r = pending.first(); - error = QHttp::NoError; - errorString = QLatin1String(QT_TRANSLATE_NOOP("QHttp", "Unknown error")); + error = QHttp::NoError; + errorString = QLatin1String(QT_TRANSLATE_NOOP("QHttp", "Unknown error")); - if (q->bytesAvailable() != 0) - q->readAll(); // clear the data - emit q->requestStarted(r->id); - r->start(q); + if(q->bytesAvailable() != 0) + q->readAll(); // clear the data + emit q->requestStarted(r->id); + r->start(q); } void QHttpPrivate::_q_slotSendRequest() { - if (hostName.isNull()) { - finishedWithError(QLatin1String(QT_TRANSLATE_NOOP("QHttp", "No server set to connect to")), - QHttp::UnknownError); - return; - } + if(hostName.isNull()) + { + finishedWithError(QLatin1String(QT_TRANSLATE_NOOP("QHttp", "No server set to connect to")), + QHttp::UnknownError); + return; + } - QString connectionHost = hostName; - int connectionPort = port; - bool sslInUse = false; + QString connectionHost = hostName; + int connectionPort = port; + bool sslInUse = false; #ifndef QT_NO_OPENSSL - QSslSocket *sslSocket = qobject_cast<QSslSocket *>(socket); - if (mode == QHttp::ConnectionModeHttps || (sslSocket && sslSocket->isEncrypted())) - sslInUse = true; + QSslSocket * sslSocket = qobject_cast<QSslSocket *>(socket); + if(mode == QHttp::ConnectionModeHttps || (sslSocket && sslSocket->isEncrypted())) + sslInUse = true; #endif #ifndef QT_NO_NETWORKPROXY - bool cachingProxyInUse = false; - bool transparentProxyInUse = false; - if (proxy.type() == QNetworkProxy::DefaultProxy) - proxy = QNetworkProxy::applicationProxy(); + bool cachingProxyInUse = false; + bool transparentProxyInUse = false; + if(proxy.type() == QNetworkProxy::DefaultProxy) + proxy = QNetworkProxy::applicationProxy(); - if (proxy.type() == QNetworkProxy::HttpCachingProxy) { - if (proxy.hostName().isEmpty()) - proxy.setType(QNetworkProxy::NoProxy); - else - cachingProxyInUse = true; - } else if (proxy.type() == QNetworkProxy::HttpProxy) { - // Compatibility behaviour: HttpProxy can be used to mean both - // transparent and caching proxy - if (proxy.hostName().isEmpty()) { - proxy.setType(QNetworkProxy::NoProxy); - } else if (sslInUse) { - // Disallow use of caching proxy with HTTPS; instead fall back to - // transparent HTTP CONNECT proxying. - transparentProxyInUse = true; - } else { - proxy.setType(QNetworkProxy::HttpCachingProxy); - cachingProxyInUse = true; - } - } + if(proxy.type() == QNetworkProxy::HttpCachingProxy) + { + if(proxy.hostName().isEmpty()) + proxy.setType(QNetworkProxy::NoProxy); + else + cachingProxyInUse = true; + } + else if(proxy.type() == QNetworkProxy::HttpProxy) + { + // Compatibility behaviour: HttpProxy can be used to mean both + // transparent and caching proxy + if(proxy.hostName().isEmpty()) + { + proxy.setType(QNetworkProxy::NoProxy); + } + else if(sslInUse) + { + // Disallow use of caching proxy with HTTPS; instead fall back to + // transparent HTTP CONNECT proxying. + transparentProxyInUse = true; + } + else + { + proxy.setType(QNetworkProxy::HttpCachingProxy); + cachingProxyInUse = true; + } + } - // Proxy support. Insert the Proxy-Authorization item into the - // header before it's sent off to the proxy. - if (cachingProxyInUse) { - QUrl proxyUrl; - proxyUrl.setScheme(QLatin1String("http")); - proxyUrl.setHost(hostName); - if (port && port != 80) - proxyUrl.setPort(port); - QString request = QString::fromLatin1(proxyUrl.resolved(QUrl::fromEncoded(header.path().toLatin1())).toEncoded()); + // Proxy support. Insert the Proxy-Authorization item into the + // header before it's sent off to the proxy. + if(cachingProxyInUse) + { + QUrl proxyUrl; + proxyUrl.setScheme(QLatin1String("http")); + proxyUrl.setHost(hostName); + if(port && port != 80) + proxyUrl.setPort(port); + QString request = QString::fromLatin1(proxyUrl.resolved(QUrl::fromEncoded(header.path().toLatin1())).toEncoded()); - header.setRequest(header.method(), request, header.majorVersion(), header.minorVersion()); - header.setValue(QLatin1String("Proxy-Connection"), QLatin1String("keep-alive")); + header.setRequest(header.method(), request, header.majorVersion(), header.minorVersion()); + header.setValue(QLatin1String("Proxy-Connection"), QLatin1String("keep-alive")); - QHttpAuthenticatorPrivate *auth = QHttpAuthenticatorPrivate::getPrivate(proxyAuthenticator); - if (auth && auth->method != QHttpAuthenticatorPrivate::None) { - QByteArray response = auth->calculateResponse(header.method().toLatin1(), header.path().toLatin1()); - header.setValue(QLatin1String("Proxy-Authorization"), QString::fromLatin1(response)); - } + QHttpAuthenticatorPrivate * auth = QHttpAuthenticatorPrivate::getPrivate(proxyAuthenticator); + if(auth && auth->method != QHttpAuthenticatorPrivate::None) + { + QByteArray response = auth->calculateResponse(header.method().toLatin1(), header.path().toLatin1()); + header.setValue(QLatin1String("Proxy-Authorization"), QString::fromLatin1(response)); + } - connectionHost = proxy.hostName(); - connectionPort = proxy.port(); - } + connectionHost = proxy.hostName(); + connectionPort = proxy.port(); + } - if (transparentProxyInUse || sslInUse) { - socket->setProxy(proxy); - } + if(transparentProxyInUse || sslInUse) + { + socket->setProxy(proxy); + } #endif - // Username support. Insert the user and password into the query - // string. - QHttpAuthenticatorPrivate *auth = QHttpAuthenticatorPrivate::getPrivate(authenticator); - if (auth && auth->method != QHttpAuthenticatorPrivate::None) { - QByteArray response = auth->calculateResponse(header.method().toLatin1(), header.path().toLatin1()); - header.setValue(QLatin1String("Authorization"), QString::fromLatin1(response)); - } + // Username support. Insert the user and password into the query + // string. + QHttpAuthenticatorPrivate * auth = QHttpAuthenticatorPrivate::getPrivate(authenticator); + if(auth && auth->method != QHttpAuthenticatorPrivate::None) + { + QByteArray response = auth->calculateResponse(header.method().toLatin1(), header.path().toLatin1()); + header.setValue(QLatin1String("Authorization"), QString::fromLatin1(response)); + } - // Do we need to setup a new connection or can we reuse an - // existing one? - if (socket->peerName() != connectionHost || socket->peerPort() != connectionPort - || socket->state() != QTcpSocket::ConnectedState + // Do we need to setup a new connection or can we reuse an + // existing one? + if(socket->peerName() != connectionHost || socket->peerPort() != connectionPort + || socket->state() != QTcpSocket::ConnectedState #ifndef QT_NO_OPENSSL - || (sslSocket && sslSocket->isEncrypted() != (mode == QHttp::ConnectionModeHttps)) + || (sslSocket && sslSocket->isEncrypted() != (mode == QHttp::ConnectionModeHttps)) #endif - ) { - socket->blockSignals(true); - socket->abort(); - socket->blockSignals(false); + ) + { + socket->blockSignals(true); + socket->abort(); + socket->blockSignals(false); - setState(QHttp::Connecting); + setState(QHttp::Connecting); #ifndef QT_NO_OPENSSL - if (sslSocket && mode == QHttp::ConnectionModeHttps) { - sslSocket->connectToHostEncrypted(hostName, port); - } else + if(sslSocket && mode == QHttp::ConnectionModeHttps) + { + sslSocket->connectToHostEncrypted(hostName, port); + } + else #endif - { - socket->connectToHost(connectionHost, connectionPort); - } - } else { - _q_slotConnected(); - } - + { + socket->connectToHost(connectionHost, connectionPort); + } + } + else + { + _q_slotConnected(); + } } void QHttpPrivate::finishedWithSuccess() { - Q_Q(QHttp); - if (pending.isEmpty()) - return; - QHttpRequest *r = pending.first(); + Q_Q(QHttp); + if(pending.isEmpty()) + return; + QHttpRequest * r = pending.first(); - // did we recurse? - if (r->finished) - return; - r->finished = true; - hasFinishedWithError = false; + // did we recurse? + if(r->finished) + return; + r->finished = true; + hasFinishedWithError = false; - emit q->requestFinished(r->id, false); - if (hasFinishedWithError) { - // we recursed and changed into an error. The finishedWithError function - // below has emitted the done(bool) signal and cleared the queue by now. - return; - } + emit q->requestFinished(r->id, false); + if(hasFinishedWithError) + { + // we recursed and changed into an error. The finishedWithError function + // below has emitted the done(bool) signal and cleared the queue by now. + return; + } - pending.removeFirst(); - delete r; + pending.removeFirst(); + delete r; - if (pending.isEmpty()) { - emit q->done(false); - } else { - _q_startNextRequest(); - } + if(pending.isEmpty()) + { + emit q->done(false); + } + else + { + _q_startNextRequest(); + } } -void QHttpPrivate::finishedWithError(const QString &detail, int errorCode) +void QHttpPrivate::finishedWithError(const QString & detail, int errorCode) { - Q_Q(QHttp); - if (pending.isEmpty()) - return; - QHttpRequest *r = pending.first(); - hasFinishedWithError = true; + Q_Q(QHttp); + if(pending.isEmpty()) + return; + QHttpRequest * r = pending.first(); + hasFinishedWithError = true; - error = QHttp::Error(errorCode); - errorString = detail; + error = QHttp::Error(errorCode); + errorString = detail; - // did we recurse? - if (!r->finished) { - r->finished = true; - emit q->requestFinished(r->id, true); - } + // did we recurse? + if(!r->finished) + { + r->finished = true; + emit q->requestFinished(r->id, true); + } - while (!pending.isEmpty()) - delete pending.takeFirst(); - emit q->done(hasFinishedWithError); + while(!pending.isEmpty()) + delete pending.takeFirst(); + emit q->done(hasFinishedWithError); } void QHttpPrivate::_q_slotClosed() { - Q_Q(QHttp); + Q_Q(QHttp); - if (state == QHttp::Reading) { - if (response.hasKey(QLatin1String("content-length"))) { - // We got Content-Length, so did we get all bytes? - if (bytesDone + q->bytesAvailable() != response.contentLength()) { - finishedWithError(QLatin1String(QT_TRANSLATE_NOOP("QHttp", "Wrong content length")), QHttp::WrongContentLength); - } - } - } else if (state == QHttp::Connecting || state == QHttp::Sending) { - finishedWithError(QLatin1String(QT_TRANSLATE_NOOP("QHttp", "Server closed connection unexpectedly")), QHttp::UnexpectedClose); - } + if(state == QHttp::Reading) + { + if(response.hasKey(QLatin1String("content-length"))) + { + // We got Content-Length, so did we get all bytes? + if(bytesDone + q->bytesAvailable() != response.contentLength()) + { + finishedWithError(QLatin1String(QT_TRANSLATE_NOOP("QHttp", "Wrong content length")), QHttp::WrongContentLength); + } + } + } + else if(state == QHttp::Connecting || state == QHttp::Sending) + { + finishedWithError(QLatin1String(QT_TRANSLATE_NOOP("QHttp", "Server closed connection unexpectedly")), QHttp::UnexpectedClose); + } - postDevice = 0; - if (state != QHttp::Closing) - setState(QHttp::Closing); - QMetaObject::invokeMethod(q, "_q_slotDoFinished", Qt::QueuedConnection); + postDevice = 0; + if(state != QHttp::Closing) + setState(QHttp::Closing); + QMetaObject::invokeMethod(q, "_q_slotDoFinished", Qt::QueuedConnection); } void QHttpPrivate::_q_continuePost() { - if (pendingPost) { - pendingPost = false; - setState(QHttp::Sending); - _q_slotBytesWritten(0); - } + if(pendingPost) + { + pendingPost = false; + setState(QHttp::Sending); + _q_slotBytesWritten(0); + } } void QHttpPrivate::_q_slotConnected() { - if (state != QHttp::Sending) { - bytesDone = 0; - setState(QHttp::Sending); - } + if(state != QHttp::Sending) + { + bytesDone = 0; + setState(QHttp::Sending); + } - QString str = header.toString(); - bytesTotal = str.length(); - socket->write(str.toLatin1(), bytesTotal); + QString str = header.toString(); + bytesTotal = str.length(); + socket->write(str.toLatin1(), bytesTotal); #if defined(QHTTP_DEBUG) - qDebug("QHttp: write request header %p:\n---{\n%s}---", &header, str.toLatin1().constData()); + qDebug("QHttp: write request header %p:\n---{\n%s}---", &header, str.toLatin1().constData()); #endif - if (postDevice) { - postDevice->seek(0); // reposition the device - bytesTotal += postDevice->size(); - //check for 100-continue - if (header.value(QLatin1String("expect")).contains(QLatin1String("100-continue"), Qt::CaseInsensitive)) { - //create a time out for 2 secs. - pendingPost = true; - post100ContinueTimer.start(2000); - } - } else { - bytesTotal += buffer.size(); - socket->write(buffer, buffer.size()); - } + if(postDevice) + { + postDevice->seek(0); // reposition the device + bytesTotal += postDevice->size(); + //check for 100-continue + if(header.value(QLatin1String("expect")).contains(QLatin1String("100-continue"), Qt::CaseInsensitive)) + { + //create a time out for 2 secs. + pendingPost = true; + post100ContinueTimer.start(2000); + } + } + else + { + bytesTotal += buffer.size(); + socket->write(buffer, buffer.size()); + } } void QHttpPrivate::_q_slotError(QAbstractSocket::SocketError err) { - Q_Q(QHttp); - postDevice = 0; + Q_Q(QHttp); + postDevice = 0; - if (state == QHttp::Connecting || state == QHttp::Reading || state == QHttp::Sending) { - switch (err) { - case QTcpSocket::ConnectionRefusedError: - finishedWithError(QLatin1String(QT_TRANSLATE_NOOP("QHttp", "Connection refused (or timed out)")), QHttp::ConnectionRefused); - break; - case QTcpSocket::HostNotFoundError: - finishedWithError(QString::fromLatin1(QT_TRANSLATE_NOOP("QHttp", "Host %1 not found")) - .arg(socket->peerName()), QHttp::HostNotFound); - break; - case QTcpSocket::RemoteHostClosedError: - if (state == QHttp::Sending && reconnectAttempts--) { - setState(QHttp::Closing); - setState(QHttp::Unconnected); - socket->blockSignals(true); - socket->abort(); - socket->blockSignals(false); - QMetaObject::invokeMethod(q, "_q_slotSendRequest", Qt::QueuedConnection); - return; - } - break; + if(state == QHttp::Connecting || state == QHttp::Reading || state == QHttp::Sending) + { + switch(err) + { + case QTcpSocket::ConnectionRefusedError: + finishedWithError(QLatin1String(QT_TRANSLATE_NOOP("QHttp", "Connection refused (or timed out)")), QHttp::ConnectionRefused); + break; + case QTcpSocket::HostNotFoundError: + finishedWithError(QString::fromLatin1(QT_TRANSLATE_NOOP("QHttp", "Host %1 not found")) + .arg(socket->peerName()), + QHttp::HostNotFound); + break; + case QTcpSocket::RemoteHostClosedError: + if(state == QHttp::Sending && reconnectAttempts--) + { + setState(QHttp::Closing); + setState(QHttp::Unconnected); + socket->blockSignals(true); + socket->abort(); + socket->blockSignals(false); + QMetaObject::invokeMethod(q, "_q_slotSendRequest", Qt::QueuedConnection); + return; + } + break; #ifndef QT_NO_NETWORKPROXY - case QTcpSocket::ProxyAuthenticationRequiredError: - finishedWithError(socket->errorString(), QHttp::ProxyAuthenticationRequiredError); - break; + case QTcpSocket::ProxyAuthenticationRequiredError: + finishedWithError(socket->errorString(), QHttp::ProxyAuthenticationRequiredError); + break; #endif - default: - finishedWithError(QLatin1String(QT_TRANSLATE_NOOP("QHttp", "HTTP request failed")), QHttp::UnknownError); - break; - } - } + default: + finishedWithError(QLatin1String(QT_TRANSLATE_NOOP("QHttp", "HTTP request failed")), QHttp::UnknownError); + break; + } + } - closeConn(); + closeConn(); } #ifndef QT_NO_OPENSSL void QHttpPrivate::_q_slotEncryptedBytesWritten(qint64 written) { - Q_UNUSED(written); - postMoreData(); + Q_UNUSED(written); + postMoreData(); } #endif void QHttpPrivate::_q_slotBytesWritten(qint64 written) { - Q_Q(QHttp); - bytesDone += written; - emit q->dataSendProgress(bytesDone, bytesTotal); - postMoreData(); + Q_Q(QHttp); + bytesDone += written; + emit q->dataSendProgress(bytesDone, bytesTotal); + postMoreData(); } // Send the POST data void QHttpPrivate::postMoreData() { - if (pendingPost) - return; + if(pendingPost) + return; - if (!postDevice) - return; + if(!postDevice) + return; - // the following is backported code from Qt 4.6 QNetworkAccessManager. - // We also have to check the encryptedBytesToWrite() if it is an SSL socket. +// the following is backported code from Qt 4.6 QNetworkAccessManager. +// We also have to check the encryptedBytesToWrite() if it is an SSL socket. #ifndef QT_NO_OPENSSL - QSslSocket *sslSocket = qobject_cast<QSslSocket*>(socket); - // if it is really an ssl socket, check more than just bytesToWrite() - if ((socket->bytesToWrite() + (sslSocket ? sslSocket->encryptedBytesToWrite() : 0)) == 0) { + QSslSocket * sslSocket = qobject_cast<QSslSocket *>(socket); + // if it is really an ssl socket, check more than just bytesToWrite() + if((socket->bytesToWrite() + (sslSocket ? sslSocket->encryptedBytesToWrite() : 0)) == 0) + { #else - if (socket->bytesToWrite() == 0) { + if(socket->bytesToWrite() == 0) + { #endif - int max = qMin<qint64>(4096, postDevice->size() - postDevice->pos()); - QByteArray arr; - arr.resize(max); + int max = qMin<qint64>(4096, postDevice->size() - postDevice->pos()); + QByteArray arr; + arr.resize(max); - int n = postDevice->read(arr.data(), max); - if (n < 0) { - qWarning("Could not read enough bytes from the device"); - closeConn(); - return; - } - if (postDevice->atEnd()) { - postDevice = 0; - } + int n = postDevice->read(arr.data(), max); + if(n < 0) + { + qWarning("Could not read enough bytes from the device"); + closeConn(); + return; + } + if(postDevice->atEnd()) + { + postDevice = 0; + } - socket->write(arr, n); - } + socket->write(arr, n); + } } void QHttpPrivate::_q_slotReadyRead() { - Q_Q(QHttp); - QHttp::State oldState = state; - if (state != QHttp::Reading) { - setState(QHttp::Reading); - readHeader = true; - headerStr = QLatin1String(""); - bytesDone = 0; - chunkedSize = -1; - repost = false; - } + Q_Q(QHttp); + QHttp::State oldState = state; + if(state != QHttp::Reading) + { + setState(QHttp::Reading); + readHeader = true; + headerStr = QLatin1String(""); + bytesDone = 0; + chunkedSize = -1; + repost = false; + } - while (readHeader) { - bool end = false; - QString tmp; - while (!end && socket->canReadLine()) { - tmp = QString::fromLatin1(socket->readLine()); - if (tmp == QLatin1String("\r\n") || tmp == QLatin1String("\n") || tmp.isEmpty()) - end = true; - else - headerStr += tmp; - } + while(readHeader) + { + bool end = false; + QString tmp; + while(!end && socket->canReadLine()) + { + tmp = QString::fromLatin1(socket->readLine()); + if(tmp == QLatin1String("\r\n") || tmp == QLatin1String("\n") || tmp.isEmpty()) + end = true; + else + headerStr += tmp; + } - if (!end) - return; + if(!end) + return; - response = QHttpResponseHeader(headerStr); - headerStr = QLatin1String(""); + response = QHttpResponseHeader(headerStr); + headerStr = QLatin1String(""); #if defined(QHTTP_DEBUG) - qDebug("QHttp: read response header:\n---{\n%s}---", response.toString().toLatin1().constData()); + qDebug("QHttp: read response header:\n---{\n%s}---", response.toString().toLatin1().constData()); #endif - // Check header - if (!response.isValid()) { - finishedWithError(QLatin1String(QT_TRANSLATE_NOOP("QHttp", "Invalid HTTP response header")), - QHttp::InvalidResponseHeader); - closeConn(); - return; - } + // Check header + if(!response.isValid()) + { + finishedWithError(QLatin1String(QT_TRANSLATE_NOOP("QHttp", "Invalid HTTP response header")), + QHttp::InvalidResponseHeader); + closeConn(); + return; + } - int statusCode = response.statusCode(); - if (statusCode == 401 || statusCode == 407) { // (Proxy) Authentication required - QHttpAuthenticator *auth = + int statusCode = response.statusCode(); + if(statusCode == 401 || statusCode == 407) + { // (Proxy) Authentication required + QHttpAuthenticator * auth = #ifndef QT_NO_NETWORKPROXY - statusCode == 407 - ? &proxyAuthenticator : + statusCode == 407 + ? &proxyAuthenticator + : #endif - &authenticator; - if (auth->isNull()) - auth->detach(); - QHttpAuthenticatorPrivate *priv = QHttpAuthenticatorPrivate::getPrivate(*auth); - priv->parseHttpResponse(response, (statusCode == 407)); - if (priv->phase == QHttpAuthenticatorPrivate::Done) { - socket->blockSignals(true); + &authenticator; + if(auth->isNull()) + auth->detach(); + QHttpAuthenticatorPrivate * priv = QHttpAuthenticatorPrivate::getPrivate(*auth); + priv->parseHttpResponse(response, (statusCode == 407)); + if(priv->phase == QHttpAuthenticatorPrivate::Done) + { + socket->blockSignals(true); #ifndef QT_NO_NETWORKPROXY - if (statusCode == 407) { - //need to emit a QAuthenticator to maintain source compatibility - QAuthenticator qauthClean = auth->toQAuthenticator(); - QAuthenticator qauthToEmit = qauthClean; - emit q->proxyAuthenticationRequired(proxy, &qauthToEmit); - if (qauthClean != qauthToEmit) { - //user changed something, copy back (which will reset our state) - *auth = qauthToEmit; - } - } else + if(statusCode == 407) + { + //need to emit a QAuthenticator to maintain source compatibility + QAuthenticator qauthClean = auth->toQAuthenticator(); + QAuthenticator qauthToEmit = qauthClean; + emit q->proxyAuthenticationRequired(proxy, &qauthToEmit); + if(qauthClean != qauthToEmit) + { + //user changed something, copy back (which will reset our state) + *auth = qauthToEmit; + } + } + else #endif - { - //need to emit a QAuthenticator to maintain source compatibility - QAuthenticator qauthClean = auth->toQAuthenticator(); - QAuthenticator qauthToEmit = qauthClean; - emit q->authenticationRequired(hostName, port, &qauthToEmit); - if (qauthClean != qauthToEmit) { - //user changed something, copy back (which will reset our state) - *auth = qauthToEmit; - } - } - socket->blockSignals(false); - } else if (priv->phase == QHttpAuthenticatorPrivate::Invalid) { - finishedWithError(QLatin1String(QT_TRANSLATE_NOOP("QHttp", "Unknown authentication method")), - QHttp::AuthenticationRequiredError); - closeConn(); - return; - } + { + //need to emit a QAuthenticator to maintain source compatibility + QAuthenticator qauthClean = auth->toQAuthenticator(); + QAuthenticator qauthToEmit = qauthClean; + emit q->authenticationRequired(hostName, port, &qauthToEmit); + if(qauthClean != qauthToEmit) + { + //user changed something, copy back (which will reset our state) + *auth = qauthToEmit; + } + } + socket->blockSignals(false); + } + else if(priv->phase == QHttpAuthenticatorPrivate::Invalid) + { + finishedWithError(QLatin1String(QT_TRANSLATE_NOOP("QHttp", "Unknown authentication method")), + QHttp::AuthenticationRequiredError); + closeConn(); + return; + } - // priv->phase will get reset to QHttpAuthenticatorPrivate::Start if the authenticator got modified in the signal above. - if (priv->phase == QHttpAuthenticatorPrivate::Done) { + // priv->phase will get reset to QHttpAuthenticatorPrivate::Start if the authenticator got modified in the signal above. + if(priv->phase == QHttpAuthenticatorPrivate::Done) + { #ifndef QT_NO_NETWORKPROXY - if (statusCode == 407) - finishedWithError(QLatin1String(QT_TRANSLATE_NOOP("QHttp", "Proxy authentication required")), - QHttp::ProxyAuthenticationRequiredError); - else + if(statusCode == 407) + finishedWithError(QLatin1String(QT_TRANSLATE_NOOP("QHttp", "Proxy authentication required")), + QHttp::ProxyAuthenticationRequiredError); + else #endif - finishedWithError(QLatin1String(QT_TRANSLATE_NOOP("QHttp", "Authentication required")), - QHttp::AuthenticationRequiredError); - closeConn(); - return; - } else { - // close the connection if it isn't already and reconnect using the chosen authentication method - bool willClose = (response.value(QLatin1String("proxy-connection")).toLower() == QLatin1String("close")) - || (response.value(QLatin1String("connection")).toLower() == QLatin1String("close")); - if (willClose) { - if (socket) { - setState(QHttp::Closing); - socket->blockSignals(true); - socket->close(); - socket->blockSignals(false); - socket->readAll(); - } - _q_slotSendRequest(); - return; - } else { - repost = true; - } - } - } else { - buffer.clear(); - } + finishedWithError(QLatin1String(QT_TRANSLATE_NOOP("QHttp", "Authentication required")), + QHttp::AuthenticationRequiredError); + closeConn(); + return; + } + else + { + // close the connection if it isn't already and reconnect using the chosen authentication method + bool willClose = (response.value(QLatin1String("proxy-connection")).toLower() == QLatin1String("close")) + || (response.value(QLatin1String("connection")).toLower() == QLatin1String("close")); + if(willClose) + { + if(socket) + { + setState(QHttp::Closing); + socket->blockSignals(true); + socket->close(); + socket->blockSignals(false); + socket->readAll(); + } + _q_slotSendRequest(); + return; + } + else + { + repost = true; + } + } + } + else + { + buffer.clear(); + } - if (response.statusCode() == 100 && pendingPost) { - // if we have pending POST, start sending data otherwise ignore - post100ContinueTimer.stop(); - QMetaObject::invokeMethod(q, "_q_continuePost", Qt::QueuedConnection); - return; - } + if(response.statusCode() == 100 && pendingPost) + { + // if we have pending POST, start sending data otherwise ignore + post100ContinueTimer.stop(); + QMetaObject::invokeMethod(q, "_q_continuePost", Qt::QueuedConnection); + return; + } - // The 100-continue header is ignored (in case of no 'expect:100-continue' header), - // because when using the POST method, we send both the request header and data in - // one chunk. - if (response.statusCode() != 100) { - post100ContinueTimer.stop(); - pendingPost = false; - readHeader = false; - if (response.hasKey(QLatin1String("transfer-encoding")) && - response.value(QLatin1String("transfer-encoding")).toLower().contains(QLatin1String("chunked"))) - chunkedSize = 0; + // The 100-continue header is ignored (in case of no 'expect:100-continue' header), + // because when using the POST method, we send both the request header and data in + // one chunk. + if(response.statusCode() != 100) + { + post100ContinueTimer.stop(); + pendingPost = false; + readHeader = false; + if(response.hasKey(QLatin1String("transfer-encoding")) && response.value(QLatin1String("transfer-encoding")).toLower().contains(QLatin1String("chunked"))) + chunkedSize = 0; - if (!repost) - emit q->responseHeaderReceived(response); - if (state == QHttp::Unconnected || state == QHttp::Closing) - return; - } else { - // Restore the state, the next incoming data will be treated as if - // we never say the 100 response. - state = oldState; - } - } + if(!repost) + emit q->responseHeaderReceived(response); + if(state == QHttp::Unconnected || state == QHttp::Closing) + return; + } + else + { + // Restore the state, the next incoming data will be treated as if + // we never say the 100 response. + state = oldState; + } + } - bool everythingRead = false; + bool everythingRead = false; - if (q->currentRequest().method() == QLatin1String("HEAD") || - response.statusCode() == 304 || response.statusCode() == 204 || - response.statusCode() == 205) { - // HEAD requests have only headers as replies - // These status codes never have a body: - // 304 Not Modified - // 204 No Content - // 205 Reset Content - everythingRead = true; - } else { - qint64 n = socket->bytesAvailable(); - QByteArray *arr = 0; - if (chunkedSize != -1) { - // transfer-encoding is chunked - for (;;) { - // get chunk size - if (chunkedSize == 0) { - if (!socket->canReadLine()) - break; - QString sizeString = QString::fromLatin1(socket->readLine()); - int tPos = sizeString.indexOf(QLatin1Char(';')); - if (tPos != -1) - sizeString.truncate(tPos); - bool ok; - chunkedSize = sizeString.toInt(&ok, 16); - if (!ok) { - finishedWithError(QLatin1String(QT_TRANSLATE_NOOP("QHttp", "Invalid HTTP chunked body")), - QHttp::WrongContentLength); - closeConn(); - delete arr; - return; - } - if (chunkedSize == 0) // last-chunk - chunkedSize = -2; - } + if(q->currentRequest().method() == QLatin1String("HEAD") || response.statusCode() == 304 || response.statusCode() == 204 || response.statusCode() == 205) + { + // HEAD requests have only headers as replies + // These status codes never have a body: + // 304 Not Modified + // 204 No Content + // 205 Reset Content + everythingRead = true; + } + else + { + qint64 n = socket->bytesAvailable(); + QByteArray * arr = 0; + if(chunkedSize != -1) + { + // transfer-encoding is chunked + for(;;) + { + // get chunk size + if(chunkedSize == 0) + { + if(!socket->canReadLine()) + break; + QString sizeString = QString::fromLatin1(socket->readLine()); + int tPos = sizeString.indexOf(QLatin1Char(';')); + if(tPos != -1) + sizeString.truncate(tPos); + bool ok; + chunkedSize = sizeString.toInt(&ok, 16); + if(!ok) + { + finishedWithError(QLatin1String(QT_TRANSLATE_NOOP("QHttp", "Invalid HTTP chunked body")), + QHttp::WrongContentLength); + closeConn(); + delete arr; + return; + } + if(chunkedSize == 0) // last-chunk + chunkedSize = -2; + } - // read trailer - while (chunkedSize == -2 && socket->canReadLine()) { - QString read = QString::fromLatin1(socket->readLine()); - if (read == QLatin1String("\r\n") || read == QLatin1String("\n")) - chunkedSize = -1; - } - if (chunkedSize == -1) { - everythingRead = true; - break; - } + // read trailer + while(chunkedSize == -2 && socket->canReadLine()) + { + QString read = QString::fromLatin1(socket->readLine()); + if(read == QLatin1String("\r\n") || read == QLatin1String("\n")) + chunkedSize = -1; + } + if(chunkedSize == -1) + { + everythingRead = true; + break; + } - // make sure that you can read the terminating CRLF, - // otherwise wait until next time... - n = socket->bytesAvailable(); - if (n == 0) - break; - if (n == chunkedSize || n == chunkedSize+1) { - n = chunkedSize - 1; - if (n == 0) - break; - } + // make sure that you can read the terminating CRLF, + // otherwise wait until next time... + n = socket->bytesAvailable(); + if(n == 0) + break; + if(n == chunkedSize || n == chunkedSize + 1) + { + n = chunkedSize - 1; + if(n == 0) + break; + } - // read data - qint64 toRead = chunkedSize < 0 ? n : qMin(n, chunkedSize); - if (!arr) - arr = new QByteArray; - uint oldArrSize = arr->size(); - arr->resize(oldArrSize + toRead); - qint64 read = socket->read(arr->data()+oldArrSize, toRead); - arr->resize(oldArrSize + read); + // read data + qint64 toRead = chunkedSize < 0 ? n : qMin(n, chunkedSize); + if(!arr) + arr = new QByteArray; + uint oldArrSize = arr->size(); + arr->resize(oldArrSize + toRead); + qint64 read = socket->read(arr->data() + oldArrSize, toRead); + arr->resize(oldArrSize + read); - chunkedSize -= read; + chunkedSize -= read; - if (chunkedSize == 0 && n - read >= 2) { - // read terminating CRLF - char tmp[2]; - socket->read(tmp, 2); - if (tmp[0] != '\r' || tmp[1] != '\n') { - finishedWithError(QLatin1String(QT_TRANSLATE_NOOP("QHttp", "Invalid HTTP chunked body")), - QHttp::WrongContentLength); - closeConn(); - delete arr; - return; - } - } - } - } else if (response.hasContentLength()) { - if (repost && (n < response.contentLength())) { - // wait for the content to be available fully - // if repost is required, the content is ignored - return; - } - n = qMin(qint64(response.contentLength() - bytesDone), n); - if (n > 0) { - arr = new QByteArray; - arr->resize(n); - qint64 read = socket->read(arr->data(), n); - arr->resize(read); - } - if (bytesDone + q->bytesAvailable() + n == response.contentLength()) - everythingRead = true; - } else if (n > 0) { - // workaround for VC++ bug - QByteArray temp = socket->readAll(); - arr = new QByteArray(temp); - } + if(chunkedSize == 0 && n - read >= 2) + { + // read terminating CRLF + char tmp[2]; + socket->read(tmp, 2); + if(tmp[0] != '\r' || tmp[1] != '\n') + { + finishedWithError(QLatin1String(QT_TRANSLATE_NOOP("QHttp", "Invalid HTTP chunked body")), + QHttp::WrongContentLength); + closeConn(); + delete arr; + return; + } + } + } + } + else if(response.hasContentLength()) + { + if(repost && (n < response.contentLength())) + { + // wait for the content to be available fully + // if repost is required, the content is ignored + return; + } + n = qMin(qint64(response.contentLength() - bytesDone), n); + if(n > 0) + { + arr = new QByteArray; + arr->resize(n); + qint64 read = socket->read(arr->data(), n); + arr->resize(read); + } + if(bytesDone + q->bytesAvailable() + n == response.contentLength()) + everythingRead = true; + } + else if(n > 0) + { + // workaround for VC++ bug + QByteArray temp = socket->readAll(); + arr = new QByteArray(temp); + } - if (arr && !repost) { - n = arr->size(); - if (toDevice) { - qint64 bytesWritten; - bytesWritten = toDevice->write(*arr, n); - delete arr; - arr = 0; - // if writing to the device does not succeed, quit with error - if (bytesWritten == -1 || bytesWritten < n) { - finishedWithError(QLatin1String(QT_TRANSLATE_NOOP("QHttp", "Error writing response to device")), QHttp::UnknownError); - } else { - bytesDone += bytesWritten; + if(arr && !repost) + { + n = arr->size(); + if(toDevice) + { + qint64 bytesWritten; + bytesWritten = toDevice->write(*arr, n); + delete arr; + arr = 0; + // if writing to the device does not succeed, quit with error + if(bytesWritten == -1 || bytesWritten < n) + { + finishedWithError(QLatin1String(QT_TRANSLATE_NOOP("QHttp", "Error writing response to device")), QHttp::UnknownError); + } + else + { + bytesDone += bytesWritten; #if defined(QHTTP_DEBUG) - qDebug("QHttp::_q_slotReadyRead(): read %lld bytes (%lld bytes done)", n, bytesDone); + qDebug("QHttp::_q_slotReadyRead(): read %lld bytes (%lld bytes done)", n, bytesDone); #endif - } - if (response.hasContentLength()) - emit q->dataReadProgress(bytesDone, response.contentLength()); - else - emit q->dataReadProgress(bytesDone, 0); - } else { - char *ptr = rba.reserve(arr->size()); - memcpy(ptr, arr->data(), arr->size()); - delete arr; - arr = 0; + } + if(response.hasContentLength()) + emit q->dataReadProgress(bytesDone, response.contentLength()); + else + emit q->dataReadProgress(bytesDone, 0); + } + else + { + char * ptr = rba.reserve(arr->size()); + memcpy(ptr, arr->data(), arr->size()); + delete arr; + arr = 0; #if defined(QHTTP_DEBUG) - qDebug("QHttp::_q_slotReadyRead(): read %lld bytes (%lld bytes done)", n, bytesDone + q->bytesAvailable()); + qDebug("QHttp::_q_slotReadyRead(): read %lld bytes (%lld bytes done)", n, bytesDone + q->bytesAvailable()); #endif - if (response.hasContentLength()) - emit q->dataReadProgress(bytesDone + q->bytesAvailable(), response.contentLength()); - else - emit q->dataReadProgress(bytesDone + q->bytesAvailable(), 0); - emit q->readyRead(response); - } - } + if(response.hasContentLength()) + emit q->dataReadProgress(bytesDone + q->bytesAvailable(), response.contentLength()); + else + emit q->dataReadProgress(bytesDone + q->bytesAvailable(), 0); + emit q->readyRead(response); + } + } - delete arr; - } + delete arr; + } - if (everythingRead) { - if (repost) { - _q_slotSendRequest(); - return; - } - // Handle "Connection: close" - if (response.value(QLatin1String("connection")).toLower() == QLatin1String("close")) { - closeConn(); - } else { - setState(QHttp::Connected); - // Start a timer, so that we emit the keep alive signal - // "after" this method returned. - QMetaObject::invokeMethod(q, "_q_slotDoFinished", Qt::QueuedConnection); - } - } + if(everythingRead) + { + if(repost) + { + _q_slotSendRequest(); + return; + } + // Handle "Connection: close" + if(response.value(QLatin1String("connection")).toLower() == QLatin1String("close")) + { + closeConn(); + } + else + { + setState(QHttp::Connected); + // Start a timer, so that we emit the keep alive signal + // "after" this method returned. + QMetaObject::invokeMethod(q, "_q_slotDoFinished", Qt::QueuedConnection); + } + } } void QHttpPrivate::_q_slotDoFinished() { - if (state == QHttp::Connected) { - finishedWithSuccess(); - } else if (state != QHttp::Unconnected) { - setState(QHttp::Unconnected); - finishedWithSuccess(); - } + if(state == QHttp::Connected) + { + finishedWithSuccess(); + } + else if(state != QHttp::Unconnected) + { + setState(QHttp::Unconnected); + finishedWithSuccess(); + } } - /*! Returns the current state of the object. When the state changes, the stateChanged() signal is emitted. @@ -3059,7 +3220,7 @@ void QHttpPrivate::_q_slotDoFinished() */ QHttp::State QHttp::state() const { - return d->state; + return d->state; } /*! @@ -3071,7 +3232,7 @@ QHttp::State QHttp::state() const */ QHttp::Error QHttp::error() const { - return d->error; + return d->error; } /*! @@ -3082,79 +3243,84 @@ QHttp::Error QHttp::error() const */ QString QHttp::errorString() const { - return d->errorString; + return d->errorString; } void QHttpPrivate::setState(int s) { - Q_Q(QHttp); + Q_Q(QHttp); #if defined(QHTTP_DEBUG) - qDebug("QHttp state changed %d -> %d", state, s); + qDebug("QHttp state changed %d -> %d", state, s); #endif - state = QHttp::State(s); - emit q->stateChanged(s); + state = QHttp::State(s); + emit q->stateChanged(s); } void QHttpPrivate::closeConn() { - Q_Q(QHttp); - // If no connection is open -> ignore - if (state == QHttp::Closing || state == QHttp::Unconnected) - return; + Q_Q(QHttp); + // If no connection is open -> ignore + if(state == QHttp::Closing || state == QHttp::Unconnected) + return; - postDevice = 0; - setState(QHttp::Closing); + postDevice = 0; + setState(QHttp::Closing); - // Already closed ? - if (!socket || !socket->isOpen()) { - QMetaObject::invokeMethod(q, "_q_slotDoFinished", Qt::QueuedConnection); - } else { - // Close now. - socket->close(); - } + // Already closed ? + if(!socket || !socket->isOpen()) + { + QMetaObject::invokeMethod(q, "_q_slotDoFinished", Qt::QueuedConnection); + } + else + { + // Close now. + socket->close(); + } } -void QHttpPrivate::setSock(QTcpSocket *sock) +void QHttpPrivate::setSock(QTcpSocket * sock) { - Q_Q(const QHttp); + Q_Q(const QHttp); - // disconnect all existing signals - if (socket) - socket->disconnect(); - if (deleteSocket) - delete socket; + // disconnect all existing signals + if(socket) + socket->disconnect(); + if(deleteSocket) + delete socket; - // use the new QTcpSocket socket, or create one if socket is 0. - deleteSocket = (sock == 0); - socket = sock; - if (!socket) { + // use the new QTcpSocket socket, or create one if socket is 0. + deleteSocket = (sock == 0); + socket = sock; + if(!socket) + { #ifndef QT_NO_OPENSSL - if (QSslSocket::supportsSsl()) - socket = new QSslSocket(); - else + if(QSslSocket::supportsSsl()) + socket = new QSslSocket(); + else #endif - socket = new QTcpSocket(); - } + socket = new QTcpSocket(); + } - // connect all signals - QObject::connect(socket, SIGNAL(connected()), q, SLOT(_q_slotConnected())); - QObject::connect(socket, SIGNAL(disconnected()), q, SLOT(_q_slotClosed())); - QObject::connect(socket, SIGNAL(readyRead()), q, SLOT(_q_slotReadyRead())); - QObject::connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), q, SLOT(_q_slotError(QAbstractSocket::SocketError))); - QObject::connect(socket, SIGNAL(bytesWritten(qint64)), - q, SLOT(_q_slotBytesWritten(qint64))); + // connect all signals + QObject::connect(socket, SIGNAL(connected()), q, SLOT(_q_slotConnected())); + QObject::connect(socket, SIGNAL(disconnected()), q, SLOT(_q_slotClosed())); + QObject::connect(socket, SIGNAL(readyRead()), q, SLOT(_q_slotReadyRead())); + QObject::connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), q, SLOT(_q_slotError(QAbstractSocket::SocketError))); + QObject::connect(socket, SIGNAL(bytesWritten(qint64)), + q, SLOT(_q_slotBytesWritten(qint64))); #ifndef QT_NO_NETWORKPROXY - QObject::connect(socket, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)), - q, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*))); + QObject::connect(socket, SIGNAL(proxyAuthenticationRequired(QNetworkProxy, QAuthenticator *)), + q, SIGNAL(proxyAuthenticationRequired(QNetworkProxy, QAuthenticator *))); #endif #ifndef QT_NO_OPENSSL - if (qobject_cast<QSslSocket *>(socket)) { - QObject::connect(socket, SIGNAL(sslErrors(QList<QSslError>)), - q, SIGNAL(sslErrors(QList<QSslError>))); - QObject::connect(socket, SIGNAL(encryptedBytesWritten(qint64)), - q, SLOT(_q_slotEncryptedBytesWritten(qint64))); - } + if(qobject_cast<QSslSocket *>(socket)) + { + QObject::connect(socket, SIGNAL(sslErrors(QList<QSslError>)), + q, SIGNAL(sslErrors(QList<QSslError>))); + QObject::connect(socket, SIGNAL(encryptedBytesWritten(qint64)), + q, SLOT(_q_slotEncryptedBytesWritten(qint64))); + } #endif } @@ -3170,9 +3336,9 @@ void QHttpPrivate::setSock(QTcpSocket *sock) #ifndef QT_NO_OPENSSL void QHttp::ignoreSslErrors() { - QSslSocket *sslSocket = qobject_cast<QSslSocket *>(d->socket); - if (sslSocket) - sslSocket->ignoreSslErrors(); + QSslSocket * sslSocket = qobject_cast<QSslSocket *>(d->socket); + if(sslSocket) + sslSocket->ignoreSslErrors(); } #endif |
