From 7bd9139d28f8212d4d7418bee2c908bb550385ad Mon Sep 17 00:00:00 2001 From: Christophe Dumez Date: Sat, 29 Sep 2012 14:26:52 +0300 Subject: [PATCH] Properly recover from HTTP parsing errors (Web UI) May be related to issue #68. --- src/webui/httpconnection.cpp | 14 ++++++++------ src/webui/httprequestparser.cpp | 3 ++- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/webui/httpconnection.cpp b/src/webui/httpconnection.cpp index 097ebe85d..0b7fe4897 100644 --- a/src/webui/httpconnection.cpp +++ b/src/webui/httpconnection.cpp @@ -77,17 +77,19 @@ void HttpConnection::handleDownloadFailure(const QString& url, << qPrintable(reason) << std::endl; } -void HttpConnection::read() { +void HttpConnection::read() +{ m_receivedData.append(m_socket->readAll()); // Parse HTTP request header - int header_end = m_receivedData.indexOf("\r\n\r\n"); + const int header_end = m_receivedData.indexOf("\r\n\r\n"); if (header_end < 0) { qDebug() << "Partial request: \n" << m_receivedData; // Partial request waiting for the rest return; } - QByteArray header = m_receivedData.left(header_end); + + const QByteArray header = m_receivedData.left(header_end); m_parser.writeHeader(header); if (m_parser.isError()) { qWarning() << Q_FUNC_INFO << "header parsing error"; @@ -102,7 +104,7 @@ void HttpConnection::read() { const int expected_length = m_parser.header().contentLength(); QByteArray message = m_receivedData.mid(header_end + 4, expected_length); - if (expected_length > 10000000) { + if (expected_length > 10000000 /* ~10MB */) { qWarning() << "Bad request: message too long"; m_generator.setStatusLine(400, "Bad Request"); m_receivedData.clear(); @@ -117,7 +119,6 @@ void HttpConnection::read() { } m_parser.writeMessage(message); - m_receivedData = m_receivedData.mid(header_end + 4 + expected_length); } else { m_receivedData.clear(); @@ -132,7 +133,8 @@ void HttpConnection::read() { } } -void HttpConnection::write() { +void HttpConnection::write() +{ m_socket->write(m_generator.toByteArray()); m_socket->disconnectFromHost(); } diff --git a/src/webui/httprequestparser.cpp b/src/webui/httprequestparser.cpp index 51c15b387..30ac51b01 100644 --- a/src/webui/httprequestparser.cpp +++ b/src/webui/httprequestparser.cpp @@ -66,6 +66,7 @@ const QList& HttpRequestParser::torrents() const { } void HttpRequestParser::writeHeader(const QByteArray& ba) { + m_error = false; // Parse header m_header = QHttpRequestHeader(ba); QUrl url = QUrl::fromEncoded(m_header.path().toAscii()); @@ -94,7 +95,7 @@ static QList splitRawData(QByteArray rawData, const QByteArray& sep) void HttpRequestParser::writeMessage(const QByteArray& ba) { // Parse message content Q_ASSERT (m_header.hasContentLength()); - + m_error = false; m_data = ba; qDebug() << Q_FUNC_INFO << "m_data.size(): " << m_data.size();