Browse Source

Properly recover from HTTP parsing errors (Web UI)

May be related to issue #68.
adaptive-webui-19844
Christophe Dumez 12 years ago
parent
commit
7bd9139d28
  1. 14
      src/webui/httpconnection.cpp
  2. 3
      src/webui/httprequestparser.cpp

14
src/webui/httpconnection.cpp

@ -77,17 +77,19 @@ void HttpConnection::handleDownloadFailure(const QString& url, @@ -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() { @@ -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() { @@ -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() { @@ -132,7 +133,8 @@ void HttpConnection::read() {
}
}
void HttpConnection::write() {
void HttpConnection::write()
{
m_socket->write(m_generator.toByteArray());
m_socket->disconnectFromHost();
}

3
src/webui/httprequestparser.cpp

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

Loading…
Cancel
Save