QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
Loading...
Searching...
No Matches
qgsnetworkreply.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsnetworkreply.cpp
3 -------------------
4 begin : November 2018
5 copyright : (C) 2018 by Nyall Dawson
6 email : nyall dot dawson at gmail dot 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
16#include "qgsnetworkreply.h"
17#include <QNetworkReply>
18#include <QRegularExpression>
19#include <QRegularExpressionMatch>
20
22 : mError( reply->error() )
23 , mErrorString( reply->errorString() )
24 , mRawHeaderPairs( reply->rawHeaderPairs() )
25 , mRequest( reply->request() )
26{
27 const int maxAttribute = static_cast< int >( QNetworkRequest::Http2DirectAttribute );
28 for ( int i = 0; i <= maxAttribute; ++i )
29 {
30 if ( reply->attribute( static_cast< QNetworkRequest::Attribute>( i ) ).isValid() )
31 mAttributes[ static_cast< QNetworkRequest::Attribute>( i ) ] = reply->attribute( static_cast< QNetworkRequest::Attribute>( i ) );
32 }
33
34 bool ok = false;
35 const int requestId = reply->property( "requestId" ).toInt( &ok );
36 if ( ok )
37 mRequestId = requestId;
38}
39
44
45QVariant QgsNetworkReplyContent::attribute( QNetworkRequest::Attribute code ) const
46{
47 return mAttributes.value( code );
48}
49
50bool QgsNetworkReplyContent::hasRawHeader( const QByteArray &headerName ) const
51{
52 for ( auto &header : mRawHeaderPairs )
53 {
54 if ( ! QString::fromLocal8Bit( header.first ).compare( QString::fromLocal8Bit( headerName ), Qt::CaseInsensitive ) )
55 return true;
56 }
57 return false;
58}
59
61{
62 QList< QByteArray > res;
63 res.reserve( mRawHeaderPairs.length() );
64 for ( auto &header : mRawHeaderPairs )
65 {
66 res << header.first;
67 }
68 return res;
69}
70
71QByteArray QgsNetworkReplyContent::rawHeader( const QByteArray &headerName ) const
72{
73 for ( auto &header : mRawHeaderPairs )
74 {
75 if ( ! QString::fromLocal8Bit( header.first ).compare( QString::fromLocal8Bit( headerName ), Qt::CaseInsensitive ) )
76 return header.second;
77 }
78 return QByteArray();
79}
80
82{
83 if ( !reply )
84 return QString();
85
86 return extractFileNameFromContentDispositionHeader( reply->header( QNetworkRequest::ContentDispositionHeader ).toString() );
87}
88
90{
91 const thread_local QRegularExpression rx( QStringLiteral( R"""(filename[^;\n]*=\s*(UTF-\d['"]*)?((['"]).*?[.]$\2|[^;\n]*)?)""" ), QRegularExpression::PatternOption::CaseInsensitiveOption );
92
93 QRegularExpressionMatchIterator i = rx.globalMatch( header, 0 );
94 QString res;
95 // we want the last match here, as that will have the UTF filename when present
96 while ( i.hasNext() )
97 {
98 const QRegularExpressionMatch match = i.next();
99 res = match.captured( 2 );
100 }
101
102 if ( res.startsWith( '"' ) )
103 {
104 res = res.mid( 1 );
105 if ( res.endsWith( '"' ) )
106 res.chop( 1 );
107 }
108 if ( !res.isEmpty() )
109 {
110 res = QUrl::fromPercentEncoding( res.toUtf8() );
111 }
112
113 return res;
114}
QVariant attribute(QNetworkRequest::Attribute code) const
Returns the attribute associated with the code.
QgsNetworkReplyContent()=default
Default constructor for an empty reply.
bool hasRawHeader(const QByteArray &headerName) const
Returns true if the reply contains a header with the specified headerName.
static QString extractFilenameFromContentDispositionHeader(QNetworkReply *reply)
Extracts the filename component of the content disposition header from a network reply.
QByteArray rawHeader(const QByteArray &headerName) const
Returns the content of the header with the specified headerName, or an empty QByteArray if the specif...
QList< QByteArray > rawHeaderList() const
Returns a list of raw header names contained within the reply.
void clear()
Clears the reply, resetting it back to a default, empty reply.
static QString extractFileNameFromContentDispositionHeader(const QString &header)
Extracts the filename component of the content disposition header from the header.
int requestId() const
Returns the unique ID identifying the original request which this response was formed from.