pion  5.0.6
plugin_service.hpp
1 // ---------------------------------------------------------------------
2 // pion: a Boost C++ framework for building lightweight HTTP interfaces
3 // ---------------------------------------------------------------------
4 // Copyright (C) 2007-2014 Splunk Inc. (https://github.com/splunk/pion)
5 //
6 // Distributed under the Boost Software License, Version 1.0.
7 // See http://www.boost.org/LICENSE_1_0.txt
8 //
9 
10 #ifndef __PION_PLUGIN_SERVICE_HEADER__
11 #define __PION_PLUGIN_SERVICE_HEADER__
12 
13 #include <string>
14 #include <boost/noncopyable.hpp>
15 #include <pion/config.hpp>
16 #include <pion/error.hpp>
17 #include <pion/algorithm.hpp>
18 #include <pion/http/request.hpp>
19 #include <pion/tcp/connection.hpp>
20 
21 
22 namespace pion { // begin namespace pion
23 namespace http { // begin namespace http
24 
25 
30  private boost::noncopyable
31 {
32 public:
33 
35  plugin_service(void) {}
36 
38  virtual ~plugin_service() {}
39 
46  virtual void operator()(const http::request_ptr& http_request_ptr, const tcp::connection_ptr& tcp_conn) = 0;
47 
54  virtual void set_option(const std::string& name, const std::string& value) {
55  BOOST_THROW_EXCEPTION( error::bad_arg() << error::errinfo_arg_name(name) );
56  }
57 
59  virtual void start(void) {}
60 
62  virtual void stop(void) {}
63 
65  inline void set_resource(const std::string& str) { m_resource = str; }
66 
68  inline const std::string& get_resource(void) const { return m_resource; }
69 
71  inline std::string get_relative_resource(const std::string& resource_requested) const {
72  if (resource_requested.size() <= get_resource().size()) {
73  // either the request matches the web service's resource path (a directory)
74  // or the request does not match (should never happen)
75  return std::string();
76  }
77  // strip the web service's resource path plus the slash after it
78  return algorithm::url_decode(resource_requested.substr(get_resource().size() + 1));
79  }
80 
81 
82 private:
83 
85  std::string m_resource;
86 };
87 
88 
89 //
90 // The following symbols must be defined for any web service that you would
91 // like to be able to load dynamically using the http::server::load_service()
92 // function. These are not required for any services that you only want to link
93 // directly into your programs.
94 //
95 // Make sure that you replace "MyPluginName" with the name of your derived class.
96 // This name must also match the name of the object file (excluding the
97 // extension). These symbols must be linked into your service's object file,
98 // not included in any headers that it may use (declarations are OK in headers
99 // but not the definitions).
100 //
101 // The "pion_create" function is used to create new instances of your service.
102 // The "pion_destroy" function is used to destroy instances of your service.
103 //
104 // extern "C" MyPluginName *pion_create_MyPluginName(void) {
105 // return new MyPluginName;
106 // }
107 //
108 // extern "C" void pion_destroy_MyPluginName(MyPluginName *service_ptr) {
109 // delete service_ptr;
110 // }
111 //
112 
113 
114 } // end namespace http
115 } // end namespace pion
116 
117 #endif
virtual ~plugin_service()
virtual destructor
void set_resource(const std::string &str)
sets the URI stem or resource that is bound to the web service
virtual void set_option(const std::string &name, const std::string &value)
virtual void stop(void)
called when the web service&#39;s server is stopping
static std::string url_decode(const std::string &str)
escapes URL-encoded strings (a%20value+with%20spaces)
Definition: algorithm.cpp:153
plugin_service(void)
default constructor
virtual void operator()(const http::request_ptr &http_request_ptr, const tcp::connection_ptr &tcp_conn)=0
const std::string & get_resource(void) const
returns the URI stem or resource that is bound to the web service
exception thrown for an invalid configuration argument or option
Definition: error.hpp:132
std::string get_relative_resource(const std::string &resource_requested) const
returns the path to the resource requested, relative to the web service&#39;s location ...
virtual void start(void)
called when the web service&#39;s server is starting