2014-08-22 23:08:44 +04:00
|
|
|
/*
|
|
|
|
* Bittorrent Client using Qt and libtorrent.
|
2017-10-14 16:27:21 +03:00
|
|
|
* Copyright (C) 2014, 2017 Vladimir Golovnev <glassez@yandex.ru>
|
2014-08-22 23:08:44 +04:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* In addition, as a special exception, the copyright holders give permission to
|
|
|
|
* link this program with the OpenSSL project's "OpenSSL" library (or with
|
|
|
|
* modified versions of it that use the same license as the "OpenSSL" library),
|
|
|
|
* and distribute the linked executables. You must obey the GNU General Public
|
|
|
|
* License in all respects for all of the code used other than "OpenSSL". If you
|
|
|
|
* modify file(s), you may extend this exception to your version of the file(s),
|
|
|
|
* but you are not obligated to do so. If you do not wish to do so, delete this
|
|
|
|
* exception statement from your version.
|
|
|
|
*/
|
|
|
|
|
2017-10-14 16:27:21 +03:00
|
|
|
#pragma once
|
2014-08-22 23:08:44 +04:00
|
|
|
|
2017-10-14 16:27:21 +03:00
|
|
|
#include <QDateTime>
|
|
|
|
#include <QHash>
|
|
|
|
#include <QMap>
|
|
|
|
#include <QObject>
|
|
|
|
#include <QRegularExpression>
|
|
|
|
#include <QSet>
|
2014-08-22 23:08:44 +04:00
|
|
|
|
2017-10-14 16:27:21 +03:00
|
|
|
#include "api/isessionmanager.h"
|
|
|
|
#include "base/http/irequesthandler.h"
|
|
|
|
#include "base/http/responsebuilder.h"
|
|
|
|
#include "base/http/types.h"
|
|
|
|
#include "base/utils/version.h"
|
|
|
|
|
|
|
|
constexpr Utils::Version<int, 3, 2> API_VERSION {2, 0, 0};
|
|
|
|
constexpr int COMPAT_API_VERSION = 18;
|
|
|
|
constexpr int COMPAT_API_VERSION_MIN = 18;
|
|
|
|
|
|
|
|
class APIController;
|
|
|
|
class WebApplication;
|
|
|
|
|
|
|
|
constexpr char C_SID[] = "SID"; // name of session id cookie
|
|
|
|
constexpr int INACTIVE_TIME = 900; // Session inactive time (in secs = 15 min.)
|
|
|
|
|
|
|
|
class WebSession : public ISession
|
|
|
|
{
|
|
|
|
friend class WebApplication;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit WebSession(const QString &sid);
|
|
|
|
|
|
|
|
QString id() const override;
|
2018-03-08 20:38:03 +03:00
|
|
|
qint64 timestamp() const;
|
2017-10-14 16:27:21 +03:00
|
|
|
|
|
|
|
QVariant getData(const QString &id) const override;
|
|
|
|
void setData(const QString &id, const QVariant &data) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
void updateTimestamp();
|
|
|
|
|
|
|
|
const QString m_sid;
|
2018-03-08 20:38:03 +03:00
|
|
|
qint64 m_timestamp;
|
2017-10-14 16:27:21 +03:00
|
|
|
QVariantHash m_data;
|
|
|
|
};
|
|
|
|
|
|
|
|
class WebApplication
|
|
|
|
: public QObject, public Http::IRequestHandler, public ISessionManager
|
|
|
|
, private Http::ResponseBuilder
|
2014-08-22 23:08:44 +04:00
|
|
|
{
|
2017-10-14 16:27:21 +03:00
|
|
|
Q_OBJECT
|
2015-01-06 22:42:44 +03:00
|
|
|
Q_DISABLE_COPY(WebApplication)
|
2014-08-22 23:08:44 +04:00
|
|
|
|
2017-10-14 16:27:21 +03:00
|
|
|
#ifndef Q_MOC_RUN
|
|
|
|
#define WEBAPI_PUBLIC
|
|
|
|
#define WEBAPI_PRIVATE
|
|
|
|
#endif
|
|
|
|
|
2014-08-22 23:08:44 +04:00
|
|
|
public:
|
2017-09-07 03:00:04 +03:00
|
|
|
explicit WebApplication(QObject *parent = nullptr);
|
2017-10-14 16:27:21 +03:00
|
|
|
~WebApplication() override;
|
|
|
|
|
2018-03-06 18:41:18 +03:00
|
|
|
Http::Response processRequest(const Http::Request &request, const Http::Environment &env) override;
|
2017-10-14 16:27:21 +03:00
|
|
|
|
|
|
|
QString clientId() const override;
|
|
|
|
WebSession *session() override;
|
|
|
|
void sessionStart() override;
|
|
|
|
void sessionEnd() override;
|
|
|
|
|
|
|
|
const Http::Request &request() const;
|
|
|
|
const Http::Environment &env() const;
|
2014-08-22 23:08:44 +04:00
|
|
|
|
|
|
|
private:
|
2017-10-14 16:27:21 +03:00
|
|
|
void doProcessRequest();
|
|
|
|
void configure();
|
|
|
|
|
|
|
|
void registerAPIController(const QString &scope, APIController *controller);
|
|
|
|
void declarePublicAPI(const QString &apiPath);
|
|
|
|
|
|
|
|
void sendFile(const QString &path);
|
|
|
|
void sendWebUIFile();
|
2014-08-22 23:08:44 +04:00
|
|
|
|
2017-10-14 16:27:21 +03:00
|
|
|
// Session management
|
|
|
|
QString generateSid() const;
|
|
|
|
void sessionInitialize();
|
|
|
|
bool isAuthNeeded();
|
|
|
|
bool isPublicAPI(const QString &scope, const QString &action) const;
|
|
|
|
|
|
|
|
bool isCrossSiteRequest(const Http::Request &request) const;
|
|
|
|
bool validateHostHeader(const QStringList &domains) const;
|
|
|
|
|
|
|
|
// Persistent data
|
|
|
|
QMap<QString, WebSession *> m_sessions;
|
|
|
|
|
|
|
|
// Current data
|
|
|
|
WebSession *m_currentSession = nullptr;
|
|
|
|
Http::Request m_request;
|
|
|
|
Http::Environment m_env;
|
2018-03-01 16:57:44 +03:00
|
|
|
QMap<QString, QString> m_params;
|
2017-10-14 16:27:21 +03:00
|
|
|
|
|
|
|
const QRegularExpression m_apiPathPattern {(QLatin1String("^/api/v2/(?<scope>[A-Za-z_][A-Za-z_0-9]*)/(?<action>[A-Za-z_][A-Za-z_0-9]*)$"))};
|
2018-02-27 20:24:26 +03:00
|
|
|
const QRegularExpression m_apiLegacyPathPattern {QLatin1String("^/(?<action>((sync|command|query)/[A-Za-z_][A-Za-z_0-9]*|login|logout))(/(?<hash>[^/]+))?$")};
|
2017-10-14 16:27:21 +03:00
|
|
|
|
|
|
|
QHash<QString, APIController *> m_apiControllers;
|
|
|
|
QSet<QString> m_publicAPIs;
|
|
|
|
bool m_isAltUIUsed = false;
|
|
|
|
QString m_rootFolder;
|
|
|
|
QStringList m_domainList;
|
|
|
|
|
|
|
|
struct TranslatedFile
|
|
|
|
{
|
|
|
|
QByteArray data;
|
|
|
|
QDateTime lastModified;
|
|
|
|
};
|
|
|
|
QMap<QString, TranslatedFile> m_translatedFiles;
|
|
|
|
};
|