23 #if GDAL_VERSION_NUM < GDAL_COMPUTE_VERSION(3,2,0)
30 Q_UNUSED( mFeedback );
43 CPLHTTPPushFetchCallback( QgsCPLHTTPFetchOverrider::callback,
this );
48 CPLHTTPPopFetchCallback();
52 CPLHTTPResult *QgsCPLHTTPFetchOverrider::callback(
const char *pszURL,
53 CSLConstList papszOptions,
56 CPLHTTPFetchWriteFunc pfnWrite,
62 auto psResult =
static_cast<CPLHTTPResult *
>( CPLCalloc(
sizeof( CPLHTTPResult ), 1 ) );
63 if ( CSLFetchNameValue( papszOptions,
"CLOSE_PERSISTENT" ) )
72 for (
const char *pszOption : {
"FORM_FILE_PATH",
"FORM_ITEM_COUNT" } )
74 if ( CSLFetchNameValue( papszOptions, pszOption ) )
76 QgsDebugMsg( QStringLiteral(
"Option %1 not handled" ).arg( pszOption ) );
84 QNetworkRequest request( QString::fromUtf8( pszURL ) );
85 for (
const auto &keyValue : pThis->mAttributes )
87 request.setAttribute( keyValue.first, keyValue.second );
91 const char *pszHeaders = CSLFetchNameValue( papszOptions,
"HEADERS" );
94 char **papszTokensHeaders = CSLTokenizeString2( pszHeaders,
"\r\n", 0 );
95 for (
int i = 0; papszTokensHeaders[i] !=
nullptr; ++i )
97 char *pszKey =
nullptr;
98 const char *pszValue = CPLParseNameValue( papszTokensHeaders[i], &pszKey );
99 if ( pszKey && pszValue )
101 request.setRawHeader(
102 QByteArray::fromStdString( pszKey ),
103 QByteArray::fromStdString( pszValue ) );
107 CSLDestroy( papszTokensHeaders );
110 constexpr
bool forceRefresh =
true;
111 const char *pszCustomRequest = CSLFetchNameValue( papszOptions,
"CUSTOMREQUEST" );
112 const char *pszPostFields = CSLFetchNameValue( papszOptions,
"POSTFIELDS" );
116 if ( pszCustomRequest ==
nullptr || EQUAL( pszCustomRequest,
"POST" ) )
118 errCode = blockingRequest.
post( request,
119 QByteArray::fromStdString( pszPostFields ),
123 else if ( EQUAL( pszCustomRequest,
"PUT" ) )
125 errCode = blockingRequest.
put( request,
126 QByteArray::fromStdString( pszPostFields ),
131 QgsDebugMsg( QStringLiteral(
"Invalid CUSTOMREQUEST = %1 when POSTFIELDS is defined" ).arg( pszCustomRequest ) );
137 if ( pszCustomRequest ==
nullptr || EQUAL( pszCustomRequest,
"GET" ) )
139 errCode = blockingRequest.
get( request, forceRefresh, pThis->mFeedback );
141 else if ( EQUAL( pszCustomRequest,
"HEAD" ) )
143 errCode = blockingRequest.
head( request, forceRefresh, pThis->mFeedback );
145 else if ( EQUAL( pszCustomRequest,
"DELETE" ) )
147 errCode = blockingRequest.
deleteResource( request, pThis->mFeedback );
151 QgsDebugMsg( QStringLiteral(
"Invalid CUSTOMREQUEST = %1 when POSTFIELDS is not defined" ).arg( pszCustomRequest ) );
157 psResult->nStatus = 1;
158 psResult->pszErrBuf = CPLStrdup( blockingRequest.
errorMessage().toUtf8() );
165 for (
const auto &pair : reply.rawHeaderPairs() )
167 if ( EQUAL( pair.first.toStdString().c_str(),
"Content-Type" ) )
169 CPLFree( psResult->pszContentType );
170 psResult->pszContentType = CPLStrdup( pair.second.toStdString().c_str() );
172 psResult->papszHeaders = CSLAddNameValue(
173 psResult->papszHeaders,
174 pair.first.toStdString().c_str(),
175 pair.second.toStdString().c_str() );
179 QByteArray content( reply.content() );
185 if (
static_cast<int>( pfnWrite( content.data(), 1, content.size(), pWriteArg ) ) != content.size() )
187 psResult->nStatus = 1;
188 psResult->pszErrBuf = CPLStrdup(
"download interrupted by user" );
194 psResult->nDataLen =
static_cast<int>( content.size() );
195 psResult->pabyData =
static_cast<GByte *
>( CPLMalloc( psResult->nDataLen + 1 ) );
196 memcpy( psResult->pabyData, content.constData(), psResult->nDataLen );
197 psResult->pabyData[psResult->nDataLen] = 0;
207 mAttributes[code] = value;