27 CPLHTTPPushFetchCallback( QgsCPLHTTPFetchOverrider::callback,
this );
32 CPLHTTPPopFetchCallback();
36CPLHTTPResult *QgsCPLHTTPFetchOverrider::callback(
const char *pszURL,
37 CSLConstList papszOptions,
40 CPLHTTPFetchWriteFunc pfnWrite,
46 auto psResult =
static_cast<CPLHTTPResult *
>( CPLCalloc(
sizeof( CPLHTTPResult ), 1 ) );
47 if ( CSLFetchNameValue( papszOptions,
"CLOSE_PERSISTENT" ) )
56 for (
const char *pszOption : {
"FORM_FILE_PATH",
"FORM_ITEM_COUNT" } )
58 if ( CSLFetchNameValue( papszOptions, pszOption ) )
60 QgsDebugMsg( QStringLiteral(
"Option %1 not handled" ).arg( pszOption ) );
68 QNetworkRequest request( QString::fromUtf8( pszURL ) );
69 for (
const auto &keyValue : pThis->mAttributes )
71 request.setAttribute( keyValue.first, keyValue.second );
75 const char *pszHeaders = CSLFetchNameValue( papszOptions,
"HEADERS" );
78 char **papszTokensHeaders = CSLTokenizeString2( pszHeaders,
"\r\n", 0 );
79 for (
int i = 0; papszTokensHeaders[i] !=
nullptr; ++i )
81 char *pszKey =
nullptr;
82 const char *pszValue = CPLParseNameValue( papszTokensHeaders[i], &pszKey );
83 if ( pszKey && pszValue )
86 QByteArray::fromStdString( pszKey ),
87 QByteArray::fromStdString( pszValue ) );
91 CSLDestroy( papszTokensHeaders );
94 constexpr bool forceRefresh =
true;
95 const char *pszCustomRequest = CSLFetchNameValue( papszOptions,
"CUSTOMREQUEST" );
96 const char *pszPostFields = CSLFetchNameValue( papszOptions,
"POSTFIELDS" );
100 if ( pszCustomRequest ==
nullptr || EQUAL( pszCustomRequest,
"POST" ) )
102 errCode = blockingRequest.
post( request,
103 QByteArray::fromStdString( pszPostFields ),
107 else if ( EQUAL( pszCustomRequest,
"PUT" ) )
109 errCode = blockingRequest.
put( request,
110 QByteArray::fromStdString( pszPostFields ),
115 QgsDebugMsg( QStringLiteral(
"Invalid CUSTOMREQUEST = %1 when POSTFIELDS is defined" ).arg( pszCustomRequest ) );
121 if ( pszCustomRequest ==
nullptr || EQUAL( pszCustomRequest,
"GET" ) )
123 errCode = blockingRequest.
get( request, forceRefresh, pThis->mFeedback );
125 else if ( EQUAL( pszCustomRequest,
"HEAD" ) )
127 errCode = blockingRequest.
head( request, forceRefresh, pThis->mFeedback );
129 else if ( EQUAL( pszCustomRequest,
"DELETE" ) )
131 errCode = blockingRequest.
deleteResource( request, pThis->mFeedback );
135 QgsDebugMsg( QStringLiteral(
"Invalid CUSTOMREQUEST = %1 when POSTFIELDS is not defined" ).arg( pszCustomRequest ) );
141 psResult->nStatus = 1;
142 psResult->pszErrBuf = CPLStrdup( blockingRequest.
errorMessage().toUtf8() );
149 for (
const auto &pair : reply.rawHeaderPairs() )
151 if ( EQUAL( pair.first.toStdString().c_str(),
"Content-Type" ) )
153 CPLFree( psResult->pszContentType );
154 psResult->pszContentType = CPLStrdup( pair.second.toStdString().c_str() );
156 psResult->papszHeaders = CSLAddNameValue(
157 psResult->papszHeaders,
158 pair.first.toStdString().c_str(),
159 pair.second.toStdString().c_str() );
163 QByteArray content( reply.content() );
169 if (
static_cast<int>( pfnWrite( content.data(), 1, content.size(), pWriteArg ) ) != content.size() )
171 psResult->nStatus = 1;
172 psResult->pszErrBuf = CPLStrdup(
"download interrupted by user" );
178 psResult->nDataLen =
static_cast<int>( content.size() );
179 psResult->pabyData =
static_cast<GByte *
>( CPLMalloc( psResult->nDataLen + 1 ) );
180 memcpy( psResult->pabyData, content.constData(), psResult->nDataLen );
181 psResult->pabyData[psResult->nDataLen] = 0;
189 mAttributes[code] = value;
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.
~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...