util/src/network/access/qhttpnetworkreply.cpp
changeset 7 f7bc934e204c
equal deleted inserted replaced
3:41300fa6a67c 7:f7bc934e204c
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtNetwork module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "qhttpnetworkreply_p.h"
       
    43 #include "qhttpnetworkconnection_p.h"
       
    44 
       
    45 #include <qbytearraymatcher.h>
       
    46 
       
    47 #ifndef QT_NO_HTTP
       
    48 
       
    49 #ifndef QT_NO_OPENSSL
       
    50 #    include <QtNetwork/qsslkey.h>
       
    51 #    include <QtNetwork/qsslcipher.h>
       
    52 #    include <QtNetwork/qsslconfiguration.h>
       
    53 #endif
       
    54 
       
    55 QT_BEGIN_NAMESPACE
       
    56 
       
    57 QHttpNetworkReply::QHttpNetworkReply(const QUrl &url, QObject *parent)
       
    58     : QObject(*new QHttpNetworkReplyPrivate(url), parent)
       
    59 {
       
    60 }
       
    61 
       
    62 QHttpNetworkReply::~QHttpNetworkReply()
       
    63 {
       
    64     Q_D(QHttpNetworkReply);
       
    65     if (d->connection) {
       
    66         d->connection->d_func()->removeReply(this);
       
    67     }
       
    68 }
       
    69 
       
    70 QUrl QHttpNetworkReply::url() const
       
    71 {
       
    72     return d_func()->url;
       
    73 }
       
    74 void QHttpNetworkReply::setUrl(const QUrl &url)
       
    75 {
       
    76     Q_D(QHttpNetworkReply);
       
    77     d->url = url;
       
    78 }
       
    79 
       
    80 qint64 QHttpNetworkReply::contentLength() const
       
    81 {
       
    82     return d_func()->contentLength();
       
    83 }
       
    84 
       
    85 void QHttpNetworkReply::setContentLength(qint64 length)
       
    86 {
       
    87     Q_D(QHttpNetworkReply);
       
    88     d->setContentLength(length);
       
    89 }
       
    90 
       
    91 QList<QPair<QByteArray, QByteArray> > QHttpNetworkReply::header() const
       
    92 {
       
    93     return d_func()->fields;
       
    94 }
       
    95 
       
    96 QByteArray QHttpNetworkReply::headerField(const QByteArray &name, const QByteArray &defaultValue) const
       
    97 {
       
    98     return d_func()->headerField(name, defaultValue);
       
    99 }
       
   100 
       
   101 void QHttpNetworkReply::setHeaderField(const QByteArray &name, const QByteArray &data)
       
   102 {
       
   103     Q_D(QHttpNetworkReply);
       
   104     d->setHeaderField(name, data);
       
   105 }
       
   106 
       
   107 void QHttpNetworkReply::parseHeader(const QByteArray &header)
       
   108 {
       
   109     Q_D(QHttpNetworkReply);
       
   110     d->parseHeader(header);
       
   111 }
       
   112 
       
   113 QHttpNetworkRequest QHttpNetworkReply::request() const
       
   114 {
       
   115     return d_func()->request;
       
   116 }
       
   117 
       
   118 void QHttpNetworkReply::setRequest(const QHttpNetworkRequest &request)
       
   119 {
       
   120     Q_D(QHttpNetworkReply);
       
   121     d->request = request;
       
   122 }
       
   123 
       
   124 int QHttpNetworkReply::statusCode() const
       
   125 {
       
   126     return d_func()->statusCode;
       
   127 }
       
   128 
       
   129 void QHttpNetworkReply::setStatusCode(int code)
       
   130 {
       
   131     Q_D(QHttpNetworkReply);
       
   132     d->statusCode = code;
       
   133 }
       
   134 
       
   135 QString QHttpNetworkReply::errorString() const
       
   136 {
       
   137     return d_func()->errorString;
       
   138 }
       
   139 
       
   140 QString QHttpNetworkReply::reasonPhrase() const
       
   141 {
       
   142     return d_func()->reasonPhrase;
       
   143 }
       
   144 
       
   145 void QHttpNetworkReply::setErrorString(const QString &error)
       
   146 {
       
   147     Q_D(QHttpNetworkReply);
       
   148     d->errorString = error;
       
   149 }
       
   150 
       
   151 int QHttpNetworkReply::majorVersion() const
       
   152 {
       
   153     return d_func()->majorVersion;
       
   154 }
       
   155 
       
   156 int QHttpNetworkReply::minorVersion() const
       
   157 {
       
   158     return d_func()->minorVersion;
       
   159 }
       
   160 
       
   161 qint64 QHttpNetworkReply::bytesAvailable() const
       
   162 {
       
   163     Q_D(const QHttpNetworkReply);
       
   164     if (d->connection)
       
   165         return d->connection->d_func()->uncompressedBytesAvailable(*this);
       
   166     else
       
   167         return -1;
       
   168 }
       
   169 
       
   170 qint64 QHttpNetworkReply::bytesAvailableNextBlock() const
       
   171 {
       
   172     Q_D(const QHttpNetworkReply);
       
   173     if (d->connection)
       
   174         return d->connection->d_func()->uncompressedBytesAvailableNextBlock(*this);
       
   175     else
       
   176         return -1;
       
   177 }
       
   178 
       
   179 QByteArray QHttpNetworkReply::readAny()
       
   180 {
       
   181     Q_D(QHttpNetworkReply);
       
   182     // we'll take the last buffer, so schedule another read from http
       
   183     if (d->downstreamLimited && d->responseData.bufferCount() == 1)
       
   184         d->connection->d_func()->readMoreLater(this);
       
   185     return d->responseData.read();
       
   186 }
       
   187 
       
   188 void QHttpNetworkReply::setDownstreamLimited(bool dsl)
       
   189 {
       
   190     Q_D(QHttpNetworkReply);
       
   191     d->downstreamLimited = dsl;
       
   192     d->connection->d_func()->readMoreLater(this);
       
   193 }
       
   194 
       
   195 bool QHttpNetworkReply::isFinished() const
       
   196 {
       
   197     return d_func()->state == QHttpNetworkReplyPrivate::AllDoneState;
       
   198 }
       
   199 
       
   200 bool QHttpNetworkReply::isPipeliningUsed() const
       
   201 {
       
   202     return d_func()->pipeliningUsed;
       
   203 }
       
   204 
       
   205 
       
   206 QHttpNetworkReplyPrivate::QHttpNetworkReplyPrivate(const QUrl &newUrl)
       
   207     : QHttpNetworkHeaderPrivate(newUrl), state(NothingDoneState), statusCode(100),
       
   208       majorVersion(0), minorVersion(0), bodyLength(0), contentRead(0), totalProgress(0),
       
   209       chunkedTransferEncoding(false),
       
   210       connectionCloseEnabled(true),
       
   211       forceConnectionCloseEnabled(false),
       
   212       currentChunkSize(0), currentChunkRead(0), connection(0), initInflate(false),
       
   213       autoDecompress(false), responseData(), requestIsPrepared(false)
       
   214       ,pipeliningUsed(false), downstreamLimited(false)
       
   215 {
       
   216 }
       
   217 
       
   218 QHttpNetworkReplyPrivate::~QHttpNetworkReplyPrivate()
       
   219 {
       
   220 }
       
   221 
       
   222 void QHttpNetworkReplyPrivate::clearHttpLayerInformation()
       
   223 {
       
   224     state = NothingDoneState;
       
   225     statusCode = 100;
       
   226     bodyLength = 0;
       
   227     contentRead = 0;
       
   228     totalProgress = 0;
       
   229     currentChunkSize = 0;
       
   230     currentChunkRead = 0;
       
   231     connectionCloseEnabled = true;
       
   232 #ifndef QT_NO_COMPRESS
       
   233     if (initInflate)
       
   234         inflateEnd(&inflateStrm);
       
   235 #endif
       
   236     initInflate = false;
       
   237     streamEnd = false;
       
   238     fields.clear();
       
   239 }
       
   240 
       
   241 // TODO: Isn't everything HTTP layer related? We don't need to set connection and connectionChannel to 0 at all
       
   242 void QHttpNetworkReplyPrivate::clear()
       
   243 {
       
   244     connection = 0;
       
   245     connectionChannel = 0;
       
   246     autoDecompress = false;
       
   247     clearHttpLayerInformation();
       
   248 }
       
   249 
       
   250 // QHttpNetworkReplyPrivate
       
   251 qint64 QHttpNetworkReplyPrivate::bytesAvailable() const
       
   252 {
       
   253     return (state != ReadingDataState ? 0 : fragment.size());
       
   254 }
       
   255 
       
   256 bool QHttpNetworkReplyPrivate::isGzipped()
       
   257 {
       
   258     QByteArray encoding = headerField("content-encoding");
       
   259     return qstricmp(encoding.constData(), "gzip") == 0;
       
   260 }
       
   261 
       
   262 void QHttpNetworkReplyPrivate::removeAutoDecompressHeader()
       
   263 {
       
   264     // The header "Content-Encoding  = gzip" is retained.
       
   265     // Content-Length is removed since the actual one send by the server is for compressed data
       
   266     QByteArray name("content-length");
       
   267     QList<QPair<QByteArray, QByteArray> >::Iterator it = fields.begin(),
       
   268                                                    end = fields.end();
       
   269     while (it != end) {
       
   270         if (qstricmp(name.constData(), it->first.constData()) == 0) {
       
   271             fields.erase(it);
       
   272             break;
       
   273         }
       
   274         ++it;
       
   275     }
       
   276 
       
   277 }
       
   278 
       
   279 bool QHttpNetworkReplyPrivate::findChallenge(bool forProxy, QByteArray &challenge) const
       
   280 {
       
   281     challenge.clear();
       
   282     // find out the type of authentication protocol requested.
       
   283     QByteArray header = forProxy ? "proxy-authenticate" : "www-authenticate";
       
   284     // pick the best protocol (has to match parsing in QAuthenticatorPrivate)
       
   285     QList<QByteArray> challenges = headerFieldValues(header);
       
   286     for (int i = 0; i<challenges.size(); i++) {
       
   287         QByteArray line = challenges.at(i);
       
   288         // todo use qstrincmp
       
   289         if (!line.toLower().startsWith("negotiate"))
       
   290             challenge = line;
       
   291     }
       
   292     return !challenge.isEmpty();
       
   293 }
       
   294 
       
   295 QAuthenticatorPrivate::Method QHttpNetworkReplyPrivate::authenticationMethod(bool isProxy) const
       
   296 {
       
   297     // The logic is same as the one used in void QAuthenticatorPrivate::parseHttpResponse()
       
   298     QAuthenticatorPrivate::Method method = QAuthenticatorPrivate::None;
       
   299     QByteArray header = isProxy ? "proxy-authenticate" : "www-authenticate";
       
   300     QList<QByteArray> challenges = headerFieldValues(header);
       
   301     for (int i = 0; i<challenges.size(); i++) {
       
   302         QByteArray line = challenges.at(i).trimmed().toLower();
       
   303         if (method < QAuthenticatorPrivate::Basic
       
   304             && line.startsWith("basic")) {
       
   305             method = QAuthenticatorPrivate::Basic;
       
   306         } else if (method < QAuthenticatorPrivate::Ntlm
       
   307             && line.startsWith("ntlm")) {
       
   308             method = QAuthenticatorPrivate::Ntlm;
       
   309         } else if (method < QAuthenticatorPrivate::DigestMd5
       
   310             && line.startsWith("digest")) {
       
   311             method = QAuthenticatorPrivate::DigestMd5;
       
   312         }
       
   313     }
       
   314     return method;
       
   315 }
       
   316 
       
   317 #ifndef QT_NO_COMPRESS
       
   318 bool QHttpNetworkReplyPrivate::gzipCheckHeader(QByteArray &content, int &pos)
       
   319 {
       
   320     int method = 0; // method byte
       
   321     int flags = 0;  // flags byte
       
   322     bool ret = false;
       
   323 
       
   324     // Assure two bytes in the buffer so we can peek ahead -- handle case
       
   325     // where first byte of header is at the end of the buffer after the last
       
   326     // gzip segment
       
   327     pos = -1;
       
   328     QByteArray &body = content;
       
   329     int maxPos = body.size()-1;
       
   330     if (maxPos < 1) {
       
   331         return ret;
       
   332     }
       
   333 
       
   334     // Peek ahead to check the gzip magic header
       
   335     if (body[0] != char(gz_magic[0]) ||
       
   336         body[1] != char(gz_magic[1])) {
       
   337         return ret;
       
   338     }
       
   339     pos += 2;
       
   340     // Check the rest of the gzip header
       
   341     if (++pos <= maxPos)
       
   342         method = body[pos];
       
   343     if (pos++ <= maxPos)
       
   344         flags = body[pos];
       
   345     if (method != Z_DEFLATED || (flags & RESERVED) != 0) {
       
   346         return ret;
       
   347     }
       
   348 
       
   349     // Discard time, xflags and OS code:
       
   350     pos += 6;
       
   351     if (pos > maxPos)
       
   352         return ret;
       
   353     if ((flags & EXTRA_FIELD) && ((pos+2) <= maxPos)) { // skip the extra field
       
   354         unsigned len =  (unsigned)body[++pos];
       
   355         len += ((unsigned)body[++pos])<<8;
       
   356         pos += len;
       
   357         if (pos > maxPos)
       
   358             return ret;
       
   359     }
       
   360     if ((flags & ORIG_NAME) != 0) { // skip the original file name
       
   361         while(++pos <= maxPos && body[pos]) {}
       
   362     }
       
   363     if ((flags & COMMENT) != 0) {   // skip the .gz file comment
       
   364         while(++pos <= maxPos && body[pos]) {}
       
   365     }
       
   366     if ((flags & HEAD_CRC) != 0) {  // skip the header crc
       
   367         pos += 2;
       
   368         if (pos > maxPos)
       
   369             return ret;
       
   370     }
       
   371     ret = (pos < maxPos); // return failed, if no more bytes left
       
   372     return ret;
       
   373 }
       
   374 
       
   375 int QHttpNetworkReplyPrivate::gunzipBodyPartially(QByteArray &compressed, QByteArray &inflated)
       
   376 {
       
   377     int ret = Z_DATA_ERROR;
       
   378     unsigned have;
       
   379     unsigned char out[CHUNK];
       
   380     int pos = -1;
       
   381 
       
   382     if (!initInflate) {
       
   383         // check the header
       
   384         if (!gzipCheckHeader(compressed, pos))
       
   385             return ret;
       
   386         // allocate inflate state
       
   387         inflateStrm.zalloc = Z_NULL;
       
   388         inflateStrm.zfree = Z_NULL;
       
   389         inflateStrm.opaque = Z_NULL;
       
   390         inflateStrm.avail_in = 0;
       
   391         inflateStrm.next_in = Z_NULL;
       
   392         ret = inflateInit2(&inflateStrm, -MAX_WBITS);
       
   393         if (ret != Z_OK)
       
   394             return ret;
       
   395         initInflate = true;
       
   396         streamEnd = false;
       
   397     }
       
   398 
       
   399     //remove the header.
       
   400     compressed.remove(0, pos+1);
       
   401     // expand until deflate stream ends
       
   402     inflateStrm.next_in = (unsigned char *)compressed.data();
       
   403     inflateStrm.avail_in = compressed.size();
       
   404     do {
       
   405         inflateStrm.avail_out = sizeof(out);
       
   406         inflateStrm.next_out = out;
       
   407         ret = inflate(&inflateStrm, Z_NO_FLUSH);
       
   408         switch (ret) {
       
   409         case Z_NEED_DICT:
       
   410             ret = Z_DATA_ERROR;
       
   411             // and fall through
       
   412         case Z_DATA_ERROR:
       
   413         case Z_MEM_ERROR:
       
   414             inflateEnd(&inflateStrm);
       
   415             initInflate = false;
       
   416             return ret;
       
   417         }
       
   418         have = sizeof(out) - inflateStrm.avail_out;
       
   419         inflated.append(QByteArray((const char *)out, have));
       
   420      } while (inflateStrm.avail_out == 0);
       
   421     // clean up and return
       
   422     if (ret <= Z_ERRNO || ret == Z_STREAM_END) {
       
   423         inflateEnd(&inflateStrm);
       
   424         initInflate = false;
       
   425     }
       
   426     streamEnd = (ret == Z_STREAM_END);
       
   427     return ret;
       
   428 }
       
   429 #endif
       
   430 
       
   431 qint64 QHttpNetworkReplyPrivate::readStatus(QAbstractSocket *socket)
       
   432 {
       
   433     if (fragment.isEmpty()) {
       
   434         // reserve bytes for the status line. This is better than always append() which reallocs the byte array
       
   435         fragment.reserve(32);
       
   436     }
       
   437 
       
   438     qint64 bytes = 0;
       
   439     char c;
       
   440     qint64 haveRead = 0;
       
   441 
       
   442     do {
       
   443         haveRead = socket->read(&c, 1);
       
   444         if (haveRead == -1)
       
   445             return -1; // unexpected EOF
       
   446         else if (haveRead == 0)
       
   447             break; // read more later
       
   448 
       
   449         bytes++;
       
   450 
       
   451         // allow both CRLF & LF (only) line endings
       
   452         if (c == '\n') {
       
   453             // remove the CR at the end
       
   454             if (fragment.endsWith('\r')) {
       
   455                 fragment.truncate(fragment.length()-1);
       
   456             }
       
   457             bool ok = parseStatus(fragment);
       
   458             state = ReadingHeaderState;
       
   459             fragment.clear();
       
   460             if (!ok) {
       
   461                 return -1;
       
   462             }
       
   463             break;
       
   464         } else {
       
   465             fragment.append(c);
       
   466         }
       
   467 
       
   468         // is this a valid reply?
       
   469         if (fragment.length() >= 5 && !fragment.startsWith("HTTP/"))
       
   470         {
       
   471             fragment.clear();
       
   472             return -1;
       
   473         }
       
   474     } while (haveRead == 1);
       
   475 
       
   476     return bytes;
       
   477 }
       
   478 
       
   479 bool QHttpNetworkReplyPrivate::parseStatus(const QByteArray &status)
       
   480 {
       
   481     // from RFC 2616:
       
   482     //        Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF
       
   483     //        HTTP-Version   = "HTTP" "/" 1*DIGIT "." 1*DIGIT
       
   484     // that makes: 'HTTP/n.n xxx Message'
       
   485     // byte count:  0123456789012
       
   486 
       
   487     static const int minLength = 11;
       
   488     static const int dotPos = 6;
       
   489     static const int spacePos = 8;
       
   490     static const char httpMagic[] = "HTTP/";
       
   491 
       
   492     if (status.length() < minLength
       
   493         || !status.startsWith(httpMagic)
       
   494         || status.at(dotPos) != '.'
       
   495         || status.at(spacePos) != ' ') {
       
   496         // I don't know how to parse this status line
       
   497         return false;
       
   498     }
       
   499 
       
   500     // optimize for the valid case: defer checking until the end
       
   501     majorVersion = status.at(dotPos - 1) - '0';
       
   502     minorVersion = status.at(dotPos + 1) - '0';
       
   503 
       
   504     int i = spacePos;
       
   505     int j = status.indexOf(' ', i + 1); // j == -1 || at(j) == ' ' so j+1 == 0 && j+1 <= length()
       
   506     const QByteArray code = status.mid(i + 1, j - i - 1);
       
   507 
       
   508     bool ok;
       
   509     statusCode = code.toInt(&ok);
       
   510     reasonPhrase = QString::fromLatin1(status.constData() + j + 1);
       
   511 
       
   512     return ok && uint(majorVersion) <= 9 && uint(minorVersion) <= 9;
       
   513 }
       
   514 
       
   515 qint64 QHttpNetworkReplyPrivate::readHeader(QAbstractSocket *socket)
       
   516 {
       
   517     if (fragment.isEmpty()) {
       
   518         // according to http://dev.opera.com/articles/view/mama-http-headers/ the average size of the header
       
   519         // block is 381 bytes.
       
   520         // reserve bytes. This is better than always append() which reallocs the byte array.
       
   521         fragment.reserve(512);
       
   522     }
       
   523 
       
   524     qint64 bytes = 0;
       
   525     char c = 0;
       
   526     bool allHeaders = false;
       
   527     qint64 haveRead = 0;
       
   528     do {
       
   529         haveRead = socket->read(&c, 1);
       
   530         if (haveRead == 0) {
       
   531             // read more later
       
   532             break;
       
   533         } else if (haveRead == -1) {
       
   534             // connection broke down
       
   535             return -1;
       
   536         } else {
       
   537             fragment.append(c);
       
   538             bytes++;
       
   539 
       
   540             if (c == '\n') {
       
   541                 // check for possible header endings. As per HTTP rfc,
       
   542                 // the header endings will be marked by CRLFCRLF. But
       
   543                 // we will allow CRLFCRLF, CRLFLF, LFLF
       
   544                 if (fragment.endsWith("\r\n\r\n")
       
   545                     || fragment.endsWith("\r\n\n")
       
   546                     || fragment.endsWith("\n\n"))
       
   547                     allHeaders = true;
       
   548 
       
   549                 // there is another case: We have no headers. Then the fragment equals just the line ending
       
   550                 if ((fragment.length() == 2 && fragment.endsWith("\r\n"))
       
   551                     || (fragment.length() == 1 && fragment.endsWith("\n")))
       
   552                     allHeaders = true;
       
   553             }
       
   554         }
       
   555     } while (!allHeaders && haveRead > 0);
       
   556 
       
   557     // we received all headers now parse them
       
   558     if (allHeaders) {
       
   559         parseHeader(fragment);
       
   560         state = ReadingDataState;
       
   561         fragment.clear(); // next fragment
       
   562         bodyLength = contentLength(); // cache the length
       
   563 
       
   564         // cache isChunked() since it is called often
       
   565         chunkedTransferEncoding = headerField("transfer-encoding").toLower().contains("chunked");
       
   566 
       
   567         // cache isConnectionCloseEnabled since it is called often
       
   568         QByteArray connectionHeaderField = headerField("connection");
       
   569         // check for explicit indication of close or the implicit connection close of HTTP/1.0
       
   570         connectionCloseEnabled = (connectionHeaderField.toLower().contains("close") ||
       
   571             headerField("proxy-connection").toLower().contains("close")) ||
       
   572             (majorVersion == 1 && minorVersion == 0 && connectionHeaderField.isEmpty());
       
   573     }
       
   574     return bytes;
       
   575 }
       
   576 
       
   577 void QHttpNetworkReplyPrivate::parseHeader(const QByteArray &header)
       
   578 {
       
   579     // see rfc2616, sec 4 for information about HTTP/1.1 headers.
       
   580     // allows relaxed parsing here, accepts both CRLF & LF line endings
       
   581     const QByteArrayMatcher lf("\n");
       
   582     const QByteArrayMatcher colon(":");
       
   583     int i = 0;
       
   584     while (i < header.count()) {
       
   585         int j = colon.indexIn(header, i); // field-name
       
   586         if (j == -1)
       
   587             break;
       
   588         const QByteArray field = header.mid(i, j - i).trimmed();
       
   589         j++;
       
   590         // any number of LWS is allowed before and after the value
       
   591         QByteArray value;
       
   592         do {
       
   593             i = lf.indexIn(header, j);
       
   594             if (i == -1)
       
   595                 break;
       
   596             if (!value.isEmpty())
       
   597                 value += ' ';
       
   598             // check if we have CRLF or only LF
       
   599             bool hasCR = (i && header[i-1] == '\r');
       
   600             int length = i -(hasCR ? 1: 0) - j;
       
   601             value += header.mid(j, length).trimmed();
       
   602             j = ++i;
       
   603         } while (i < header.count() && (header.at(i) == ' ' || header.at(i) == '\t'));
       
   604         if (i == -1)
       
   605             break; // something is wrong
       
   606 
       
   607         fields.append(qMakePair(field, value));
       
   608     }
       
   609 }
       
   610 
       
   611 bool QHttpNetworkReplyPrivate::isChunked()
       
   612 {
       
   613     return chunkedTransferEncoding;
       
   614 }
       
   615 
       
   616 bool QHttpNetworkReplyPrivate::isConnectionCloseEnabled()
       
   617 {
       
   618     return connectionCloseEnabled || forceConnectionCloseEnabled;
       
   619 }
       
   620 
       
   621 // note this function can only be used for non-chunked, non-compressed with
       
   622 // known content length
       
   623 qint64 QHttpNetworkReplyPrivate::readBodyFast(QAbstractSocket *socket, QByteDataBuffer *rb)
       
   624 {
       
   625     qint64 toBeRead = qMin(socket->bytesAvailable(), bodyLength - contentRead);
       
   626     QByteArray bd;
       
   627     bd.resize(toBeRead);
       
   628     qint64 haveRead = socket->read(bd.data(), bd.size());
       
   629     if (haveRead == -1) {
       
   630         bd.clear();
       
   631         return 0; // ### error checking here;
       
   632     }
       
   633     bd.resize(haveRead);
       
   634 
       
   635     rb->append(bd);
       
   636 
       
   637     if (contentRead + haveRead == bodyLength) {
       
   638         state = AllDoneState;
       
   639     }
       
   640 
       
   641     contentRead += haveRead;
       
   642     return haveRead;
       
   643 }
       
   644 
       
   645 
       
   646 qint64 QHttpNetworkReplyPrivate::readBody(QAbstractSocket *socket, QByteDataBuffer *out)
       
   647 {
       
   648     qint64 bytes = 0;
       
   649     if (isChunked()) {
       
   650         bytes += readReplyBodyChunked(socket, out); // chunked transfer encoding (rfc 2616, sec 3.6)
       
   651     } else if (bodyLength > 0) { // we have a Content-Length
       
   652         bytes += readReplyBodyRaw(socket, out, bodyLength - contentRead);
       
   653         if (contentRead + bytes == bodyLength)
       
   654             state = AllDoneState;
       
   655     } else {
       
   656         bytes += readReplyBodyRaw(socket, out, socket->bytesAvailable());
       
   657     }
       
   658     contentRead += bytes;
       
   659     return bytes;
       
   660 }
       
   661 
       
   662 qint64 QHttpNetworkReplyPrivate::readReplyBodyRaw(QIODevice *in, QByteDataBuffer *out, qint64 size)
       
   663 {
       
   664     qint64 bytes = 0;
       
   665     Q_ASSERT(in);
       
   666     Q_ASSERT(out);
       
   667 
       
   668     int toBeRead = qMin<qint64>(128*1024, qMin<qint64>(size, in->bytesAvailable()));
       
   669     while (toBeRead > 0) {
       
   670         QByteArray byteData;
       
   671         byteData.resize(toBeRead);
       
   672         qint64 haveRead = in->read(byteData.data(), byteData.size());
       
   673         if (haveRead <= 0) {
       
   674             // ### error checking here
       
   675             byteData.clear();
       
   676             return bytes;
       
   677         }
       
   678 
       
   679         byteData.resize(haveRead);
       
   680         out->append(byteData);
       
   681         bytes += haveRead;
       
   682         size -= haveRead;
       
   683 
       
   684         toBeRead = qMin<qint64>(128*1024, qMin<qint64>(size, in->bytesAvailable()));
       
   685     }
       
   686     return bytes;
       
   687 
       
   688 }
       
   689 
       
   690 qint64 QHttpNetworkReplyPrivate::readReplyBodyChunked(QIODevice *in, QByteDataBuffer *out)
       
   691 {
       
   692     qint64 bytes = 0;
       
   693     while (in->bytesAvailable()) { // while we can read from input
       
   694         // if we are done with the current chunk, get the size of the new chunk
       
   695         if (currentChunkRead >= currentChunkSize) {
       
   696             currentChunkSize = 0;
       
   697             currentChunkRead = 0;
       
   698             if (bytes) {
       
   699                 char crlf[2];
       
   700                 bytes += in->read(crlf, 2); // read the "\r\n" after the chunk
       
   701             }
       
   702             bytes += getChunkSize(in, &currentChunkSize);
       
   703             if (currentChunkSize == -1)
       
   704                 break;
       
   705         }
       
   706         // if the chunk size is 0, end of the stream
       
   707         if (currentChunkSize == 0) {
       
   708             state = AllDoneState;
       
   709             break;
       
   710         }
       
   711 
       
   712         // otherwise, try to read what is missing for this chunk
       
   713         qint64 haveRead = readReplyBodyRaw (in, out, currentChunkSize - currentChunkRead);
       
   714         currentChunkRead += haveRead;
       
   715         bytes += haveRead;
       
   716 
       
   717         // ### error checking here
       
   718 
       
   719     }
       
   720     return bytes;
       
   721 }
       
   722 
       
   723 qint64 QHttpNetworkReplyPrivate::getChunkSize(QIODevice *in, qint64 *chunkSize)
       
   724 {
       
   725     qint64 bytes = 0;
       
   726     char crlf[2];
       
   727     *chunkSize = -1;
       
   728     int bytesAvailable = in->bytesAvailable();
       
   729     while (bytesAvailable > bytes) {
       
   730         qint64 sniffedBytes =  in->peek(crlf, 2);
       
   731         int fragmentSize = fragment.size();
       
   732         // check the next two bytes for a "\r\n", skip blank lines
       
   733         if ((fragmentSize && sniffedBytes == 2 && crlf[0] == '\r' && crlf[1] == '\n')
       
   734            ||(fragmentSize > 1 && fragment.endsWith('\r')  && crlf[0] == '\n'))
       
   735         {
       
   736             bytes += in->read(crlf, 1);     // read the \r or \n
       
   737             if (crlf[0] == '\r')
       
   738                 bytes += in->read(crlf, 1); // read the \n
       
   739             bool ok = false;
       
   740             // ignore the chunk-extension
       
   741             fragment = fragment.mid(0, fragment.indexOf(';')).trimmed();
       
   742             *chunkSize = fragment.toLong(&ok, 16);
       
   743             fragment.clear();
       
   744             break; // size done
       
   745         } else {
       
   746             // read the fragment to the buffer
       
   747             char c = 0;
       
   748             bytes += in->read(&c, 1);
       
   749             fragment.append(c);
       
   750         }
       
   751     }
       
   752     return bytes;
       
   753 }
       
   754 
       
   755 void QHttpNetworkReplyPrivate::appendUncompressedReplyData(QByteArray &qba)
       
   756 {
       
   757     responseData.append(qba);
       
   758 
       
   759     // clear the original! helps with implicit sharing and
       
   760     // avoiding memcpy when the user is reading the data
       
   761     qba.clear();
       
   762 }
       
   763 
       
   764 void QHttpNetworkReplyPrivate::appendUncompressedReplyData(QByteDataBuffer &data)
       
   765 {
       
   766     responseData.append(data);
       
   767 
       
   768     // clear the original! helps with implicit sharing and
       
   769     // avoiding memcpy when the user is reading the data
       
   770     data.clear();
       
   771 }
       
   772 
       
   773 void QHttpNetworkReplyPrivate::appendCompressedReplyData(QByteDataBuffer &data)
       
   774 {
       
   775     // Work in progress: Later we will directly use a list of QByteArray or a QRingBuffer
       
   776     // instead of one QByteArray.
       
   777     for(int i = 0; i < data.bufferCount(); i++) {
       
   778         QByteArray &byteData = data[i];
       
   779         compressedData.append(byteData.constData(), byteData.size());
       
   780     }
       
   781     data.clear();
       
   782 }
       
   783 
       
   784 
       
   785 bool QHttpNetworkReplyPrivate::shouldEmitSignals()
       
   786 {
       
   787     // for 401 & 407 don't emit the data signals. Content along with these
       
   788     // responses are send only if the authentication fails.
       
   789     return (statusCode != 401 && statusCode != 407);
       
   790 }
       
   791 
       
   792 bool QHttpNetworkReplyPrivate::expectContent()
       
   793 {
       
   794     // check whether we can expect content after the headers (rfc 2616, sec4.4)
       
   795     if ((statusCode >= 100 && statusCode < 200)
       
   796         || statusCode == 204 || statusCode == 304)
       
   797         return false;
       
   798     if (request.operation() == QHttpNetworkRequest::Head)
       
   799         return !shouldEmitSignals();
       
   800     if (contentLength() == 0)
       
   801         return false;
       
   802     return true;
       
   803 }
       
   804 
       
   805 void QHttpNetworkReplyPrivate::eraseData()
       
   806 {
       
   807     compressedData.clear();
       
   808     responseData.clear();
       
   809 }
       
   810 
       
   811 
       
   812 // SSL support below
       
   813 #ifndef QT_NO_OPENSSL
       
   814 
       
   815 QSslConfiguration QHttpNetworkReply::sslConfiguration() const
       
   816 {
       
   817     Q_D(const QHttpNetworkReply);
       
   818 
       
   819     if (!d->connectionChannel)
       
   820         return QSslConfiguration();
       
   821 
       
   822     QSslSocket *sslSocket = qobject_cast<QSslSocket*>(d->connectionChannel->socket);
       
   823     if (!sslSocket)
       
   824         return QSslConfiguration();
       
   825 
       
   826     return sslSocket->sslConfiguration();
       
   827 }
       
   828 
       
   829 void QHttpNetworkReply::setSslConfiguration(const QSslConfiguration &config)
       
   830 {
       
   831     Q_D(QHttpNetworkReply);
       
   832     if (d->connection)
       
   833         d->connection->setSslConfiguration(config);
       
   834 }
       
   835 
       
   836 void QHttpNetworkReply::ignoreSslErrors()
       
   837 {
       
   838     Q_D(QHttpNetworkReply);
       
   839     if (d->connection)
       
   840         d->connection->ignoreSslErrors();
       
   841 }
       
   842 
       
   843 void QHttpNetworkReply::ignoreSslErrors(const QList<QSslError> &errors)
       
   844 {
       
   845     Q_D(QHttpNetworkReply);
       
   846     if (d->connection)
       
   847         d->connection->ignoreSslErrors(errors);
       
   848 }
       
   849 
       
   850 
       
   851 #endif //QT_NO_OPENSSL
       
   852 
       
   853 
       
   854 QT_END_NAMESPACE
       
   855 
       
   856 #endif // QT_NO_HTTP