Browse Source

HTTP server: Greatly improved HTTP request parsing

adaptive-webui-19844
Christophe Dumez 13 years ago
parent
commit
eeda9379d5
  1. 37
      src/webui/httpconnection.cpp
  2. 21
      src/webui/httprequestparser.cpp
  3. 3
      src/webui/httprequestparser.h

37
src/webui/httpconnection.cpp

@ -78,26 +78,41 @@ void HttpConnection::handleDownloadFailure(const QString& url,
} }
void HttpConnection::read() { void HttpConnection::read() {
static QByteArray input; QByteArray input = m_socket->readAll();
input.append(m_socket->readAll()); // Parse HTTP request header
if(input.size() > 100000) { int header_end = input.indexOf("\r\n\r\n");
qDebug("Request too big"); QByteArray header = input.left(header_end);
input.clear(); m_parser.writeHeader(header);
if (m_parser.isError()) {
qDebug() << Q_FUNC_INFO << "parsing error";
m_generator.setStatusLine(400, "Bad Request"); m_generator.setStatusLine(400, "Bad Request");
write(); write();
return; return;
} }
if (!input.endsWith("\r\n")) { // Parse HTTP request message
// incomplete, let wait for more data if (m_parser.header().hasContentLength()) {
qDebug() << Q_FUNC_INFO << "Incomplete HTTP request, let's wait for more data..."; QByteArray message = input.mid(header_end + 4);
int expected_length = m_parser.header().contentLength();
if (expected_length > 100000) {
m_generator.setStatusLine(400, "Bad Request");
write();
return; return;
} }
// HTTP Request is complete, let's parse it bool is_reading = true;
m_parser.write(input); while (message.length() < expected_length && is_reading) {
input.clear(); disconnect(m_socket, SIGNAL(readyRead()), this, SLOT(read()));
is_reading = m_socket->waitForReadyRead(2000);
if (is_reading) {
message.append(m_socket->readAll());
}
connect(m_socket, SIGNAL(readyRead()), this, SLOT(read()));
}
m_parser.writeMessage(message);
}
if(m_parser.isError()) { if(m_parser.isError()) {
qDebug() << Q_FUNC_INFO << "parsing error"; qDebug() << Q_FUNC_INFO << "parsing error";

21
src/webui/httprequestparser.cpp

@ -65,16 +65,9 @@ const QByteArray& HttpRequestParser::torrent() const {
return m_torrentContent; return m_torrentContent;
} }
void HttpRequestParser::write(const QByteArray& ba) { void HttpRequestParser::writeHeader(const QByteArray& ba) {
int end_of_header = ba.indexOf("\r\n\r\n");
if (end_of_header < 0) {
qWarning() << "Could not find HTTP header: " << ba.constData();
m_error = true;
return;
}
// Parse header // Parse header
m_header = QHttpRequestHeader(ba.left(end_of_header)); m_header = QHttpRequestHeader(ba);
QUrl url = QUrl::fromEncoded(m_header.path().toAscii()); QUrl url = QUrl::fromEncoded(m_header.path().toAscii());
m_path = url.path(); m_path = url.path();
@ -84,12 +77,13 @@ void HttpRequestParser::write(const QByteArray& ba) {
QPair<QString, QString> pair = i.next(); QPair<QString, QString> pair = i.next();
m_getMap[pair.first] = pair.second; m_getMap[pair.first] = pair.second;
} }
}
void HttpRequestParser::writeMessage(const QByteArray& ba) {
// Parse message content // Parse message content
if (m_header.hasContentLength()) { Q_ASSERT (m_header.hasContentLength());
qDebug() << Q_FUNC_INFO << "ba.size(): " << ba.size();
qDebug() << Q_FUNC_INFO << "\n\n" << "header: " << ba.left(end_of_header) << "\n\n"; m_data = ba;
m_data = ba.mid(end_of_header + 4, m_header.contentLength()); // +4 to skip "\r\n\r\n"
qDebug() << "m_data.size(): " << m_data.size(); qDebug() << "m_data.size(): " << m_data.size();
// Parse POST data // Parse POST data
@ -139,5 +133,4 @@ Submit Query
} }
} }
} }
}
} }

3
src/webui/httprequestparser.h

@ -45,7 +45,8 @@ public:
QString get(const QString& key) const; QString get(const QString& key) const;
QString post(const QString& key) const; QString post(const QString& key) const;
const QByteArray& torrent() const; const QByteArray& torrent() const;
void write(const QByteArray& ba); void writeHeader(const QByteArray& ba);
void writeMessage(const QByteArray& ba);
inline QHttpRequestHeader& header() { return m_header; } inline QHttpRequestHeader& header() { return m_header; }
private: private:

Loading…
Cancel
Save