diff --git a/code/espurna/api.cpp b/code/espurna/api.cpp index 5e7bd0a7..723ceb03 100644 --- a/code/espurna/api.cpp +++ b/code/espurna/api.cpp @@ -1,8 +1,9 @@ /* -API MODULE +API & WEB API MODULE Copyright (C) 2016-2019 by Xose Pérez +Copyright (C) 2020-2021 by Maxim Prokhorov */ @@ -10,16 +11,17 @@ Copyright (C) 2016-2019 by Xose Pérez // ----------------------------------------------------------------------------- -#if API_SUPPORT - #include "system.h" -#include "web.h" #include "rpc.h" +#if WEB_SUPPORT +#include "web.h" #include #include +#endif #include +#include #include #include #include @@ -230,6 +232,8 @@ size_t ApiRequest::wildcards() const { // ----------------------------------------------------------------------------- +#if API_SUPPORT + bool _apiAccepts(AsyncWebServerRequest* request, const __FlashStringHelper* str) { auto* header = request->getHeader(F("Accept")); if (header) { diff --git a/code/espurna/api.h b/code/espurna/api.h index 91a19ba5..2b12e5b6 100644 --- a/code/espurna/api.h +++ b/code/espurna/api.h @@ -3,41 +3,39 @@ API MODULE Copyright (C) 2016-2019 by Xose Pérez +Copyright (C) 2020-2021 by Maxim Prokhorov */ #pragma once #include "espurna.h" + +#include "api_impl.h" #include "web.h" -#if WEB_SUPPORT +#include +#if WEB_SUPPORT bool apiAuthenticateHeader(AsyncWebServerRequest*, const String& key); bool apiAuthenticateParam(AsyncWebServerRequest*, const String& key); bool apiAuthenticate(AsyncWebServerRequest*); +#endif + void apiCommonSetup(); bool apiEnabled(); bool apiRestFul(); String apiKey(); -#endif // WEB_SUPPORT == 1 - -#if WEB_SUPPORT && API_SUPPORT - -#include "api_impl.h" - -#include - +#if WEB_SUPPORT using ApiBasicHandler = std::function; using ApiJsonHandler = std::function; void apiRegister(const String& path, ApiBasicHandler&& get, ApiBasicHandler&& put); void apiRegister(const String& path, ApiJsonHandler&& get, ApiJsonHandler&& put); +#endif void apiSetup(); bool apiError(ApiRequest&); bool apiOk(ApiRequest&); - -#endif // API_SUPPORT == 1 diff --git a/code/espurna/api_impl.h b/code/espurna/api_impl.h index 57fbe492..31618017 100644 --- a/code/espurna/api_impl.h +++ b/code/espurna/api_impl.h @@ -16,84 +16,7 @@ Copyright (C) 2020 by Maxim Prokhorov #include #include -// ----------------------------------------------------------------------------- - -struct PathPart { - enum class Type { - Unknown, - Value, - SingleWildcard, - MultiWildcard - }; - - Type type; - size_t offset; - size_t length; -}; - -struct PathParts { - using Parts = std::vector; - - PathParts() = delete; - - PathParts(const PathParts&) = default; - PathParts(PathParts&&) noexcept = default; - - explicit PathParts(const String& path); - - explicit operator bool() const { - return _ok; - } - - void clear() { - _parts.clear(); - } - - void reserve(size_t size) { - _parts.reserve(size); - } - - String operator[](size_t index) const { - auto& part = _parts[index]; - return _path.substring(part.offset, part.offset + part.length); - } - - const String& path() const { - return _path; - } - - const Parts& parts() const { - return _parts; - } - - size_t size() const { - return _parts.size(); - } - - Parts::const_iterator begin() const { - return _parts.begin(); - } - - Parts::const_iterator end() const { - return _parts.end(); - } - - bool match(const PathParts& path) const; - bool match(const String& path) const { - return match(PathParts(path)); - } - -private: - PathPart& emplace_back(PathPart::Type type, size_t offset, size_t length) { - PathPart part { type, offset, length }; - _parts.push_back(std::move(part)); - return _parts.back(); - } - - const String& _path; - Parts _parts; - bool _ok { false }; -}; +#include "api_path.h" // this is a purely temporary object, which we can only create while doing the API dispatch diff --git a/code/espurna/api_path.h b/code/espurna/api_path.h new file mode 100644 index 00000000..9bab4209 --- /dev/null +++ b/code/espurna/api_path.h @@ -0,0 +1,92 @@ +/* + +Part of the API MODULE + +Copyright (C) 2021 by Maxim Prokhorov + +*/ + +#pragma once + +#include + +#include + +// ----------------------------------------------------------------------------- + +struct PathPart { + enum class Type { + Unknown, + Value, + SingleWildcard, + MultiWildcard + }; + + Type type; + size_t offset; + size_t length; +}; + +struct PathParts { + using Parts = std::vector; + + PathParts() = delete; + + PathParts(const PathParts&) = default; + PathParts(PathParts&&) noexcept = default; + + explicit PathParts(const String& path); + + explicit operator bool() const { + return _ok; + } + + void clear() { + _parts.clear(); + } + + void reserve(size_t size) { + _parts.reserve(size); + } + + String operator[](size_t index) const { + auto& part = _parts[index]; + return _path.substring(part.offset, part.offset + part.length); + } + + const String& path() const { + return _path; + } + + const Parts& parts() const { + return _parts; + } + + size_t size() const { + return _parts.size(); + } + + Parts::const_iterator begin() const { + return _parts.begin(); + } + + Parts::const_iterator end() const { + return _parts.end(); + } + + bool match(const PathParts& path) const; + bool match(const String& path) const { + return match(PathParts(path)); + } + +private: + PathPart& emplace_back(PathPart::Type type, size_t offset, size_t length) { + PathPart part { type, offset, length }; + _parts.push_back(std::move(part)); + return _parts.back(); + } + + const String& _path; + Parts _parts; + bool _ok { false }; +};