mirror of
https://github.com/xmrig/xmrig.git
synced 2024-12-22 19:49:36 +00:00
Added classes IApiRequest, ApiRequest, HttpApiRequest.
This commit is contained in:
parent
dd036368e2
commit
9c66c9b30f
9 changed files with 335 additions and 9 deletions
|
@ -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 "")
|
||||
|
|
|
@ -26,7 +26,9 @@
|
|||
#include <uv.h>
|
||||
|
||||
|
||||
#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));
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
72
src/api/interfaces/IApiRequest.h
Normal file
72
src/api/interfaces/IApiRequest.h
Normal file
|
@ -0,0 +1,72 @@
|
|||
/* XMRig
|
||||
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
|
||||
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
|
||||
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
|
||||
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
|
||||
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
|
||||
* Copyright 2016-2018 XMRig <support@xmrig.com>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#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
|
39
src/api/requests/ApiRequest.cpp
Normal file
39
src/api/requests/ApiRequest.cpp
Normal file
|
@ -0,0 +1,39 @@
|
|||
/* XMRig
|
||||
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
|
||||
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
|
||||
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
|
||||
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
|
||||
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
|
||||
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
|
||||
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
#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()
|
||||
{
|
||||
}
|
67
src/api/requests/ApiRequest.h
Normal file
67
src/api/requests/ApiRequest.h
Normal file
|
@ -0,0 +1,67 @@
|
|||
/* XMRig
|
||||
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
|
||||
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
|
||||
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
|
||||
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
|
||||
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
|
||||
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
|
||||
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
#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
|
||||
|
55
src/api/requests/HttpApiRequest.cpp
Normal file
55
src/api/requests/HttpApiRequest.cpp
Normal file
|
@ -0,0 +1,55 @@
|
|||
/* XMRig
|
||||
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
|
||||
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
|
||||
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
|
||||
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
|
||||
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
|
||||
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
|
||||
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
#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<IApiRequest::Method>(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();
|
||||
}
|
67
src/api/requests/HttpApiRequest.h
Normal file
67
src/api/requests/HttpApiRequest.h
Normal file
|
@ -0,0 +1,67 @@
|
|||
/* XMRig
|
||||
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
|
||||
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
|
||||
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
|
||||
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
|
||||
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
|
||||
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
|
||||
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
#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
|
||||
|
|
@ -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<http_status>(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<http_status>(statusCode()))), m_doc.GetAllocator());
|
||||
}
|
||||
}
|
||||
|
||||
if (!m_doc.MemberCount()) {
|
||||
|
|
Loading…
Reference in a new issue