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"
35 #include "base/logger.h"
36 #include "irequesthandler.h"
37 #include "requestparser.h"
38 #include "responsegenerator.h"
42 Connection::Connection(QTcpSocket
*socket
, IRequestHandler
*requestHandler
, QObject
*parent
)
45 , m_requestHandler(requestHandler
)
47 m_socket
->setParent(this);
49 connect(m_socket
, &QTcpSocket::readyRead
, this, &Connection::read
);
52 Connection::~Connection()
57 void Connection::read()
59 m_idleTimer
.restart();
60 m_receivedData
.append(m_socket
->readAll());
62 while (!m_receivedData
.isEmpty())
64 const RequestParser::ParseResult result
= RequestParser::parse(m_receivedData
);
66 switch (result
.status
)
68 case RequestParser::ParseStatus::Incomplete
:
70 const long bufferLimit
= RequestParser::MAX_CONTENT_SIZE
* 1.1; // some margin for headers
71 if (m_receivedData
.size() > bufferLimit
)
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
);
76 Response
resp(413, "Payload Too Large");
77 resp
.headers
[HEADER_CONNECTION
] = "close";
85 case RequestParser::ParseStatus::BadRequest
:
87 Logger::instance()->addMessage(tr("Bad Http request, closing socket. IP: %1")
88 .arg(m_socket
->peerAddress().toString()), Log::WARNING
);
90 Response
resp(400, "Bad Request");
91 resp
.headers
[HEADER_CONNECTION
] = "close";
98 case RequestParser::ParseStatus::OK
:
100 const Environment env
{m_socket
->localAddress(), m_socket
->localPort(), m_socket
->peerAddress(), m_socket
->peerPort()};
102 Response resp
= m_requestHandler
->processRequest(result
.request
, env
);
104 if (acceptsGzipEncoding(result
.request
.headers
["accept-encoding"]))
105 resp
.headers
[HEADER_CONTENT_ENCODING
] = "gzip";
107 resp
.headers
[HEADER_CONNECTION
] = "keep-alive";
110 m_receivedData
= m_receivedData
.mid(result
.frameSize
);
121 void Connection::sendResponse(const Response
&response
) const
123 m_socket
->write(toByteArray(response
));
126 bool Connection::hasExpired(const qint64 timeout
) const
128 return m_idleTimer
.hasExpired(timeout
);
131 bool Connection::isClosed() const
133 return (m_socket
->state() == QAbstractSocket::UnconnectedState
);
136 bool Connection::acceptsGzipEncoding(QString codings
)
138 // [rfc7231] 5.3.4. Accept-Encoding
140 const auto isCodingAvailable
= [](const QList
<QStringView
> &list
, const QStringView encoding
) -> bool
142 for (const QStringView
&str
: list
)
144 if (!str
.startsWith(encoding
))
147 // without quality values
151 // [rfc7231] 5.3.1. Quality Values
152 const QStringView substr
= str
.mid(encoding
.size() + 3); // ex. skip over "gzip;q="
155 const double qvalue
= substr
.toDouble(&ok
);
156 if (!ok
|| (qvalue
<= 0))
164 const QList
<QStringView
> list
= QStringView(codings
.remove(' ').remove('\t')).split(u
',', Qt::SkipEmptyParts
);
168 const bool canGzip
= isCodingAvailable(list
, QString::fromLatin1("gzip"));
172 const bool canAny
= isCodingAvailable(list
, QString::fromLatin1("*"));