2020-08-17 17:09:03 +02:00
|
|
|
/* API Request.
|
|
|
|
*
|
2022-03-15 09:18:01 -04:00
|
|
|
* Author: Steffen Vogel <post@steffenvogel.de>
|
2022-03-15 09:28:57 -04:00
|
|
|
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
|
2022-07-04 18:20:03 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2020-08-17 17:09:03 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <jansson.h>
|
2023-09-07 11:46:39 +02:00
|
|
|
#include <regex>
|
|
|
|
#include <vector>
|
2020-08-17 17:09:03 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
#include <villas/api/session.hpp>
|
2020-10-20 22:17:55 +02:00
|
|
|
#include <villas/buffer.hpp>
|
2023-09-07 11:46:39 +02:00
|
|
|
#include <villas/log.hpp>
|
2020-08-17 17:09:03 +02:00
|
|
|
#include <villas/plugin.hpp>
|
2020-08-25 20:27:26 +02:00
|
|
|
#include <villas/super_node.hpp>
|
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
#define RE_UUID \
|
|
|
|
"[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{" \
|
|
|
|
"12}"
|
2020-08-17 17:09:03 +02:00
|
|
|
|
|
|
|
namespace villas {
|
|
|
|
namespace node {
|
|
|
|
namespace api {
|
|
|
|
|
|
|
|
// Forward declarations
|
|
|
|
class Session;
|
|
|
|
class Response;
|
|
|
|
class RequestFactory;
|
|
|
|
|
|
|
|
class Request {
|
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
friend Session;
|
|
|
|
friend RequestFactory;
|
|
|
|
friend Response;
|
2020-08-17 17:09:03 +02:00
|
|
|
|
|
|
|
protected:
|
2023-09-07 11:46:39 +02:00
|
|
|
Session *session;
|
2020-08-17 17:09:03 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
Logger logger;
|
|
|
|
Buffer buffer;
|
2020-08-17 17:09:03 +02:00
|
|
|
|
|
|
|
public:
|
2023-09-07 11:46:39 +02:00
|
|
|
std::vector<std::string> matches;
|
|
|
|
Session::Method method;
|
|
|
|
unsigned long contentLength;
|
|
|
|
json_t *body;
|
2020-08-17 17:09:03 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
RequestFactory *factory;
|
2020-08-17 17:09:03 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
Request(Session *s)
|
|
|
|
: session(s), method(Session::Method::UNKNOWN), contentLength(0),
|
|
|
|
body(nullptr), factory(nullptr) {}
|
2020-08-17 17:09:03 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
virtual ~Request() {
|
|
|
|
if (body)
|
|
|
|
json_decref(body);
|
|
|
|
}
|
2020-08-17 17:09:03 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
virtual void prepare() {}
|
2021-02-25 17:17:11 -05:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
virtual Response *execute() = 0;
|
2020-08-17 17:09:03 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
virtual void decode();
|
2020-08-25 20:27:26 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
const std::string &getMatch(int idx) const { return matches[idx]; }
|
2020-10-20 22:17:55 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
std::string getQueryArg(const std::string &arg) {
|
|
|
|
char buf[1024];
|
|
|
|
const char *val;
|
2020-10-20 22:17:55 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
val = lws_get_urlarg_by_name(session->wsi, (arg + "=").c_str(), buf,
|
|
|
|
sizeof(buf));
|
2020-10-20 22:17:55 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
return val ? std::string(val) : std::string();
|
|
|
|
}
|
2020-10-20 22:17:55 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
std::string getHeader(enum lws_token_indexes hdr) {
|
|
|
|
char buf[1024];
|
2020-10-20 22:17:55 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
lws_hdr_copy(session->wsi, buf, sizeof(buf), hdr);
|
2020-10-20 22:17:55 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
return std::string(buf);
|
|
|
|
}
|
2020-10-20 22:17:55 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
virtual std::string toString();
|
2020-08-17 17:09:03 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class RequestFactory : public plugin::Plugin {
|
|
|
|
|
|
|
|
public:
|
2023-09-07 11:46:39 +02:00
|
|
|
using plugin::Plugin::Plugin;
|
|
|
|
|
|
|
|
virtual bool match(const std::string &uri, std::smatch &m) const = 0;
|
|
|
|
|
|
|
|
virtual Request *make(Session *s) = 0;
|
|
|
|
|
|
|
|
static Request *create(Session *s, const std::string &uri,
|
|
|
|
Session::Method meth, unsigned long ct);
|
|
|
|
|
|
|
|
virtual void init(Request *r) { r->logger = getLogger(); }
|
|
|
|
|
|
|
|
virtual std::string getType() const { return "api:request"; }
|
|
|
|
|
|
|
|
virtual bool isHidden() const { return false; }
|
2020-08-17 17:09:03 +02:00
|
|
|
};
|
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
template <typename T, const char *name, const char *re, const char *desc>
|
2020-08-17 17:09:03 +02:00
|
|
|
class RequestPlugin : public RequestFactory {
|
|
|
|
|
|
|
|
protected:
|
2023-09-07 11:46:39 +02:00
|
|
|
std::regex regex;
|
2020-08-17 17:09:03 +02:00
|
|
|
|
|
|
|
public:
|
2023-09-07 11:46:39 +02:00
|
|
|
RequestPlugin() : RequestFactory(), regex(re) {}
|
|
|
|
|
|
|
|
virtual Request *make(Session *s) {
|
|
|
|
auto *r = new T(s);
|
|
|
|
|
|
|
|
init(r);
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get plugin name
|
|
|
|
virtual std::string getName() const { return name; }
|
|
|
|
|
|
|
|
// Get plugin description
|
|
|
|
virtual std::string getDescription() const { return desc; }
|
|
|
|
|
|
|
|
virtual bool match(const std::string &uri, std::smatch &match) const {
|
|
|
|
return std::regex_match(uri, match, regex);
|
|
|
|
}
|
2020-08-17 17:09:03 +02:00
|
|
|
};
|
|
|
|
|
2023-08-28 09:34:02 +02:00
|
|
|
} // namespace api
|
|
|
|
} // namespace node
|
|
|
|
} // namespace villas
|