Skip to content

Commit d07ece5

Browse files
authored
Merge pull request #7610 from glassez/webapi2
Redesign Web API
2 parents afd2f6b + 27d8dbf commit d07ece5

File tree

96 files changed

+4911
-3029
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

96 files changed

+4911
-3029
lines changed

src/base/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ bittorrent/torrentinfo.h
1919
bittorrent/tracker.h
2020
bittorrent/trackerentry.h
2121
http/connection.h
22+
http/httperror.h
2223
http/irequesthandler.h
2324
http/requestparser.h
2425
http/responsebuilder.h
@@ -51,6 +52,7 @@ utils/random.h
5152
utils/string.h
5253
utils/version.h
5354
asyncfilestorage.h
55+
exceptions.h
5456
filesystemwatcher.h
5557
global.h
5658
iconprovider.h
@@ -84,6 +86,7 @@ bittorrent/torrentinfo.cpp
8486
bittorrent/tracker.cpp
8587
bittorrent/trackerentry.cpp
8688
http/connection.cpp
89+
http/httperror.cpp
8790
http/requestparser.cpp
8891
http/responsebuilder.cpp
8992
http/responsegenerator.cpp
@@ -113,6 +116,7 @@ utils/net.cpp
113116
utils/random.cpp
114117
utils/string.cpp
115118
asyncfilestorage.cpp
119+
exceptions.cpp
116120
filesystemwatcher.cpp
117121
iconprovider.cpp
118122
logger.cpp

src/base/base.pri

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ HEADERS += \
1717
$$PWD/bittorrent/torrentinfo.h \
1818
$$PWD/bittorrent/tracker.h \
1919
$$PWD/bittorrent/trackerentry.h \
20+
$$PWD/exceptions.h \
2021
$$PWD/filesystemwatcher.h \
2122
$$PWD/global.h \
2223
$$PWD/http/connection.h \
24+
$$PWD/http/httperror.h \
2325
$$PWD/http/irequesthandler.h \
2426
$$PWD/http/requestparser.h \
2527
$$PWD/http/responsebuilder.h \
@@ -82,8 +84,10 @@ SOURCES += \
8284
$$PWD/bittorrent/torrentinfo.cpp \
8385
$$PWD/bittorrent/tracker.cpp \
8486
$$PWD/bittorrent/trackerentry.cpp \
87+
$$PWD/exceptions.cpp \
8588
$$PWD/filesystemwatcher.cpp \
8689
$$PWD/http/connection.cpp \
90+
$$PWD/http/httperror.cpp \
8791
$$PWD/http/requestparser.cpp \
8892
$$PWD/http/responsebuilder.cpp \
8993
$$PWD/http/responsegenerator.cpp \

src/base/bittorrent/torrentinfo.cpp

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,23 +60,32 @@ TorrentInfo &TorrentInfo::operator=(const TorrentInfo &other)
6060
return *this;
6161
}
6262

