mirror of
https://github.com/qbittorrent/qBittorrent
synced 2025-07-11 07:46:17 -07:00
Follow project coding style (Issue #2192).
This commit is contained in:
parent
2707f5205f
commit
898d454b78
11 changed files with 710 additions and 766 deletions
|
@ -59,8 +59,7 @@ void Connection::read()
|
|||
|
||||
Request request;
|
||||
RequestParser::ErrorCode err = RequestParser::parse(m_receivedData, request);
|
||||
switch (err)
|
||||
{
|
||||
switch (err) {
|
||||
case RequestParser::IncompleteRequest:
|
||||
// Partial request waiting for the rest
|
||||
break;
|
||||
|
|
|
@ -31,7 +31,6 @@
|
|||
|
||||
#include <QStringList>
|
||||
#include <QUrl>
|
||||
//#include <QVariant>
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
|
||||
#include <QUrlQuery>
|
||||
#endif
|
||||
|
@ -69,38 +68,32 @@ RequestParser::ErrorCode RequestParser::parseHttpRequest(const QByteArray& data,
|
|||
|
||||
// Parse HTTP request header
|
||||
const int header_end = data.indexOf(EOH);
|
||||
if (header_end < 0)
|
||||
{
|
||||
if (header_end < 0) {
|
||||
qDebug() << Q_FUNC_INFO << "incomplete request";
|
||||
return IncompleteRequest;
|
||||
}
|
||||
|
||||
if (!parseHttpHeader(data.left(header_end)))
|
||||
{
|
||||
if (!parseHttpHeader(data.left(header_end))) {
|
||||
qWarning() << Q_FUNC_INFO << "header parsing error";
|
||||
return BadRequest;
|
||||
}
|
||||
|
||||
// Parse HTTP request message
|
||||
int content_length = 0;
|
||||
if (m_request.headers.contains("content-length"))
|
||||
{
|
||||
if (m_request.headers.contains("content-length")) {
|
||||
content_length = m_request.headers["content-length"].toInt();
|
||||
if (content_length > static_cast<int>(m_maxContentLength))
|
||||
{
|
||||
if (content_length > static_cast<int>(m_maxContentLength)) {
|
||||
qWarning() << Q_FUNC_INFO << "bad request: message too long";
|
||||
return BadRequest;
|
||||
}
|
||||
|
||||
QByteArray content = data.mid(header_end + EOH.length(), content_length);
|
||||
if (content.length() < content_length)
|
||||
{
|
||||
if (content.length() < content_length) {
|
||||
qDebug() << Q_FUNC_INFO << "incomplete request";
|
||||
return IncompleteRequest;
|
||||
}
|
||||
|
||||
if (!parseContent(content))
|
||||
{
|
||||
if (!parseContent(content)) {
|
||||
qWarning() << Q_FUNC_INFO << "message parsing error";
|
||||
return BadRequest;
|
||||
}
|
||||
|
@ -118,8 +111,7 @@ bool RequestParser::parseStartingLine(const QString &line)
|
|||
{
|
||||
const QRegExp rx("^([A-Z]+)\\s+(\\S+)\\s+HTTP/\\d\\.\\d$");
|
||||
|
||||
if (rx.indexIn(line.trimmed()) >= 0)
|
||||
{
|
||||
if (rx.indexIn(line.trimmed()) >= 0) {
|
||||
m_request.method = rx.cap(1);
|
||||
|
||||
QUrl url = QUrl::fromEncoded(rx.cap(2).toLatin1());
|
||||
|
@ -131,8 +123,7 @@ bool RequestParser::parseStartingLine(const QString &line)
|
|||
#else
|
||||
QListIterator<QPair<QString, QString> > i(QUrlQuery(url).queryItems());
|
||||
#endif
|
||||
while (i.hasNext())
|
||||
{
|
||||
while (i.hasNext()) {
|
||||
QPair<QString, QString> pair = i.next();
|
||||
m_request.gets[pair.first] = pair.second;
|
||||
}
|
||||
|
@ -147,8 +138,7 @@ bool RequestParser::parseStartingLine(const QString &line)
|
|||
bool RequestParser::parseHeaderLine(const QString &line, QPair<QString, QString>& out)
|
||||
{
|
||||
int i = line.indexOf(QLatin1Char(':'));
|
||||
if (i == -1)
|
||||
{
|
||||
if (i == -1) {
|
||||
qWarning() << Q_FUNC_INFO << "invalid http header:" << line;
|
||||
return false;
|
||||
}
|
||||
|
@ -163,18 +153,14 @@ bool RequestParser::parseHttpHeader(const QByteArray &data)
|
|||
QStringList lines = str.trimmed().split(EOL);
|
||||
|
||||
QStringList headerLines;
|
||||
foreach (const QString& line, lines)
|
||||
{
|
||||
if (line[0].isSpace()) // header line continuation
|
||||
{
|
||||
if (!headerLines.isEmpty()) // really continuation
|
||||
{
|
||||
foreach (const QString& line, lines) {
|
||||
if (line[0].isSpace()) { // header line continuation
|
||||
if (!headerLines.isEmpty()) { // really continuation
|
||||
headerLines.last() += QLatin1Char(' ');
|
||||
headerLines.last() += line.trimmed();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
headerLines.append(line);
|
||||
}
|
||||
}
|
||||
|
@ -187,8 +173,7 @@ bool RequestParser::parseHttpHeader(const QByteArray &data)
|
|||
return false;
|
||||
|
||||
++it;
|
||||
for (; it != headerLines.end(); ++it)
|
||||
{
|
||||
for (; it != headerLines.end(); ++it) {
|
||||
QPair<QString, QString> header;
|
||||
if (!parseHeaderLine(*it, header))
|
||||
return false;
|
||||
|
@ -206,12 +191,10 @@ QList<QByteArray> RequestParser::splitMultipartData(const QByteArray& data, cons
|
|||
const int sepLength = sep.size();
|
||||
|
||||
int start = 0, end = 0;
|
||||
if ((end = data.indexOf(sep, start)) >= 0)
|
||||
{
|
||||
if ((end = data.indexOf(sep, start)) >= 0) {
|
||||
start = end + sepLength; // skip first boundary
|
||||
|
||||
while ((end = data.indexOf(sep, start)) >= 0)
|
||||
{
|
||||
while ((end = data.indexOf(sep, start)) >= 0) {
|
||||
ret << data.mid(start, end - start);
|
||||
start = end + sepLength;
|
||||
}
|
||||
|
@ -232,8 +215,7 @@ bool RequestParser::parseContent(const QByteArray& data)
|
|||
qDebug() << Q_FUNC_INFO << "data.size(): " << data.size();
|
||||
|
||||
// Parse url-encoded POST data
|
||||
if (m_request.headers["content-type"].startsWith("application/x-www-form-urlencoded"))
|
||||
{
|
||||
if (m_request.headers["content-type"].startsWith("application/x-www-form-urlencoded")) {
|
||||
QUrl url;
|
||||
#if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0))
|
||||
url.setEncodedQuery(data);
|
||||
|
@ -242,8 +224,7 @@ bool RequestParser::parseContent(const QByteArray& data)
|
|||
url.setQuery(data);
|
||||
QListIterator<QPair<QString, QString> > i(QUrlQuery(url).queryItems(QUrl::FullyDecoded));
|
||||
#endif
|
||||
while (i.hasNext())
|
||||
{
|
||||
while (i.hasNext()) {
|
||||
QPair<QString, QString> pair = i.next();
|
||||
m_request.posts[pair.first.toLower()] = pair.second;
|
||||
}
|
||||
|
@ -271,26 +252,21 @@ Submit Query
|
|||
--cH2ae0GI3KM7GI3Ij5ae0ei4Ij5Ij5--
|
||||
**/
|
||||
QString content_type = m_request.headers["content-type"];
|
||||
if (content_type.startsWith("multipart/form-data"))
|
||||
{
|
||||
if (content_type.startsWith("multipart/form-data")) {
|
||||
const QRegExp boundaryRegexQuoted("boundary=\"([ \\w'()+,-\\./:=\\?]+)\"");
|
||||
const QRegExp boundaryRegexNotQuoted("boundary=([\\w'()+,-\\./:=\\?]+)");
|
||||
|
||||
QByteArray boundary;
|
||||
if (boundaryRegexQuoted.indexIn(content_type) < 0)
|
||||
{
|
||||
if (boundaryRegexNotQuoted.indexIn(content_type) < 0)
|
||||
{
|
||||
if (boundaryRegexQuoted.indexIn(content_type) < 0) {
|
||||
if (boundaryRegexNotQuoted.indexIn(content_type) < 0) {
|
||||
qWarning() << "Could not find boundary in multipart/form-data header!";
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
boundary = "--" + boundaryRegexNotQuoted.cap(1).toLatin1();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
boundary = "--" + boundaryRegexQuoted.cap(1).toLatin1();
|
||||
}
|
||||
|
||||
|
@ -298,8 +274,7 @@ Submit Query
|
|||
QList<QByteArray> parts = splitMultipartData(data, boundary);
|
||||
qDebug() << parts.size() << "parts in data";
|
||||
|
||||
foreach (const QByteArray& part, parts)
|
||||
{
|
||||
foreach (const QByteArray& part, parts) {
|
||||
if (!parseFormData(part))
|
||||
return false;
|
||||
}
|
||||
|
@ -315,8 +290,7 @@ bool RequestParser::parseFormData(const QByteArray& data)
|
|||
{
|
||||
// Parse form data header
|
||||
const int header_end = data.indexOf(EOH);
|
||||
if (header_end < 0)
|
||||
{
|
||||
if (header_end < 0) {
|
||||
qDebug() << "Invalid form data: \n" << data;
|
||||
return false;
|
||||
}
|
||||
|
@ -324,8 +298,7 @@ bool RequestParser::parseFormData(const QByteArray& data)
|
|||
QString header_str = QString::fromUtf8(data.left(header_end));
|
||||
QStringList lines = header_str.trimmed().split(EOL);
|
||||
QStringMap headers;
|
||||
foreach (const QString& line, lines)
|
||||
{
|
||||
foreach (const QString& line, lines) {
|
||||
QPair<QString, QString> header;
|
||||
if (!parseHeaderLine(line, header))
|
||||
return false;
|
||||
|
@ -334,16 +307,14 @@ bool RequestParser::parseFormData(const QByteArray& data)
|
|||
}
|
||||
|
||||
QStringMap disposition;
|
||||
if (!headers.contains("content-disposition") ||
|
||||
!parseHeaderValue(headers["content-disposition"], disposition) ||
|
||||
!disposition.contains("name"))
|
||||
{
|
||||
if (!headers.contains("content-disposition")
|
||||
|| !parseHeaderValue(headers["content-disposition"], disposition)
|
||||
|| !disposition.contains("name")) {
|
||||
qDebug() << "Invalid form data header: \n" << header_str;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (disposition.contains("filename"))
|
||||
{
|
||||
if (disposition.contains("filename")) {
|
||||
UploadedFile ufile;
|
||||
ufile.filename = disposition["filename"];
|
||||
ufile.type = disposition["content-type"];
|
||||
|
@ -351,8 +322,7 @@ bool RequestParser::parseFormData(const QByteArray& data)
|
|||
|
||||
m_request.files[disposition["name"]] = ufile;
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
m_request.posts[disposition["name"]] = QString::fromUtf8(data.mid(header_end + EOH.length()));
|
||||
}
|
||||
|
||||
|
@ -364,8 +334,7 @@ bool RequestParser::parseHeaderValue(const QString& value, QStringMap& out)
|
|||
QStringList items = value.split(QLatin1Char(';'));
|
||||
out[""] = items[0];
|
||||
|
||||
for (QStringList::size_type i = 1; i < items.size(); ++i)
|
||||
{
|
||||
for (QStringList::size_type i = 1; i < items.size(); ++i) {
|
||||
int pos = items[i].indexOf("=");
|
||||
if (pos < 0)
|
||||
return false;
|
||||
|
|
|
@ -40,7 +40,12 @@ namespace Http
|
|||
class RequestParser
|
||||
{
|
||||
public:
|
||||
enum ErrorCode { NoError = 0, IncompleteRequest, BadRequest };
|
||||
enum ErrorCode
|
||||
{
|
||||
NoError = 0,
|
||||
IncompleteRequest,
|
||||
BadRequest
|
||||
};
|
||||
|
||||
// when result != NoError parsed request is undefined
|
||||
// Warning! Header names are converted to lower-case.
|
||||
|
|
|
@ -38,22 +38,17 @@ using namespace Http;
|
|||
|
||||
QByteArray ResponseGenerator::generate(Response response)
|
||||
{
|
||||
if (response.headers[HEADER_CONTENT_ENCODING] == "gzip")
|
||||
{
|
||||
if (response.headers[HEADER_CONTENT_ENCODING] == "gzip") {
|
||||
// A gzip seems to have 23 bytes overhead.
|
||||
// Also "Content-Encoding: gzip\r\n" is 26 bytes long
|
||||
// So we only benefit from gzip if the message is bigger than 23+26 = 49
|
||||
// If the message is smaller than 49 bytes we actually send MORE data if we gzip
|
||||
QByteArray dest_buf;
|
||||
if ((response.content.size() > 49) && (gCompress(response.content, dest_buf)))
|
||||
{
|
||||
response.content = dest_buf;
|
||||
}
|
||||
else
|
||||
{
|
||||
response.headers.remove(HEADER_CONTENT_ENCODING);
|
||||
}
|
||||
}
|
||||
|
||||
if (response.content.length() > 0)
|
||||
response.headers[HEADER_CONTENT_LENGTH] = QString::number(response.content.length());
|
||||
|
@ -96,14 +91,12 @@ bool gCompress(QByteArray data, QByteArray& dest_buffer)
|
|||
if (ret != Z_OK)
|
||||
return false;
|
||||
|
||||
while (strm.avail_in != 0)
|
||||
{
|
||||
while (strm.avail_in != 0) {
|
||||
ret = deflate(&strm, Z_NO_FLUSH);
|
||||
if (ret != Z_OK)
|
||||
return false;
|
||||
|
||||
if (strm.avail_out == 0)
|
||||
{
|
||||
if (strm.avail_out == 0) {
|
||||
dest_buffer.append(tmp_buf, BUFSIZE);
|
||||
strm.next_out = reinterpret_cast<unsigned char*>(tmp_buf);
|
||||
strm.avail_out = BUFSIZE;
|
||||
|
@ -111,10 +104,8 @@ bool gCompress(QByteArray data, QByteArray& dest_buffer)
|
|||
}
|
||||
|
||||
int deflate_res = Z_OK;
|
||||
while (deflate_res == Z_OK)
|
||||
{
|
||||
if (strm.avail_out == 0)
|
||||
{
|
||||
while (deflate_res == Z_OK) {
|
||||
if (strm.avail_out == 0) {
|
||||
dest_buffer.append(tmp_buf, BUFSIZE);
|
||||
strm.next_out = reinterpret_cast<unsigned char*>(tmp_buf);
|
||||
strm.avail_out = BUFSIZE;
|
||||
|
|
|
@ -80,11 +80,10 @@ void Server::incomingConnection(int socketDescriptor)
|
|||
else
|
||||
#endif
|
||||
serverSocket = new QTcpSocket(this);
|
||||
if (serverSocket->setSocketDescriptor(socketDescriptor))
|
||||
{
|
||||
|
||||
if (serverSocket->setSocketDescriptor(socketDescriptor)) {
|
||||
#ifndef QT_NO_OPENSSL
|
||||
if (m_https)
|
||||
{
|
||||
if (m_https) {
|
||||
static_cast<QSslSocket*>(serverSocket)->setProtocol(QSsl::AnyProtocol);
|
||||
static_cast<QSslSocket*>(serverSocket)->setPrivateKey(m_key);
|
||||
static_cast<QSslSocket*>(serverSocket)->setLocalCertificate(m_certificate);
|
||||
|
@ -93,8 +92,7 @@ void Server::incomingConnection(int socketDescriptor)
|
|||
#endif
|
||||
new Connection(serverSocket, m_requestHandler, this);
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
serverSocket->deleteLater();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -201,7 +201,8 @@ void QTracker::respondToAnnounceRequest()
|
|||
m_torrents[annonce_req.info_hash].remove(annonce_req.peer.qhash());
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
// Unknown torrent
|
||||
if (m_torrents.size() == MAX_TORRENTS) {
|
||||
// Reached max size, remove a random torrent
|
||||
|
|
|
@ -144,8 +144,7 @@ bool AbstractWebApplication::sessionInitialize()
|
|||
|
||||
QString sessionId;
|
||||
int pos = cookie.indexOf(SID_START);
|
||||
if (pos >= 0)
|
||||
{
|
||||
if (pos >= 0) {
|
||||
pos += SID_START.length();
|
||||
int end = cookie.indexOf(QRegExp("[,;]"), pos);
|
||||
sessionId = cookie.mid(pos, end >= 0 ? end - pos : end);
|
||||
|
@ -153,16 +152,13 @@ bool AbstractWebApplication::sessionInitialize()
|
|||
|
||||
// TODO: Additional session check
|
||||
|
||||
if (!sessionId.isNull())
|
||||
{
|
||||
if (sessions_.contains(sessionId))
|
||||
{
|
||||
if (!sessionId.isNull()) {
|
||||
if (sessions_.contains(sessionId)) {
|
||||
session_ = sessions_[sessionId];
|
||||
session_->updateTimestamp();
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
qDebug() << Q_FUNC_INFO << "session does not exist!";
|
||||
}
|
||||
}
|
||||
|
@ -179,15 +175,12 @@ bool AbstractWebApplication::readFile(const QString& path, QByteArray &data, QSt
|
|||
ext = path.mid(index);
|
||||
|
||||
// find translated file in cache
|
||||
if (translatedFiles_.contains(path))
|
||||
{
|
||||
if (translatedFiles_.contains(path)) {
|
||||
data = translatedFiles_[path];
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
QFile file(path);
|
||||
if (!file.open(QIODevice::ReadOnly))
|
||||
{
|
||||
if (!file.open(QIODevice::ReadOnly)) {
|
||||
qDebug("File %s was not found!", qPrintable(path));
|
||||
return false;
|
||||
}
|
||||
|
@ -196,15 +189,12 @@ bool AbstractWebApplication::readFile(const QString& path, QByteArray &data, QSt
|
|||
file.close();
|
||||
|
||||
// Translate the file
|
||||
if ((ext == "html") || ((ext == "js") && !path.endsWith("excanvas-compressed.js")))
|
||||
{
|
||||
if ((ext == "html") || ((ext == "js") && !path.endsWith("excanvas-compressed.js"))) {
|
||||
QString dataStr = QString::fromUtf8(data.constData());
|
||||
translateDocument(dataStr);
|
||||
|
||||
if (path.endsWith("about.html"))
|
||||
{
|
||||
dataStr.replace("${VERSION}", VERSION);
|
||||
}
|
||||
|
||||
data = dataStr.toUtf8();
|
||||
translatedFiles_[path] = data; // cashing translated file
|
||||
|
@ -227,8 +217,7 @@ QString AbstractWebApplication::generateSid()
|
|||
QString sid;
|
||||
|
||||
qsrand(QDateTime::currentDateTime().toTime_t());
|
||||
do
|
||||
{
|
||||
do {
|
||||
const size_t size = 6;
|
||||
quint32 tmp[size];
|
||||
|
||||
|
@ -260,20 +249,16 @@ void AbstractWebApplication::translateDocument(QString& data)
|
|||
const QString locale = Preferences::instance()->getLocale();
|
||||
bool isTranslationNeeded = !locale.startsWith("en") || locale.startsWith("en_AU") || locale.startsWith("en_GB");
|
||||
|
||||
while(i < data.size() && found)
|
||||
{
|
||||
while(i < data.size() && found) {
|
||||
i = regex.indexIn(data, i);
|
||||
if (i >= 0)
|
||||
{
|
||||
if (i >= 0) {
|
||||
//qDebug("Found translatable string: %s", regex.cap(1).toUtf8().data());
|
||||
QByteArray word = regex.cap(1).toUtf8();
|
||||
|
||||
QString translation = word;
|
||||
if (isTranslationNeeded)
|
||||
{
|
||||
if (isTranslationNeeded) {
|
||||
size_t context_index = 0;
|
||||
while ((context_index < context_count) && (translation == word))
|
||||
{
|
||||
while ((context_index < context_count) && (translation == word)) {
|
||||
#if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0))
|
||||
translation = qApp->translate(contexts[context_index].c_str(), word.constData(), 0, QCoreApplication::UnicodeUTF8, 1);
|
||||
#else
|
||||
|
@ -288,8 +273,7 @@ void AbstractWebApplication::translateDocument(QString& data)
|
|||
data.replace(i, regex.matchedLength(), translation);
|
||||
i += translation.length();
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
found = false; // no more translatable strings
|
||||
}
|
||||
}
|
||||
|
@ -315,8 +299,7 @@ void AbstractWebApplication::increaseFailedAttempts()
|
|||
const int nb_fail = clientFailedAttempts_.value(env_.clientAddress, 0) + 1;
|
||||
|
||||
clientFailedAttempts_[env_.clientAddress] = nb_fail;
|
||||
if (nb_fail == MAX_AUTH_FAILED_ATTEMPTS)
|
||||
{
|
||||
if (nb_fail == MAX_AUTH_FAILED_ATTEMPTS) {
|
||||
// Max number of failed attempts reached
|
||||
// Start ban period
|
||||
UnbanTimer* ubantimer = new UnbanTimer(env_.clientAddress, this);
|
||||
|
@ -347,8 +330,7 @@ void AbstractWebApplication::printFile(const QString& path)
|
|||
|
||||
bool AbstractWebApplication::sessionStart()
|
||||
{
|
||||
if (session_ == 0)
|
||||
{
|
||||
if (session_ == 0) {
|
||||
session_ = new WebSession(generateSid());
|
||||
session_->updateTimestamp();
|
||||
sessions_[session_->id] = session_;
|
||||
|
@ -365,8 +347,7 @@ bool AbstractWebApplication::sessionStart()
|
|||
|
||||
bool AbstractWebApplication::sessionEnd()
|
||||
{
|
||||
if ((session_ != 0) && (sessions_.contains(session_->id)))
|
||||
{
|
||||
if ((session_ != 0) && (sessions_.contains(session_->id))) {
|
||||
QNetworkCookie cookie(C_SID, session_->id.toUtf8());
|
||||
cookie.setPath(QLatin1String("/"));
|
||||
cookie.setExpirationDate(QDateTime::currentDateTime());
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue