From ca249f1f1ca84701daa0e2ac6dc65f1f9e5fc540 Mon Sep 17 00:00:00 2001 From: XMRig Date: Sun, 31 Mar 2019 01:44:53 +0700 Subject: [PATCH] Cleanup. --- cmake/FindMHD.cmake | 49 --------- src/api/v1/ApiRouter.cpp | 2 - src/common/api/HttpBody.h | 69 ------------- src/common/api/HttpReply.h | 53 ---------- src/common/api/HttpRequest.cpp | 175 --------------------------------- src/common/api/HttpRequest.h | 84 ---------------- 6 files changed, 432 deletions(-) delete mode 100644 cmake/FindMHD.cmake delete mode 100644 src/common/api/HttpBody.h delete mode 100644 src/common/api/HttpReply.h delete mode 100644 src/common/api/HttpRequest.cpp delete mode 100644 src/common/api/HttpRequest.h diff --git a/cmake/FindMHD.cmake b/cmake/FindMHD.cmake deleted file mode 100644 index 7a598e02..00000000 --- a/cmake/FindMHD.cmake +++ /dev/null @@ -1,49 +0,0 @@ -# - Try to find MHD -# Once done this will define -# -# MHD_FOUND - system has MHD -# MHD_INCLUDE_DIRS - the MHD include directory -# MHD_LIBRARY - Link these to use MHD - -find_path( - MHD_INCLUDE_DIR - NAMES microhttpd.h - PATHS "${XMRIG_DEPS}" ENV "XMRIG_DEPS" - PATH_SUFFIXES "include" - DOC "microhttpd include dir" - NO_DEFAULT_PATH -) - -find_path(MHD_INCLUDE_DIR NAMES microhttpd.h) - -find_library( - MHD_LIBRARY - NAMES libmicrohttpd.a microhttpd libmicrohttpd - PATHS "${XMRIG_DEPS}" ENV "XMRIG_DEPS" - PATH_SUFFIXES "lib" - DOC "microhttpd library" - NO_DEFAULT_PATH -) - -find_library(MHD_LIBRARY NAMES microhttpd libmicrohttpd) - -set(MHD_INCLUDE_DIRS ${MHD_INCLUDE_DIR}) -set(MHD_LIBRARIES ${MHD_LIBRARY}) - -# debug library on windows -# same naming convention as in qt (appending debug library with d) -# boost is using the same "hack" as us with "optimized" and "debug" -# official MHD project actually uses _d suffix -if (${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC) - find_library( - MHD_LIBRARY_DEBUG - NAMES microhttpd_d microhttpd-10_d libmicrohttpd_d libmicrohttpd-dll_d - DOC "mhd debug library" - ) - set(MHD_LIBRARIES optimized ${MHD_LIBRARIES} debug ${MHD_LIBRARY_DEBUG}) -endif() - -include(FindPackageHandleStandardArgs) -find_package_handle_standard_args(MHD DEFAULT_MSG MHD_LIBRARY MHD_INCLUDE_DIR) -mark_as_advanced(MHD_INCLUDE_DIR MHD_LIBRARY) - diff --git a/src/api/v1/ApiRouter.cpp b/src/api/v1/ApiRouter.cpp index 2914b755..029d0a3b 100644 --- a/src/api/v1/ApiRouter.cpp +++ b/src/api/v1/ApiRouter.cpp @@ -63,8 +63,6 @@ xmrig::ApiRouter::~ApiRouter() void xmrig::ApiRouter::onRequest(IApiRequest &request) { - printf("xmrig::ApiRouter::onRequest\n %d", request.method()); - if (request.method() == IApiRequest::METHOD_GET) { if (request.url() == "/1/summary" || request.url() == "/api.json") { request.accept(); diff --git a/src/common/api/HttpBody.h b/src/common/api/HttpBody.h deleted file mode 100644 index 0b143fb7..00000000 --- a/src/common/api/HttpBody.h +++ /dev/null @@ -1,69 +0,0 @@ -/* 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 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 __HTTPBODY_H__ -#define __HTTPBODY_H__ - - -#include - - -namespace xmrig { - - -class HttpBody -{ -public: - inline HttpBody() : - m_pos(0) - {} - - - inline bool write(const char *data, size_t size) - { - if (size > (sizeof(m_data) - m_pos - 1)) { - return false; - } - - memcpy(m_data + m_pos, data, size); - - m_pos += size; - m_data[m_pos] = '\0'; - - return true; - } - - - inline const char *data() const { return m_data; } - -private: - char m_data[32768]; - size_t m_pos; -}; - - -} /* namespace xmrig */ - - -#endif /* __HTTPBODY_H__ */ diff --git a/src/common/api/HttpReply.h b/src/common/api/HttpReply.h deleted file mode 100644 index 6a6cb802..00000000 --- a/src/common/api/HttpReply.h +++ /dev/null @@ -1,53 +0,0 @@ -/* 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 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 __HTTPREPLY_H__ -#define __HTTPREPLY_H__ - - -#include - - -namespace xmrig { - - -class HttpReply -{ -public: - HttpReply() : - buf(nullptr), - status(200), - size(0) - {} - - char *buf; - int status; - size_t size; -}; - - -} /* namespace xmrig */ - - -#endif /* __HTTPREPLY_H__ */ diff --git a/src/common/api/HttpRequest.cpp b/src/common/api/HttpRequest.cpp deleted file mode 100644 index 6898a385..00000000 --- a/src/common/api/HttpRequest.cpp +++ /dev/null @@ -1,175 +0,0 @@ -/* 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 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 . - */ - - -#include -#include - -#include "common/api/HttpBody.h" -#include "common/api/HttpRequest.h" -#include "common/api/HttpReply.h" - - -#ifndef MHD_HTTP_PAYLOAD_TOO_LARGE -# define MHD_HTTP_PAYLOAD_TOO_LARGE 413 -#endif - - -xmrig::HttpRequest::HttpRequest(MHD_Connection *connection, const char *url, const char *method, const char *uploadData, size_t *uploadSize, void **cls) : - m_fulfilled(true), - m_restricted(true), - m_uploadData(uploadData), - m_url(url), - m_body(static_cast(*cls)), - m_method(Unsupported), - m_connection(connection), - m_uploadSize(uploadSize), - m_cls(cls) -{ - if (strcmp(method, MHD_HTTP_METHOD_OPTIONS) == 0) { - m_method = Options; - } - else if (strcmp(method, MHD_HTTP_METHOD_GET) == 0) { - m_method = Get; - } - else if (strcmp(method, MHD_HTTP_METHOD_PUT) == 0) { - m_method = Put; - } -} - - -xmrig::HttpRequest::~HttpRequest() -{ - if (m_fulfilled) { - delete m_body; - } -} - - -bool xmrig::HttpRequest::match(const char *path) const -{ - return strcmp(m_url, path) == 0; -} - - -bool xmrig::HttpRequest::process(const char *accessToken, bool restricted, xmrig::HttpReply &reply) -{ - m_restricted = restricted || !accessToken; - - if (m_body) { - if (*m_uploadSize != 0) { - if (!m_body->write(m_uploadData, *m_uploadSize)) { - *m_cls = nullptr; - m_fulfilled = true; - reply.status = MHD_HTTP_PAYLOAD_TOO_LARGE; - return false; - } - - *m_uploadSize = 0; - m_fulfilled = false; - return true; - } - - m_fulfilled = true; - return true; - } - - reply.status = auth(accessToken); - if (reply.status != MHD_HTTP_OK) { - return false; - } - - if (m_restricted && m_method != Get) { - reply.status = MHD_HTTP_FORBIDDEN; - return false; - } - - if (m_method == Get) { - return true; - } - - const char *contentType = MHD_lookup_connection_value(m_connection, MHD_HEADER_KIND, "Content-Type"); - if (!contentType || strcmp(contentType, "application/json") != 0) { - reply.status = MHD_HTTP_UNSUPPORTED_MEDIA_TYPE; - return false; - } - - m_body = new xmrig::HttpBody(); - m_fulfilled = false; - *m_cls = m_body; - - return true; -} - - -const char *xmrig::HttpRequest::body() const -{ - return m_body ? m_body->data() : nullptr; -} - - -int xmrig::HttpRequest::end(const HttpReply &reply) -{ - if (reply.buf) { - return end(reply.status, MHD_create_response_from_buffer(reply.size ? reply.size : strlen(reply.buf), (void*) reply.buf, MHD_RESPMEM_MUST_FREE)); - } - - return end(reply.status, nullptr); -} - - -int xmrig::HttpRequest::end(int status, MHD_Response *rsp) -{ - if (!rsp) { - rsp = MHD_create_response_from_buffer(0, nullptr, MHD_RESPMEM_PERSISTENT); - } - - MHD_add_response_header(rsp, "Content-Type", "application/json"); - MHD_add_response_header(rsp, "Access-Control-Allow-Origin", "*"); - MHD_add_response_header(rsp, "Access-Control-Allow-Methods", "GET, PUT"); - MHD_add_response_header(rsp, "Access-Control-Allow-Headers", "Authorization, Content-Type"); - - const int ret = MHD_queue_response(m_connection, status, rsp); - MHD_destroy_response(rsp); - return ret; -} - - -int xmrig::HttpRequest::auth(const char *accessToken) -{ - if (!accessToken) { - return MHD_HTTP_OK; - } - - const char *header = MHD_lookup_connection_value(m_connection, MHD_HEADER_KIND, "Authorization"); - if (accessToken && !header) { - return MHD_HTTP_UNAUTHORIZED; - } - - const size_t size = strlen(header); - if (size < 8 || strlen(accessToken) != size - 7 || memcmp("Bearer ", header, 7) != 0) { - return MHD_HTTP_FORBIDDEN; - } - - return strncmp(accessToken, header + 7, strlen(accessToken)) == 0 ? MHD_HTTP_OK : MHD_HTTP_FORBIDDEN; -} diff --git a/src/common/api/HttpRequest.h b/src/common/api/HttpRequest.h deleted file mode 100644 index f6ff9a40..00000000 --- a/src/common/api/HttpRequest.h +++ /dev/null @@ -1,84 +0,0 @@ -/* 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 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 __HTTPREQUEST_H__ -#define __HTTPREQUEST_H__ - - -#include - - -struct MHD_Connection; -struct MHD_Response; - - -namespace xmrig { - - -class HttpBody; -class HttpReply; - - -class HttpRequest -{ -public: - enum Method { - Unsupported, - Options, - Get, - Put - }; - - HttpRequest(MHD_Connection *connection, const char *url, const char *method, const char *uploadData, size_t *uploadSize, void **cls); - ~HttpRequest(); - - inline bool isFulfilled() const { return m_fulfilled; } - inline bool isRestricted() const { return m_restricted; } - inline Method method() const { return m_method; } - - bool match(const char *path) const; - bool process(const char *accessToken, bool restricted, xmrig::HttpReply &reply); - const char *body() const; - int end(const HttpReply &reply); - int end(int status, MHD_Response *rsp); - -private: - int auth(const char *accessToken); - - bool m_fulfilled; - bool m_restricted; - const char *m_uploadData; - const char *m_url; - HttpBody *m_body; - Method m_method; - MHD_Connection *m_connection; - size_t *m_uploadSize; - void **m_cls; -}; - - -} /* namespace xmrig */ - - -#endif /* __HTTPREQUEST_H__ */