/usr/include/zeep/http/webapp.hpp is in libzeep-dev 3.0.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 | // Copyright Maarten L. Hekkelman, Radboud University 2008-2011.
// 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)
//
// webapp is a base class used to construct web applications in C++ using libzeep
//
#pragma once
#include <map>
#include <string>
#include <vector>
#include <boost/type_traits/is_arithmetic.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/format.hpp>
#include <boost/foreach.hpp>
#include <boost/regex.hpp>
#include <boost/filesystem.hpp>
#include <zeep/exception.hpp>
#include <zeep/http/request.hpp>
#include <zeep/http/server.hpp>
// --------------------------------------------------------------------
//
namespace zeep {
namespace http {
struct unauthorized_exception : public std::exception
{
unauthorized_exception(bool stale, const std::string& realm)
: m_stale(stale)
{
std::string::size_type n = realm.length();
if (n >= sizeof(m_realm))
n = sizeof(m_realm) - 1;
realm.copy(m_realm, n);
m_realm[n] = 0;
}
bool m_stale; ///< Is true when the authorization information is valid but stale (too old)
char m_realm[256]; ///< Realm for which the authorization failed
};
#ifndef BOOST_XPRESSIVE_DOXYGEN_INVOKED
namespace el { class scope; class object; }
#endif
class parameter_value
{
public:
parameter_value() : m_defaulted(false) {}
parameter_value(const std::string& v, bool defaulted)
: m_v(v), m_defaulted(defaulted) {}
template<class T>
T as() const;
bool empty() const { return m_v.empty(); }
bool defaulted() const { return m_defaulted; }
private:
std::string m_v;
bool m_defaulted;
};
/// parameter_map is used to pass parameters from forms. The parameters can have 'any' type.
/// Works a bit like the program_options code in boost.
class parameter_map : public std::multimap<std::string, parameter_value>
{
public:
/// add a name/value pair as a string formatted as 'name=value'
void add(const std::string& param);
void add(std::string name, std::string value);
void replace(std::string name, std::string value);
template<class T>
const parameter_value&
get(const std::string& name, T defaultValue);
};
/// webapp is a specialization of zeep::http::server, it is used to create
/// interactive web applications.
class webapp : public http::server
{
public:
/// first parameter to constructor is the
/// namespace to use in template XHTML files.
webapp(const std::string& ns = "http://www.cmbi.ru.nl/libzeep/ml",
const boost::filesystem::path& docroot = ".");
virtual ~webapp();
virtual void set_docroot(const boost::filesystem::path& docroot);
boost::filesystem::path
get_docroot() const { return m_docroot; }
protected:
virtual void handle_request(const request& req, reply& rep);
virtual void create_unauth_reply(bool stale, const std::string& realm, reply& rep);
// webapp works with 'handlers' that are methods 'mounted' on a path in the requested URI
typedef boost::function<void(const request& request, const el::scope& scope, reply& reply)> handler_type;
/// assign a handler function to a path in the server's namespace
/// Usually called like this:
///
/// mount("page", boost::bind(&page_handler, this, _1, _2, _3));
///
/// Where page_handler is defined as:
///
/// void my_server::page_handler(const request& request, const el::scope& scope, reply& reply);
///
void mount(const std::string& path, handler_type handler);
/// Default handler for serving files out of our doc root
virtual void handle_file(const request& request, const el::scope& scope, reply& reply);
/// Use load_template to fetch the XHTML template file
virtual void load_template(const std::string& file, xml::document& doc);
void load_template(const boost::filesystem::path& file, xml::document& doc)
{
load_template(file.string(), doc);
}
/// create a reply based on a template
virtual void create_reply_from_template(const std::string& file, const el::scope& scope, reply& reply);
/// process xml parses the XHTML and fills in the special tags and evaluates the el constructs
virtual void process_xml(xml::node* node, const el::scope& scope, boost::filesystem::path dir);
typedef boost::function<void(xml::element* node, const el::scope& scope, boost::filesystem::path dir)> processor_type;
/// To add additional processors
virtual void add_processor(const std::string& name, processor_type processor);
/// default handler for mrs:include tags
virtual void process_include(xml::element* node, const el::scope& scope, boost::filesystem::path dir);
/// default handler for mrs:if tags
virtual void process_if(xml::element* node, const el::scope& scope, boost::filesystem::path dir);
/// default handler for mrs:iterate tags
virtual void process_iterate(xml::element* node, const el::scope& scope, boost::filesystem::path dir);
/// default handler for mrs:for tags
virtual void process_for(xml::element* node, const el::scope& scope, boost::filesystem::path dir);
/// default handler for mrs:number tags
virtual void process_number(xml::element* node, const el::scope& scope, boost::filesystem::path dir);
/// default handler for mrs:options tags
virtual void process_options(xml::element* node, const el::scope& scope, boost::filesystem::path dir);
/// default handler for mrs:option tags
virtual void process_option(xml::element* node, const el::scope& scope, boost::filesystem::path dir);
/// default handler for mrs:checkbox tags
virtual void process_checkbox(xml::element* node, const el::scope& scope, boost::filesystem::path dir);
/// default handler for mrs:url tags
virtual void process_url(xml::element* node, const el::scope& scope, boost::filesystem::path dir);
/// default handler for mrs:param tags
virtual void process_param(xml::element* node, const el::scope& scope, boost::filesystem::path dir);
/// default handler for mrs:embed tags
virtual void process_embed(xml::element* node, const el::scope& scope, boost::filesystem::path dir);
/// Initialize the el::scope object
virtual void init_scope(el::scope& scope);
/// Return a parameter_map containing the cookies as found in the current request
virtual void get_cookies(const el::scope& scope, parameter_map& cookies);
/// Return the original parameters as found in the current request
virtual void get_parameters(const el::scope& scope, parameter_map& parameters);
private:
typedef std::map<std::string,handler_type> handler_map;
typedef std::map<std::string,processor_type> processor_map;
std::string m_ns;
boost::filesystem::path
m_docroot;
handler_map m_dispatch_table;
processor_map m_processor_table;
};
template<class T>
inline
T parameter_value::as() const
{
T result;
if (boost::is_arithmetic<T>::value and m_v.empty())
result = 0;
else
result = boost::lexical_cast<T>(m_v);
return result;
}
template<>
inline
std::string parameter_value::as<std::string>() const
{
return m_v;
}
template<>
inline
bool parameter_value::as<bool>() const
{
bool result = false;
if (not m_v.empty() and m_v != "false")
{
if (m_v == "true")
result = true;
else
{
try { result = boost::lexical_cast<int>(m_v) != 0; } catch (...) {}
}
}
return result;
}
template<class T>
inline
const parameter_value&
parameter_map::get(
const std::string& name,
T defaultValue)
{
iterator i = lower_bound(name);
if (i == end() or i->first != name)
i = insert(std::make_pair(name, parameter_value(boost::lexical_cast<std::string>(defaultValue), true)));
return i->second;
}
// specialisation for const char*
template<>
inline
const parameter_value&
parameter_map::get(
const std::string& name,
const char* defaultValue)
{
if (defaultValue == nullptr)
defaultValue = "";
iterator i = lower_bound(name);
if (i == end() or i->first != name)
i = insert(std::make_pair(name, parameter_value(defaultValue, true)));
else if (i->second.empty())
i->second = parameter_value(defaultValue, true);
return i->second;
}
// specialisation for bool (if missing, value is false)
template<>
inline
const parameter_value&
parameter_map::get(
const std::string& name,
bool defaultValue)
{
iterator i = lower_bound(name);
if (i == end() or i->first != name)
i = insert(std::make_pair(name, parameter_value("false", true)));
else if (i->second.empty())
i->second = parameter_value("false", true);
return i->second;
}
}
}
|