1 // RunUriHandler.cxx -- Run a flightgear command
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.
22 #include "RunUriHandler.hxx"
23 #include "jsonprops.hxx"
24 #include <simgear/props/props.hxx>
25 #include <simgear/structure/commands.hxx>
26 #include <Main/globals.hxx>
27 #include <3rdparty/cjson/cJSON.h>
32 namespace flightgear {
36 bool RunUriHandler::handleRequest( const HTTPRequest & request, HTTPResponse & response, Connection * connection )
38 response.Header["Content-Type"] = "text/plain";
39 string command = request.RequestVariables.get("value");
40 if( command.empty() ) {
41 response.StatusCode = 400;
42 response.Content = "command not specified";
46 SGPropertyNode_ptr args = new SGPropertyNode();
47 cJSON * json = cJSON_Parse( request.Content.c_str() );
48 JSON::toProp( json, args );
50 SG_LOG( SG_NETWORK, SG_INFO, "RunUriHandler("<< request.Content << "): command='" << command << "', arg='" << JSON::toJsonString(false,args,5) << "'");
53 if ( globals->get_commands()->execute(command.c_str(), args) ) {
54 response.Content = "ok.";
58 response.Content = "command '" + command + "' failed.";
59 response.StatusCode = 501; // Not implemented probably suits best
60 SG_LOG( SG_NETWORK, SG_WARN, response.Content );
65 } // namespace flightgear