2008-05-17 16:44:42 +04:00
|
|
|
/*
|
2009-04-05 22:48:45 +04:00
|
|
|
* Bittorrent Client using Qt4 and libtorrent.
|
|
|
|
* Copyright (C) 2006 Ishan Arora and Christophe Dumez
|
2008-05-17 16:44:42 +04:00
|
|
|
*
|
2009-04-05 22:48:45 +04:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2008-05-17 16:44:42 +04:00
|
|
|
*
|
2009-04-05 22:48:45 +04:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2008-05-17 16:44:42 +04:00
|
|
|
*
|
2009-04-05 22:48:45 +04:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* In addition, as a special exception, the copyright holders give permission to
|
|
|
|
* link this program with the OpenSSL project's "OpenSSL" library (or with
|
|
|
|
* modified versions of it that use the same license as the "OpenSSL" library),
|
|
|
|
* and distribute the linked executables. You must obey the GNU General Public
|
|
|
|
* License in all respects for all of the code used other than "OpenSSL". If you
|
|
|
|
* modify file(s), you may extend this exception to your version of the file(s),
|
|
|
|
* but you are not obligated to do so. If you do not wish to do so, delete this
|
|
|
|
* exception statement from your version.
|
|
|
|
*
|
|
|
|
* Contact : chris@qbittorrent.org
|
2008-05-17 16:44:42 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "httpresponsegenerator.h"
|
2013-10-21 01:04:32 +04:00
|
|
|
#include <zlib.h>
|
2008-05-17 16:44:42 +04:00
|
|
|
|
2011-09-24 22:37:34 +04:00
|
|
|
void HttpResponseGenerator::setMessage(const QByteArray& message)
|
2008-05-17 16:44:42 +04:00
|
|
|
{
|
2012-05-27 11:57:00 +04:00
|
|
|
m_message = message;
|
2008-05-17 16:44:42 +04:00
|
|
|
}
|
|
|
|
|
2012-05-27 11:57:00 +04:00
|
|
|
void HttpResponseGenerator::setMessage(const QString& message)
|
|
|
|
{
|
2010-06-01 18:34:58 +04:00
|
|
|
setMessage(message.toUtf8());
|
2008-05-17 16:44:42 +04:00
|
|
|
}
|
|
|
|
|
2011-09-25 12:29:29 +04:00
|
|
|
void HttpResponseGenerator::setContentTypeByExt(const QString& ext) {
|
2012-02-20 21:30:53 +04:00
|
|
|
if (ext == "css") {
|
2008-05-17 16:44:42 +04:00
|
|
|
setContentType("text/css");
|
|
|
|
return;
|
|
|
|
}
|
2012-02-20 21:30:53 +04:00
|
|
|
if (ext == "gif") {
|
2008-05-17 16:44:42 +04:00
|
|
|
setContentType("image/gif");
|
|
|
|
return;
|
|
|
|
}
|
2012-02-20 21:30:53 +04:00
|
|
|
if (ext == "htm" || ext == "html") {
|
2008-05-17 16:44:42 +04:00
|
|
|
setContentType("text/html");
|
|
|
|
return;
|
|
|
|
}
|
2012-02-20 21:30:53 +04:00
|
|
|
if (ext == "js") {
|
2008-05-17 16:44:42 +04:00
|
|
|
setContentType("text/javascript");
|
|
|
|
return;
|
|
|
|
}
|
2012-02-20 21:30:53 +04:00
|
|
|
if (ext == "png") {
|
2012-06-19 03:52:54 +04:00
|
|
|
setContentType("image/png");
|
2008-05-17 16:44:42 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2013-10-21 01:04:32 +04:00
|
|
|
|
|
|
|
bool HttpResponseGenerator::gCompress(QByteArray &dest_buffer) {
|
|
|
|
static const int BUFSIZE = 128 * 1024;
|
|
|
|
char tmp_buf[BUFSIZE];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
z_stream strm;
|
|
|
|
strm.zalloc = Z_NULL;
|
|
|
|
strm.zfree = Z_NULL;
|
|
|
|
strm.opaque = Z_NULL;
|
|
|
|
strm.next_in = reinterpret_cast<unsigned char*>(m_message.data());
|
|
|
|
strm.avail_in = m_message.length();
|
|
|
|
strm.next_out = reinterpret_cast<unsigned char*>(tmp_buf);
|
|
|
|
strm.avail_out = BUFSIZE;
|
|
|
|
|
2013-10-23 21:45:20 +04:00
|
|
|
//windowBits = 15+16 to enable gzip
|
|
|
|
//From the zlib manual: windowBits can also be greater than 15 for optional gzip encoding. Add 16 to windowBits
|
|
|
|
//to write a simple gzip header and trailer around the compressed data instead of a zlib wrapper.
|
|
|
|
ret = deflateInit2(&strm, Z_BEST_COMPRESSION, Z_DEFLATED, 15+16, 8, Z_DEFAULT_STRATEGY);
|
2013-10-21 01:04:32 +04:00
|
|
|
|
|
|
|
if (ret != Z_OK)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
while (strm.avail_in != 0)
|
|
|
|
{
|
|
|
|
ret = deflate(&strm, Z_NO_FLUSH);
|
|
|
|
if (ret != Z_OK)
|
|
|
|
return false;
|
|
|
|
if (strm.avail_out == 0)
|
|
|
|
{
|
|
|
|
dest_buffer.append(tmp_buf, BUFSIZE);
|
|
|
|
strm.next_out = reinterpret_cast<unsigned char*>(tmp_buf);
|
|
|
|
strm.avail_out = BUFSIZE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int deflate_res = Z_OK;
|
|
|
|
while (deflate_res == Z_OK) {
|
|
|
|
if (strm.avail_out == 0) {
|
|
|
|
dest_buffer.append(tmp_buf, BUFSIZE);
|
|
|
|
strm.next_out = reinterpret_cast<unsigned char*>(tmp_buf);
|
|
|
|
strm.avail_out = BUFSIZE;
|
|
|
|
}
|
|
|
|
deflate_res = deflate(&strm, Z_FINISH);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (deflate_res != Z_STREAM_END)
|
|
|
|
return false;
|
2013-11-06 00:57:33 +04:00
|
|
|
dest_buffer.append(tmp_buf, BUFSIZE - strm.avail_out);
|
2013-10-21 01:04:32 +04:00
|
|
|
deflateEnd(&strm);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray HttpResponseGenerator::toByteArray() {
|
2013-11-09 22:02:31 +04:00
|
|
|
if (m_gzip && m_message.size() > 0) {// prevents writing a useless and wasteful header
|
2013-10-21 01:04:32 +04:00
|
|
|
QByteArray dest_buf;
|
|
|
|
if (gCompress(dest_buf)) {
|
2013-11-06 00:57:33 +04:00
|
|
|
setValue("content-encoding", "gzip");
|
2013-11-06 01:06:20 +04:00
|
|
|
#if QT_VERSION < 0x040800
|
|
|
|
m_message = dest_buf;
|
|
|
|
#else
|
2013-10-21 01:04:32 +04:00
|
|
|
m_message.swap(dest_buf);
|
2013-11-06 01:06:20 +04:00
|
|
|
#endif
|
2013-10-21 01:04:32 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-06 00:57:33 +04:00
|
|
|
setContentLength(m_message.size());
|
2013-10-21 01:04:32 +04:00
|
|
|
return QHttpResponseHeader::toString().toUtf8() + m_message;
|
|
|
|
}
|