63-
TorrentInfo TorrentInfo::loadFromFile(const QString &path, QString &error)
63+
TorrentInfo TorrentInfo::load(const QByteArray &data, QString *error) noexcept
6464
{
65-
error.clear();
6665
libt::error_code ec;
67-
TorrentInfo info(NativePtr(new libt::torrent_info(Utils::Fs::toNativePath(path).toStdString(), ec)));
68-
if (ec) {
69-
error = QString::fromUtf8(ec.message().c_str());
70-
qDebug("Cannot load .torrent file: %s", qUtf8Printable(error));
66+
TorrentInfo info(NativePtr(new libt::torrent_info(data.constData(), data.size(), ec)));
67+
if (error) {
68+
if (ec)
69+
*error = QString::fromStdString(ec.message());
70+
else
71+
error->clear();
7172
}
7273

7374
return info;
7475
}
7576

76-
TorrentInfo TorrentInfo::loadFromFile(const QString &path)
77+
TorrentInfo TorrentInfo::loadFromFile(const QString &path, QString *error) noexcept
7778
{
78-
QString error;
79-
return loadFromFile(path, error);
79+
libt::error_code ec;
80+
TorrentInfo info(NativePtr(new libt::torrent_info(Utils::Fs::toNativePath(path).toStdString(), ec)));
81+
if (error) {
82+
if (ec)
83+
*error = QString::fromStdString(ec.message());
84+
else
85+
error->clear();
86+
}
87+
88+
return info;
8089
}
8190

8291
bool TorrentInfo::isValid() const

src/base/bittorrent/torrentinfo.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ namespace BitTorrent
6363
explicit TorrentInfo(NativeConstPtr nativeInfo = NativeConstPtr());
6464
TorrentInfo(const TorrentInfo &other);
6565

66-
static TorrentInfo loadFromFile(const QString &path, QString &error);
67-
static TorrentInfo loadFromFile(const QString &path);
66+
static TorrentInfo load(const QByteArray &data, QString *error = nullptr) noexcept;
67+
static TorrentInfo loadFromFile(const QString &path, QString *error = nullptr) noexcept;
6868

6969
TorrentInfo &operator=(const TorrentInfo &other);
7070

src/base/bittorrent/tracker.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ libtorrent::entry Peer::toEntry(bool noPeerId) const
7474
// Tracker
7575

7676
Tracker::Tracker(QObject *parent)
77-
: Http::ResponseBuilder(parent)
77+
: QObject(parent)
7878
, m_server(new Http::Server(this, this))
7979
{
8080
}

src/base/bittorrent/tracker.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#define BITTORRENT_TRACKER_H
3232

3333
#include <QHash>
34+
#include <QObject>
3435

3536
#include "base/http/irequesthandler.h"
3637
#include "base/http/responsebuilder.h"
@@ -75,7 +76,7 @@ namespace BitTorrent
7576

7677
/* Basic Bittorrent tracker implementation in Qt */
7778
/* Following http://wiki.theory.org/BitTorrent_Tracker_Protocol */
78-
class Tracker : public Http::ResponseBuilder, public Http::IRequestHandler
79+
class Tracker : public QObject, public Http::IRequestHandler, private Http::ResponseBuilder
7980
{
8081
Q_OBJECT
8182
Q_DISABLE_COPY(Tracker)

src/base/exceptions.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Bittorrent Client using Qt and libtorrent.
3+
* Copyright (C) 2018 Vladimir Golovnev <glassez@yandex.ru>
4+
*
5+
* This program is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU General Public License
7+
* as published by the Free Software Foundation; either version 2
8+
* of the License, or (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18+
*
19+
* In addition, as a special exception, the copyright holders give permission to
20+
* link this program with the OpenSSL project's "OpenSSL" library (or with
21+
* modified versions of it that use the same license as the "OpenSSL" library),
22+
* and distribute the linked executables. You must obey the GNU General Public
23+
* License in all respects for all of the code used other than "OpenSSL". If you
24+
* modify file(s), you may extend this exception to your version of the file(s),
25+
* but you are not obligated to do so. If you do not wish to do so, delete this
26+
* exception statement from your version.
27+
*/
28+
29+
#include "exceptions.h"
30+
31+
RuntimeError::RuntimeError(const QString &message)
32+
: std::runtime_error {message.toUtf8().data()}
33+
, m_message {message}
34+
{
35+
}
36+
37+
QString RuntimeError::message() const
38+
{
39+
return m_message;
40+
}
Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
2-
* Bittorrent Client using Qt4 and libtorrent.
3-
* Copyright (C) 2006-2012 Ishan Arora and Christophe Dumez
2+
* Bittorrent Client using Qt and libtorrent.
3+
* Copyright (C) 2018 Vladimir Golovnev <glassez@yandex.ru>
44
*
55
* This program is free software; you can redistribute it and/or
66
* modify it under the terms of the GNU General Public License
@@ -24,24 +24,19 @@
2424
* modify file(s), you may extend this exception to your version of the file(s),
2525
* but you are not obligated to do so. If you do not wish to do so, delete this
2626
* exception statement from your version.
27-
*
28-
* Contact : chris@qbittorrent.org
2927
*/
3028

31-
#ifndef PREFJSON_H
32-
#define PREFJSON_H
29+
#pragma once
3330

31+
#include <stdexcept>
3432
#include <QString>
3533

36-
class prefjson
34+
class RuntimeError : public std::runtime_error
3735
{
38-
private:
39-
prefjson();
40-
4136
public:
42-
static QByteArray getPreferences();
43-
static void setPreferences(const QString& json);
37+
explicit RuntimeError(const QString &message = "");
38+
QString message() const;
4439

40+
private:
41+
const QString m_message;
4542
};
46-
47-
#endif // PREFJSON_H

src/base/http/httperror.cpp

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Bittorrent Client using Qt and libtorrent.
3+
* Copyright (C) 2017 Vladimir Golovnev <glassez@yandex.ru>
4+
*
5+
* This program is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU General Public License
7+
* as published by the Free Software Foundation; either version 2
8+
* of the License, or (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18+
*
19+
* In addition, as a special exception, the copyright holders give permission to
20+
* link this program with the OpenSSL project's "OpenSSL" library (or with
21+
* modified versions of it that use the same license as the "OpenSSL" library),
22+
* and distribute the linked executables. You must obey the GNU General Public
23+
* License in all respects for all of the code used other than "OpenSSL". If you
24+
* modify file(s), you may extend this exception to your version of the file(s),
25+
* but you are not obligated to do so. If you do not wish to do so, delete this
26+
* exception statement from your version.
27+
*/
28+
29+
#include "httperror.h"
30+
31+
HTTPError::HTTPError(int statusCode, const QString &statusText, const QString &message)
32+
: RuntimeError {message}
33+
, m_statusCode {statusCode}
34+
, m_statusText {statusText}
35+
{
36+
}
37+
38+
int HTTPError::statusCode() const
39+
{
40+
return m_statusCode;
41+
}
42+
43+
QString HTTPError::statusText() const
44+
{
45+
return m_statusText;
46+
}
47+
48+
BadRequestHTTPError::BadRequestHTTPError(const QString &message)
49+
: HTTPError(400, QLatin1String("Bad Request"), message)
50+
{
51+
}
52+
53+
ConflictHTTPError::ConflictHTTPError(const QString &message)
54+
: HTTPError(409, QLatin1String("Conflict"), message)
55+
{
56+
}
57+
58+
ForbiddenHTTPError::ForbiddenHTTPError(const QString &message)
59+
: HTTPError(403, QLatin1String("Forbidden"), message)
60+
{
61+
}
62+
63+
NotFoundHTTPError::NotFoundHTTPError(const QString &message)
64+
: HTTPError(404, QLatin1String("Not Found"), message)
65+
{
66+
}
67+
68+
UnsupportedMediaTypeHTTPError::UnsupportedMediaTypeHTTPError(const QString &message)
69+
: HTTPError(415, QLatin1String("Unsupported Media Type"), message)
70+
{
71+
}
72+
73+
UnauthorizedHTTPError::UnauthorizedHTTPError(const QString &message)
74+
: HTTPError(401, QLatin1String("Unauthorized"), message)
75+
{
76+
}
77+
78+
InternalServerErrorHTTPError::InternalServerErrorHTTPError(const QString &message)
79+
: HTTPError(500, QLatin1String("Internal Server Error"), message)
80+
{
81+
}

src/base/http/httperror.h

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Bittorrent Client using Qt and libtorrent.
3+
* Copyright (C) 2017 Vladimir Golovnev <glassez@yandex.ru>
4+
*
5+
* This program is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU General Public License
7+
* as published by the Free Software Foundation; either version 2
8+
* of the License, or (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18+
*
19+
* In addition, as a special exception, the copyright holders give permission to
20+
* link this program with the OpenSSL project's "OpenSSL" library (or with
21+
* modified versions of it that use the same license as the "OpenSSL" library),
22+
* and distribute the linked executables. You must obey the GNU General Public
23+
* License in all respects for all of the code used other than "OpenSSL". If you
24+
* modify file(s), you may extend this exception to your version of the file(s),
25+
* but you are not obligated to do so. If you do not wish to do so, delete this
26+
* exception statement from your version.
27+
*/
28+
29+
#pragma once
30+
31+
#include "base/exceptions.h"
32+
33+
class HTTPError : public RuntimeError
34+
{
35+
public:
36+
HTTPError(int statusCode, const QString &statusText, const QString &message = "");
37+
38+
int statusCode() const;
39+
QString statusText() const;
40+
41+
private:
42+
const int m_statusCode;
43+
const QString m_statusText;
44+
};
45+
46+
class BadRequestHTTPError : public HTTPError
47+
{
48+
public:
49+
explicit BadRequestHTTPError(const QString &message = "");
50+
};
51+
52+
class ForbiddenHTTPError : public HTTPError
53+
{
54+
public:
55+
explicit ForbiddenHTTPError(const QString &message = "");
56+
};
57+
58+
class NotFoundHTTPError : public HTTPError
59+
{
60+
public:
61+
explicit NotFoundHTTPError(const QString &message = "");
62+
};
63+
64+
class ConflictHTTPError : public HTTPError
65+
{
66+
public:
67+
explicit ConflictHTTPError(const QString &message = "");
68+
};
69+
70+
class UnsupportedMediaTypeHTTPError : public HTTPError
71+
{
72+
public:
73+
explicit UnsupportedMediaTypeHTTPError(const QString &message = "");
74+
};
75+
76+
class UnauthorizedHTTPError : public HTTPError
77+
{
78+
public:
79+
explicit UnauthorizedHTTPError(const QString &message = "");
80+
};
81+
82+
class InternalServerErrorHTTPError : public HTTPError
83+
{
84+
public:
85+
explicit InternalServerErrorHTTPError(const QString &message = "");
86+
};

0 commit comments

Comments
 (0)