QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
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
24#include <QString>
25
26using namespace Qt::StringLiterals;
27
29 : mAuthCfg( authCfg )
30 , mFeedback( feedback )
31 , mThread( QThread::currentThread() )
32{
33 CPLHTTPPushFetchCallback( QgsCPLHTTPFetchOverrider::callback, this );
34}
35
37{
38 CPLHTTPPopFetchCallback();
39}
40
41
42CPLHTTPResult *QgsCPLHTTPFetchOverrider::callback(
43 const char *pszURL, CSLConstList papszOptions, GDALProgressFunc /* pfnProgress */, void * /*pProgressArg */, CPLHTTPFetchWriteFunc pfnWrite, void *pWriteArg, void *pUserData
44)
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( u"Option %1 not handled"_s.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( QByteArray::fromStdString( pszKey ), QByteArray::fromStdString( pszValue ) );
95 }
96 CPLFree( pszKey );
97 }
98 CSLDestroy( papszTokensHeaders );
99 }
100
101 constexpr bool forceRefresh = true;
102 const char *pszCustomRequest = CSLFetchNameValue( papszOptions, "CUSTOMREQUEST" );
103 const char *pszPostFields = CSLFetchNameValue( papszOptions, "POSTFIELDS" );
105 if ( pszPostFields )
106 {
107 if ( !pszCustomRequest || EQUAL( pszCustomRequest, "POST" ) )
108 {
109 errCode = blockingRequest.post( request, QByteArray::fromStdString( pszPostFields ), forceRefresh, pThis->mFeedback );
110 }
111 else if ( EQUAL( pszCustomRequest, "PUT" ) )
112 {
113 errCode = blockingRequest.put( request, QByteArray::fromStdString( pszPostFields ), pThis->mFeedback );
114 }
115 else
116 {
117 QgsDebugError( u"Invalid CUSTOMREQUEST = %1 when POSTFIELDS is defined"_s.arg( pszCustomRequest ) );
118 return nullptr;
119 }
120 }
121 else
122 {
123 if ( !pszCustomRequest || EQUAL( pszCustomRequest, "GET" ) )
124 {
125 errCode = blockingRequest.get( request, forceRefresh, pThis->mFeedback, QgsBlockingNetworkRequest::RequestFlag::EmptyResponseIsValid );
126 }
127 else if ( EQUAL( pszCustomRequest, "HEAD" ) )
128 {
129 errCode = blockingRequest.head( request, forceRefresh, pThis->mFeedback );
130 }
131 else if ( EQUAL( pszCustomRequest, "DELETE" ) )
132 {
133 errCode = blockingRequest.deleteResource( request, pThis->mFeedback );
134 }
135 else
136 {
137 QgsDebugError( u"Invalid CUSTOMREQUEST = %1 when POSTFIELDS is not defined"_s.arg( pszCustomRequest ) );
138 return nullptr;
139 }
140 }
141 if ( errCode != QgsBlockingNetworkRequest::NoError )
142 {
143 psResult->nStatus = 1;
144 psResult->pszErrBuf = CPLStrdup( blockingRequest.errorMessage().toUtf8() );
145 return psResult;
146 }
147
148 const QgsNetworkReplyContent reply( blockingRequest.reply() );
149
150 // Store response headers
151 for ( const auto &pair : reply.rawHeaderPairs() )
152 {
153 if ( EQUAL( pair.first.toStdString().c_str(), "Content-Type" ) )
154 {
155 CPLFree( psResult->pszContentType );
156 psResult->pszContentType = CPLStrdup( pair.second.toStdString().c_str() );
157 }
158 psResult->papszHeaders = CSLAddNameValue( psResult->papszHeaders, pair.first.toStdString().c_str(), pair.second.toStdString().c_str() );
159 }
160
161 // Process content
162 QByteArray content( reply.content() );
163
164 // Poor-man implementation of the pfnWrite mechanism which is supposed to be
165 // called on the fly as bytes are received
166 if ( pfnWrite )
167 {
168 if ( static_cast<int>( pfnWrite( content.data(), 1, content.size(), pWriteArg ) ) != content.size() )
169 {
170 psResult->nStatus = 1;
171 psResult->pszErrBuf = CPLStrdup( "download interrupted by user" );
172 return psResult;
173 }
174 }
175 else
176 {
177 psResult->nDataLen = static_cast<int>( content.size() );
178 psResult->pabyData = static_cast<GByte *>( CPLMalloc( psResult->nDataLen + 1 ) );
179 memcpy( psResult->pabyData, content.constData(), psResult->nDataLen );
180 psResult->pabyData[psResult->nDataLen] = 0;
181 }
182
183 return psResult;
184}
185
186void QgsCPLHTTPFetchOverrider::setAttribute( QNetworkRequest::Attribute code, const QVariant &value )
187{
188 mAttributes[code] = value;
189}
190
192{
193 mFeedback = feedback;
194}
195
197{
198 return mThread;
199}
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:56
#define QgsDebugError(str)
Definition qgslogger.h:59