FlightGear next
JsonUriHandler.cxx
Go to the documentation of this file.
1// JsonUriHandler.cxx -- json interface to the property tree
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
22#include "JsonUriHandler.hxx"
23#include "jsonprops.hxx"
24#include <Main/fg_props.hxx>
25
26using std::string;
27
28namespace flightgear {
29namespace http {
30
31bool JsonUriHandler::handleRequest( const HTTPRequest & request, HTTPResponse & response, Connection * connection )
32{
33 response.Header["Content-Type"] = "application/json; charset=UTF-8";
34 response.Header["Access-Control-Allow-Origin"] = "*";
35 response.Header["Access-Control-Allow-Methods"] = "OPTIONS, GET, POST";
36 response.Header["Access-Control-Allow-Headers"] = "Origin, Accept, Content-Type, X-Requested-With, X-CSRF-Token";
37
38 if( request.Method == "OPTIONS" ){
39 return true; // OPTIONS only needs the headers
40 }
41
42 if( request.Method == "GET" ){
43
44 // max recursion depth
45 int depth = atoi(request.RequestVariables.get("d").c_str());
46 if( depth < 1 ) depth = 1; // at least one level
47
48 // pretty print (y) or compact print (default)
49 bool indent = request.RequestVariables.get("i") == "y";
50 bool timestamp = request.RequestVariables.get("t") == "y";
51
52 SGPropertyNode_ptr node = getRequestedNode(request );
53 if( !node.valid() ) {
54 response.StatusCode = 404;
55 response.Content = "{}";
56 return true;
57 }
58
59 response.Content = JSON::toJsonString( indent, node, depth, timestamp ? fgGetDouble("/sim/time/elapsed-sec") : -1.0 );
60
61 return true;
62 }
63
64 if( request.Method == "POST" ) {
65 SGPropertyNode_ptr node = getRequestedNode(request );
66 if( !node.valid() ) {
67 response.StatusCode = 404;
68 response.Content = "{}";
69 return true;
70 }
71
72 SG_LOG(SG_NETWORK,SG_INFO, "JsonUriHandler: setting property from'" << request.Content << "'" );
73 cJSON * json = cJSON_Parse( request.Content.c_str() );
74 if( NULL != json ) {
75 JSON::toProp( json, node );
76 cJSON_Delete(json);
77 }
78
79 response.Content = "{}";
80 return true;
81 }
82
83 SG_LOG(SG_NETWORK,SG_INFO, "JsonUriHandler: invalid request method '" << request.Method << "'" );
84 response.Header["Allow"] = "OPTIONS, GET, POST";
85 response.StatusCode = 405;
86 response.Content = "{}";
87 return true;
88
89}
90
91SGPropertyNode_ptr JsonUriHandler::getRequestedNode(const HTTPRequest & request)
92{
93 SG_LOG(SG_NETWORK,SG_INFO, "JsonUriHandler: request is '" << request.Uri << "'" );
94 string propertyPath = request.Uri;
95 propertyPath = propertyPath.substr( getUri().size() );
96
97 // skip trailing '/' - not very efficient but shouldn't happen too often
98 while( !propertyPath.empty() && propertyPath[ propertyPath.length()-1 ] == '/' )
99 propertyPath = propertyPath.substr(0,propertyPath.length()-1);
100
101 SGPropertyNode_ptr reply = fgGetNode( string("/") + propertyPath );
102 if( !reply.valid() ) {
103 SG_LOG(SG_NETWORK,SG_WARN, "JsonUriHandler: requested node not found: '" << propertyPath << "'");
104 }
105 return reply;
106}
107
108
109} // namespace http
110} // namespace flightgear
111
std::string get(const std::string &key) const
static std::string toJsonString(bool indent, SGPropertyNode_ptr n, int depth, double timestamp=-1.0)
static void toProp(cJSON *json, SGPropertyNode_ptr base)
virtual bool handleRequest(const HTTPRequest &request, HTTPResponse &response, Connection *connection)
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.
FlightPlan.hxx - defines a full flight-plan object, including departure, cruise, arrival information ...
Definition Addon.cxx:53
static int atoi(const string &str)
Definition options.cxx:113
double fgGetDouble(const char *name, double defaultValue)
Get a double value for a property.
Definition proptest.cpp:30
SGPropertyNode * fgGetNode(const char *path, bool create)
Get a property node.
Definition proptest.cpp:27