Fine tune translations loading for Chinese locales
[qBittorrent.git] / src / base / http / responsegenerator.cpp
blob8e3c738f97943b12d991515e788f1520dc128040
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2014 Vladimir Golovnev <glassez@yandex.ru>
4 * Copyright (C) 2006 Ishan Arora and Christophe Dumez <chris@qbittorrent.org>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * In addition, as a special exception, the copyright holders give permission to
21 * link this program with the OpenSSL project's "OpenSSL" library (or with
22 * modified versions of it that use the same license as the "OpenSSL" library),
23 * and distribute the linked executables. You must obey the GNU General Public
24 * License in all respects for all of the code used other than "OpenSSL". If you
25 * modify file(s), you may extend this exception to your version of the file(s),
26 * but you are not obligated to do so. If you do not wish to do so, delete this
27 * exception statement from your version.
30 #include "responsegenerator.h"
32 #include <QDateTime>
34 #include "base/http/types.h"
35 #include "base/utils/gzip.h"
37 QByteArray Http::toByteArray(Response response)
39 compressContent(response);
41 response.headers[HEADER_CONTENT_LENGTH] = QString::number(response.content.length());
42 response.headers[HEADER_DATE] = httpDate();
44 QByteArray buf;
45 buf.reserve(1024 + response.content.length());
47 // Status Line
48 buf.append("HTTP/1.1 ") // TODO: depends on request
49 .append(QByteArray::number(response.status.code))
50 .append(' ')
51 .append(response.status.text.toLatin1())
52 .append(CRLF);
54 // Header Fields
55 for (auto i = response.headers.constBegin(); i != response.headers.constEnd(); ++i)
57 buf.append(i.key().toLatin1())
58 .append(": ")
59 .append(i.value().toLatin1())
60 .append(CRLF);
63 // the first empty line
64 buf += CRLF;
66 // message body // TODO: support HEAD request
67 buf += response.content;
69 return buf;
72 QString Http::httpDate()
74 // [RFC 7231] 7.1.1.1. Date/Time Formats
75 // example: "Sun, 06 Nov 1994 08:49:37 GMT"
77 return QLocale::c().toString(QDateTime::currentDateTimeUtc(), u"ddd, dd MMM yyyy HH:mm:ss"_qs)
78 .append(u" GMT");
81 void Http::compressContent(Response &response)
83 if (response.headers.value(HEADER_CONTENT_ENCODING) != u"gzip")
84 return;
86 response.headers.remove(HEADER_CONTENT_ENCODING);
88 // for very small files, compressing them only wastes cpu cycles
89 const int contentSize = response.content.size();
90 if (contentSize <= 1024) // 1 kb
91 return;
93 // filter out known hard-to-compress types
94 const QString contentType = response.headers[HEADER_CONTENT_TYPE];
95 if ((contentType == CONTENT_TYPE_GIF) || (contentType == CONTENT_TYPE_PNG))
96 return;
98 // try compressing
99 bool ok = false;
100 const QByteArray compressedData = Utils::Gzip::compress(response.content, 6, &ok);
101 if (!ok)
102 return;
104 // "Content-Encoding: gzip\r\n" is 24 bytes long
105 if ((compressedData.size() + 24) >= contentSize)
106 return;
108 response.content = compressedData;
109 response.headers[HEADER_CONTENT_ENCODING] = u"gzip"_qs;