src/network/socket/qhttpsocketengine.cpp
changeset 7 f7bc934e204c
parent 3 41300fa6a67c
child 30 5dc02b23752f
equal deleted inserted replaced
3:41300fa6a67c 7:f7bc934e204c
     1 /****************************************************************************
     1 /****************************************************************************
     2 **
     2 **
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
     4 ** All rights reserved.
     4 ** All rights reserved.
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
     6 **
     6 **
     7 ** This file is part of the QtNetwork module of the Qt Toolkit.
     7 ** This file is part of the QtNetwork module of the Qt Toolkit.
     8 **
     8 **
   210 }
   210 }
   211 
   211 
   212 qint64 QHttpSocketEngine::read(char *data, qint64 maxlen)
   212 qint64 QHttpSocketEngine::read(char *data, qint64 maxlen)
   213 {
   213 {
   214     Q_D(QHttpSocketEngine);
   214     Q_D(QHttpSocketEngine);
   215     qint64 bytesRead = 0;
   215     qint64 bytesRead = d->socket->read(data, maxlen);
   216 
       
   217     if (!d->readBuffer.isEmpty()) {
       
   218         // Read as much from the buffer as we can.
       
   219         bytesRead = qMin((qint64)d->readBuffer.size(), maxlen);
       
   220         memcpy(data, d->readBuffer.constData(), bytesRead);
       
   221         data += bytesRead;
       
   222         maxlen -= bytesRead;
       
   223         d->readBuffer = d->readBuffer.mid(bytesRead);
       
   224     }
       
   225 
       
   226     qint64 bytesReadFromSocket = d->socket->read(data, maxlen);
       
   227 
   216 
   228     if (d->socket->state() == QAbstractSocket::UnconnectedState
   217     if (d->socket->state() == QAbstractSocket::UnconnectedState
   229         && d->socket->bytesAvailable() == 0) {
   218         && d->socket->bytesAvailable() == 0) {
   230         emitReadNotification();
   219         emitReadNotification();
   231     }
   220     }
   232 
   221 
   233     if (bytesReadFromSocket > 0) {
   222     if (bytesRead == -1) {
   234         // Add to what we read so far.
       
   235         bytesRead += bytesReadFromSocket;
       
   236     } else if (bytesRead == 0 && bytesReadFromSocket == -1) {
       
   237         // If nothing has been read so far, and the direct socket read
   223         // If nothing has been read so far, and the direct socket read
   238         // failed, return the socket's error. Otherwise, fall through and
   224         // failed, return the socket's error. Otherwise, fall through and
   239         // return as much as we read so far.
   225         // return as much as we read so far.
   240         close();
   226         close();
   241         setError(QAbstractSocket::RemoteHostClosedError,
   227         setError(QAbstractSocket::RemoteHostClosedError,
   558         emitConnectionNotification();
   544         emitConnectionNotification();
   559         return;
   545         return;
   560     }
   546     }
   561 
   547 
   562     QHttpResponseHeader responseHeader(QString::fromLatin1(d->readBuffer));
   548     QHttpResponseHeader responseHeader(QString::fromLatin1(d->readBuffer));
   563     d->readBuffer.clear();
   549     d->readBuffer.clear(); // we parsed the proxy protocol response. from now on direct socket reading will be done
   564 
   550 
   565     int statusCode = responseHeader.statusCode();
   551     int statusCode = responseHeader.statusCode();
   566     if (statusCode == 200) {
   552     if (statusCode == 200) {
   567         d->state = Connected;
   553         d->state = Connected;
   568         setLocalAddress(d->socket->localAddress());
   554         setLocalAddress(d->socket->localAddress());