Cleanup API code

This commit is contained in:
Tony Butler 2022-04-16 16:21:22 -06:00
parent e7de104d88
commit f42b3e83a7
5 changed files with 38 additions and 20 deletions

View file

@ -52,6 +52,8 @@ public:
enum RequestType {
REQ_UNKNOWN,
REQ_SUMMARY,
REQ_BACKENDS,
REQ_CONFIG,
REQ_JSON_RPC
};

View file

@ -60,7 +60,7 @@ protected:
inline Source source() const override { return m_source; }
inline void done(int) override { m_state = STATE_DONE; }
int m_version = 1;
int m_version = 0;
RequestType m_type = REQ_UNKNOWN;
State m_state = STATE_NEW;
String m_rpcMethod;

View file

@ -67,10 +67,33 @@ xmrig::HttpApiRequest::HttpApiRequest(const HttpData &req, bool restricted) :
m_res(req.id()),
m_url(req.url.c_str())
{
if (method() == METHOD_GET) {
if (url() == "/1/summary" || url() == "/2/summary" || url() == "/api.json") {
m_type = REQ_SUMMARY;
if (url().size() > 4 && memcmp(url().data(), "/", 1) == 0 && memcmp(url().data()+2, "/", 1) == 0) {
if (memcmp(url().data(), "/2/", 3) == 0) {
m_version = 2;
} else if (memcmp(url().data(), "/1/", 3) == 0) {
m_version = 1;
}
switch (url().size()) {
case 9:
if (memcmp(url().data()+3, "config", 6) == 0) {
m_type = REQ_CONFIG;
}
break;
case 10:
if (memcmp(url().data()+3, "summary", 7) == 0) {
m_type = REQ_SUMMARY;
}
break;
case 11:
if (memcmp(url().data()+3, "backends", 8) == 0) {
m_type = REQ_BACKENDS;
}
break;
}
}
if (url() == "/api.json") {
m_type = REQ_SUMMARY;
}
if (method() == METHOD_POST && url() == "/json_rpc") {
@ -94,12 +117,6 @@ xmrig::HttpApiRequest::HttpApiRequest(const HttpData &req, bool restricted) :
return;
}
if (url().size() > 4) {
if (memcmp(url().data(), "/2/", 3) == 0) {
m_version = 2;
}
}
}

View file

@ -45,13 +45,6 @@
#ifdef XMRIG_FEATURE_API
# include "base/api/Api.h"
# include "base/api/interfaces/IApiRequest.h"
namespace xmrig {
static const char *kConfigPathV1 = "/1/config";
static const char *kConfigPathV2 = "/2/config";
} // namespace xmrig
#endif
@ -317,7 +310,7 @@ void xmrig::Base::onFileChanged(const String &fileName)
void xmrig::Base::onRequest(IApiRequest &request)
{
if (request.method() == IApiRequest::METHOD_GET) {
if (request.url() == kConfigPathV1 || request.url() == kConfigPathV2) {
if (request.type() == IApiRequest::REQ_CONFIG) {
if (request.isRestricted()) {
return request.done(403);
}
@ -327,7 +320,7 @@ void xmrig::Base::onRequest(IApiRequest &request)
}
}
else if (request.method() == IApiRequest::METHOD_PUT || request.method() == IApiRequest::METHOD_POST) {
if (request.url() == kConfigPathV1 || request.url() == kConfigPathV2) {
if (request.type() == IApiRequest::REQ_CONFIG) {
request.accept();
if (!reload(request.json())) {

View file

@ -689,7 +689,7 @@ void xmrig::Miner::onRequest(IApiRequest &request)
d_ptr->getMiner(request.reply(), request.doc(), request.version());
d_ptr->getHashrate(request.reply(), request.doc(), request.version());
}
else if (request.url() == "/2/backends") {
else if (request.type() == IApiRequest::REQ_BACKENDS && request.version() == 2) {
request.accept();
d_ptr->getBackends(request.reply(), request.doc());
@ -711,6 +711,12 @@ void xmrig::Miner::onRequest(IApiRequest &request)
stop();
}
else if (request.rpcMethod() == "start") {
request.accept();
const auto config = d_ptr->controller->config();
onConfigChanged(config, config);
}
}
for (IBackend *backend : d_ptr->backends) {