25 #include "qgsconfig.h"
27 #include <QApplication>
43 QNetworkProxy::ProxyType proxyType,
50 , httphost( proxyHost )
51 , httpredirections( 0 )
52 , mWatchdogTimer( NULL )
54 Q_UNUSED( proxyPort );
55 Q_UNUSED( proxyUser );
56 Q_UNUSED( proxyPass );
57 Q_UNUSED( proxyType );
61 mNetworkTimeoutMsec = s.value(
"/qgis/networkAndProxy/networkTimeout",
"20000" ).toInt();
64 QgsHttpTransaction::QgsHttpTransaction()
68 , httpredirections( 0 )
69 , mWatchdogTimer( NULL )
72 mNetworkTimeoutMsec = s.value(
"/qgis/networkAndProxy/networkTimeout",
"20000" ).toInt();
96 httpredirections = redirections;
100 QgsDebugMsg(
"Creds: " + mUserName +
"/" + mPassword );
104 QUrl qurl( httpurl );
108 QHttpRequestHeader header(
"GET", qurl.host() );
112 header.setValue(
"Host", qurl.host() );
116 header.setValue(
"Host", QString(
"%1:%2" ).arg( qurl.host() ).arg( qurl.port() ) );
119 header.setValue(
"User-agent", QString(
"QGIS - " ) + VERSION );
124 if ( !mUserName.isEmpty() && !mPassword.isEmpty() )
126 http->setUser( mUserName, mPassword );
131 httphost = qurl.host();
138 httphost = settings.value(
"proxy/proxyHost",
"" ).toString();
139 httpport = settings.value(
"proxy/proxyPort",
"" ).toString().toInt();
144 mWatchdogTimer =
new QTimer(
this );
146 QgsDebugMsg(
"qurl.host() is '" + qurl.host() +
"'." );
148 httpresponse.truncate( 0 );
156 QString pathAndQuery = httpurl.remove( 0, httpurl.indexOf( qurl.host() ) );
157 pathAndQuery = httpurl.remove( 0, pathAndQuery.indexOf( qurl.path() ) );
160 header.setRequest(
"GET", pathAndQuery );
162 httpid = http->request( header );
166 header.setRequest(
"POST", pathAndQuery );
168 httpid = http->request( header, *postData );
171 connect( http, SIGNAL( requestStarted(
int ) ),
174 connect( http, SIGNAL( responseHeaderReceived(
const QHttpResponseHeader& ) ),
177 connect( http, SIGNAL( readyRead(
const QHttpResponseHeader& ) ),
178 this, SLOT(
dataReceived(
const QHttpResponseHeader& ) ) );
183 connect( http, SIGNAL( requestFinished(
int,
bool ) ),
186 connect( http, SIGNAL( done(
bool ) ),
189 connect( http, SIGNAL( stateChanged(
int ) ),
193 connect( mWatchdogTimer, SIGNAL( timeout() ),
196 mWatchdogTimer->setSingleShot(
true );
197 mWatchdogTimer->start( mNetworkTimeoutMsec );
199 QgsDebugMsg(
"Starting get with id " + QString::number( httpid ) +
"." );
208 qApp->processEvents();
222 if ( !mError.isEmpty() )
224 QgsDebugMsg(
"Processing an error '" + mError +
"'." );
231 if ( !httpredirecturl.isEmpty() )
233 QgsDebugMsg(
"Starting get of '" + httpredirecturl +
"'." );
249 respondedContent = httpresponse;
257 return httpresponsecontenttype;
264 QgsDebugMsg(
"ID=" + QString::number(
id ) +
"." );
271 QString::number( resp.statusCode() ) +
", reason '" + resp.reasonPhrase() +
"', content type: '" +
272 resp.value(
"Content-Type" ) +
"'." );
275 mWatchdogTimer->start( mNetworkTimeoutMsec );
277 if ( resp.statusCode() == 302 )
281 httpredirecturl = resp.value(
"Location" );
283 else if ( resp.statusCode() == 200 )
289 mError =
tr(
"WMS Server responded unexpectedly with HTTP Status Code %1 (%2)" )
290 .arg( resp.statusCode() )
291 .arg( resp.reasonPhrase() );
294 httpresponsecontenttype = resp.value(
"Content-Type" );
308 if ( 0 < http->readBlock( temp, http->bytesAvailable() ) )
310 httpresponse.append( temp );
323 mWatchdogTimer->start( mNetworkTimeoutMsec );
332 status =
tr(
"Received %1 of %2 bytes" ).arg( done ).arg( total );
336 status =
tr(
"Received %1 bytes (total unknown)" ).arg( done );
346 QgsDebugMsg(
"ID=" + QString::number(
id ) +
"." );
359 QgsDebugMsg(
"http activity loop already false." );
368 mError =
tr(
"HTTP response completed, however there was an error: %1" ).arg( http->errorString() );
384 httpresponse = http->readAll();
419 mError =
tr(
"HTTP transaction completed, however there was an error: %1" ).arg( http->errorString() );
430 httpresponse = http->readAll();
439 QgsDebugMsg(
"state " + QString::number( state ) +
"." );
442 mWatchdogTimer->start( mNetworkTimeoutMsec );
446 case QHttp::Unconnected:
447 QgsDebugMsg(
"There is no connection to the host." );
451 case QHttp::HostLookup:
452 QgsDebugMsg(
"A host name lookup is in progress." );
457 case QHttp::Connecting:
458 QgsDebugMsg(
"An attempt to connect to the host is in progress." );
464 QgsDebugMsg(
"The client is sending its request to the server." );
470 QgsDebugMsg(
"The client's request has been sent and the client is reading the server's response." );
475 case QHttp::Connected:
476 QgsDebugMsg(
"The connection to the host is open, but the client is neither sending a request, nor waiting for a response." );
482 QgsDebugMsg(
"The connection is closing down, but is not yet closed. (The state will be Unconnected when the connection is closed.)" );
494 mError =
tr(
"Network timed out after %n second(s) of inactivity.\n"
495 "This may be a problem in your network connection or at the WMS server.",
"inactivity timeout", mNetworkTimeoutMsec / 1000 );
512 bool proxyEnabled = settings.value(
"proxy/proxyEnabled",
false ).toBool();
519 QString proxyExcludedURLs = settings.value(
"proxy/proxyExcludedUrls",
"" ).toString();
520 if ( !proxyExcludedURLs.isEmpty() )
522 QStringList excludedURLs = proxyExcludedURLs.split(
"|" );
523 QStringList::const_iterator exclIt = excludedURLs.constBegin();
524 for ( ; exclIt != excludedURLs.constEnd(); ++exclIt )
526 if ( url.startsWith( *exclIt ) )
534 QString proxyHost = settings.value(
"proxy/proxyHost",
"" ).toString();
535 int proxyPort = settings.value(
"proxy/proxyPort",
"" ).toString().toInt();
536 QString proxyUser = settings.value(
"proxy/proxyUser",
"" ).toString();
537 QString proxyPassword = settings.value(
"proxy/proxyPassword",
"" ).toString();
539 QString proxyTypeString = settings.value(
"proxy/proxyType",
"" ).toString();
540 QNetworkProxy::ProxyType proxyType = QNetworkProxy::NoProxy;
541 if ( proxyTypeString ==
"DefaultProxy" )
543 proxyType = QNetworkProxy::DefaultProxy;
545 else if ( proxyTypeString ==
"Socks5Proxy" )
547 proxyType = QNetworkProxy::Socks5Proxy;
549 else if ( proxyTypeString ==
"HttpProxy" )
551 proxyType = QNetworkProxy::HttpProxy;
553 else if ( proxyTypeString ==
"HttpCachingProxy" )
555 proxyType = QNetworkProxy::HttpCachingProxy;
557 else if ( proxyTypeString ==
"FtpCachingProxy" )
559 proxyType = QNetworkProxy::FtpCachingProxy;
561 http.setProxy( QNetworkProxy( proxyType, proxyHost, proxyPort, proxyUser, proxyPassword ) );