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