# 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/PionAlgorithms.hpp>
    00017 #include <pion/net/HTTPRequest.hpp>
    00018 #include <pion/net/TCPConnection.hpp>
    00019 #include <string>
    00020
    00021
    00022 namespace pion {    // begin namespace pion
    00023 namespace net {     // begin namespace net (Pion Network Library)
    00024
    00028 class WebService :
    00029     private boost::noncopyable
    00030 {
    00031 public:
    00032
    00034     class UnknownOptionException : public PionException {
    00035     public:
    00036         UnknownOptionException(const std::string& name)
    00037             : PionException("Option not recognized by web service: ", name) {}
    00038     };
    00039
    00041     WebService(void) {}
    00042
    00044     virtual ~WebService() {}
    00045
    00052     virtual void operator()(HTTPRequestPtr& request, TCPConnectionPtr& tcp_conn) = 0;
    00053
    00060     virtual void setOption(const std::string& name, const std::string& value) {
    00061         throw UnknownOptionException(name);
    00062     }
    00063
    00065     virtual void start(void) {}
    00066
    00068     virtual void stop(void) {}
    00069
    00071     inline void setResource(const std::string& str) { m_resource = str; }
    00072
    00074     inline const std::string& getResource(void) const { return m_resource; }
    00075
    00077     inline std::string getRelativeResource(const std::string& resource_requested) const {
    00078         if (resource_requested.size() <= getResource().size()) {
    00079             // either the request matches the web service's resource path (a directory)
    00080             // or the request does not match (should never happen)
    00081             return std::string();
    00082         }
    00083         // strip the web service's resource path plus the slash after it
    00084         return algo::url_decode(resource_requested.substr(getResource().size() + 1));
    00085     }
    00086
    00087
    00088 private:
    00089
    00091     std::string m_resource;
    00092 };
    00093
    00094
    00095 //
    00096 // The following symbols must be defined for any web service that you would
    00097 // like to be able to load dynamically using the HTTPServer::loadService()
    00098 // function.  These are not required for any services that you only want to link
    00099 // directly into your programs.
    00100 //
    00101 // Make sure that you replace "WebService" with the name of your derived class.
    00102 // This name must also match the name of the object file (excluding the
    00103 // extension).  These symbols must be linked into your service's object file,
    00104 // not included in any headers that it may use (declarations are OK in headers
    00105 // but not the definitions).
    00106 //
    00107 // The "pion_create" function is used to create new instances of your service.
    00108 // The "pion_destroy" function is used to destroy instances of your service.
    00109 //
    00110 // extern "C" WebService *pion_create_WebService(void) {
    00111 //      return new WebService;
    00112 // }
    00113 //
    00114 // extern "C" void pion_destroy_WebService(WebService *service_ptr) {
    00115 //      delete service_ptr;
    00116 // }
    00117 //
    00118
    00119 }   // end namespace net
    00120 }   // end namespace pion
    00121
    00122 #endif