QGIS API Documentation 3.34.0-Prizren (ffbdd678812)
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#include "qgslogger.h"
19
20#include "cpl_http.h"
21#include "gdal.h"
22
24 : mAuthCfg( authCfg )
25 , mFeedback( feedback )
26 , mThread( QThread::currentThread() )
27{
28 CPLHTTPPushFetchCallback( QgsCPLHTTPFetchOverrider::callback, this );
29}
30
32{
33 CPLHTTPPopFetchCallback();
34}
35
36
37CPLHTTPResult *QgsCPLHTTPFetchOverrider::callback( const char *pszURL,
38 CSLConstList papszOptions,
39 GDALProgressFunc /* pfnProgress */,
40 void * /*pProgressArg */,
41 CPLHTTPFetchWriteFunc pfnWrite,
42 void *pWriteArg,
43 void *pUserData )
44{
45 QgsCPLHTTPFetchOverrider *pThis = static_cast<QgsCPLHTTPFetchOverrider *>( pUserData );
46
47 auto psResult = static_cast<CPLHTTPResult *>( CPLCalloc( sizeof( CPLHTTPResult ), 1 ) );
48 if ( CSLFetchNameValue( papszOptions, "CLOSE_PERSISTENT" ) )
49 {
50 // CLOSE_PERSISTENT is a CPL trick to maintain a curl handle open over
51 // a series of CPLHTTPFetch() call to the same server.
52 // Just return a dummy result to acknowledge we 'processed' it
53 return psResult;
54 }
55
56 // Look for important options we don't handle yet
57 for ( const char *pszOption : { "FORM_FILE_PATH", "FORM_ITEM_COUNT" } )
58 {
59 if ( CSLFetchNameValue( papszOptions, pszOption ) )
60 {
61 QgsDebugError( QStringLiteral( "Option %1 not handled" ).arg( pszOption ) );
62 return nullptr;
63 }
64 }
65
66 QgsBlockingNetworkRequest blockingRequest;
67 blockingRequest.setAuthCfg( pThis->mAuthCfg );
68
69 QNetworkRequest request( QString::fromUtf8( pszURL ) );
70 for ( const auto &keyValue : pThis->mAttributes )
71 {
72 request.setAttribute( keyValue.first, keyValue.second );
73 }
74
75 // Store request headers
76 const char *pszHeaders = CSLFetchNameValue( papszOptions, "HEADERS" );
77 if ( pszHeaders )
78 {
79 char **papszTokensHeaders = CSLTokenizeString2( pszHeaders, "\r\n", 0 );
80 for ( int i = 0; papszTokensHeaders[i] != nullptr; ++i )
81 {
82 char *pszKey = nullptr;
83 const char *pszValue = CPLParseNameValue( papszTokensHeaders[i], &pszKey );
84 if ( pszKey && pszValue )
85 {
86 request.setRawHeader(
87 QByteArray::fromStdString( pszKey ),
88 QByteArray::fromStdString( pszValue ) );
89 }
90 CPLFree( pszKey );
91 }
92 CSLDestroy( papszTokensHeaders );
93 }
94
95 constexpr bool forceRefresh = true;
96 const char *pszCustomRequest = CSLFetchNameValue( papszOptions, "CUSTOMREQUEST" );
97 const char *pszPostFields = CSLFetchNameValue( papszOptions, "POSTFIELDS" );
99 if ( pszPostFields )
100 {
101 if ( pszCustomRequest == nullptr || EQUAL( pszCustomRequest, "POST" ) )
102 {
103 errCode = blockingRequest.post( request,
104 QByteArray::fromStdString( pszPostFields ),
105 forceRefresh,
106 pThis->mFeedback );
107 }
108 else if ( EQUAL( pszCustomRequest, "PUT" ) )
109 {
110 errCode = blockingRequest.put( request,
111 QByteArray::fromStdString( pszPostFields ),
112 pThis->mFeedback );
113 }
114 else
115 {
116 QgsDebugError( QStringLiteral( "Invalid CUSTOMREQUEST = %1 when POSTFIELDS is defined" ).arg( pszCustomRequest ) );
117 return nullptr;
118 }
119 }
120 else
121 {
122 if ( pszCustomRequest == nullptr || EQUAL( pszCustomRequest, "GET" ) )
123 {
124 errCode = blockingRequest.get( request, forceRefresh, pThis->mFeedback );
125 }
126 else if ( EQUAL( pszCustomRequest, "HEAD" ) )
127 {
128 errCode = blockingRequest.head( request, forceRefresh, pThis->mFeedback );
129 }
130 else if ( EQUAL( pszCustomRequest, "DELETE" ) )
131 {
132 errCode = blockingRequest.deleteResource( request, pThis->mFeedback );
133 }
134 else
135 {
136 QgsDebugError( QStringLiteral( "Invalid CUSTOMREQUEST = %1 when POSTFIELDS is not defined" ).arg( pszCustomRequest ) );
137 return nullptr;
138 }
139 }
140 if ( errCode != QgsBlockingNetworkRequest::NoError )
141 {
142 psResult->nStatus = 1;
143 psResult->pszErrBuf = CPLStrdup( blockingRequest.errorMessage().toUtf8() );
144 return psResult;
145 }
146
147 const QgsNetworkReplyContent reply( blockingRequest.reply() );
148
149 // Store response headers
150 for ( const auto &pair : reply.rawHeaderPairs() )
151 {
152 if ( EQUAL( pair.first.toStdString().c_str(), "Content-Type" ) )
153 {
154 CPLFree( psResult->pszContentType );
155 psResult->pszContentType = CPLStrdup( pair.second.toStdString().c_str() );
156 }
157 psResult->papszHeaders = CSLAddNameValue(
158 psResult->papszHeaders,
159 pair.first.toStdString().c_str(),
160 pair.second.toStdString().c_str() );
161 }
162
163 // Process content
164 QByteArray content( reply.content() );
165
166 // Poor-man implementation of the pfnWrite mechanism which is supposed to be
167 // called on the fly as bytes are received
168 if ( pfnWrite )
169 {
170 if ( static_cast<int>( pfnWrite( content.data(), 1, content.size(), pWriteArg ) ) != content.size() )
171 {
172 psResult->nStatus = 1;
173 psResult->pszErrBuf = CPLStrdup( "download interrupted by user" );
174 return psResult;
175 }
176 }
177 else
178 {
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;
183 }
184
185 return psResult;
186}
187
188void QgsCPLHTTPFetchOverrider::setAttribute( QNetworkRequest::Attribute code, const QVariant &value )
189{
190 mAttributes[code] = value;
191}
192
194{
195 mFeedback = feedback;
196}
197
199{
200 return mThread;
201}
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.
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition qgsfeedback.h:45
Encapsulates a network reply within a container which is inexpensive to copy and safe to pass between...
#define QgsDebugError(str)
Definition qgslogger.h:38