|
|
@ -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"; |
|
|
|