QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
Loading...
Searching...
No Matches
qgsnetworkreplyparser.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsnetworkreplyparser.cpp - Multipart QNetworkReply parser
3 -------------------
4 begin : 4 January, 2013
5 copyright : (C) 2013 by Radim Blazek
6 email : radim dot blazek at gmail.com
7
8 ***************************************************************************/
9
10/***************************************************************************
11 * *
12 * This program is free software; you can redistribute it and/or modify *
13 * it under the terms of the GNU General Public License as published by *
14 * the Free Software Foundation; either version 2 of the License, or *
15 * (at your option) any later version. *
16 * *
17 ***************************************************************************/
18
19#include "qgslogger.h"
21#include "moc_qgsnetworkreplyparser.cpp"
22
23#include <QNetworkReply>
24#include <QObject>
25#include <QRegularExpression>
26#include <QString>
27#include <QStringList>
28
30 : mReply( reply )
31 , mValid( false )
32{
33 if ( !mReply ) return;
34
35 // Content type examples:
36 // multipart/mixed; boundary=wcs
37 // multipart/mixed; boundary="wcs"\n
38 if ( !isMultipart( mReply ) )
39 {
40 // reply is not multipart, copy body and headers
41 QMap<QByteArray, QByteArray> headers;
42 const auto constRawHeaderList = mReply->rawHeaderList();
43 for ( const QByteArray &h : constRawHeaderList )
44 {
45 headers.insert( h, mReply->rawHeader( h ) );
46 }
47 mHeaders.append( headers );
48 mBodies.append( mReply->readAll() );
49 }
50 else // multipart
51 {
52 const QString contentType = mReply->header( QNetworkRequest::ContentTypeHeader ).toString();
53 QgsDebugMsgLevel( "contentType: " + contentType, 2 );
54
55 const thread_local QRegularExpression re( ".*boundary=\"?([^\"]+)\"?\\s?", QRegularExpression::CaseInsensitiveOption );
56 const QRegularExpressionMatch match = re.match( contentType );
57 if ( !( match.capturedStart( 0 ) == 0 ) )
58 {
59 mError = tr( "Cannot find boundary in multipart content type" );
60 return;
61 }
62
63 QString boundary = match.captured( 1 );
64 QgsDebugMsgLevel( QStringLiteral( "boundary = %1 size = %2" ).arg( boundary ).arg( boundary.size() ), 2 );
65 boundary = "--" + boundary;
66
67 // Lines should be terminated by CRLF ("\r\n") but any new line combination may appear
68 const QByteArray data = mReply->readAll();
69 int from, to;
70 from = data.indexOf( boundary.toLatin1(), 0 ) + boundary.length() + 1;
71 //QVector<QByteArray> partHeaders;
72 //QVector<QByteArray> partBodies;
73 while ( true )
74 {
75 // 'to' is not really 'to', but index of the next byte after the end of part
76 to = data.indexOf( boundary.toLatin1(), from );
77 if ( to < 0 )
78 {
79 QgsDebugMsgLevel( QStringLiteral( "No more boundaries, rest size = %1" ).arg( data.size() - from - 1 ), 2 );
80 // It may be end, last boundary is followed by '--'
81 if ( data.size() - from - 1 == 2 && QString( data.mid( from, 2 ) ) == QLatin1String( "--" ) ) // end
82 {
83 break;
84 }
85
86 // It may happen that boundary is missing at the end (GeoServer)
87 // in that case, take everything to the end
88 if ( data.size() - from > 1 )
89 {
90 to = data.size(); // out of range OK
91 }
92 else
93 {
94 break;
95 }
96 }
97 QgsDebugMsgLevel( QStringLiteral( "part %1 - %2" ).arg( from ).arg( to ), 2 );
98 QByteArray part = data.mid( from, to - from );
99 // Remove possible new line from beginning
100 while ( !part.isEmpty() && ( part.at( 0 ) == '\r' || part.at( 0 ) == '\n' ) )
101 {
102 part.remove( 0, 1 );
103 }
104 // Split header and data (find empty new line)
105 // New lines should be CRLF, but we support also CRLFCRLF, LFLF to find empty line
106 int pos = 0; // body start
107 while ( pos < part.size() - 1 )
108 {
109 if ( part.at( pos ) == '\n' && ( part.at( pos + 1 ) == '\n' || part.at( pos + 1 ) == '\r' ) )
110 {
111 if ( part.at( pos + 1 ) == '\r' ) pos++;
112 pos += 2;
113 break;
114 }
115 pos++;
116 }
117 // parse headers
118 RawHeaderMap headersMap;
119 const QByteArray headers = part.left( pos );
120 QgsDebugMsgLevel( "headers:\n" + headers, 2 );
121
122 const QStringList headerRows = QString( headers ).split( QRegularExpression( "[\n\r]+" ) );
123 const auto constHeaderRows = headerRows;
124 for ( const QString &row : constHeaderRows )
125 {
126 QgsDebugMsgLevel( "row = " + row, 2 );
127 const QStringList kv = row.split( QStringLiteral( ": " ) );
128 headersMap.insert( kv.value( 0 ).toLatin1(), kv.value( 1 ).toLatin1() );
129 }
130 mHeaders.append( headersMap );
131
132 mBodies.append( part.mid( pos ) );
133
134 from = to + boundary.length();
135 }
136 }
137 mValid = true;
138}
139
140bool QgsNetworkReplyParser::isMultipart( QNetworkReply *reply )
141{
142 if ( !reply ) return false;
143
144 const QString contentType = reply->header( QNetworkRequest::ContentTypeHeader ).toString();
145 QgsDebugMsgLevel( "contentType: " + contentType, 2 );
146
147 // Multipart content type examples:
148 // multipart/mixed; boundary=wcs
149 // multipart/mixed; boundary="wcs"\n
150 return contentType.startsWith( QLatin1String( "multipart/" ), Qt::CaseInsensitive );
151}
QMap< QByteArray, QByteArray > RawHeaderMap
QgsNetworkReplyParser(QNetworkReply *reply)
Constructor.
static bool isMultipart(QNetworkReply *reply)
Test if reply is multipart.
QList< RawHeaderMap > headers() const
Gets headers.
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:39