25 , mFeedback( feedback )
26 , mThread( QThread::currentThread() )
28 CPLHTTPPushFetchCallback( QgsCPLHTTPFetchOverrider::callback,
this );
33 CPLHTTPPopFetchCallback();
37CPLHTTPResult *QgsCPLHTTPFetchOverrider::callback(
const char *pszURL,
38 CSLConstList papszOptions,
41 CPLHTTPFetchWriteFunc pfnWrite,
47 auto psResult =
static_cast<CPLHTTPResult *
>( CPLCalloc(
sizeof( CPLHTTPResult ), 1 ) );
48 if ( CSLFetchNameValue( papszOptions,
"CLOSE_PERSISTENT" ) )
57 for (
const char *pszOption : {
"FORM_FILE_PATH",
"FORM_ITEM_COUNT" } )
59 if ( CSLFetchNameValue( papszOptions, pszOption ) )
61 QgsDebugError( QStringLiteral(
"Option %1 not handled" ).arg( pszOption ) );
69 QNetworkRequest request( QString::fromUtf8( pszURL ) );
70 for (
const auto &keyValue : pThis->mAttributes )
72 request.setAttribute( keyValue.first, keyValue.second );
76 const char *pszHeaders = CSLFetchNameValue( papszOptions,
"HEADERS" );
79 char **papszTokensHeaders = CSLTokenizeString2( pszHeaders,
"\r\n", 0 );
80 for (
int i = 0; papszTokensHeaders[i] !=
nullptr; ++i )
82 char *pszKey =
nullptr;
83 const char *pszValue = CPLParseNameValue( papszTokensHeaders[i], &pszKey );
84 if ( pszKey && pszValue )
87 QByteArray::fromStdString( pszKey ),
88 QByteArray::fromStdString( pszValue ) );
92 CSLDestroy( papszTokensHeaders );
95 constexpr bool forceRefresh =
true;
96 const char *pszCustomRequest = CSLFetchNameValue( papszOptions,
"CUSTOMREQUEST" );
97 const char *pszPostFields = CSLFetchNameValue( papszOptions,
"POSTFIELDS" );
101 if ( pszCustomRequest ==
nullptr || EQUAL( pszCustomRequest,
"POST" ) )
103 errCode = blockingRequest.
post( request,
104 QByteArray::fromStdString( pszPostFields ),
108 else if ( EQUAL( pszCustomRequest,
"PUT" ) )
110 errCode = blockingRequest.
put( request,
111 QByteArray::fromStdString( pszPostFields ),
116 QgsDebugError( QStringLiteral(
"Invalid CUSTOMREQUEST = %1 when POSTFIELDS is defined" ).arg( pszCustomRequest ) );
122 if ( pszCustomRequest ==
nullptr || EQUAL( pszCustomRequest,
"GET" ) )
124 errCode = blockingRequest.
get( request, forceRefresh, pThis->mFeedback );
126 else if ( EQUAL( pszCustomRequest,
"HEAD" ) )
128 errCode = blockingRequest.
head( request, forceRefresh, pThis->mFeedback );
130 else if ( EQUAL( pszCustomRequest,
"DELETE" ) )
132 errCode = blockingRequest.
deleteResource( request, pThis->mFeedback );
136 QgsDebugError( QStringLiteral(
"Invalid CUSTOMREQUEST = %1 when POSTFIELDS is not defined" ).arg( pszCustomRequest ) );
142 psResult->nStatus = 1;
143 psResult->pszErrBuf = CPLStrdup( blockingRequest.
errorMessage().toUtf8() );
150 for (
const auto &pair : reply.rawHeaderPairs() )
152 if ( EQUAL( pair.first.toStdString().c_str(),
"Content-Type" ) )
154 CPLFree( psResult->pszContentType );
155 psResult->pszContentType = CPLStrdup( pair.second.toStdString().c_str() );
157 psResult->papszHeaders = CSLAddNameValue(
158 psResult->papszHeaders,
159 pair.first.toStdString().c_str(),
160 pair.second.toStdString().c_str() );
164 QByteArray content( reply.content() );
170 if (
static_cast<int>( pfnWrite( content.data(), 1, content.size(), pWriteArg ) ) != content.size() )
172 psResult->nStatus = 1;
173 psResult->pszErrBuf = CPLStrdup(
"download interrupted by user" );
179 psResult->nDataLen =
static_cast<int>( content.size() );
180 psResult->pabyData =
static_cast<GByte *
>( CPLMalloc( psResult->nDataLen + 1 ) );
181 memcpy( psResult->pabyData, content.constData(), psResult->nDataLen );
182 psResult->pabyData[psResult->nDataLen] = 0;
190 mAttributes[code] = value;
195 mFeedback = feedback;
A thread safe class for performing blocking (sync) network requests, with full support for QGIS proxy...
ErrorCode get(QNetworkRequest &request, bool forceRefresh=false, QgsFeedback *feedback=nullptr)
Performs a "get" operation on the specified request.
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.
@ 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()
Destructor.
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Encapsulates a network reply within a container which is inexpensive to copy and safe to pass between...
#define QgsDebugError(str)