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 
QgsNetworkReplyContent(QNetworkReply * reply)19 QgsNetworkReplyContent::QgsNetworkReplyContent( QNetworkReply *reply )
20   : mError( reply->error() )
21   , mErrorString( reply->errorString() )
22   , mRawHeaderPairs( reply->rawHeaderPairs() )
23   , mRequest( reply->request() )
24 {
25   int maxAttribute = static_cast< int >( QNetworkRequest::RedirectPolicyAttribute );
26 #if QT_VERSION >= QT_VERSION_CHECK( 5, 11, 0 )
27   maxAttribute = static_cast< int >( QNetworkRequest::Http2DirectAttribute );
28 #endif
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   int requestId = reply->property( "requestId" ).toInt( &ok );
37   if ( ok )
38     mRequestId = requestId;
39 }
40 
clear()41 void QgsNetworkReplyContent::clear()
42 {
43   *this = QgsNetworkReplyContent();
44 }
45 
attribute(QNetworkRequest::Attribute code) const46 QVariant QgsNetworkReplyContent::attribute( QNetworkRequest::Attribute code ) const
47 {
48   return mAttributes.value( code );
49 }
50 
hasRawHeader(const QByteArray & headerName) const51 bool QgsNetworkReplyContent::hasRawHeader( const QByteArray &headerName ) const
52 {
53   for ( auto &header : mRawHeaderPairs )
54   {
55     if ( header.first == headerName )
56       return true;
57   }
58   return false;
59 }
60 
rawHeaderList() const61 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 
rawHeader(const QByteArray & headerName) const72 QByteArray QgsNetworkReplyContent::rawHeader( const QByteArray &headerName ) const
73 {
74   for ( auto &header : mRawHeaderPairs )
75   {
76     if ( header.first == headerName )
77       return header.second;
78   }
79   return QByteArray();
80 }
81