From 9c66c9b30f184169da764d12d8726cd6f6b0c57f Mon Sep 17 00:00:00 2001 From: XMRig Date: Sat, 30 Mar 2019 02:26:30 +0700 Subject: [PATCH] Added classes IApiRequest, ApiRequest, HttpApiRequest. --- CMakeLists.txt | 11 ++-- src/api/Api.cpp | 12 +++++ src/api/Api.h | 2 + src/api/interfaces/IApiRequest.h | 72 +++++++++++++++++++++++++++ src/api/requests/ApiRequest.cpp | 39 +++++++++++++++ src/api/requests/ApiRequest.h | 67 +++++++++++++++++++++++++ src/api/requests/HttpApiRequest.cpp | 55 ++++++++++++++++++++ src/api/requests/HttpApiRequest.h | 67 +++++++++++++++++++++++++ src/base/net/http/HttpApiResponse.cpp | 19 +++++-- 9 files changed, 335 insertions(+), 9 deletions(-) create mode 100644 src/api/interfaces/IApiRequest.h create mode 100644 src/api/requests/ApiRequest.cpp create mode 100644 src/api/requests/ApiRequest.h create mode 100644 src/api/requests/HttpApiRequest.cpp create mode 100644 src/api/requests/HttpApiRequest.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 1522da861..57df6d59c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -204,15 +204,14 @@ endif() if (WITH_HTTPD) set(HTTPD_SOURCES src/api/Api.h -# src/api/ApiRouter.h -# src/common/api/HttpBody.h + src/api/interfaces/IApiRequest.h + src/api/requests/ApiRequest.h + src/api/requests/ApiRequest.cpp + src/api/requests/HttpApiRequest.h src/api/Httpd.h -# src/common/api/HttpReply.h -# src/common/api/HttpRequest.h src/api/Api.cpp -# src/api/ApiRouter.cpp + src/api/requests/HttpApiRequest.cpp src/api/Httpd.cpp -# src/common/api/HttpRequest.cpp ) else() set(HTTPD_SOURCES "") diff --git a/src/api/Api.cpp b/src/api/Api.cpp index 84463922c..013090021 100644 --- a/src/api/Api.cpp +++ b/src/api/Api.cpp @@ -26,7 +26,9 @@ #include +#include "3rdparty/http-parser/http_parser.h" #include "api/Api.h" +#include "api/requests/HttpApiRequest.h" #include "base/tools/Buffer.h" #include "common/crypto/keccak.h" #include "core/Config.h" @@ -61,7 +63,9 @@ xmrig::Api::~Api() void xmrig::Api::request(const HttpRequest &req) { + HttpApiRequest request(req, m_controller->config()->http().isRestricted()); + exec(request); } @@ -96,6 +100,14 @@ void xmrig::Api::onConfigChanged(Config *config, Config *previousConfig) } +void xmrig::Api::exec(IApiRequest &request) +{ + if (request.isNew()) { + request.done(HTTP_STATUS_NOT_FOUND); + } +} + + void xmrig::Api::genId(const String &id) { memset(m_id, 0, sizeof(m_id)); diff --git a/src/api/Api.h b/src/api/Api.h index a46e95c1d..04d923939 100644 --- a/src/api/Api.h +++ b/src/api/Api.h @@ -35,6 +35,7 @@ namespace xmrig { class Controller; class Httpd; class HttpRequest; +class IApiRequest; class String; @@ -55,6 +56,7 @@ protected: void onConfigChanged(Config *config, Config *previousConfig) override; private: + void exec(IApiRequest &request); void genId(const String &id); void genWorkerId(const String &id); diff --git a/src/api/interfaces/IApiRequest.h b/src/api/interfaces/IApiRequest.h new file mode 100644 index 000000000..2c2f56344 --- /dev/null +++ b/src/api/interfaces/IApiRequest.h @@ -0,0 +1,72 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2016-2018 XMRig + * + * 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 3 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, see . + */ + +#ifndef XMRIG_IAPIREQUEST_H +#define XMRIG_IAPIREQUEST_H + + +#include "rapidjson/fwd.h" + + +namespace xmrig { + + +class String; + + +class IApiRequest +{ +public: + enum Method { + METHOD_DELETE, + METHOD_GET, + METHOD_HEAD, + METHOD_POST, + METHOD_PUT + }; + + + enum Source { + SOURCE_HTTP + }; + + + virtual ~IApiRequest() = default; + + virtual bool isDone() const = 0; + virtual bool isNew() const = 0; + virtual bool isRestricted() const = 0; + virtual const rapidjson::Value &json() const = 0; + virtual const String &url() const = 0; + virtual Method method() const = 0; + virtual rapidjson::Document &doc() = 0; + virtual rapidjson::Value &reply() = 0; + virtual Source source() const = 0; + virtual void accept() = 0; + virtual void done(int status) = 0; +}; + + +} /* namespace xmrig */ + + +#endif // XMRIG_IAPIREQUEST_H diff --git a/src/api/requests/ApiRequest.cpp b/src/api/requests/ApiRequest.cpp new file mode 100644 index 000000000..c092a3340 --- /dev/null +++ b/src/api/requests/ApiRequest.cpp @@ -0,0 +1,39 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , + * + * 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 3 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, see . + */ + + +#include "api/requests/ApiRequest.h" + + +xmrig::ApiRequest::ApiRequest(Source source, bool restricted) : + m_restricted(restricted), + m_source(source), + m_state(STATE_NEW) +{ +} + + +xmrig::ApiRequest::~ApiRequest() +{ +} diff --git a/src/api/requests/ApiRequest.h b/src/api/requests/ApiRequest.h new file mode 100644 index 000000000..1754aa9c6 --- /dev/null +++ b/src/api/requests/ApiRequest.h @@ -0,0 +1,67 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , + * + * 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 3 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, see . + */ + + +#ifndef XMRIG_APIREQUEST_H +#define XMRIG_APIREQUEST_H + + +#include "api/interfaces/IApiRequest.h" + + +namespace xmrig { + + +class ApiRequest : public IApiRequest +{ +public: + ApiRequest(Source source, bool restricted); + ~ApiRequest() override; + +protected: + inline bool isDone() const override { return m_state == STATE_DONE; } + inline bool isNew() const override { return m_state == STATE_NEW; } + inline bool isRestricted() const override { return m_restricted; } + inline Source source() const override { return m_source; } + inline void accept() override { m_state = STATE_ACCEPTED; } + inline void done(int) override { m_state = STATE_DONE; } + +private: + enum State { + STATE_NEW, + STATE_ACCEPTED, + STATE_DONE + }; + + bool m_restricted; + Source m_source; + State m_state; +}; + + +} // namespace xmrig + + +#endif // XMRIG_APIREQUEST_H + diff --git a/src/api/requests/HttpApiRequest.cpp b/src/api/requests/HttpApiRequest.cpp new file mode 100644 index 000000000..fac69f51c --- /dev/null +++ b/src/api/requests/HttpApiRequest.cpp @@ -0,0 +1,55 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , + * + * 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 3 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, see . + */ + + +#include "api/requests/HttpApiRequest.h" +#include "base/net/http/HttpRequest.h" + + +xmrig::HttpApiRequest::HttpApiRequest(const HttpRequest &req, bool restricted) : + ApiRequest(SOURCE_HTTP, restricted), + m_req(req), + m_res(req.id()), + m_url(req.url.c_str()) +{ +} + + +xmrig::IApiRequest::Method xmrig::HttpApiRequest::method() const +{ + return static_cast(m_req.method); +} + + +void xmrig::HttpApiRequest::done(int status) +{ + ApiRequest::done(status); + + if (status >= 400) { + reply().AddMember("status", status, doc().GetAllocator()); + } + + m_res.setStatus(status); + m_res.end(); +} diff --git a/src/api/requests/HttpApiRequest.h b/src/api/requests/HttpApiRequest.h new file mode 100644 index 000000000..dbe640ef3 --- /dev/null +++ b/src/api/requests/HttpApiRequest.h @@ -0,0 +1,67 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2017-2018 XMR-Stak , + * Copyright 2018-2019 SChernykh + * Copyright 2016-2019 XMRig , + * + * 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 3 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, see . + */ + + +#ifndef XMRIG_HTTPAPIREQUEST_H +#define XMRIG_HTTPAPIREQUEST_H + + +#include "api/requests/ApiRequest.h" +#include "base/net/http/HttpApiResponse.h" +#include "base/tools/String.h" + + +namespace xmrig { + + +class HttpRequest; + + +class HttpApiRequest : public ApiRequest +{ +public: + HttpApiRequest(const HttpRequest &req, bool restricted); + +protected: + inline const rapidjson::Value &json() const override { return m_body; } + inline rapidjson::Document &doc() override { return m_res.doc(); } + inline rapidjson::Value &reply() override { return m_res.doc(); } + inline const String &url() const override { return m_url; } + + Method method() const override; + void done(int status) override; + +private: + const HttpRequest &m_req; + HttpApiResponse m_res; + rapidjson::Document m_body; + String m_url; +}; + + +} // namespace xmrig + + +#endif // XMRIG_HTTPAPIREQUEST_H + diff --git a/src/base/net/http/HttpApiResponse.cpp b/src/base/net/http/HttpApiResponse.cpp index 56dd9bb8e..bf91445a9 100644 --- a/src/base/net/http/HttpApiResponse.cpp +++ b/src/base/net/http/HttpApiResponse.cpp @@ -30,6 +30,14 @@ #include "rapidjson/stringbuffer.h" +namespace xmrig { + +static const char *kError = "error"; +static const char *kStatus = "status"; + +} // namespace xmrig + + xmrig::HttpApiResponse::HttpApiResponse(uint64_t id) : HttpResponse(id), m_doc(rapidjson::kObjectType) @@ -53,9 +61,14 @@ void xmrig::HttpApiResponse::end() setHeader("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE"); setHeader("Access-Control-Allow-Headers", "Authorization, Content-Type"); - if (statusCode() >= 400 && !m_doc.MemberCount()) { - m_doc.AddMember("status", statusCode(), m_doc.GetAllocator()); - m_doc.AddMember("error", StringRef(http_status_str(static_cast(statusCode()))), m_doc.GetAllocator()); + if (statusCode() >= 400) { + if (!m_doc.HasMember(kStatus)) { + m_doc.AddMember(StringRef(kStatus), statusCode(), m_doc.GetAllocator()); + } + + if (!m_doc.HasMember(kError)) { + m_doc.AddMember(StringRef(kError), StringRef(http_status_str(static_cast(statusCode()))), m_doc.GetAllocator()); + } } if (!m_doc.MemberCount()) {