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 "irequesthandler.h"
36 #include "requestparser.h"
37 #include "responsegenerator.h"
41 Connection::Connection(QTcpSocket
*socket
, IRequestHandler
*requestHandler
, QObject
*parent
)
44 , m_requestHandler(requestHandler
)
46 m_socket
->setParent(this);
48 // reserve common size for requests, don't use the max allowed size which is too big for
49 // memory constrained platforms
50 m_receivedData
.reserve(1024 * 1024);
52 // reset timer when there are activity
54 connect(m_socket
, &QIODevice::readyRead
, this, [this]()
59 connect(m_socket
, &QIODevice::bytesWritten
, this, [this]()
65 Connection::~Connection()
70 void Connection::read()
72 // reuse existing buffer and avoid unnecessary memory allocation/relocation
73 const qsizetype previousSize
= m_receivedData
.size();
74 const qint64 bytesAvailable
= m_socket
->bytesAvailable();
75 m_receivedData
.resize(previousSize
+ bytesAvailable
);
76 const qint64 bytesRead
= m_socket
->read((m_receivedData
.data() + previousSize
), bytesAvailable
);
77 if (Q_UNLIKELY(bytesRead
< 0))
82 if (Q_UNLIKELY(bytesRead
< bytesAvailable
))
83 m_receivedData
.chop(bytesAvailable
- bytesRead
);
85 while (!m_receivedData
.isEmpty())
87 const RequestParser::ParseResult result
= RequestParser::parse(m_receivedData
);
89 switch (result
.status
)
91 case RequestParser::ParseStatus::Incomplete
:
93 const long bufferLimit
= RequestParser::MAX_CONTENT_SIZE
* 1.1; // some margin for headers
94 if (m_receivedData
.size() > bufferLimit
)
96 qWarning("%s", qUtf8Printable(tr("Http request size exceeds limitation, closing socket. Limit: %1, IP: %2")
97 .arg(QString::number(bufferLimit
), m_socket
->peerAddress().toString())));
99 Response
resp(413, u
"Payload Too Large"_s
);
100 resp
.headers
[HEADER_CONNECTION
] = u
"close"_s
;
108 case RequestParser::ParseStatus::BadMethod
:
110 qWarning("%s", qUtf8Printable(tr("Bad Http request method, closing socket. IP: %1. Method: \"%2\"")
111 .arg(m_socket
->peerAddress().toString(), result
.request
.method
)));
113 Response
resp(501, u
"Not Implemented"_s
);
114 resp
.headers
[HEADER_CONNECTION
] = u
"close"_s
;
121 case RequestParser::ParseStatus::BadRequest
:
123 qWarning("%s", qUtf8Printable(tr("Bad Http request, closing socket. IP: %1")
124 .arg(m_socket
->peerAddress().toString())));
126 Response
resp(400, u
"Bad Request"_s
);
127 resp
.headers
[HEADER_CONNECTION
] = u
"close"_s
;
134 case RequestParser::ParseStatus::OK
:
136 const Environment env
{m_socket
->localAddress(), m_socket
->localPort(), m_socket
->peerAddress(), m_socket
->peerPort()};
138 if (result
.request
.method
== HEADER_REQUEST_METHOD_HEAD
)
140 Request getRequest
= result
.request
;
141 getRequest
.method
= HEADER_REQUEST_METHOD_GET
;
143 Response resp
= m_requestHandler
->processRequest(getRequest
, env
);
145 resp
.headers
[HEADER_CONNECTION
] = u
"keep-alive"_s
;
146 resp
.headers
[HEADER_CONTENT_LENGTH
] = QString::number(resp
.content
.length());
147 resp
.content
.clear();
153 Response resp
= m_requestHandler
->processRequest(result
.request
, env
);
155 if (acceptsGzipEncoding(result
.request
.headers
.value(u
"accept-encoding"_s
)))
156 resp
.headers
[HEADER_CONTENT_ENCODING
] = u
"gzip"_s
;
157 resp
.headers
[HEADER_CONNECTION
] = u
"keep-alive"_s
;
162 m_receivedData
.remove(0, result
.frameSize
);
173 void Connection::sendResponse(const Response
&response
) const
175 m_socket
->write(toByteArray(response
));
178 bool Connection::hasExpired(const qint64 timeout
) const
180 return (m_socket
->bytesAvailable() == 0)
181 && (m_socket
->bytesToWrite() == 0)
182 && m_idleTimer
.hasExpired(timeout
);
185 bool Connection::isClosed() const
187 return (m_socket
->state() == QAbstractSocket::UnconnectedState
);
190 bool Connection::acceptsGzipEncoding(QString codings
)
192 // [rfc7231] 5.3.4. Accept-Encoding
194 const auto isCodingAvailable
= [](const QList
<QStringView
> &list
, const QStringView encoding
) -> bool
196 for (const QStringView
&str
: list
)
198 if (!str
.startsWith(encoding
))
201 // without quality values
205 // [rfc7231] 5.3.1. Quality Values
206 const QStringView substr
= str
.mid(encoding
.size() + 3); // ex. skip over "gzip;q="
209 const double qvalue
= substr
.toDouble(&ok
);
210 if (!ok
|| (qvalue
<= 0))
218 const QList
<QStringView
> list
= QStringView(codings
.remove(u
' ').remove(u
'\t')).split(u
',', Qt::SkipEmptyParts
);
222 const bool canGzip
= isCodingAvailable(list
, u
"gzip"_s
);
226 const bool canAny
= isCodingAvailable(list
, u
"*"_s
);