WebUI: Improve table overflow handling
[qBittorrent.git] / src / base / http / connection.cpp
blob32a7ce840b303dd04f476910966a41061e881bf7
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 "irequesthandler.h"
36 #include "requestparser.h"
37 #include "responsegenerator.h"
39 using namespace Http;
41 Connection::Connection(QTcpSocket *socket, IRequestHandler *requestHandler, QObject *parent)
42 : QObject(parent)
43 , m_socket(socket)
44 , m_requestHandler(requestHandler)
46 m_socket->setParent(this);
47 connect(m_socket, &QAbstractSocket::disconnected, this, &Connection::closed);
49 // reserve common size for requests, don't use the max allowed size which is too big for
50 // memory constrained platforms
51 m_receivedData.reserve(1024 * 1024);
53 // reset timer when there are activity
54 m_idleTimer.start();
55 connect(m_socket, &QIODevice::readyRead, this, [this]()
57 m_idleTimer.start();
58 read();
59 });
60 connect(m_socket, &QIODevice::bytesWritten, this, [this]()
62 m_idleTimer.start();
63 });
66 void Connection::read()
68 // reuse existing buffer and avoid unnecessary memory allocation/relocation
69 const qsizetype previousSize = m_receivedData.size();
70 const qint64 bytesAvailable = m_socket->bytesAvailable();
71 m_receivedData.resize(previousSize + bytesAvailable);
72 const qint64 bytesRead = m_socket->read((m_receivedData.data() + previousSize), bytesAvailable);
73 if (bytesRead < 0) [[unlikely]]
75 m_socket->close();
76 return;
78 if (bytesRead < bytesAvailable) [[unlikely]]
79 m_receivedData.chop(bytesAvailable - bytesRead);
81 while (!m_receivedData.isEmpty())
83 const RequestParser::ParseResult result = RequestParser::parse(m_receivedData);
85 switch (result.status)
87 case RequestParser::ParseStatus::Incomplete:
89 const long bufferLimit = RequestParser::MAX_CONTENT_SIZE * 1.1; // some margin for headers
90 if (m_receivedData.size() > bufferLimit)
92 qWarning("%s", qUtf8Printable(tr("Http request size exceeds limitation, closing socket. Limit: %1, IP: %2")
93 .arg(QString::number(bufferLimit), m_socket->peerAddress().toString())));
95 Response resp(413, u"Payload Too Large"_s);
96 resp.headers[HEADER_CONNECTION] = u"close"_s;
98 sendResponse(resp);
99 m_socket->close();
102 return;
104 case RequestParser::ParseStatus::BadMethod:
106 qWarning("%s", qUtf8Printable(tr("Bad Http request method, closing socket. IP: %1. Method: \"%2\"")
107 .arg(m_socket->peerAddress().toString(), result.request.method)));
109 Response resp(501, u"Not Implemented"_s);
110 resp.headers[HEADER_CONNECTION] = u"close"_s;
112 sendResponse(resp);
113 m_socket->close();
115 return;
117 case RequestParser::ParseStatus::BadRequest:
119 qWarning("%s", qUtf8Printable(tr("Bad Http request, closing socket. IP: %1")
120 .arg(m_socket->peerAddress().toString())));
122 Response resp(400, u"Bad Request"_s);
123 resp.headers[HEADER_CONNECTION] = u"close"_s;
125 sendResponse(resp);
126 m_socket->close();
128 return;
130 case RequestParser::ParseStatus::OK:
132 const Environment env {m_socket->localAddress(), m_socket->localPort(), m_socket->peerAddress(), m_socket->peerPort()};
134 if (result.request.method == HEADER_REQUEST_METHOD_HEAD)
136 Request getRequest = result.request;
137 getRequest.method = HEADER_REQUEST_METHOD_GET;
139 Response resp = m_requestHandler->processRequest(getRequest, env);
141 resp.headers[HEADER_CONNECTION] = u"keep-alive"_s;
142 resp.headers[HEADER_CONTENT_LENGTH] = QString::number(resp.content.length());
143 resp.content.clear();
145 sendResponse(resp);
147 else
149 Response resp = m_requestHandler->processRequest(result.request, env);
151 if (acceptsGzipEncoding(result.request.headers.value(u"accept-encoding"_s)))
152 resp.headers[HEADER_CONTENT_ENCODING] = u"gzip"_s;
153 resp.headers[HEADER_CONNECTION] = u"keep-alive"_s;
155 sendResponse(resp);
158 m_receivedData.remove(0, result.frameSize);
160 break;
162 default:
163 Q_ASSERT(false);
164 return;
169 void Connection::sendResponse(const Response &response) const
171 m_socket->write(toByteArray(response));
174 bool Connection::hasExpired(const qint64 timeout) const
176 return (m_socket->bytesAvailable() == 0)
177 && (m_socket->bytesToWrite() == 0)
178 && m_idleTimer.hasExpired(timeout);
181 bool Connection::acceptsGzipEncoding(QString codings)
183 // [rfc7231] 5.3.4. Accept-Encoding
185 const auto isCodingAvailable = [](const QList<QStringView> &list, const QStringView encoding) -> bool
187 for (const QStringView &str : list)
189 if (!str.startsWith(encoding))
190 continue;
192 // without quality values
193 if (str == encoding)
194 return true;
196 // [rfc7231] 5.3.1. Quality Values
197 const QStringView substr = str.mid(encoding.size() + 3); // ex. skip over "gzip;q="
199 bool ok = false;
200 const double qvalue = substr.toDouble(&ok);
201 if (!ok || (qvalue <= 0))
202 return false;
204 return true;
206 return false;
209 const QList<QStringView> list = QStringView(codings.remove(u' ').remove(u'\t')).split(u',', Qt::SkipEmptyParts);
210 if (list.isEmpty())
211 return false;
213 const bool canGzip = isCodingAvailable(list, u"gzip"_s);
214 if (canGzip)
215 return true;
217 const bool canAny = isCodingAvailable(list, u"*"_s);
218 if (canAny)
219 return true;
221 return false;