Browse Source

Rewrite rules for gzipping http response content

adaptive-webui-19844
Chocobo1 8 years ago
parent
commit
94b496354b
  1. 42
      src/base/http/responsegenerator.cpp
  2. 1
      src/base/http/responsegenerator.h

42
src/base/http/responsegenerator.cpp

@ -37,17 +37,7 @@ @@ -37,17 +37,7 @@
QByteArray Http::toByteArray(Response response)
{
if (response.headers.value(HEADER_CONTENT_ENCODING) == "gzip") {
// 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);
}
compressContent(response);
response.headers[HEADER_CONTENT_LENGTH] = QString::number(response.content.length());
response.headers[HEADER_DATE] = httpDate();
@ -84,3 +74,33 @@ QString Http::httpDate() @@ -84,3 +74,33 @@ QString Http::httpDate()
return QLocale::c().toString(QDateTime::currentDateTimeUtc(), QLatin1String("ddd, dd MMM yyyy HH:mm:ss"))
.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");
}

1
src/base/http/responsegenerator.h

@ -39,6 +39,7 @@ namespace Http @@ -39,6 +39,7 @@ namespace Http
{
QByteArray toByteArray(Response response);
QString httpDate();
void compressContent(Response &response);
}
#endif // HTTP_RESPONSEGENERATOR_H

Loading…
Cancel
Save