mirror of
https://github.com/qbittorrent/qBittorrent
synced 2025-08-21 22:03:27 -07:00
Rewrite rules for gzipping http response content
This commit is contained in:
parent
6353c2ca3c
commit
751f64c98b
2 changed files with 32 additions and 11 deletions
|
@ -37,17 +37,7 @@
|
||||||
|
|
||||||
QByteArray Http::toByteArray(Response response)
|
QByteArray Http::toByteArray(Response response)
|
||||||
{
|
{
|
||||||
if (response.headers.value(HEADER_CONTENT_ENCODING) == "gzip") {
|
compressContent(response);
|
||||||
// A gzip seems to have 23 bytes overhead.
|
|
||||||
// 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
|
|
||||||
// If the message is smaller than 49 bytes we actually send MORE data if we gzip
|
|
||||||
QByteArray destBuf;
|
|
||||||
if ((response.content.size() > 49) && (Utils::Gzip::compress(response.content, destBuf)))
|
|
||||||
response.content = destBuf;
|
|
||||||
else
|
|
||||||
response.headers.remove(HEADER_CONTENT_ENCODING);
|
|
||||||
}
|
|
||||||
|
|
||||||
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();
|
||||||
|
@ -84,3 +74,33 @@ QString Http::httpDate()
|
||||||
return QLocale::c().toString(QDateTime::currentDateTimeUtc(), QLatin1String("ddd, dd MMM yyyy HH:mm:ss"))
|
return QLocale::c().toString(QDateTime::currentDateTimeUtc(), QLatin1String("ddd, dd MMM yyyy HH:mm:ss"))
|
||||||
.append(QLatin1String(" GMT"));
|
.append(QLatin1String(" GMT"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Http::compressContent(Response &response)
|
||||||
|
{
|
||||||
|
if (response.headers.value(HEADER_CONTENT_ENCODING) != QLatin1String("gzip"))
|
||||||
|
return;
|
||||||
|
|
||||||
|
response.headers.remove(HEADER_CONTENT_ENCODING);
|
||||||
|
|
||||||
|
// for very small files, compressing them only wastes cpu cycles
|
||||||
|
const int contentSize = response.content.size();
|
||||||
|
if (contentSize <= 1024) // 1 kb
|
||||||
|
return;
|
||||||
|
|
||||||
|
// filter out known hard-to-compress types
|
||||||
|
const QString contentType = response.headers[HEADER_CONTENT_TYPE];
|
||||||
|
if ((contentType == CONTENT_TYPE_GIF) || (contentType == CONTENT_TYPE_PNG))
|
||||||
|
return;
|
||||||
|
|
||||||
|
// try compressing
|
||||||
|
QByteArray buf;
|
||||||
|
if (!Utils::Gzip::compress(response.content, buf))
|
||||||
|
return;
|
||||||
|
|
||||||
|
// "Content-Encoding: gzip\r\n" is 24 bytes long
|
||||||
|
if ((buf.size() + 24) >= contentSize)
|
||||||
|
return;
|
||||||
|
|
||||||
|
response.content = buf;
|
||||||
|
response.headers[HEADER_CONTENT_ENCODING] = QLatin1String("gzip");
|
||||||
|
}
|
||||||
|
|
|
@ -39,6 +39,7 @@ namespace Http
|
||||||
{
|
{
|
||||||
QByteArray toByteArray(Response response);
|
QByteArray toByteArray(Response response);
|
||||||
QString httpDate();
|
QString httpDate();
|
||||||
|
void compressContent(Response &response);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // HTTP_RESPONSEGENERATOR_H
|
#endif // HTTP_RESPONSEGENERATOR_H
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue