1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
|
// Copyright Maarten L. Hekkelman 2025-2026
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#pragma once
#include "zeep/el/object.hpp"
#include "zeep/http/header.hpp"
#include "zeep/http/request.hpp"
#include <zeem/node.hpp>
#include <iosfwd>
#include <locale>
#include <map>
#include <optional>
#include <ranges>
#include <string>
#include <string_view>
#include <type_traits>
#include <utility>
#include <vector>
namespace zeep::http
{
class basic_server;
// --------------------------------------------------------------------
/// \brief The class that stores variables for the current scope
///
/// When processing tags and in expression language constructs we use
/// variables. These are stored in scope instances.
class scope
{
public:
scope &operator=(const scope &) = delete;
using param = header;
/// \brief simple constructor, used where there's no request available
scope();
/// \brief constructor to be used only in debugging
///
/// \param req The incomming HTTP request
explicit scope(const request &req);
/// \brief constructor used in a HTTP request context
///
/// \param server The server that handles the incomming request
/// \param req The incomming HTTP request
scope(const basic_server &server, const request &req)
: scope(&server, req)
{
}
/// \brief constructor used in a HTTP request context
///
/// \param server The server that handles the incomming request, pointer version
/// \param req The incomming HTTP request
scope(const basic_server *server, const request &req);
/// \brief chaining constructor
///
/// Scopes can be nested, introducing new namespaces
/// \param next The next scope up the chain.
explicit scope(const scope &next);
/// \brief add a path parameter, should only be used by controller::handle_request
void add_path_param(std::string name, std::string value);
/// \brief Return the list of headers
[[nodiscard]] auto get_headers() const { return m_req->get_headers(); }
/// \brief Return the named header
[[nodiscard]] std::string get_header(std::string_view name) const { return m_req->get_header(name); }
/// \brief Return the payload
[[nodiscard]] const std::string &get_payload() const { return m_req->get_payload(); }
/// \brief Return the Accept-Language header value in the request as a std::locale object
[[nodiscard]] std::locale get_locale() const { return m_req->get_locale(); }
/// \brief get the optional parameter value for @a name
[[nodiscard]] std::optional<std::string> get_parameter(std::string_view name) const
{
std::optional<std::string> result;
auto p = std::ranges::find_if(m_path_parameters,
[name](auto &pp)
{ return pp.name == name; });
if (p == m_path_parameters.end())
result = m_req->get_parameter(name);
else if (not p->value.empty())
result = p->value;
return result;
}
/// \brief get all parameter values for @a name
[[nodiscard]] std::vector<std::string> get_parameters(std::string_view name) const
{
auto p = std::ranges::find_if(m_path_parameters,
[name](auto &pp)
{ return pp.name == name; });
if (p != m_path_parameters.end())
return { p->value };
else
{
std::vector<std::string> result;
for (const auto &[p_name, p_value] : m_req->get_parameters())
{
if (p_name != name)
continue;
result.push_back(p_value);
}
return result;
}
}
/// \brief get the file parameter value for @a name
[[nodiscard]] file_param get_file_parameter(std::string name) const
{
return m_req->get_file_parameter(std::move(name));
}
/// \brief get all file parameters value for @a name
[[nodiscard]] std::vector<file_param> get_file_parameters(std::string name) const
{
return m_req->get_file_parameters(std::move(name));
}
/// \brief put variable in the scope with \a name and \a value
template <typename T>
void put(const std::string &name, const T &value)
requires(std::is_assignable_v<el::object, T>)
{
m_data[name] = value;
}
/// \brief put variable in the scope with \a name and \a value
void put(const std::string &name, el::object &&value)
{
m_data[name] = std::move(value);
}
/// \brief put variable in the scope with \a name and \a value
void put(const std::string &name, const el::object &value)
{
m_data[name] = value;
}
/// \brief put variable of type array in the scope with \a name and values from \a begin to \a end
template <typename ForwardIterator>
void put(const std::string &name, ForwardIterator begin, ForwardIterator end);
/// \brief return variable with \a name
///
/// \param name The name of the variable to return
/// \param includeSelected If this is true, and the variable was not found as a regular variable
/// in the current scope, the selected el::objects will be searched for members
/// with \a name This is used by the tag processing lib v2 in _z2:el::object_
/// \return The value found or null if there was no such variable.
[[nodiscard]] const el::object &lookup(const std::string &name, bool includeSelected = false) const;
/// \brief return variable with \a name
[[nodiscard]] const el::object &operator[](const std::string &name) const;
/// \brief return variable with \a name
///
/// \param name The name of the variable to return
/// \return The value found or null if there was no such variable.
[[nodiscard]] el::object &lookup(const std::string &name);
/// \brief return variable with \a name
[[nodiscard]] el::object &operator[](const std::string &name);
/// \brief return the HTTP request, will throw if the scope chain was not created with a request
[[nodiscard]] const request &get_request() const;
/// \brief return the context_name of the server
[[nodiscard]] std::string get_context_name() const;
/// \brief return the credentials of the current user
[[nodiscard]] el::object get_credentials() const;
/// \brief returns whether the current user has role \a role
[[nodiscard]] bool has_role(std::string_view role) const;
/// \brief select el::object \a o , used in z2:el::object constructs
void select_object(const el::object &o);
/// \brief a nodeset for a selector, cached to avoid recusive expansion
///
/// In tag processors it is sometimes needed to take a selection of zeem::nodes
/// and reuse these, as a copy when inserting templates e.g.
using node_set_type = zeem::element;
/// \brief return the node_set_type with name \a name
[[nodiscard]] node_set_type get_nodeset(const std::string &name) const;
/// \brief store node_set_type \a nodes with name \a name
void set_nodeset(const std::string &name, node_set_type &&nodes);
/// \brief return whether a node_set with name \a name is stored
[[nodiscard]] bool has_nodeset(const std::string &name) const
{
return m_nodesets.count(name) or (m_next != nullptr and m_next->has_nodeset(name));
}
/// \brief get the CSRF token from the request burried in \a scope
[[nodiscard]] std::string get_csrf_token() const;
private:
/// for debugging purposes
friend std::ostream &operator<<(std::ostream &lhs, const scope &rhs);
using data_map = std::map<std::string, el::object>;
data_map m_data;
scope *m_next;
unsigned m_depth;
const request *m_req;
const basic_server *m_server;
std::vector<param> m_path_parameters;
el::object m_selected;
using nodeset_map = std::map<std::string, node_set_type>;
nodeset_map m_nodesets;
};
template <typename ForwardIterator>
inline void scope::put(const std::string &name, ForwardIterator begin, ForwardIterator end)
{
std::vector<el::object> elements;
while (begin != end)
elements.push_back(el::object(*begin++));
m_data.emplace(name, std::move(elements));
}
// --------------------------------------------------------------------
} // namespace zeep::http
|