|
|
@ -42,9 +42,9 @@ QByteArray Http::toByteArray(Response response) |
|
|
|
// Also "Content-Encoding: gzip\r\n" is 26 bytes long
|
|
|
|
// Also "Content-Encoding: gzip\r\n" is 26 bytes long
|
|
|
|
// So we only benefit from gzip if the message is bigger than 23+26 = 49
|
|
|
|
// So we only benefit from gzip if the message is bigger than 23+26 = 49
|
|
|
|
// If the message is smaller than 49 bytes we actually send MORE data if we gzip
|
|
|
|
// If the message is smaller than 49 bytes we actually send MORE data if we gzip
|
|
|
|
QByteArray dest_buf; |
|
|
|
QByteArray destBuf; |
|
|
|
if ((response.content.size() > 49) && (Utils::Gzip::compress(response.content, dest_buf))) |
|
|
|
if ((response.content.size() > 49) && (Utils::Gzip::compress(response.content, destBuf))) |
|
|
|
response.content = dest_buf; |
|
|
|
response.content = destBuf; |
|
|
|
else |
|
|
|
else |
|
|
|
response.headers.remove(HEADER_CONTENT_ENCODING); |
|
|
|
response.headers.remove(HEADER_CONTENT_ENCODING); |
|
|
|
} |
|
|
|
} |
|
|
@ -52,19 +52,28 @@ QByteArray Http::toByteArray(Response response) |
|
|
|
response.headers[HEADER_CONTENT_LENGTH] = QString::number(response.content.length()); |
|
|
|
response.headers[HEADER_CONTENT_LENGTH] = QString::number(response.content.length()); |
|
|
|
response.headers[HEADER_DATE] = httpDate(); |
|
|
|
response.headers[HEADER_DATE] = httpDate(); |
|
|
|
|
|
|
|
|
|
|
|
QString ret(QLatin1String("HTTP/1.1 %1 %2\r\n%3\r\n")); |
|
|
|
QByteArray buf; |
|
|
|
|
|
|
|
buf.reserve(10 * 1024); |
|
|
|
|
|
|
|
|
|
|
|
QString header; |
|
|
|
// Status Line
|
|
|
|
foreach (const QString& key, response.headers.keys()) |
|
|
|
buf += QString("HTTP/%1 %2 %3") |
|
|
|
header += QString("%1: %2\r\n").arg(key).arg(response.headers[key]); |
|
|
|
.arg("1.1", // TODO: depends on request
|
|
|
|
|
|
|
|
QString::number(response.status.code), |
|
|
|
|
|
|
|
response.status.text) |
|
|
|
|
|
|
|
.toLatin1() |
|
|
|
|
|
|
|
.append(CRLF); |
|
|
|
|
|
|
|
|
|
|
|
ret = ret.arg(response.status.code).arg(response.status.text).arg(header); |
|
|
|
// Header Fields
|
|
|
|
|
|
|
|
for (auto i = response.headers.constBegin(); i != response.headers.constEnd(); ++i) |
|
|
|
|
|
|
|
buf += QString("%1: %2").arg(i.key(), i.value()).toLatin1().append(CRLF); |
|
|
|
|
|
|
|
|
|
|
|
// qDebug() << Q_FUNC_INFO;
|
|
|
|
// the first empty line
|
|
|
|
// qDebug() << "HTTP Response header:";
|
|
|
|
buf += CRLF; |
|
|
|
// qDebug() << ret;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return ret.toUtf8() + response.content; |
|
|
|
// message body // TODO: support HEAD request
|
|
|
|
|
|
|
|
buf += response.content; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return buf; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
QString Http::httpDate() |
|
|
|
QString Http::httpDate() |
|
|
|