Fix #1688 - Fragmented http-Headers (#1741)

This commit is contained in:
LordGrey 2024-05-18 09:14:30 +02:00 committed by GitHub
parent 051072ee46
commit 4a5b0b6bf2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 130 additions and 124 deletions

View File

@ -37,7 +37,7 @@ QString QtHttpClientWrapper::getGuid (void)
{ {
m_guid = QString::fromLocal8Bit ( m_guid = QString::fromLocal8Bit (
QCryptographicHash::hash ( QCryptographicHash::hash (
QByteArray::number ((quint64) (this)), QByteArray::number (reinterpret_cast<quint64>(this)),
QCryptographicHash::Md5 QCryptographicHash::Md5
).toHex () ).toHex ()
); );
@ -50,7 +50,7 @@ void QtHttpClientWrapper::onClientDataReceived (void)
{ {
if (m_sockClient != Q_NULLPTR) if (m_sockClient != Q_NULLPTR)
{ {
while (m_sockClient->bytesAvailable ()) while (m_sockClient->bytesAvailable () != 0)
{ {
QByteArray line = m_sockClient->readLine (); QByteArray line = m_sockClient->readLine ();
@ -62,9 +62,9 @@ void QtHttpClientWrapper::onClientDataReceived (void)
QStringList parts = QStringUtils::split(str,SPACE, QStringUtils::SplitBehavior::SkipEmptyParts); QStringList parts = QStringUtils::split(str,SPACE, QStringUtils::SplitBehavior::SkipEmptyParts);
if (parts.size () == 3) if (parts.size () == 3)
{ {
QString command = parts.at (0); const QString& command = parts.at (0);
QString url = parts.at (1); const QString& url = parts.at (1);
QString version = parts.at (2); const QString& version = parts.at (2);
if (version == QtHttpServer::HTTP_VERSION) if (version == QtHttpServer::HTTP_VERSION)
{ {
@ -85,31 +85,35 @@ void QtHttpClientWrapper::onClientDataReceived (void)
m_parsingStatus = ParsingError; m_parsingStatus = ParsingError;
// Error : incorrect HTTP command line // Error : incorrect HTTP command line
} }
m_fragment.clear();
break; break;
} }
case AwaitingHeaders: // "header: value" × N (until empty line) case AwaitingHeaders: // "header: value" × N (until empty line)
{ {
QByteArray raw = line.trimmed (); m_fragment.append(line);
if ( m_fragment.endsWith(CRLF))
{
QByteArray raw = m_fragment.trimmed ();
if (!raw.isEmpty ()) // parse headers if (!raw.isEmpty ()) // parse headers
{ {
int pos = raw.indexOf (COLON); int pos = raw.indexOf (COLON);
if (pos > 0) if (pos > 0)
{ {
QByteArray header = raw.left (pos).trimmed(); QByteArray header = raw.left (pos).trimmed();
QByteArray value = raw.mid (pos +1).trimmed(); QByteArray value = raw.mid (pos +1).trimmed();
m_currentRequest->addHeader (header, value); m_currentRequest->addHeader (header, value);
#if (QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)) #if (QT_VERSION >= QT_VERSION_CHECK(5, 12, 0))
if (header.compare(QtHttpHeader::ContentLength, Qt::CaseInsensitive) == 0) if (header.compare(QtHttpHeader::ContentLength, Qt::CaseInsensitive) == 0)
#else #else
if (header.toLower() == QtHttpHeader::ContentLength.toLower()) if (header.toLower() == QtHttpHeader::ContentLength.toLower())
#endif #endif
{ {
bool ok = false; bool isConversionOk = false;
const int len = value.toInt (&ok, 10); const int len = value.toInt (&isConversionOk, 10);
if (ok) if (isConversionOk)
{ {
m_currentRequest->addHeader (QtHttpHeader::ContentLength, QByteArray::number (len)); m_currentRequest->addHeader (QtHttpHeader::ContentLength, QByteArray::number (len));
} }
@ -132,6 +136,8 @@ void QtHttpClientWrapper::onClientDataReceived (void)
m_parsingStatus = RequestParsed; m_parsingStatus = RequestParsed;
} }
} }
m_fragment.clear();
}
break; break;
} }
@ -157,7 +163,7 @@ void QtHttpClientWrapper::onClientDataReceived (void)
case RequestParsed: // a valid request has ben fully parsed case RequestParsed: // a valid request has ben fully parsed
{ {
// Catch websocket header "Upgrade" // Catch websocket header "Upgrade"
if(m_currentRequest->getHeader(QtHttpHeader::Upgrade) == "websocket") if(m_currentRequest->getHeader(QtHttpHeader::Upgrade).toLower() == "websocket")
{ {
if(m_websocketClient == Q_NULLPTR) if(m_websocketClient == Q_NULLPTR)
{ {
@ -315,10 +321,9 @@ QtHttpClientWrapper::ParsingStatus QtHttpClientWrapper::sendReplyToClient (QtHtt
{ {
if (!reply->useChunked ()) if (!reply->useChunked ())
{ {
//reply->appendRawData (CRLF);
// send all headers and all data in one shot // send all headers and all data in one shot
reply->requestSendHeaders (); emit reply->requestSendHeaders ();
reply->requestSendData (); emit reply->requestSendData ();
} }
else else
{ {
@ -331,7 +336,7 @@ QtHttpClientWrapper::ParsingStatus QtHttpClientWrapper::sendReplyToClient (QtHtt
{ {
static const QByteArray & CLOSE = QByteArrayLiteral ("close"); static const QByteArray & CLOSE = QByteArrayLiteral ("close");
if (m_currentRequest->getHeader(QtHttpHeader::Connection) == CLOSE) if (m_currentRequest->getHeader(QtHttpHeader::Connection).toLower() == CLOSE)
{ {
// must close connection after this request // must close connection after this request
m_sockClient->close (); m_sockClient->close ();

View File

@ -58,6 +58,7 @@ private:
const bool m_localConnection; const bool m_localConnection;
WebSocketClient * m_websocketClient; WebSocketClient * m_websocketClient;
WebJsonRpc * m_webJsonRpc; WebJsonRpc * m_webJsonRpc;
QByteArray m_fragment;
}; };
#endif // QTHTTPCLIENTWRAPPER_H #endif // QTHTTPCLIENTWRAPPER_H