Browse Source

api: allow to use path helper without api enabled

dev
Maxim Prokhorov 3 years ago
parent
commit
4854f91b3d
4 changed files with 110 additions and 93 deletions
  1. +8
    -4
      code/espurna/api.cpp
  2. +9
    -11
      code/espurna/api.h
  3. +1
    -78
      code/espurna/api_impl.h
  4. +92
    -0
      code/espurna/api_path.h

+ 8
- 4
code/espurna/api.cpp View File

@ -1,8 +1,9 @@
/* /*
API MODULE
API & WEB API MODULE
Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com> Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com>
Copyright (C) 2020-2021 by Maxim Prokhorov <prokhorov dot max at outlook dot com>
*/ */
@ -10,16 +11,17 @@ Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com>
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
#if API_SUPPORT
#include "system.h" #include "system.h"
#include "web.h"
#include "rpc.h" #include "rpc.h"
#if WEB_SUPPORT
#include "web.h"
#include <ESPAsyncTCP.h> #include <ESPAsyncTCP.h>
#include <ArduinoJson.h> #include <ArduinoJson.h>
#endif
#include <algorithm> #include <algorithm>
#include <memory>
#include <cstring> #include <cstring>
#include <forward_list> #include <forward_list>
#include <vector> #include <vector>
@ -230,6 +232,8 @@ size_t ApiRequest::wildcards() const {
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
#if API_SUPPORT
bool _apiAccepts(AsyncWebServerRequest* request, const __FlashStringHelper* str) { bool _apiAccepts(AsyncWebServerRequest* request, const __FlashStringHelper* str) {
auto* header = request->getHeader(F("Accept")); auto* header = request->getHeader(F("Accept"));
if (header) { if (header) {


+ 9
- 11
code/espurna/api.h View File

@ -3,41 +3,39 @@
API MODULE API MODULE
Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com> Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com>
Copyright (C) 2020-2021 by Maxim Prokhorov <prokhorov dot max at outlook dot com>
*/ */
#pragma once #pragma once
#include "espurna.h" #include "espurna.h"
#include "api_impl.h"
#include "web.h" #include "web.h"
#if WEB_SUPPORT
#include <functional>
#if WEB_SUPPORT
bool apiAuthenticateHeader(AsyncWebServerRequest*, const String& key); bool apiAuthenticateHeader(AsyncWebServerRequest*, const String& key);
bool apiAuthenticateParam(AsyncWebServerRequest*, const String& key); bool apiAuthenticateParam(AsyncWebServerRequest*, const String& key);
bool apiAuthenticate(AsyncWebServerRequest*); bool apiAuthenticate(AsyncWebServerRequest*);
#endif
void apiCommonSetup(); void apiCommonSetup();
bool apiEnabled(); bool apiEnabled();
bool apiRestFul(); bool apiRestFul();
String apiKey(); String apiKey();
#endif // WEB_SUPPORT == 1
#if WEB_SUPPORT && API_SUPPORT
#include "api_impl.h"
#include <functional>
#if WEB_SUPPORT
using ApiBasicHandler = std::function<bool(ApiRequest&)>; using ApiBasicHandler = std::function<bool(ApiRequest&)>;
using ApiJsonHandler = std::function<bool(ApiRequest&, JsonObject& reponse)>; using ApiJsonHandler = std::function<bool(ApiRequest&, JsonObject& reponse)>;
void apiRegister(const String& path, ApiBasicHandler&& get, ApiBasicHandler&& put); void apiRegister(const String& path, ApiBasicHandler&& get, ApiBasicHandler&& put);
void apiRegister(const String& path, ApiJsonHandler&& get, ApiJsonHandler&& put); void apiRegister(const String& path, ApiJsonHandler&& get, ApiJsonHandler&& put);
#endif
void apiSetup(); void apiSetup();
bool apiError(ApiRequest&); bool apiError(ApiRequest&);
bool apiOk(ApiRequest&); bool apiOk(ApiRequest&);
#endif // API_SUPPORT == 1

+ 1
- 78
code/espurna/api_impl.h View File

@ -16,84 +16,7 @@ Copyright (C) 2020 by Maxim Prokhorov <prokhorov dot max at outlook dot com>
#include <memory> #include <memory>
#include <vector> #include <vector>
// -----------------------------------------------------------------------------
struct PathPart {
enum class Type {
Unknown,
Value,
SingleWildcard,
MultiWildcard
};
Type type;
size_t offset;
size_t length;
};
struct PathParts {
using Parts = std::vector<PathPart>;
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 // this is a purely temporary object, which we can only create while doing the API dispatch


+ 92
- 0
code/espurna/api_path.h View File

@ -0,0 +1,92 @@
/*
Part of the API MODULE
Copyright (C) 2021 by Maxim Prokhorov <prokhorov dot max at outlook dot com>
*/
#pragma once
#include <Arduino.h>
#include <vector>
// -----------------------------------------------------------------------------
struct PathPart {
enum class Type {
Unknown,
Value,
SingleWildcard,
MultiWildcard
};
Type type;
size_t offset;
size_t length;
};
struct PathParts {
using Parts = std::vector<PathPart>;
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 };
};

Loading…
Cancel
Save