22 #include <QNetworkReply> 26 #include <QStringList> 32 if ( !mReply )
return;
40 QMap<QByteArray, QByteArray>
headers;
41 Q_FOREACH ( QByteArray h, mReply->rawHeaderList() )
43 headers.insert( h, mReply->rawHeader( h ) );
45 mHeaders.append( headers );
46 mBodies.append( mReply->readAll() );
50 QString contentType = mReply->header( QNetworkRequest::ContentTypeHeader ).toString();
53 QRegExp re(
".*boundary=\"?([^\"]+)\"?\\s?", Qt::CaseInsensitive );
55 if ( !( re.indexIn( contentType ) == 0 ) )
57 mError = tr(
"Cannot find boundary in multipart content type" );
61 QString boundary = re.cap( 1 );
62 QgsDebugMsg( QStringLiteral(
"boundary = %1 size = %2" ).arg( boundary ).arg( boundary.size() ) );
63 boundary =
"--" + boundary;
66 QByteArray data = mReply->readAll();
68 from = data.indexOf( boundary.toLatin1(), 0 ) + boundary.length() + 1;
74 to = data.indexOf( boundary.toLatin1(), from );
77 QgsDebugMsg( QStringLiteral(
"No more boundaries, rest size = %1" ).arg( data.size() - from - 1 ) );
79 if ( data.size() - from - 1 == 2 && QString( data.mid( from, 2 ) ) == QLatin1String(
"--" ) )
86 if ( data.size() - from > 1 )
95 QgsDebugMsg( QStringLiteral(
"part %1 - %2" ).arg( from ).arg( to ) );
96 QByteArray part = data.mid( from, to - from );
98 while ( !part.isEmpty() && ( part.at( 0 ) ==
'\r' || part.at( 0 ) ==
'\n' ) )
105 while ( pos < part.size() - 1 )
107 if ( part.at( pos ) ==
'\n' && ( part.at( pos + 1 ) ==
'\n' || part.at( pos + 1 ) ==
'\r' ) )
109 if ( part.at( pos + 1 ) ==
'\r' ) pos++;
117 QByteArray
headers = part.left( pos );
120 QStringList headerRows = QString( headers ).split( QRegExp(
"[\n\r]+" ) );
121 Q_FOREACH (
const QString &row, headerRows )
124 QStringList kv = row.split( QStringLiteral(
": " ) );
125 headersMap.insert( kv.value( 0 ).toLatin1(), kv.value( 1 ).toLatin1() );
127 mHeaders.append( headersMap );
129 mBodies.append( part.mid( pos ) );
131 from = to + boundary.length();
139 if ( !reply )
return false;
141 QString contentType = reply->header( QNetworkRequest::ContentTypeHeader ).toString();
147 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.