2014-03-03 21:59:16 +01:00
|
|
|
// RunUriHandler.cxx -- Run a flightgear command
|
|
|
|
//
|
|
|
|
// Written by Torsten Dreyer, started April 2014.
|
|
|
|
//
|
|
|
|
// Copyright (C) 2014 Torsten Dreyer
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License as
|
|
|
|
// published by the Free Software Foundation; either version 2 of the
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful, but
|
|
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
|
|
|
|
#include "RunUriHandler.hxx"
|
2014-03-12 22:39:37 +01:00
|
|
|
#include "jsonprops.hxx"
|
2014-03-03 21:59:16 +01:00
|
|
|
#include <simgear/props/props.hxx>
|
|
|
|
#include <simgear/structure/commands.hxx>
|
|
|
|
#include <Main/globals.hxx>
|
2014-03-10 22:58:52 +01:00
|
|
|
#include <3rdparty/cjson/cJSON.h>
|
|
|
|
|
2014-03-03 21:59:16 +01:00
|
|
|
|
|
|
|
using std::string;
|
|
|
|
|
|
|
|
namespace flightgear {
|
|
|
|
namespace http {
|
|
|
|
|
2014-03-10 22:58:52 +01:00
|
|
|
|
2014-03-03 21:59:16 +01:00
|
|
|
bool RunUriHandler::handleRequest( const HTTPRequest & request, HTTPResponse & response )
|
|
|
|
{
|
|
|
|
response.Header["Content-Type"] = "text/plain";
|
|
|
|
string command = request.RequestVariables.get("value");
|
|
|
|
if( command.empty() ) {
|
|
|
|
response.StatusCode = 400;
|
|
|
|
response.Content = "command not specified";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-03-10 22:58:52 +01:00
|
|
|
SGPropertyNode_ptr args = new SGPropertyNode();
|
|
|
|
cJSON * json = cJSON_Parse( request.Content.c_str() );
|
2014-03-12 22:39:37 +01:00
|
|
|
JSON::toProp( json, args );
|
2014-03-10 22:58:52 +01:00
|
|
|
|
|
|
|
cJSON_Delete( json );
|
|
|
|
if ( globals->get_commands()->execute(command.c_str(), args) ) {
|
2014-03-03 21:59:16 +01:00
|
|
|
response.Content = "ok.";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
response.Content = "command '" + command + "' failed.";
|
|
|
|
SG_LOG( SG_NETWORK, SG_ALERT, response.Content );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace http
|
|
|
|
} // namespace flightgear
|
|
|
|
|