1 // urihandler.hxx -- a base class for URI handler
3 // Written by Torsten Dreyer, started April 2014.
5 // Copyright (C) 2014 Torsten Dreyer
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.
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.
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.
21 #ifndef __FG_URI_HANDLER_HXX
22 #define __FG_URI_HANDLER_HXX
24 #include "HTTPRequest.hxx"
25 #include "HTTPResponse.hxx"
26 #include <simgear/structure/SGReferenced.hxx>
27 #include <simgear/structure/SGSharedPtr.hxx>
31 namespace flightgear {
34 class ConnectionData : public SGReferenced {
36 // make this polymorphic
37 virtual ~ConnectionData() {}
42 void put( const std::string & key, SGSharedPtr<ConnectionData> value ) {
43 connectionData[key] = value;
45 SGSharedPtr<ConnectionData> get(const std::string & key ) {
46 return connectionData[key];
49 void remove( const std::string & key ) {
50 connectionData.erase(key);
53 virtual void write(const char * data, size_t len) = 0;
56 std::map<std::string,SGSharedPtr<ConnectionData> > connectionData;
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
64 class URIHandler : public SGReferenced {
66 URIHandler( const char * uri ) : _uri(uri) {}
67 virtual ~URIHandler() {}
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
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 );
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()
90 virtual bool handleGetRequest( const HTTPRequest & request, HTTPResponse & response, Connection * connection ) {
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()
102 virtual bool handlePutRequest( const HTTPRequest & request, HTTPResponse & response, Connection * connection ) {
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()
111 virtual bool poll( Connection * connection ) { return false; }
114 * Getter for the URI this handler serves
116 * @return the URI this handler serves
118 const std::string & getUri() const { return _uri; }
125 } // namespace flightgear
127 #endif //#define __FG_URI_HANDLER_HXX