diff options
Diffstat (limited to 'src/network')
-rw-r--r-- | src/network/access/qhttpnetworkconnection.cpp | 9 | ||||
-rw-r--r-- | src/network/access/qhttpnetworkconnection_p.h | 1 | ||||
-rw-r--r-- | src/network/access/qhttpnetworkconnectionchannel.cpp | 11 | ||||
-rw-r--r-- | src/network/access/qnetworkaccessbackend.cpp | 76 | ||||
-rw-r--r-- | src/network/access/qnetworkaccessmanager.cpp | 4 | ||||
-rw-r--r-- | src/network/access/qnetworkreplyimpl.cpp | 17 | ||||
-rw-r--r-- | src/network/kernel/qnetworkproxy.cpp | 98 | ||||
-rw-r--r-- | src/network/kernel/qnetworkproxy.h | 16 | ||||
-rw-r--r-- | src/network/kernel/qnetworkproxy_symbian.cpp | 13 | ||||
-rw-r--r-- | src/network/socket/qabstractsocket.cpp | 20 | ||||
-rw-r--r-- | src/network/socket/qhttpsocketengine.cpp | 8 | ||||
-rw-r--r-- | src/network/socket/qsocks5socketengine.cpp | 7 | ||||
-rw-r--r-- | src/network/socket/qsymbiansocketengine.cpp | 70 | ||||
-rw-r--r-- | src/network/socket/qsymbiansocketengine_p.h | 2 | ||||
-rw-r--r-- | src/network/socket/qtcpserver.cpp | 5 | ||||
-rw-r--r-- | src/network/ssl/qsslsocket.cpp | 2 |
16 files changed, 299 insertions, 60 deletions
diff --git a/src/network/access/qhttpnetworkconnection.cpp b/src/network/access/qhttpnetworkconnection.cpp index 07dd729..a471559 100644 --- a/src/network/access/qhttpnetworkconnection.cpp +++ b/src/network/access/qhttpnetworkconnection.cpp @@ -519,6 +519,15 @@ bool QHttpNetworkConnectionPrivate::dequeueRequest(QAbstractSocket *socket) return false; } +QHttpNetworkRequest QHttpNetworkConnectionPrivate::predictNextRequest() +{ + if (!highPriorityQueue.isEmpty()) + return highPriorityQueue.last().first; + if (!lowPriorityQueue.isEmpty()) + return lowPriorityQueue.last().first; + return QHttpNetworkRequest(); +} + // this is called from _q_startNextRequest and when a request has been sent down a socket from the channel void QHttpNetworkConnectionPrivate::fillPipeline(QAbstractSocket *socket) { diff --git a/src/network/access/qhttpnetworkconnection_p.h b/src/network/access/qhttpnetworkconnection_p.h index adb779f4..329d3626 100644 --- a/src/network/access/qhttpnetworkconnection_p.h +++ b/src/network/access/qhttpnetworkconnection_p.h @@ -169,6 +169,7 @@ public: void requeueRequest(const HttpMessagePair &pair); // e.g. after pipeline broke bool dequeueRequest(QAbstractSocket *socket); void prepareRequest(HttpMessagePair &request); + QHttpNetworkRequest predictNextRequest(); void fillPipeline(QAbstractSocket *socket); bool fillPipeline(QList<HttpMessagePair> &queue, QHttpNetworkConnectionChannel &channel); diff --git a/src/network/access/qhttpnetworkconnectionchannel.cpp b/src/network/access/qhttpnetworkconnectionchannel.cpp index 6fbc6f8..f440101 100644 --- a/src/network/access/qhttpnetworkconnectionchannel.cpp +++ b/src/network/access/qhttpnetworkconnectionchannel.cpp @@ -579,6 +579,17 @@ bool QHttpNetworkConnectionChannel::ensureConnection() connectHost = connection->d_func()->networkProxy.hostName(); connectPort = connection->d_func()->networkProxy.port(); } + if (socket->proxy().type() == QNetworkProxy::HttpProxy) { + // Make user-agent field available to HTTP proxy socket engine (QTBUG-17223) + QByteArray value; + // ensureConnection is called before any request has been assigned, but can also be called again if reconnecting + if (request.url().isEmpty()) + value = connection->d_func()->predictNextRequest().headerField("user-agent"); + else + value = request.headerField("user-agent"); + if (!value.isEmpty()) + socket->setProperty("_q_user-agent", value); + } #endif if (ssl) { #ifndef QT_NO_OPENSSL diff --git a/src/network/access/qnetworkaccessbackend.cpp b/src/network/access/qnetworkaccessbackend.cpp index 6220abe..2aea350 100644 --- a/src/network/access/qnetworkaccessbackend.cpp +++ b/src/network/access/qnetworkaccessbackend.cpp @@ -41,6 +41,7 @@ #include "qnetworkaccessbackend_p.h" #include "qnetworkaccessmanager_p.h" +#include "qnetworkconfigmanager.h" #include "qnetworkrequest.h" #include "qnetworkreply.h" #include "qnetworkreply_p.h" @@ -343,8 +344,6 @@ void QNetworkAccessBackend::sslErrors(const QList<QSslError> &errors) #endif } -#ifndef QT_NO_BEARERMANAGEMENT - /*! Starts the backend. Returns true if the backend is started. Returns false if the backend could not be started due to an unopened or roaming session. The caller should recall this @@ -352,31 +351,62 @@ void QNetworkAccessBackend::sslErrors(const QList<QSslError> &errors) */ bool QNetworkAccessBackend::start() { - if (!manager->networkSession) { - open(); - return true; - } - - // This is not ideal. - const QString host = reply->url.host(); - if (host == QLatin1String("localhost") || - QHostAddress(host) == QHostAddress::LocalHost || - QHostAddress(host) == QHostAddress::LocalHostIPv6) { - // Don't need an open session for localhost access. - open(); - return true; +#ifndef QT_NO_BEARERMANAGEMENT + // For bearer, check if session start is required + if (manager->networkSession) { + // session required + if (manager->networkSession->isOpen() && + manager->networkSession->state() == QNetworkSession::Connected) { + // Session is already open and ready to use. + // copy network session down to the backend + setProperty("_q_networksession", QVariant::fromValue(manager->networkSession)); + } else { + // Session not ready, but can skip for loopback connections + + // This is not ideal. + const QString host = reply->url.host(); + + if (host == QLatin1String("localhost") || + QHostAddress(host) == QHostAddress::LocalHost || + QHostAddress(host) == QHostAddress::LocalHostIPv6) { + // Don't need an open session for localhost access. + } else { + // need to wait for session to be opened + return false; + } + } } +#endif - if (manager->networkSession->isOpen() && - manager->networkSession->state() == QNetworkSession::Connected) { - //copy network session down to the backend - setProperty("_q_networksession", QVariant::fromValue(manager->networkSession)); - open(); - return true; +#ifndef QT_NO_NETWORKPROXY +#ifndef QT_NO_BEARERMANAGEMENT + // Get the proxy settings from the network session (in the case of service networks, + // the proxy settings change depending which AP was activated) + QNetworkSession *session = manager->networkSession.data(); + QNetworkConfiguration config; + if (session) { + QNetworkConfigurationManager configManager; + // The active configuration tells us what IAP is in use + QVariant v = session->sessionProperty(QLatin1String("ActiveConfiguration")); + if (v.isValid()) + config = configManager.configurationFromIdentifier(qvariant_cast<QString>(v)); + // Fallback to using the configuration if no active configuration + if (!config.isValid()) + config = session->configuration(); + // or unspecified configuration if that is no good either + if (!config.isValid()) + config = QNetworkConfiguration(); } + reply->proxyList = manager->queryProxy(QNetworkProxyQuery(config, url())); +#else // QT_NO_BEARERMANAGEMENT + // Without bearer management, the proxy depends only on the url + reply->proxyList = manager->queryProxy(QNetworkProxyQuery(url())); +#endif +#endif - return false; + // now start the request + open(); + return true; } -#endif QT_END_NAMESPACE diff --git a/src/network/access/qnetworkaccessmanager.cpp b/src/network/access/qnetworkaccessmanager.cpp index 0d7b52a..55d6a7c 100644 --- a/src/network/access/qnetworkaccessmanager.cpp +++ b/src/network/access/qnetworkaccessmanager.cpp @@ -986,10 +986,6 @@ QNetworkReply *QNetworkAccessManager::createRequest(QNetworkAccessManager::Opera // third step: find a backend priv->backend = d->findBackend(op, request); -#ifndef QT_NO_NETWORKPROXY - QList<QNetworkProxy> proxyList = d->queryProxy(QNetworkProxyQuery(request.url())); - priv->proxyList = proxyList; -#endif if (priv->backend) { priv->backend->setParent(reply); priv->backend->reply = priv; diff --git a/src/network/access/qnetworkreplyimpl.cpp b/src/network/access/qnetworkreplyimpl.cpp index 9eb505d..34fd7a7 100644 --- a/src/network/access/qnetworkreplyimpl.cpp +++ b/src/network/access/qnetworkreplyimpl.cpp @@ -90,10 +90,10 @@ void QNetworkReplyImplPrivate::_q_startOperation() return; } + if (!backend->start()) { #ifndef QT_NO_BEARERMANAGEMENT - if (!backend->start()) { // ### we should call that method even if bearer is not used // backend failed to start because the session state is not Connected. - // QNetworkAccessManager will call reply->backend->start() again for us when the session + // QNetworkAccessManager will call _q_startOperation again for us when the session // state changes. state = WaitingForSession; @@ -109,11 +109,20 @@ void QNetworkReplyImplPrivate::_q_startOperation() session->open(); } else { qWarning("Backend is waiting for QNetworkSession to connect, but there is none!"); + state = Working; + error(QNetworkReplyImpl::UnknownNetworkError, + QCoreApplication::translate("QNetworkReply", "Network session error.")); + finished(); } - +#else + qWarning("Backend start failed"); + state = Working; + error(QNetworkReplyImpl::UnknownNetworkError, + QCoreApplication::translate("QNetworkReply", "backend start error.")); + finished(); +#endif return; } -#endif if (backend && backend->isSynchronous()) { state = Finished; diff --git a/src/network/kernel/qnetworkproxy.cpp b/src/network/kernel/qnetworkproxy.cpp index 68ff955..14db913 100644 --- a/src/network/kernel/qnetworkproxy.cpp +++ b/src/network/kernel/qnetworkproxy.cpp @@ -228,6 +228,10 @@ #include "qmutex.h" #include "qurl.h" +#ifndef QT_NO_BEARERMANAGEMENT +#include <QtNetwork/QNetworkConfiguration> +#endif + QT_BEGIN_NAMESPACE class QSocks5SocketEngineHandler; @@ -716,6 +720,9 @@ public: QUrl remote; int localPort; QNetworkProxyQuery::QueryType type; +#ifndef QT_NO_BEARERMANAGEMENT + QNetworkConfiguration config; +#endif }; template<> void QSharedDataPointer<QNetworkProxyQueryPrivate>::detach() @@ -777,6 +784,11 @@ template<> void QSharedDataPointer<QNetworkProxyQueryPrivate>::detach() like choosing an caching HTTP proxy for HTTP-based connections, but a more powerful SOCKSv5 proxy for all others. + The network configuration specifies which configuration to use, + when bearer management is used. For example on a mobile phone + the proxy settings are likely to be different for the cellular + network vs WLAN. + Some of the criteria may not make sense in all of the types of query. The following table lists the criteria that are most commonly used, according to the type of query. @@ -902,6 +914,68 @@ QNetworkProxyQuery::QNetworkProxyQuery(quint16 bindPort, const QString &protocol d->type = queryType; } +#ifndef QT_NO_BEARERMANAGEMENT +/*! + Constructs a QNetworkProxyQuery with the URL \a requestUrl and + sets the query type to \a queryType. The specified \a networkConfiguration + is used to resolve the proxy settings. + + \sa protocolTag(), peerHostName(), peerPort(), networkConfiguration() +*/ +QNetworkProxyQuery::QNetworkProxyQuery(const QNetworkConfiguration &networkConfiguration, + const QUrl &requestUrl, QueryType queryType) +{ + d->config = networkConfiguration; + d->remote = requestUrl; + d->type = queryType; +} + +/*! + Constructs a QNetworkProxyQuery of type \a queryType and sets the + protocol tag to be \a protocolTag. This constructor is suitable + for QNetworkProxyQuery::TcpSocket queries, because it sets the + peer hostname to \a hostname and the peer's port number to \a + port. The specified \a networkConfiguration + is used to resolve the proxy settings. + + \sa networkConfiguration() +*/ +QNetworkProxyQuery::QNetworkProxyQuery(const QNetworkConfiguration &networkConfiguration, + const QString &hostname, int port, + const QString &protocolTag, + QueryType queryType) +{ + d->config = networkConfiguration; + d->remote.setScheme(protocolTag); + d->remote.setHost(hostname); + d->remote.setPort(port); + d->type = queryType; +} + +/*! + Constructs a QNetworkProxyQuery of type \a queryType and sets the + protocol tag to be \a protocolTag. This constructor is suitable + for QNetworkProxyQuery::TcpSocket queries because it sets the + local port number to \a bindPort. The specified \a networkConfiguration + is used to resolve the proxy settings. + + Note that \a bindPort is of type quint16 to indicate the exact + port number that is requested. The value of -1 (unknown) is not + allowed in this context. + + \sa localPort(), networkConfiguration() +*/ +QNetworkProxyQuery::QNetworkProxyQuery(const QNetworkConfiguration &networkConfiguration, + quint16 bindPort, const QString &protocolTag, + QueryType queryType) +{ + d->config = networkConfiguration; + d->remote.setScheme(protocolTag); + d->localPort = bindPort; + d->type = queryType; +} +#endif + /*! Constructs a QNetworkProxyQuery object that is a copy of \a other. */ @@ -1116,6 +1190,30 @@ void QNetworkProxyQuery::setUrl(const QUrl &url) d->remote = url; } +#ifndef QT_NO_BEARERMANAGEMENT +QNetworkConfiguration QNetworkProxyQuery::networkConfiguration() const +{ + return d ? d->config : QNetworkConfiguration(); +} + +/*! + Sets the network configuration component of this QNetworkProxyQuery + object to be \a networkConfiguration. The network configuration can + be used to return different proxy settings based on the network in + use, for example WLAN vs cellular networks on a mobile phone. + + In the case of "user choice" or "service network" configurations, + you should first start the QNetworkSession and obtain the active + configuration from its properties. + + \sa networkConfiguration +*/ +void QNetworkProxyQuery::setNetworkConfiguration(const QNetworkConfiguration &networkConfiguration) +{ + d->config = networkConfiguration; +} +#endif + /*! \class QNetworkProxyFactory \brief The QNetworkProxyFactory class provides fine-grained proxy selection. diff --git a/src/network/kernel/qnetworkproxy.h b/src/network/kernel/qnetworkproxy.h index 26562d5..e16b29e 100644 --- a/src/network/kernel/qnetworkproxy.h +++ b/src/network/kernel/qnetworkproxy.h @@ -54,6 +54,7 @@ QT_BEGIN_NAMESPACE QT_MODULE(Network) class QUrl; +class QNetworkConfiguration; class QNetworkProxyQueryPrivate; class Q_NETWORK_EXPORT QNetworkProxyQuery @@ -73,6 +74,16 @@ public: QNetworkProxyQuery(quint16 bindPort, const QString &protocolTag = QString(), QueryType queryType = TcpServer); QNetworkProxyQuery(const QNetworkProxyQuery &other); +#ifndef QT_NO_BEARERMANAGEMENT + QNetworkProxyQuery(const QNetworkConfiguration &networkConfiguration, + const QUrl &requestUrl, QueryType queryType = UrlRequest); + QNetworkProxyQuery(const QNetworkConfiguration &networkConfiguration, + const QString &hostname, int port, const QString &protocolTag = QString(), + QueryType queryType = TcpSocket); + QNetworkProxyQuery(const QNetworkConfiguration &networkConfiguration, + quint16 bindPort, const QString &protocolTag = QString(), + QueryType queryType = TcpServer); +#endif ~QNetworkProxyQuery(); QNetworkProxyQuery &operator=(const QNetworkProxyQuery &other); bool operator==(const QNetworkProxyQuery &other) const; @@ -97,6 +108,11 @@ public: QUrl url() const; void setUrl(const QUrl &url); +#ifndef QT_NO_BEARERMANAGEMENT + QNetworkConfiguration networkConfiguration() const; + void setNetworkConfiguration(const QNetworkConfiguration &networkConfiguration); +#endif + private: QSharedDataPointer<QNetworkProxyQueryPrivate> d; }; diff --git a/src/network/kernel/qnetworkproxy_symbian.cpp b/src/network/kernel/qnetworkproxy_symbian.cpp index 79dfb27..4ba14c0 100644 --- a/src/network/kernel/qnetworkproxy_symbian.cpp +++ b/src/network/kernel/qnetworkproxy_symbian.cpp @@ -58,6 +58,7 @@ #include <commsdattypeinfov1_1.h> // CCDIAPRecord, CCDProxiesRecord #include <commsdattypesv1_1.h> // KCDTIdIAPRecord, KCDTIdProxiesRecord #include <QtNetwork/QNetworkConfigurationManager> +#include <QtNetwork/QNetworkConfiguration> #include <QFlags> using namespace CommsDat; @@ -88,7 +89,7 @@ class SymbianProxyQuery { public: static QNetworkConfiguration findCurrentConfiguration(QNetworkConfigurationManager& configurationManager); - static SymbianIapId getIapId(QNetworkConfigurationManager& configurationManager); + static SymbianIapId getIapId(QNetworkConfigurationManager &configurationManager, const QNetworkProxyQuery &query); static CCDIAPRecord *getIapRecordLC(TUint32 aIAPId, CMDBSession &aDb); static CMDBRecordSet<CCDProxiesRecord> *prepareQueryLC(TUint32 serviceId, TDesC& serviceType); static QList<QNetworkProxy> proxyQueryL(TUint32 aIAPId, const QNetworkProxyQuery &query); @@ -137,11 +138,15 @@ QNetworkConfiguration SymbianProxyQuery::findCurrentConfiguration(QNetworkConfig return currentConfig; } -SymbianIapId SymbianProxyQuery::getIapId(QNetworkConfigurationManager& configurationManager) +SymbianIapId SymbianProxyQuery::getIapId(QNetworkConfigurationManager& configurationManager, const QNetworkProxyQuery &query) { SymbianIapId iapId; - QNetworkConfiguration currentConfig = findCurrentConfiguration(configurationManager); + QNetworkConfiguration currentConfig = query.networkConfiguration(); + if (!currentConfig.isValid()) { + //If config is not specified, then try to find out an active or default one + currentConfig = findCurrentConfiguration(configurationManager); + } if (currentConfig.isValid()) { // Note: the following code assumes that the identifier is in format // I_xxxx where xxxx is the identifier of IAP. This is meant as a @@ -249,7 +254,7 @@ QList<QNetworkProxy> QNetworkProxyFactory::systemProxyForQuery(const QNetworkPro SymbianIapId iapId; TInt error; QNetworkConfigurationManager manager; - iapId = SymbianProxyQuery::getIapId(manager); + iapId = SymbianProxyQuery::getIapId(manager, query); if (iapId.isValid()) { TRAP(error, proxies = SymbianProxyQuery::proxyQueryL(iapId.iapId(), query)) if (error != KErrNone) { diff --git a/src/network/socket/qabstractsocket.cpp b/src/network/socket/qabstractsocket.cpp index cfb1413..fc0bb85 100644 --- a/src/network/socket/qabstractsocket.cpp +++ b/src/network/socket/qabstractsocket.cpp @@ -547,15 +547,19 @@ bool QAbstractSocketPrivate::initSocketLayer(QAbstractSocket::NetworkLayerProtoc resetSocketLayer(); socketEngine = QAbstractSocketEngine::createSocketEngine(q->socketType(), proxyInUse, q); -#ifndef QT_NO_BEARERMANAGEMENT - //copy network session down to the socket engine (if it has been set) - socketEngine->setProperty("_q_networksession", q->property("_q_networksession")); -#endif if (!socketEngine) { socketError = QAbstractSocket::UnsupportedSocketOperationError; q->setErrorString(QAbstractSocket::tr("Operation on socket is not supported")); return false; } +#ifndef QT_NO_BEARERMANAGEMENT + //copy network session down to the socket engine (if it has been set) + socketEngine->setProperty("_q_networksession", q->property("_q_networksession")); +#endif +#ifndef QT_NO_NETWORKPROXY + //copy user agent to socket engine (if it has been set) + socketEngine->setProperty("_q_user-agent", q->property("_q_user-agent")); +#endif if (!socketEngine->initialize(q->socketType(), protocol)) { #if defined (QABSTRACTSOCKET_DEBUG) qDebug("QAbstractSocketPrivate::initSocketLayer(%s, %s) failed (%s)", @@ -1605,15 +1609,15 @@ bool QAbstractSocket::setSocketDescriptor(int socketDescriptor, SocketState sock d->resetSocketLayer(); d->socketEngine = QAbstractSocketEngine::createSocketEngine(socketDescriptor, this); -#ifndef QT_NO_BEARERMANAGEMENT - //copy network session down to the socket engine (if it has been set) - d->socketEngine->setProperty("_q_networksession", property("_q_networksession")); -#endif if (!d->socketEngine) { d->socketError = UnsupportedSocketOperationError; setErrorString(tr("Operation on socket is not supported")); return false; } +#ifndef QT_NO_BEARERMANAGEMENT + //copy network session down to the socket engine (if it has been set) + d->socketEngine->setProperty("_q_networksession", property("_q_networksession")); +#endif bool result = d->socketEngine->initialize(socketDescriptor, socketState); if (!result) { d->socketError = d->socketEngine->error(); diff --git a/src/network/socket/qhttpsocketengine.cpp b/src/network/socket/qhttpsocketengine.cpp index 7846056..a8a11a7 100644 --- a/src/network/socket/qhttpsocketengine.cpp +++ b/src/network/socket/qhttpsocketengine.cpp @@ -501,7 +501,13 @@ void QHttpSocketEngine::slotSocketConnected() data += path; data += " HTTP/1.1\r\n"; data += "Proxy-Connection: keep-alive\r\n" - "User-Agent: Mozilla/5.0\r\n" + "User-Agent: "; + QVariant v = property("_q_user-agent"); + if (v.isValid()) + data += v.toByteArray(); + else + data += "Mozilla/5.0"; + data += "\r\n" "Host: " + peerAddress + "\r\n"; QAuthenticatorPrivate *priv = QAuthenticatorPrivate::getPrivate(d->authenticator); //qDebug() << "slotSocketConnected: priv=" << priv << (priv ? (int)priv->method : -1); diff --git a/src/network/socket/qsocks5socketengine.cpp b/src/network/socket/qsocks5socketengine.cpp index c365635..88b5aca 100644 --- a/src/network/socket/qsocks5socketengine.cpp +++ b/src/network/socket/qsocks5socketengine.cpp @@ -1540,8 +1540,13 @@ qint64 QSocks5SocketEngine::write(const char *data, qint64 len) // ### Handle this error. } - d->data->controlSocket->write(sealedBuf); + qint64 written = d->data->controlSocket->write(sealedBuf); + if (written <= 0) { + QSOCKS5_Q_DEBUG << "native write returned" << written; + return written; + } d->data->controlSocket->waitForBytesWritten(0); + //NB: returning len rather than written for the OK case, because the "sealing" may increase the length return len; #ifndef QT_NO_UDPSOCKET } else if (d->mode == QSocks5SocketEnginePrivate::UdpAssociateMode) { diff --git a/src/network/socket/qsymbiansocketengine.cpp b/src/network/socket/qsymbiansocketengine.cpp index f1b2982..b6d12fe 100644 --- a/src/network/socket/qsymbiansocketengine.cpp +++ b/src/network/socket/qsymbiansocketengine.cpp @@ -251,7 +251,8 @@ QSymbianSocketEnginePrivate::QSymbianSocketEnginePrivate() : readNotificationsEnabled(false), writeNotificationsEnabled(false), exceptNotificationsEnabled(false), - asyncSelect(0) + asyncSelect(0), + hasReceivedBufferedDatagram(false) { } @@ -781,21 +782,34 @@ qint64 QSymbianSocketEngine::pendingDatagramSize() const Q_D(const QSymbianSocketEngine); Q_CHECK_VALID_SOCKETLAYER(QSymbianSocketEngine::pendingDatagramSize(), false); Q_CHECK_TYPE(QSymbianSocketEngine::hasPendingDatagrams(), QAbstractSocket::UdpSocket, false); - int nbytes; + //can only buffer one datagram at a time + if (d->hasReceivedBufferedDatagram) + return d->receivedDataBuffer.size(); + int nbytes = 0; TInt err = d->nativeSocket.GetOpt(KSOReadBytesPending,KSOLSocket, nbytes); if (nbytes > 0) { //nbytes includes IP header, which is of variable length (IPv4 with or without options, IPv6...) - QByteArray next(nbytes,0); - TPtr8 buffer((TUint8*)next.data(), next.size()); + //therefore read the datagram into a buffer to find its true size + d->receivedDataBuffer.resize(nbytes); + TPtr8 buffer((TUint8*)d->receivedDataBuffer.data(), nbytes); + //nbytes = size including IP header, buffer is a pointer descriptor backed by the receivedDataBuffer TInetAddr addr; TRequestStatus status; - //TODO: rather than peek, should we save this for next call to readDatagram? - //what if calls don't match though? - d->nativeSocket.RecvFrom(buffer, addr, KSockReadPeek, status); + //RecvFrom copies only the payload (we don't want the header so don't specify the option to retrieve it) + d->nativeSocket.RecvFrom(buffer, addr, 0, status); User::WaitForRequest(status); - if (status.Int()) + if (status != KErrNone) { + d->receivedDataBuffer.clear(); return 0; - return buffer.Length(); + } + nbytes = buffer.Length(); + //nbytes = size of payload, resize the receivedDataBuffer to the final size + d->receivedDataBuffer.resize(nbytes); + d->hasReceivedBufferedDatagram = true; + //now receivedDataBuffer contains one datagram, which has been removed from the socket's internal buffer +#if defined (QNATIVESOCKETENGINE_DEBUG) + qDebug() << "QSymbianSocketEngine::pendingDatagramSize buffering" << nbytes << "bytes"; +#endif } return qint64(nbytes); } @@ -807,6 +821,19 @@ qint64 QSymbianSocketEngine::readDatagram(char *data, qint64 maxSize, Q_D(QSymbianSocketEngine); Q_CHECK_VALID_SOCKETLAYER(QSymbianSocketEngine::readDatagram(), -1); Q_CHECK_TYPE(QSymbianSocketEngine::readDatagram(), QAbstractSocket::UdpSocket, false); + + // if a datagram was buffered in pendingDatagramSize(), return it now + if (d->hasReceivedBufferedDatagram) { + qint64 size = qMin(maxSize, (qint64)d->receivedDataBuffer.size()); + memcpy(data, d->receivedDataBuffer.constData(), size); + d->receivedDataBuffer.clear(); + d->hasReceivedBufferedDatagram = false; +#if defined (QNATIVESOCKETENGINE_DEBUG) + qDebug() << "QSymbianSocketEngine::readDatagram returning" << size << "bytes from buffer"; +#endif + return size; + } + TPtr8 buffer((TUint8*)data, (int)maxSize); TInetAddr addr; TRequestStatus status; @@ -985,6 +1012,9 @@ void QSymbianSocketEngine::close() d->localAddress.clear(); d->peerPort = 0; d->peerAddress.clear(); + + d->hasReceivedBufferedDatagram = false; + d->receivedDataBuffer.clear(); } qint64 QSymbianSocketEngine::write(const char *data, qint64 len) @@ -1036,6 +1066,18 @@ qint64 QSymbianSocketEngine::read(char *data, qint64 maxSize) Q_CHECK_VALID_SOCKETLAYER(QSymbianSocketEngine::read(), -1); Q_CHECK_STATES(QSymbianSocketEngine::read(), QAbstractSocket::ConnectedState, QAbstractSocket::BoundState, -1); + // if a datagram was buffered in pendingDatagramSize(), return it now + if (d->hasReceivedBufferedDatagram) { + qint64 size = qMin(maxSize, (qint64)d->receivedDataBuffer.size()); + memcpy(data, d->receivedDataBuffer.constData(), size); + d->receivedDataBuffer.clear(); + d->hasReceivedBufferedDatagram = false; +#if defined (QNATIVESOCKETENGINE_DEBUG) + qDebug() << "QSymbianSocketEngine::read returning" << size << "bytes from buffer"; +#endif + return size; + } + TPtr8 buffer((TUint8*)data, (int)maxSize); TSockXfrLength received = 0; TRequestStatus status; @@ -1440,6 +1482,7 @@ void QSymbianSocketEngine::startNotifications() qDebug() << "QSymbianSocketEngine::startNotifications" << d->readNotificationsEnabled << d->writeNotificationsEnabled << d->exceptNotificationsEnabled; #endif if (!d->asyncSelect && (d->readNotificationsEnabled || d->writeNotificationsEnabled || d->exceptNotificationsEnabled)) { + Q_CHECK_VALID_SOCKETLAYER(QSymbianSocketEngine::startNotifications(), Q_VOID); if (d->threadData->eventDispatcher) { d->asyncSelect = q_check_ptr(new QAsyncSelect( static_cast<QEventDispatcherSymbian*> (d->threadData->eventDispatcher), d->nativeSocket, @@ -1456,14 +1499,12 @@ void QSymbianSocketEngine::startNotifications() bool QSymbianSocketEngine::isReadNotificationEnabled() const { Q_D(const QSymbianSocketEngine); - Q_CHECK_VALID_SOCKETLAYER(QSymbianSocketEngine::isReadNotificationEnabled(), false); return d->readNotificationsEnabled; } void QSymbianSocketEngine::setReadNotificationEnabled(bool enable) { Q_D(QSymbianSocketEngine); - Q_CHECK_VALID_SOCKETLAYER(QSymbianSocketEngine::setReadNotificationEnabled(), Q_VOID); #ifdef QNATIVESOCKETENGINE_DEBUG qDebug() << "QSymbianSocketEngine::setReadNotificationEnabled" << enable << "socket" << d->socketDescriptor; #endif @@ -1474,14 +1515,12 @@ void QSymbianSocketEngine::setReadNotificationEnabled(bool enable) bool QSymbianSocketEngine::isWriteNotificationEnabled() const { Q_D(const QSymbianSocketEngine); - Q_CHECK_VALID_SOCKETLAYER(QSymbianSocketEngine::isWriteNotificationEnabled(), false); return d->writeNotificationsEnabled; } void QSymbianSocketEngine::setWriteNotificationEnabled(bool enable) { Q_D(QSymbianSocketEngine); - Q_CHECK_VALID_SOCKETLAYER(QSymbianSocketEngine::setWriteNotificationEnabled(), Q_VOID); #ifdef QNATIVESOCKETENGINE_DEBUG qDebug() << "QSymbianSocketEngine::setWriteNotificationEnabled" << enable << "socket" << d->socketDescriptor; #endif @@ -1492,7 +1531,6 @@ void QSymbianSocketEngine::setWriteNotificationEnabled(bool enable) bool QSymbianSocketEngine::isExceptionNotificationEnabled() const { Q_D(const QSymbianSocketEngine); - Q_CHECK_VALID_SOCKETLAYER(QSymbianSocketEngine::isExceptionNotificationEnabled(), false); return d->exceptNotificationsEnabled; return false; } @@ -1500,7 +1538,6 @@ bool QSymbianSocketEngine::isExceptionNotificationEnabled() const void QSymbianSocketEngine::setExceptionNotificationEnabled(bool enable) { Q_D(QSymbianSocketEngine); - Q_CHECK_VALID_SOCKETLAYER(QSymbianSocketEngine::setExceptionNotificationEnabled(), Q_VOID); #ifdef QNATIVESOCKETENGINE_DEBUG qDebug() << "QSymbianSocketEngine::setExceptionNotificationEnabled" << enable << "socket" << d->socketDescriptor; #endif @@ -1660,6 +1697,9 @@ void QAsyncSelect::run() //when event loop disabled socket events, defer until later if (maybeDeferSocketEvent()) return; +#if defined (QNATIVESOCKETENGINE_DEBUG) + qDebug() << "QAsyncSelect::run" << m_selectBuf() << m_selectFlags; +#endif m_inSocketEvent = true; m_selectBuf() &= m_selectFlags; //the select ioctl reports everything, so mask to only what we requested //KSockSelectReadContinuation is for reading datagrams in a mode that doesn't discard when the diff --git a/src/network/socket/qsymbiansocketengine_p.h b/src/network/socket/qsymbiansocketengine_p.h index 85ab54a..2e7c155 100644 --- a/src/network/socket/qsymbiansocketengine_p.h +++ b/src/network/socket/qsymbiansocketengine_p.h @@ -196,6 +196,8 @@ public: bool exceptNotificationsEnabled; QAsyncSelect* asyncSelect; + mutable QByteArray receivedDataBuffer; + mutable bool hasReceivedBufferedDatagram; // FIXME this is duplicated from qnativesocketengine_p.h enum ErrorString { NonBlockingInitFailedErrorString, diff --git a/src/network/socket/qtcpserver.cpp b/src/network/socket/qtcpserver.cpp index 5a60764..026ceb4 100644 --- a/src/network/socket/qtcpserver.cpp +++ b/src/network/socket/qtcpserver.cpp @@ -416,6 +416,11 @@ bool QTcpServer::setSocketDescriptor(int socketDescriptor) if (d->socketEngine) delete d->socketEngine; d->socketEngine = QAbstractSocketEngine::createSocketEngine(socketDescriptor, this); + if (!d->socketEngine) { + d->serverSocketError = QAbstractSocket::UnsupportedSocketOperationError; + d->serverSocketErrorString = tr("Operation on socket is not supported"); + return false; + } #ifndef QT_NO_BEARERMANAGEMENT //copy network session down to the socket engine (if it has been set) d->socketEngine->setProperty("_q_networksession", property("_q_networksession")); diff --git a/src/network/ssl/qsslsocket.cpp b/src/network/ssl/qsslsocket.cpp index 0dbf4b5..2695df1 100644 --- a/src/network/ssl/qsslsocket.cpp +++ b/src/network/ssl/qsslsocket.cpp @@ -1736,6 +1736,8 @@ void QSslSocket::connectToHostImplementation(const QString &hostName, quint16 po } #ifndef QT_NO_NETWORKPROXY d->plainSocket->setProxy(proxy()); + //copy user agent down to the plain socket (if it has been set) + d->plainSocket->setProperty("_q_user-agent", property("_q_user-agent")); #endif QIODevice::open(openMode); d->plainSocket->connectToHost(hostName, port, openMode); |