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>
6  *
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.
11  *
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.
16  *
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.
20  *
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.
29  */
30 
31 #include "connection.h"
32 
33 #include <QTcpSocket>
34 
35 #include "base/logger.h"
36 #include "irequesthandler.h"
37 #include "requestparser.h"
38 #include "responsegenerator.h"
39 
40 using namespace Http;
41 
Connection(QTcpSocket * socket,IRequestHandler * requestHandler,QObject * parent)42 Connection::Connection(QTcpSocket *socket, IRequestHandler *requestHandler, QObject *parent)
43     : QObject(parent)
44     , m_socket(socket)
45     , m_requestHandler(requestHandler)
46 {
47     m_socket->setParent(this);
48     m_idleTimer.start();
49     connect(m_socket, &QTcpSocket::readyRead, this, &Connection::read);
50 }
51 
~Connection()52 Connection::~Connection()
53 {
54     m_socket->close();
55 }
56 
read()57 void Connection::read()
58 {
59     m_idleTimer.restart();
60     m_receivedData.append(m_socket->readAll());
61 
62     while (!m_receivedData.isEmpty())
63     {
64         const RequestParser::ParseResult result = RequestParser::parse(m_receivedData);
65 
66         switch (result.status)
67         {
68         case RequestParser::ParseStatus::Incomplete:
69         {
70                 const long bufferLimit = RequestParser::MAX_CONTENT_SIZE * 1.1;  // some margin for headers
71                 if (m_receivedData.size() > bufferLimit)
72                 {
73                     Logger::instance()->addMessage(tr("Http request size exceeds limitation, closing socket. Limit: %1, IP: %2")
74                         .arg(bufferLimit).arg(m_socket->peerAddress().toString()), Log::WARNING);
75 
76                     Response resp(413, "Payload Too Large");
77                     resp.headers[HEADER_CONNECTION] = "close";
78 
79                     sendResponse(resp);
80                     m_socket->close();
81                 }
82             }
83             return;
84 
85         case RequestParser::ParseStatus::BadRequest:
86         {
87                 Logger::instance()->addMessage(tr("Bad Http request, closing socket. IP: %1")
88                     .arg(m_socket->peerAddress().toString()), Log::WARNING);
89 
90                 Response resp(400, "Bad Request");
91                 resp.headers[HEADER_CONNECTION] = "close";
92 
93                 sendResponse(resp);
94                 m_socket->close();
95             }
96             return;
97 
98         case RequestParser::ParseStatus::OK:
99         {
100                 const Environment env {m_socket->localAddress(), m_socket->localPort(), m_socket->peerAddress(), m_socket->peerPort()};
101 
102                 Response resp = m_requestHandler->processRequest(result.request, env);
103 
104                 if (acceptsGzipEncoding(result.request.headers["accept-encoding"]))
105                     resp.headers[HEADER_CONTENT_ENCODING] = "gzip";
106 
107                 resp.headers[HEADER_CONNECTION] = "keep-alive";
108 
109                 sendResponse(resp);
110                 m_receivedData = m_receivedData.mid(result.frameSize);
111             }
112             break;
113 
114         default:
115             Q_ASSERT(false);
116             return;
117         }
118     }
119 }
120 
sendResponse(const Response & response) const121 void Connection::sendResponse(const Response &response) const
122 {
123     m_socket->write(toByteArray(response));
124 }
125 
hasExpired(const qint64 timeout) const126 bool Connection::hasExpired(const qint64 timeout) const
127 {
128     return m_idleTimer.hasExpired(timeout);
129 }
130 
isClosed() const131 bool Connection::isClosed() const
132 {
133     return (m_socket->state() == QAbstractSocket::UnconnectedState);
134 }
135 
acceptsGzipEncoding(QString codings)136 bool Connection::acceptsGzipEncoding(QString codings)
137 {
138     // [rfc7231] 5.3.4. Accept-Encoding
139 
140     const auto isCodingAvailable = [](const QVector<QStringRef> &list, const QString &encoding) -> bool
141     {
142         for (const QStringRef &str : list)
143         {
144             if (!str.startsWith(encoding))
145                 continue;
146 
147             // without quality values
148             if (str == encoding)
149                 return true;
150 
151             // [rfc7231] 5.3.1. Quality Values
152             const QStringRef substr = str.mid(encoding.size() + 3);  // ex. skip over "gzip;q="
153 
154             bool ok = false;
155             const double qvalue = substr.toDouble(&ok);
156             if (!ok || (qvalue <= 0))
157                 return false;
158 
159             return true;
160         }
161         return false;
162     };
163 
164     const QVector<QStringRef> list = codings.remove(' ').remove('\t').splitRef(',', QString::SkipEmptyParts);
165     if (list.isEmpty())
166         return false;
167 
168     const bool canGzip = isCodingAvailable(list, QLatin1String("gzip"));
169     if (canGzip)
170         return true;
171 
172     const bool canAny = isCodingAvailable(list, QLatin1String("*"));
173     if (canAny)
174         return true;
175 
176     return false;
177 }
178