QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
qgscplhttpfetchoverrider.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgscplhttpfetchoverrider.cpp
3 ----------------------------
4 begin : September 2020
5 copyright : (C) 2020 by Even Rouault
6 email : even.rouault at spatialys.com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
17
18#include <cpl_http.h>
19#include <gdal.h>
20
22#include "qgslogger.h"
23
25 : mAuthCfg( authCfg )
26 , mFeedback( feedback )
27 , mThread( QThread::currentThread() )
28{
29 CPLHTTPPushFetchCallback( QgsCPLHTTPFetchOverrider::callback, this );
30}
31
33{
34 CPLHTTPPopFetchCallback();
35}
36
37
38CPLHTTPResult *QgsCPLHTTPFetchOverrider::callback( const char *pszURL,
39 CSLConstList papszOptions,
40 GDALProgressFunc /* pfnProgress */,
41 void * /*pProgressArg */,
42 CPLHTTPFetchWriteFunc pfnWrite,
43 void *pWriteArg,
44 void *pUserData )
45{
46 QgsCPLHTTPFetchOverrider *pThis = static_cast<QgsCPLHTTPFetchOverrider *>( pUserData );
47
48 auto psResult = static_cast<CPLHTTPResult *>( CPLCalloc( sizeof( CPLHTTPResult ), 1 ) );
49 if ( CSLFetchNameValue( papszOptions, "CLOSE_PERSISTENT" ) )
50 {
51 // CLOSE_PERSISTENT is a CPL trick to maintain a curl handle open over
52 // a series of CPLHTTPFetch() call to the same server.
53 // Just return a dummy result to acknowledge we 'processed' it
54 return psResult;
55 }
56
57 // Look for important options we don't handle yet
58 for ( const char *pszOption : { "FORM_FILE_PATH", "FORM_ITEM_COUNT" } )
59 {
60 if ( CSLFetchNameValue( papszOptions, pszOption ) )
61 {
62 QgsDebugError( QStringLiteral( "Option %1 not handled" ).arg( pszOption ) );
63 return nullptr;
64 }
65 }
66
67 if ( pThis->mFeedback && pThis->mFeedback->isCanceled() )
68 {
69 psResult->nStatus = 1;
70 psResult->pszErrBuf = CPLStrdup( "download interrupted by user" );
71 return psResult;
72 }
73
74 QgsBlockingNetworkRequest blockingRequest;
75 blockingRequest.setAuthCfg( pThis->mAuthCfg );
76
77 QNetworkRequest request( QString::fromUtf8( pszURL ) );
78 for ( const auto &keyValue : pThis->mAttributes )
79 {
80 request.setAttribute( keyValue.first, keyValue.second );
81 }
82
83 // Store request headers
84 const char *pszHeaders = CSLFetchNameValue( papszOptions, "HEADERS" );
85 if ( pszHeaders )
86 {
87 char **papszTokensHeaders = CSLTokenizeString2( pszHeaders, "\r\n", 0 );
88 for ( int i = 0; papszTokensHeaders[i] != nullptr; ++i )
89 {
90 char *pszKey = nullptr;
91 const char *pszValue = CPLParseNameValue( papszTokensHeaders[i], &pszKey );
92 if ( pszKey && pszValue )
93 {
94 request.setRawHeader(
95 QByteArray::fromStdString( pszKey ),
96 QByteArray::fromStdString( pszValue ) );
97 }
98 CPLFree( pszKey );
99 }
100 CSLDestroy( papszTokensHeaders );
101 }
102
103 constexpr bool forceRefresh = true;
104 const char *pszCustomRequest = CSLFetchNameValue( papszOptions, "CUSTOMREQUEST" );
105 const char *pszPostFields = CSLFetchNameValue( papszOptions, "POSTFIELDS" );
107 if ( pszPostFields )
108 {
109 if ( !pszCustomRequest || EQUAL( pszCustomRequest, "POST" ) )
110 {
111 errCode = blockingRequest.post( request,
112 QByteArray::fromStdString( pszPostFields ),
113 forceRefresh,
114 pThis->mFeedback );
115 }
116 else if ( EQUAL( pszCustomRequest, "PUT" ) )
117 {
118 errCode = blockingRequest.put( request,
119 QByteArray::fromStdString( pszPostFields ),
120 pThis->mFeedback );
121 }
122 else
123 {
124 QgsDebugError( QStringLiteral( "Invalid CUSTOMREQUEST = %1 when POSTFIELDS is defined" ).arg( pszCustomRequest ) );
125 return nullptr;
126 }
127 }
128 else
129 {
130 if ( !pszCustomRequest || EQUAL( pszCustomRequest, "GET" ) )
131 {
132 errCode = blockingRequest.get( request, forceRefresh, pThis->mFeedback, QgsBlockingNetworkRequest::RequestFlag::EmptyResponseIsValid );
133 }
134 else if ( EQUAL( pszCustomRequest, "HEAD" ) )
135 {
136 errCode = blockingRequest.head( request, forceRefresh, pThis->mFeedback );
137 }
138 else if ( EQUAL( pszCustomRequest, "DELETE" ) )
139 {
140 errCode = blockingRequest.deleteResource( request, pThis->mFeedback );
141 }
142 else
143 {
144 QgsDebugError( QStringLiteral( "Invalid CUSTOMREQUEST = %1 when POSTFIELDS is not defined" ).arg( pszCustomRequest ) );
145 return nullptr;
146 }
147 }
148 if ( errCode != QgsBlockingNetworkRequest::NoError )
149 {
150 psResult->nStatus = 1;
151 psResult->pszErrBuf = CPLStrdup( blockingRequest.errorMessage().toUtf8() );
152 return psResult;
153 }
154
155 const QgsNetworkReplyContent reply( blockingRequest.reply() );
156
157 // Store response headers
158 for ( const auto &pair : reply.rawHeaderPairs() )
159 {
160 if ( EQUAL( pair.first.toStdString().c_str(), "Content-Type" ) )
161 {
162 CPLFree( psResult->pszContentType );
163 psResult->pszContentType = CPLStrdup( pair.second.toStdString().c_str() );
164 }
165 psResult->papszHeaders = CSLAddNameValue(
166 psResult->papszHeaders,
167 pair.first.toStdString().c_str(),
168 pair.second.toStdString().c_str() );
169 }
170
171 // Process content
172 QByteArray content( reply.content() );
173
174 // Poor-man implementation of the pfnWrite mechanism which is supposed to be
175 // called on the fly as bytes are received
176 if ( pfnWrite )
177 {
178 if ( static_cast<int>( pfnWrite( content.data(), 1, content.size(), pWriteArg ) ) != content.size() )
179 {
180 psResult->nStatus = 1;
181 psResult->pszErrBuf = CPLStrdup( "download interrupted by user" );
182 return psResult;
183 }
184 }
185 else
186 {
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;
191 }
192
193 return psResult;
194}
195
196void QgsCPLHTTPFetchOverrider::setAttribute( QNetworkRequest::Attribute code, const QVariant &value )
197{
198 mAttributes[code] = value;
199}
200
202{
203 mFeedback = feedback;
204}
205
207{
208 return mThread;
209}
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.
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition qgsfeedback.h:44
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:53
#define QgsDebugError(str)
Definition qgslogger.h:57