26 , mFeedback( feedback )
27 , mThread( QThread::currentThread() )
29 CPLHTTPPushFetchCallback( QgsCPLHTTPFetchOverrider::callback,
this );
34 CPLHTTPPopFetchCallback();
38CPLHTTPResult *QgsCPLHTTPFetchOverrider::callback(
const char *pszURL,
39 CSLConstList papszOptions,
42 CPLHTTPFetchWriteFunc pfnWrite,
48 auto psResult =
static_cast<CPLHTTPResult *
>( CPLCalloc(
sizeof( CPLHTTPResult ), 1 ) );
49 if ( CSLFetchNameValue( papszOptions,
"CLOSE_PERSISTENT" ) )
58 for (
const char *pszOption : {
"FORM_FILE_PATH",
"FORM_ITEM_COUNT" } )
60 if ( CSLFetchNameValue( papszOptions, pszOption ) )
62 QgsDebugError( QStringLiteral(
"Option %1 not handled" ).arg( pszOption ) );
67 if ( pThis->mFeedback && pThis->mFeedback->
isCanceled() )
69 psResult->nStatus = 1;
70 psResult->pszErrBuf = CPLStrdup(
"download interrupted by user" );
74 QgsBlockingNetworkRequest blockingRequest;
77 QNetworkRequest request( QString::fromUtf8( pszURL ) );
78 for (
const auto &keyValue : pThis->mAttributes )
80 request.setAttribute( keyValue.first, keyValue.second );
84 const char *pszHeaders = CSLFetchNameValue( papszOptions,
"HEADERS" );
87 char **papszTokensHeaders = CSLTokenizeString2( pszHeaders,
"\r\n", 0 );
88 for (
int i = 0; papszTokensHeaders[i] !=
nullptr; ++i )
90 char *pszKey =
nullptr;
91 const char *pszValue = CPLParseNameValue( papszTokensHeaders[i], &pszKey );
92 if ( pszKey && pszValue )
95 QByteArray::fromStdString( pszKey ),
96 QByteArray::fromStdString( pszValue ) );
100 CSLDestroy( papszTokensHeaders );
103 constexpr bool forceRefresh =
true;
104 const char *pszCustomRequest = CSLFetchNameValue( papszOptions,
"CUSTOMREQUEST" );
105 const char *pszPostFields = CSLFetchNameValue( papszOptions,
"POSTFIELDS" );
109 if ( !pszCustomRequest || EQUAL( pszCustomRequest,
"POST" ) )
111 errCode = blockingRequest.
post( request,
112 QByteArray::fromStdString( pszPostFields ),
116 else if ( EQUAL( pszCustomRequest,
"PUT" ) )
118 errCode = blockingRequest.
put( request,
119 QByteArray::fromStdString( pszPostFields ),
124 QgsDebugError( QStringLiteral(
"Invalid CUSTOMREQUEST = %1 when POSTFIELDS is defined" ).arg( pszCustomRequest ) );
130 if ( !pszCustomRequest || EQUAL( pszCustomRequest,
"GET" ) )
134 else if ( EQUAL( pszCustomRequest,
"HEAD" ) )
136 errCode = blockingRequest.
head( request, forceRefresh, pThis->mFeedback );
138 else if ( EQUAL( pszCustomRequest,
"DELETE" ) )
140 errCode = blockingRequest.
deleteResource( request, pThis->mFeedback );
144 QgsDebugError( QStringLiteral(
"Invalid CUSTOMREQUEST = %1 when POSTFIELDS is not defined" ).arg( pszCustomRequest ) );
150 psResult->nStatus = 1;
151 psResult->pszErrBuf = CPLStrdup( blockingRequest.
errorMessage().toUtf8() );
155 const QgsNetworkReplyContent reply( blockingRequest.
reply() );
158 for (
const auto &pair : reply.rawHeaderPairs() )
160 if ( EQUAL( pair.first.toStdString().c_str(),
"Content-Type" ) )
162 CPLFree( psResult->pszContentType );
163 psResult->pszContentType = CPLStrdup( pair.second.toStdString().c_str() );
165 psResult->papszHeaders = CSLAddNameValue(
166 psResult->papszHeaders,
167 pair.first.toStdString().c_str(),
168 pair.second.toStdString().c_str() );
172 QByteArray content( reply.content() );
178 if (
static_cast<int>( pfnWrite( content.data(), 1, content.size(), pWriteArg ) ) != content.size() )
180 psResult->nStatus = 1;
181 psResult->pszErrBuf = CPLStrdup(
"download interrupted by user" );
187 psResult->nDataLen =
static_cast<int>( content.size() );
188 psResult->pabyData =
static_cast<GByte *
>( CPLMalloc( psResult->nDataLen + 1 ) );
189 memcpy( psResult->pabyData, content.constData(), psResult->nDataLen );
190 psResult->pabyData[psResult->nDataLen] = 0;
198 mAttributes[code] = value;
203 mFeedback = feedback;
ErrorCode put(QNetworkRequest &request, QIODevice *data, QgsFeedback *feedback=nullptr)
Performs a "put" operation on the specified request, using the given data.
ErrorCode head(QNetworkRequest &request, bool forceRefresh=false, QgsFeedback *feedback=nullptr)
Performs a "head" operation on the specified request.
ErrorCode post(QNetworkRequest &request, QIODevice *data, bool forceRefresh=false, QgsFeedback *feedback=nullptr)
Performs a "post" operation on the specified request, using the given data.
ErrorCode deleteResource(QNetworkRequest &request, QgsFeedback *feedback=nullptr)
Performs a "delete" operation on the specified request.
void setAuthCfg(const QString &authCfg)
Sets the authentication config id which should be used during the request.
QString errorMessage() const
Returns the error message string, after a get(), post(), head() or put() request has been made.
ErrorCode get(QNetworkRequest &request, bool forceRefresh=false, QgsFeedback *feedback=nullptr, RequestFlags requestFlags=QgsBlockingNetworkRequest::RequestFlags())
Performs a "get" operation on the specified request.
@ EmptyResponseIsValid
Do not generate an error if getting an empty response (e.g. HTTP 204).
@ NoError
No error was encountered.
QgsNetworkReplyContent reply() const
Returns the content of the network reply, after a get(), post(), head() or put() request has been mad...
Utility class to redirect GDAL's CPL HTTP calls through QgsBlockingNetworkRequest.
QgsCPLHTTPFetchOverrider(const QString &authCfg=QString(), QgsFeedback *feedback=nullptr)
Installs the redirection for the current thread.
void setAttribute(QNetworkRequest::Attribute code, const QVariant &value)
Define attribute that must be forwarded to the actual QNetworkRequest.
QThread * thread() const
Returns the thread associated with the overrider.
void setFeedback(QgsFeedback *feedback)
Sets the feedback cancellation object for the redirection.
~QgsCPLHTTPFetchOverrider()
Base class for feedback objects to be used for cancellation of something running in a worker thread.
bool isCanceled() const
Tells whether the operation has been canceled already.
#define QgsDebugError(str)