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