QGIS API Documentation 3.99.0-Master (26c88405ac0)
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
20
21#include "qgslogger.h"
22
23#include <QNetworkReply>
24#include <QObject>
25#include <QRegularExpression>
26#include <QString>
27#include <QStringList>
28
29#include "moc_qgsnetworkreplyparser.cpp"
30
32 : mReply( reply )
33 , mValid( false )
34{
35 if ( !mReply ) return;
36
37 // Content type examples:
38 // multipart/mixed; boundary=wcs
39 // multipart/mixed; boundary="wcs"\n
40 if ( !isMultipart( mReply ) )
41 {
42 // reply is not multipart, copy body and headers
43 QMap<QByteArray, QByteArray> headers;
44 const auto constRawHeaderList = mReply->rawHeaderList();
45 for ( const QByteArray &h : constRawHeaderList )
46 {
47 headers.insert( h, mReply->rawHeader( h ) );
48 }
49 mHeaders.append( headers );
50 mBodies.append( mReply->readAll() );
51 }
52 else // multipart
53 {
54 const QString contentType = mReply->header( QNetworkRequest::ContentTypeHeader ).toString();
55 QgsDebugMsgLevel( "contentType: " + contentType, 2 );
56
57 const thread_local QRegularExpression re( ".*boundary=\"?([^\"]+)\"?\\s?", QRegularExpression::CaseInsensitiveOption );
58 const QRegularExpressionMatch match = re.match( contentType );
59 if ( !( match.capturedStart( 0 ) == 0 ) )
60 {
61 mError = tr( "Cannot find boundary in multipart content type" );
62 return;
63 }
64
65 QString boundary = match.captured( 1 );
66 QgsDebugMsgLevel( QStringLiteral( "boundary = %1 size = %2" ).arg( boundary ).arg( boundary.size() ), 2 );
67 boundary = "--" + boundary;
68
69 // Lines should be terminated by CRLF ("\r\n") but any new line combination may appear
70 const QByteArray data = mReply->readAll();
71 int from, to;
72 from = data.indexOf( boundary.toLatin1(), 0 ) + boundary.length() + 1;
73 //QVector<QByteArray> partHeaders;
74 //QVector<QByteArray> partBodies;
75 while ( true )
76 {
77 // 'to' is not really 'to', but index of the next byte after the end of part
78 to = data.indexOf( boundary.toLatin1(), from );
79 if ( to < 0 )
80 {
81 QgsDebugMsgLevel( QStringLiteral( "No more boundaries, rest size = %1" ).arg( data.size() - from - 1 ), 2 );
82 // It may be end, last boundary is followed by '--'
83 if ( data.size() - from - 1 == 2 && QString( data.mid( from, 2 ) ) == QLatin1String( "--" ) ) // end
84 {
85 break;
86 }
87
88 // It may happen that boundary is missing at the end (GeoServer)
89 // in that case, take everything to the end
90 if ( data.size() - from > 1 )
91 {
92 to = data.size(); // out of range OK
93 }
94 else
95 {
96 break;
97 }
98 }
99 QgsDebugMsgLevel( QStringLiteral( "part %1 - %2" ).arg( from ).arg( to ), 2 );
100 QByteArray part = data.mid( from, to - from );
101 // Remove possible new line from beginning
102 while ( !part.isEmpty() && ( part.at( 0 ) == '\r' || part.at( 0 ) == '\n' ) )
103 {
104 part.remove( 0, 1 );
105 }
106 // Split header and data (find empty new line)
107 // New lines should be CRLF, but we support also CRLFCRLF, LFLF to find empty line
108 int pos = 0; // body start
109 while ( pos < part.size() - 1 )
110 {
111 if ( part.at( pos ) == '\n' && ( part.at( pos + 1 ) == '\n' || part.at( pos + 1 ) == '\r' ) )
112 {
113 if ( part.at( pos + 1 ) == '\r' ) pos++;
114 pos += 2;
115 break;
116 }
117 pos++;
118 }
119 // parse headers
120 RawHeaderMap headersMap;
121 const QByteArray headers = part.left( pos );
122 QgsDebugMsgLevel( "headers:\n" + headers, 2 );
123
124 const QStringList headerRows = QString( headers ).split( QRegularExpression( "[\n\r]+" ) );
125 const auto constHeaderRows = headerRows;
126 for ( const QString &row : constHeaderRows )
127 {
128 QgsDebugMsgLevel( "row = " + row, 2 );
129 const QStringList kv = row.split( QStringLiteral( ": " ) );
130 headersMap.insert( kv.value( 0 ).toLatin1(), kv.value( 1 ).toLatin1() );
131 }
132 mHeaders.append( headersMap );
133
134 mBodies.append( part.mid( pos ) );
135
136 from = to + boundary.length();
137 }
138 }
139 mValid = true;
140}
141
142bool QgsNetworkReplyParser::isMultipart( QNetworkReply *reply )
143{
144 if ( !reply ) return false;
145
146 const QString contentType = reply->header( QNetworkRequest::ContentTypeHeader ).toString();
147 QgsDebugMsgLevel( "contentType: " + contentType, 2 );
148
149 // Multipart content type examples:
150 // multipart/mixed; boundary=wcs
151 // multipart/mixed; boundary="wcs"\n
152 return contentType.startsWith( QLatin1String( "multipart/" ), Qt::CaseInsensitive );
153}
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:61