Fine tune translations loading for Chinese locales
[qBittorrent.git] / src / base / http / connection.cpp
blob9cc9ba4fe27eafa552e8fbed02e17106184226ea
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2018 Mike Tzou (Chocobo1)
4 * Copyright (C) 2014 Vladimir Golovnev <glassez@yandex.ru>
5 * Copyright (C) 2006 Ishan Arora and Christophe Dumez <chris@qbittorrent.org>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * In addition, as a special exception, the copyright holders give permission to
22 * link this program with the OpenSSL project's "OpenSSL" library (or with
23 * modified versions of it that use the same license as the "OpenSSL" library),
24 * and distribute the linked executables. You must obey the GNU General Public
25 * License in all respects for all of the code used other than "OpenSSL". If you
26 * modify file(s), you may extend this exception to your version of the file(s),
27 * but you are not obligated to do so. If you do not wish to do so, delete this
28 * exception statement from your version.
31 #include "connection.h"
33 #include <QTcpSocket>
35 #include "base/logger.h"
36 #include "irequesthandler.h"
37 #include "requestparser.h"
38 #include "responsegenerator.h"
40 using namespace Http;
42 Connection::Connection(QTcpSocket *socket, IRequestHandler *requestHandler, QObject *parent)
43 : QObject(parent)
44 , m_socket(socket)
45 , m_requestHandler(requestHandler)
47 m_socket->setParent(this);
49 // reset timer when there are activity
50 m_idleTimer.start();
51 connect(m_socket, &QIODevice::readyRead, this, [this]()
53 m_idleTimer.start();
54 read();
55 });
56 connect(m_socket, &QIODevice::bytesWritten, this, [this]()
58 m_idleTimer.start();
59 });
62 Connection::~Connection()
64 m_socket->close();
67 void Connection::read()
69 m_receivedData.append(m_socket->readAll());
71 while (!m_receivedData.isEmpty())
73 const RequestParser::ParseResult result = RequestParser::parse(m_receivedData);
75 switch (result.status)
77 case RequestParser::ParseStatus::Incomplete:
79 const long bufferLimit = RequestParser::MAX_CONTENT_SIZE * 1.1; // some margin for headers
80 if (m_receivedData.size() > bufferLimit)
82 LogMsg(tr("Http request size exceeds limitation, closing socket. Limit: %1, IP: %2")
83 .arg(bufferLimit).arg(m_socket->peerAddress().toString()), Log::WARNING);
85 Response resp(413, u"Payload Too Large"_qs);
86 resp.headers[HEADER_CONNECTION] = u"close"_qs;
88 sendResponse(resp);
89 m_socket->close();
92 return;
94 case RequestParser::ParseStatus::BadRequest:
96 LogMsg(tr("Bad Http request, closing socket. IP: %1")
97 .arg(m_socket->peerAddress().toString()), Log::WARNING);
99 Response resp(400, u"Bad Request"_qs);
100 resp.headers[HEADER_CONNECTION] = u"close"_qs;
102 sendResponse(resp);
103 m_socket->close();
105 return;
107 case RequestParser::ParseStatus::OK:
109 const Environment env {m_socket->localAddress(), m_socket->localPort(), m_socket->peerAddress(), m_socket->peerPort()};
111 Response resp = m_requestHandler->processRequest(result.request, env);
113 if (acceptsGzipEncoding(result.request.headers[u"accept-encoding"_qs]))
114 resp.headers[HEADER_CONTENT_ENCODING] = u"gzip"_qs;
116 resp.headers[HEADER_CONNECTION] = u"keep-alive"_qs;
118 sendResponse(resp);
119 m_receivedData = m_receivedData.mid(result.frameSize);
121 break;
123 default:
124 Q_ASSERT(false);
125 return;
130 void Connection::sendResponse(const Response &response) const
132 m_socket->write(toByteArray(response));
135 bool Connection::hasExpired(const qint64 timeout) const
137 return (m_socket->bytesAvailable() == 0)
138 && (m_socket->bytesToWrite() == 0)
139 && m_idleTimer.hasExpired(timeout);
142 bool Connection::isClosed() const
144 return (m_socket->state() == QAbstractSocket::UnconnectedState);
147 bool Connection::acceptsGzipEncoding(QString codings)
149 // [rfc7231] 5.3.4. Accept-Encoding
151 const auto isCodingAvailable = [](const QList<QStringView> &list, const QStringView encoding) -> bool
153 for (const QStringView &str : list)
155 if (!str.startsWith(encoding))
156 continue;
158 // without quality values
159 if (str == encoding)
160 return true;
162 // [rfc7231] 5.3.1. Quality Values
163 const QStringView substr = str.mid(encoding.size() + 3); // ex. skip over "gzip;q="
165 bool ok = false;
166 const double qvalue = substr.toDouble(&ok);
167 if (!ok || (qvalue <= 0))
168 return false;
170 return true;
172 return false;
175 const QList<QStringView> list = QStringView(codings.remove(u' ').remove(u'\t')).split(u',', Qt::SkipEmptyParts);
176 if (list.isEmpty())
177 return false;
179 const bool canGzip = isCodingAvailable(list, u"gzip"_qs);
180 if (canGzip)
181 return true;
183 const bool canAny = isCodingAvailable(list, u"*"_qs);
184 if (canAny)
185 return true;
187 return false;