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