net/include/pion/net/WebService.hpp

00001 // ------------------------------------------------------------------
00002 // pion-net: a C++ framework for building lightweight HTTP interfaces
00003 // ------------------------------------------------------------------
00004 // Copyright (C) 2007-2008 Atomic Labs, Inc.  (http://www.atomiclabs.com)
00005 //
00006 // Distributed under the Boost Software License, Version 1.0.
00007 // See http://www.boost.org/LICENSE_1_0.txt
00008 //
00009 
00010 #ifndef __PION_WEBSERVICE_HEADER__
00011 #define __PION_WEBSERVICE_HEADER__
00012 
00013 #include <boost/noncopyable.hpp>
00014 #include <pion/PionConfig.hpp>
00015 #include <pion/PionException.hpp>
00016 #include <pion/net/HTTPRequest.hpp>
00017 #include <pion/net/TCPConnection.hpp>
00018 #include <string>
00019 
00020 
00021 namespace pion {    // begin namespace pion
00022 namespace net {     // begin namespace net (Pion Network Library)
00023 
00027 class WebService :
00028     private boost::noncopyable
00029 {
00030 public:
00031 
00033     class UnknownOptionException : public PionException {
00034     public:
00035         UnknownOptionException(const std::string& name)
00036             : PionException("Option not recognized by web service: ", name) {}
00037     };
00038 
00040     WebService(void) {}
00041 
00043     virtual ~WebService() {}
00044 
00051     virtual void operator()(HTTPRequestPtr& request, TCPConnectionPtr& tcp_conn) = 0;
00052     
00059     virtual void setOption(const std::string& name, const std::string& value) {
00060         throw UnknownOptionException(name);
00061     }
00062     
00064     virtual void start(void) {}
00065     
00067     virtual void stop(void) {}
00068     
00070     inline void setResource(const std::string& str) { m_resource = str; }
00071 
00073     inline const std::string& getResource(void) const { return m_resource; }
00074     
00076     inline std::string getRelativeResource(const std::string& resource_requested) const {
00077         if (resource_requested.size() <= getResource().size()) {
00078             // either the request matches the web service's resource path (a directory)
00079             // or the request does not match (should never happen)
00080             return std::string();
00081         }
00082         // strip the web service's resource path plus the slash after it
00083         return HTTPTypes::url_decode(resource_requested.substr(getResource().size() + 1));
00084     }
00085     
00086     
00087 private:
00088         
00090     std::string m_resource;
00091 };
00092 
00093 
00094 //
00095 // The following symbols must be defined for any web service that you would
00096 // like to be able to load dynamically using the HTTPServer::loadService()
00097 // function.  These are not required for any services that you only want to link
00098 // directly into your programs.
00099 //
00100 // Make sure that you replace "WebService" with the name of your derived class.
00101 // This name must also match the name of the object file (excluding the
00102 // extension).  These symbols must be linked into your service's object file,
00103 // not included in any headers that it may use (declarations are OK in headers
00104 // but not the definitions).
00105 //
00106 // The "pion_create" function is used to create new instances of your service.
00107 // The "pion_destroy" function is used to destroy instances of your service.
00108 //
00109 // extern "C" WebService *pion_create_WebService(void) {
00110 //      return new WebService;
00111 // }
00112 //
00113 // extern "C" void pion_destroy_WebService(WebService *service_ptr) {
00114 //      delete service_ptr;
00115 // }
00116 //
00117 
00118 }   // end namespace net
00119 }   // end namespace pion
00120 
00121 #endif

Generated on Fri Apr 30 14:48:53 2010 for pion-net by  doxygen 1.4.7