| 1 | // urihandler.hxx -- a base class for URI handler |
| 2 | // |
| 3 | // Written by Torsten Dreyer, started April 2014. |
| 4 | // |
| 5 | // Copyright (C) 2014 Torsten Dreyer |
| 6 | // |
| 7 | // This program is free software; you can redistribute it and/or |
| 8 | // modify it under the terms of the GNU General Public License as |
| 9 | // published by the Free Software Foundation; either version 2 of the |
| 10 | // License, or (at your option) any later version. |
| 11 | // |
| 12 | // This program is distributed in the hope that it will be useful, but |
| 13 | // WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | // General Public License for more details. |
| 16 | // |
| 17 | // You should have received a copy of the GNU General Public License |
| 18 | // along with this program; if not, write to the Free Software |
| 19 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 20 | |
| 21 | #ifndef __FG_URI_HANDLER_HXX |
| 22 | #define __FG_URI_HANDLER_HXX |
| 23 | |
| 24 | #include "HTTPRequest.hxx" |
| 25 | #include "HTTPResponse.hxx" |
| 26 | #include <simgear/structure/SGReferenced.hxx> |
| 27 | #include <simgear/structure/SGSharedPtr.hxx> |
| 28 | #include <string> |
| 29 | #include <map> |
| 30 | |
| 31 | namespace flightgear { |
| 32 | namespace http { |
| 33 | |
| 34 | class ConnectionData : public SGReferenced { |
| 35 | public: |
| 36 | // make this polymorphic |
| 37 | virtual ~ConnectionData() {} |
| 38 | }; |
| 39 | |
| 40 | class Connection { |
| 41 | public: |
| 42 | void put( const std::string & key, SGSharedPtr<ConnectionData> value ) { |
| 43 | connectionData[key] = value; |
| 44 | } |
| 45 | SGSharedPtr<ConnectionData> get(const std::string & key ) { |
| 46 | return connectionData[key]; |
| 47 | } |
| 48 | |
| 49 | void remove( const std::string & key ) { |
| 50 | connectionData.erase(key); |
| 51 | } |
| 52 | |
| 53 | virtual void write(const char * data, size_t len) = 0; |
| 54 | |
| 55 | private: |
| 56 | std::map<std::string,SGSharedPtr<ConnectionData> > connectionData; |
| 57 | }; |
| 58 | |
| 59 | /** |
| 60 | * A Base class for URI Handlers. |
| 61 | * Provides methods for handling a request and handling subsequent polls. |
| 62 | * All methods are implemented as noops and should be overridden by deriving classes |
| 63 | */ |
| 64 | class URIHandler : public SGReferenced { |
| 65 | public: |
| 66 | URIHandler( const std::string& uri ) : _uri(uri) {} |
| 67 | virtual ~URIHandler() {} |
| 68 | |
| 69 | /** |
| 70 | * This method gets called from the httpd if a request has been detected on the connection |
| 71 | * @param request The HTTP Request filled in by the httpd |
| 72 | * @param response the HTTP Response to be filled in by the hander |
| 73 | * @param connection Connection specific information, can be used to store persistent state |
| 74 | * @return true if the request has been completely answered, false to get poll()ed |
| 75 | */ |
| 76 | virtual bool handleRequest( const HTTPRequest & request, HTTPResponse & response, Connection * connection = NULL ) { |
| 77 | if( request.Method == "GET" ) return handleGetRequest( request, response, connection ); |
| 78 | if( request.Method == "PUT" ) return handlePutRequest( request, response, connection ); |
| 79 | return true; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Convenience method for GET Requests, gets called by handleRequest if not overridden |
| 84 | * @param request @see handleRequest() |
| 85 | * @param response @see handleRequest() |
| 86 | * @param connection @see handleRequest() |
| 87 | * @return @see handleRequest() |
| 88 | * |
| 89 | */ |
| 90 | virtual bool handleGetRequest( const HTTPRequest & request, HTTPResponse & response, Connection * connection ) { |
| 91 | return true; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Convenience method for PUT Requests, gets called by handleRequest if not overridden |
| 96 | * @param request @see handleRequest() |
| 97 | * @param response @see handleRequest() |
| 98 | * @param connection @see handleRequest() |
| 99 | * @return @see handleRequest() |
| 100 | * |
| 101 | */ |
| 102 | virtual bool handlePutRequest( const HTTPRequest & request, HTTPResponse & response, Connection * connection ) { |
| 103 | return true; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * This method gets called from the httpd if the preceding handleRequest() or poll() method returned false. |
| 108 | * @param connection @see handleRequest() |
| 109 | * @return @see handleRequest() |
| 110 | */ |
| 111 | virtual bool poll( Connection * connection ) { return false; } |
| 112 | |
| 113 | /** |
| 114 | * Getter for the URI this handler serves |
| 115 | * |
| 116 | * @return the URI this handler serves |
| 117 | */ |
| 118 | const std::string & getUri() const { return _uri; } |
| 119 | |
| 120 | private: |
| 121 | std::string _uri; |
| 122 | }; |
| 123 | |
| 124 | } // namespace http |
| 125 | } // namespace flightgear |
| 126 | |
| 127 | #endif //#define __FG_URI_HANDLER_HXX |