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