FlightGear next
urihandler.hxx
Go to the documentation of this file.
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
31namespace flightgear {
32namespace http {
33
34class ConnectionData : public SGReferenced {
35public:
36 // make this polymorphic
37 virtual ~ConnectionData() {}
38};
39
41public:
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
55private:
56 std::map<std::string,SGSharedPtr<ConnectionData> > connectionData;
57};
58
64class URIHandler : public SGReferenced {
65public:
66 URIHandler( const std::string& uri ) : _uri(uri) {}
67 virtual ~URIHandler() {}
68
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
90 virtual bool handleGetRequest( const HTTPRequest & request, HTTPResponse & response, Connection * connection ) {
91 return true;
92 }
93
102 virtual bool handlePutRequest( const HTTPRequest & request, HTTPResponse & response, Connection * connection ) {
103 return true;
104 }
105
111 virtual bool poll( Connection * connection ) { return false; }
112
118 const std::string & getUri() const { return _uri; }
119
120private:
121 std::string _uri;
122};
123
124} // namespace http
125} // namespace flightgear
126
127#endif //#define __FG_URI_HANDLER_HXX
SGSharedPtr< ConnectionData > get(const std::string &key)
virtual void write(const char *data, size_t len)=0
void put(const std::string &key, SGSharedPtr< ConnectionData > value)
void remove(const std::string &key)
virtual bool poll(Connection *connection)
This method gets called from the httpd if the preceding handleRequest() or poll() method returned fal...
virtual bool handleRequest(const HTTPRequest &request, HTTPResponse &response, Connection *connection=NULL)
This method gets called from the httpd if a request has been detected on the connection.
const std::string & getUri() const
Getter for the URI this handler serves.
virtual bool handlePutRequest(const HTTPRequest &request, HTTPResponse &response, Connection *connection)
Convenience method for PUT Requests, gets called by handleRequest if not overridden.
URIHandler(const std::string &uri)
virtual bool handleGetRequest(const HTTPRequest &request, HTTPResponse &response, Connection *connection)
Convenience method for GET Requests, gets called by handleRequest if not overridden.
FlightPlan.hxx - defines a full flight-plan object, including departure, cruise, arrival information ...
Definition Addon.cxx:53