1
0
mirror of https://github.com/d47081/qBittorrent.git synced 2025-01-28 15:34:16 +00:00

Properly recover from HTTP parsing errors (Web UI)

May be related to issue #68.
This commit is contained in:
Christophe Dumez 2012-09-29 14:26:52 +03:00
parent 32bfd8741f
commit 7bd9139d28
2 changed files with 10 additions and 7 deletions

View File

@ -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();
}

View File

@ -66,6 +66,7 @@ const QList<QByteArray>& 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<QByteArray> 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();