22 #include <QNetworkReply> 26 #include <QStringList> 32 if ( !mReply )
return;
40 QMap<QByteArray, QByteArray>
headers;
41 const auto constRawHeaderList = mReply->rawHeaderList();
42 for ( QByteArray h : constRawHeaderList )
44 headers.insert( h, mReply->rawHeader( h ) );
46 mHeaders.append( headers );
47 mBodies.append( mReply->readAll() );
51 QString contentType = mReply->header( QNetworkRequest::ContentTypeHeader ).toString();
54 QRegExp re(
".*boundary=\"?([^\"]+)\"?\\s?", Qt::CaseInsensitive );
56 if ( !( re.indexIn( contentType ) == 0 ) )
58 mError = tr(
"Cannot find boundary in multipart content type" );
62 QString boundary = re.cap( 1 );
63 QgsDebugMsg( QStringLiteral(
"boundary = %1 size = %2" ).arg( boundary ).arg( boundary.size() ) );
64 boundary =
"--" + boundary;
67 QByteArray data = mReply->readAll();
69 from = data.indexOf( boundary.toLatin1(), 0 ) + boundary.length() + 1;
75 to = data.indexOf( boundary.toLatin1(), from );
78 QgsDebugMsg( QStringLiteral(
"No more boundaries, rest size = %1" ).arg( data.size() - from - 1 ) );
80 if ( data.size() - from - 1 == 2 && QString( data.mid( from, 2 ) ) == QLatin1String(
"--" ) )
87 if ( data.size() - from > 1 )
96 QgsDebugMsg( QStringLiteral(
"part %1 - %2" ).arg( from ).arg( to ) );
97 QByteArray part = data.mid( from, to - from );
99 while ( !part.isEmpty() && ( part.at( 0 ) ==
'\r' || part.at( 0 ) ==
'\n' ) )
106 while ( pos < part.size() - 1 )
108 if ( part.at( pos ) ==
'\n' && ( part.at( pos + 1 ) ==
'\n' || part.at( pos + 1 ) ==
'\r' ) )
110 if ( part.at( pos + 1 ) ==
'\r' ) pos++;
118 QByteArray
headers = part.left( pos );
121 QStringList headerRows = QString( headers ).split( QRegExp(
"[\n\r]+" ) );
122 const auto constHeaderRows = headerRows;
123 for (
const QString &row : constHeaderRows )
126 QStringList kv = row.split( QStringLiteral(
": " ) );
127 headersMap.insert( kv.value( 0 ).toLatin1(), kv.value( 1 ).toLatin1() );
129 mHeaders.append( headersMap );
131 mBodies.append( part.mid( pos ) );
133 from = to + boundary.length();
141 if ( !reply )
return false;
143 QString contentType = reply->header( QNetworkRequest::ContentTypeHeader ).toString();
149 return contentType.startsWith( QLatin1String(
"multipart/" ), Qt::CaseInsensitive );
QList< RawHeaderMap > headers() const
Gets headers.
QMap< QByteArray, QByteArray > RawHeaderMap
static bool isMultipart(QNetworkReply *reply)
Test if reply is multipart.
QgsNetworkReplyParser(QNetworkReply *reply)
Constructor.