25 #include "qgsconfig.h" 27 #include <QApplication> 43 QNetworkProxy::ProxyType proxyType,
50 , httphost( proxyHost )
51 , httpredirections( 0 )
52 , mWatchdogTimer( nullptr )
54 Q_UNUSED( proxyPort );
55 Q_UNUSED( proxyUser );
56 Q_UNUSED( proxyPass );
57 Q_UNUSED( proxyType );
61 mNetworkTimeoutMsec = s.
value(
"/qgis/networkAndProxy/networkTimeout",
"60000" ).
toInt();
68 , httpredirections( 0 )
69 , mWatchdogTimer(
nullptr )
72 mNetworkTimeoutMsec = s.
value(
"/qgis/networkAndProxy/networkTimeout",
"60000" ).
toInt();
96 httpredirections = redirections;
100 QgsDebugMsg(
"Creds: " + mUserName +
'/' + mPassword );
104 QUrl qurl( httpurl );
116 header.setValue(
"Host",
QString(
"%1:%2" ).arg( qurl.
host() ).arg( qurl.
port() ) );
119 header.setValue(
"User-agent",
QString(
"QGIS - " ) + VERSION );
126 http->
setUser( mUserName, mPassword );
131 httphost = qurl.
host();
138 httphost = settings.
value(
"proxy/proxyHost",
"" ).
toString();
144 mWatchdogTimer =
new QTimer(
this );
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 ) ),
183 connect( http, SIGNAL( requestFinished(
int,
bool ) ),
186 connect( http, SIGNAL( done(
bool ) ),
189 connect( http, SIGNAL( stateChanged(
int ) ),
193 connect( mWatchdogTimer, SIGNAL( timeout() ),
197 mWatchdogTimer->
start( mNetworkTimeoutMsec );
208 qApp->processEvents();
224 QgsDebugMsg(
"Processing an error '" + mError +
"'." );
231 if ( !httpredirecturl.
isEmpty() )
233 QgsDebugMsg(
"Starting get of '" + httpredirecturl +
"'." );
249 respondedContent = httpresponse;
257 return httpresponsecontenttype;
272 resp.
value(
"Content-Type" ) +
"'." );
275 mWatchdogTimer->
start( mNetworkTimeoutMsec );
281 httpredirecturl = resp.
value(
"Location" );
289 mError =
tr(
"WMS Server responded unexpectedly with HTTP Status Code %1 (%2)" )
294 httpresponsecontenttype = resp.
value(
"Content-Type" );
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 );
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();
418 mError =
tr(
"HTTP transaction completed, however there was an error: %1" ).
arg( http->
errorString() );
429 httpresponse = http->
readAll();
441 mWatchdogTimer->
start( mNetworkTimeoutMsec );
445 case QHttp::Unconnected:
446 QgsDebugMsg(
"There is no connection to the host." );
450 case QHttp::HostLookup:
451 QgsDebugMsg(
"A host name lookup is in progress." );
456 case QHttp::Connecting:
457 QgsDebugMsg(
"An attempt to connect to the host is in progress." );
463 QgsDebugMsg(
"The client is sending its request to the server." );
469 QgsDebugMsg(
"The client's request has been sent and the client is reading the server's response." );
474 case QHttp::Connected:
475 QgsDebugMsg(
"The connection to the host is open, but the client is neither sending a request, nor waiting for a response." );
481 QgsDebugMsg(
"The connection is closing down, but is not yet closed. (The state will be Unconnected when the connection is closed.)" );
492 mError =
tr(
"Network timed out after %n second(s) of inactivity.\n" 493 "This may be a problem in your network connection or at the WMS server.",
"inactivity timeout", mNetworkTimeoutMsec / 1000 );
510 bool proxyEnabled = settings.
value(
"proxy/proxyEnabled",
false ).
toBool();
518 if ( !proxyExcludedURLs.
isEmpty() )
522 for ( ; exclIt != excludedURLs.
constEnd(); ++exclIt )
538 QNetworkProxy::ProxyType proxyType = QNetworkProxy::NoProxy;
539 if ( proxyTypeString ==
"DefaultProxy" )
541 proxyType = QNetworkProxy::DefaultProxy;
543 else if ( proxyTypeString ==
"Socks5Proxy" )
545 proxyType = QNetworkProxy::Socks5Proxy;
547 else if ( proxyTypeString ==
"HttpProxy" )
549 proxyType = QNetworkProxy::HttpProxy;
551 else if ( proxyTypeString ==
"HttpCachingProxy" )
553 proxyType = QNetworkProxy::HttpCachingProxy;
555 else if ( proxyTypeString ==
"FtpCachingProxy" )
557 proxyType = QNetworkProxy::FtpCachingProxy;
int indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
QString errorString() const
QgsHttpTransaction(const QString &uri, const QString &proxyHost=QString(), int proxyPort=80, const QString &proxyUser=QString(), const QString &proxyPass=QString(), QNetworkProxy::ProxyType proxyType=QNetworkProxy::NoProxy, const QString &userName=QString(), const QString &password=QString())
Constructor.
bool getSynchronously(QByteArray &respondedContent, int redirections=0, const QByteArray *postData=nullptr)
Gets the response synchronously.
QStringList split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
QString errorString()
If an operation returns 0 (e.g.
qint64 readBlock(char *data, quint64 maxlen)
void dataReadProgress(int theProgress)
Signal for progress update.
QString & remove(int position, int n)
QString tr(const char *sourceText, const char *disambiguation, int n)
int request(const QHttpRequestHeader &header, QIODevice *data, QIODevice *to)
QString number(int n, int base)
void setCredentials(const QString &username, const QString &password)
Set the credentials (username and password)
int toInt(bool *ok) const
void dataProgress(int done, int total)
int setProxy(const QString &host, int port, const QString &username, const QString &password)
int toInt(bool *ok, int base) const
int setHost(const QString &hostName, quint16 port)
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
void totalSteps(int theTotalSteps)
Signal for adjusted number of steps.
int setUser(const QString &userName, const QString &password)
QString responseContentType()
void dataFinished(int id, bool error)
QByteArray & append(char ch)
virtual ~QgsHttpTransaction()
Destructor.
void dataStateChanged(int state)
QVariant value(const QString &key, const QVariant &defaultValue) const
HTTP request/response manager that is redirect-aware.
void statusChanged(const QString &theStatusQString)
emit a signal to be caught by qgisapp and display a msg on status bar
qint64 bytesAvailable() const
const_iterator constEnd() const
void abort()
Aborts the current transaction.
const_iterator constBegin() const
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void dataReceived(const QHttpResponseHeader &resp)
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
void transactionFinished(bool error)
void dataHeaderReceived(const QHttpResponseHeader &resp)
static int HTTP_PORT_DEFAULT
static bool applyProxySettings(QHttp &http, const QString &url)
Apply proxy settings from QSettings to a http object.
void setSingleShot(bool singleShot)