pion-net  4.0.9
WebService.hpp
1 // ------------------------------------------------------------------
2 // pion-net: a C++ framework for building lightweight HTTP interfaces
3 // ------------------------------------------------------------------
4 // Copyright (C) 2007-2008 Atomic Labs, Inc. (http://www.atomiclabs.com)
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_WEBSERVICE_HEADER__
11 #define __PION_WEBSERVICE_HEADER__
12 
13 #include <boost/noncopyable.hpp>
14 #include <pion/PionConfig.hpp>
15 #include <pion/PionException.hpp>
16 #include <pion/PionAlgorithms.hpp>
17 #include <pion/net/HTTPRequest.hpp>
18 #include <pion/net/TCPConnection.hpp>
19 #include <string>
20 
21 
22 namespace pion { // begin namespace pion
23 namespace net { // begin namespace net (Pion Network Library)
24 
28 class WebService :
29  private boost::noncopyable
30 {
31 public:
32 
35  public:
36  UnknownOptionException(const std::string& name)
37  : PionException("Option not recognized by web service: ", name) {}
38  };
39 
41  WebService(void) {}
42 
44  virtual ~WebService() {}
45 
52  virtual void operator()(HTTPRequestPtr& request, TCPConnectionPtr& tcp_conn) = 0;
53 
60  virtual void setOption(const std::string& name, const std::string& value) {
61  throw UnknownOptionException(name);
62  }
63 
65  virtual void start(void) {}
66 
68  virtual void stop(void) {}
69 
71  inline void setResource(const std::string& str) { m_resource = str; }
72 
74  inline const std::string& getResource(void) const { return m_resource; }
75 
77  inline std::string getRelativeResource(const std::string& resource_requested) const {
78  if (resource_requested.size() <= getResource().size()) {
79  // either the request matches the web service's resource path (a directory)
80  // or the request does not match (should never happen)
81  return std::string();
82  }
83  // strip the web service's resource path plus the slash after it
84  return algo::url_decode(resource_requested.substr(getResource().size() + 1));
85  }
86 
87 
88 private:
89 
91  std::string m_resource;
92 };
93 
94 
95 //
96 // The following symbols must be defined for any web service that you would
97 // like to be able to load dynamically using the HTTPServer::loadService()
98 // function. These are not required for any services that you only want to link
99 // directly into your programs.
100 //
101 // Make sure that you replace "WebService" with the name of your derived class.
102 // This name must also match the name of the object file (excluding the
103 // extension). These symbols must be linked into your service's object file,
104 // not included in any headers that it may use (declarations are OK in headers
105 // but not the definitions).
106 //
107 // The "pion_create" function is used to create new instances of your service.
108 // The "pion_destroy" function is used to destroy instances of your service.
109 //
110 // extern "C" WebService *pion_create_WebService(void) {
111 // return new WebService;
112 // }
113 //
114 // extern "C" void pion_destroy_WebService(WebService *service_ptr) {
115 // delete service_ptr;
116 // }
117 //
118 
119 } // end namespace net
120 } // end namespace pion
121 
122 #endif
std::string getRelativeResource(const std::string &resource_requested) const
returns the path to the resource requested, relative to the web service's location ...
Definition: WebService.hpp:77
virtual void start(void)
called when the web service's server is starting
Definition: WebService.hpp:65
WebService(void)
default constructor
Definition: WebService.hpp:41
const std::string & getResource(void) const
returns the URI stem or resource that is bound to the web service
Definition: WebService.hpp:74
virtual void stop(void)
called when the web service's server is stopping
Definition: WebService.hpp:68
void setResource(const std::string &str)
sets the URI stem or resource that is bound to the web service
Definition: WebService.hpp:71
static std::string url_decode(const std::string &str)
escapes URL-encoded strings (a%20value+with%20spaces)
exception thrown if the service does not recognize a configuration option
Definition: WebService.hpp:34
virtual ~WebService()
virtual destructor
Definition: WebService.hpp:44
virtual void setOption(const std::string &name, const std::string &value)
Definition: WebService.hpp:60
virtual void operator()(HTTPRequestPtr &request, TCPConnectionPtr &tcp_conn)=0