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);
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
55 connect(m_socket
, &QIODevice::readyRead
, this, [this]()
60 connect(m_socket
, &QIODevice::bytesWritten
, this, [this]()
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
]]
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
;
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
;
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
;
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();
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
;
158 m_receivedData
.remove(0, result
.frameSize
);
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
))
192 // without quality values
196 // [rfc7231] 5.3.1. Quality Values
197 const QStringView substr
= str
.mid(encoding
.size() + 3); // ex. skip over "gzip;q="
200 const double qvalue
= substr
.toDouble(&ok
);
201 if (!ok
|| (qvalue
<= 0))
209 const QList
<QStringView
> list
= QStringView(codings
.remove(u
' ').remove(u
'\t')).split(u
',', Qt::SkipEmptyParts
);
213 const bool canGzip
= isCodingAvailable(list
, u
"gzip"_s
);
217 const bool canAny
= isCodingAvailable(list
, u
"*"_s
);