| 1 | // RunUriHandler.cxx -- Run a flightgear command |
| 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 "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 <cJSON.h> |
| 28 | |
| 29 | |
| 30 | using std::string; |
| 31 | |
| 32 | namespace flightgear { |
| 33 | namespace http { |
| 34 | |
| 35 | |
| 36 | bool RunUriHandler::handleRequest( const HTTPRequest & request, HTTPResponse & response, Connection * connection ) |
| 37 | { |
| 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"; |
| 43 | return true; |
| 44 | } |
| 45 | |
| 46 | SGPropertyNode_ptr args = new SGPropertyNode(); |
| 47 | cJSON * json = cJSON_Parse( request.Content.c_str() ); |
| 48 | JSON::toProp( json, args ); |
| 49 | |
| 50 | SG_LOG( SG_NETWORK, SG_INFO, "RunUriHandler("<< request.Content << "): command='" << command << "', arg='" << JSON::toJsonString(false,args,5) << "'"); |
| 51 | |
| 52 | cJSON_Delete( json ); |
| 53 | if ( globals->get_commands()->execute(command.c_str(), args, nullptr) ) { |
| 54 | response.Content = "ok."; |
| 55 | return true; |
| 56 | } |
| 57 | |
| 58 | response.Content = "command '" + command + "' failed."; |
| 59 | response.StatusCode = 501; // Not implemented probably suits best |
| 60 | SG_LOG( SG_NETWORK, SG_WARN, response.Content ); |
| 61 | return true; |
| 62 | } |
| 63 | |
| 64 | } // namespace http |
| 65 | } // namespace flightgear |
| 66 |