1 // PkgUriHandler.cxx -- service for the package system
3 // Written by Torsten Dreyer, started February 2015.
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 "PkgUriHandler.hxx"
23 #include <3rdparty/cjson/cJSON.h>
25 #include <simgear/package/Root.hxx>
26 #include <simgear/package/Catalog.hxx>
27 #include <simgear/package/Delegate.hxx>
28 #include <simgear/package/Install.hxx>
29 #include <simgear/package/Package.hxx>
31 #include <Main/fg_props.hxx>
35 namespace flightgear {
39 url: /pkg/command/args
56 static cJSON * StringListToJson( const string_list & l )
58 cJSON * jsonArray = cJSON_CreateArray();
59 for( string_list::const_iterator it = l.begin(); it != l.end(); ++it )
60 cJSON_AddItemToArray(jsonArray, cJSON_CreateString((*it).c_str()) );
64 static cJSON * PackageToJson( simgear::pkg::Package * p )
66 cJSON * json = cJSON_CreateObject();
68 cJSON_AddItemToObject(json, "id", cJSON_CreateString( p->id().c_str() ));
69 cJSON_AddItemToObject(json, "name", cJSON_CreateString( p->name().c_str() ));
70 cJSON_AddItemToObject(json, "description", cJSON_CreateString( p->description().c_str() ));
71 cJSON_AddItemToObject(json, "installed", cJSON_CreateBool( p->isInstalled() ));
72 cJSON_AddItemToObject(json, "thumbnails", StringListToJson( p->thumbnailUrls() ));
73 cJSON_AddItemToObject(json, "variants", StringListToJson( p->variants() ));
74 cJSON_AddItemToObject(json, "revision", cJSON_CreateNumber( p->revision() ));
75 cJSON_AddItemToObject(json, "fileSize", cJSON_CreateNumber( p->fileSizeBytes() ));
76 cJSON_AddItemToObject(json, "author", cJSON_CreateString( p->getLocalisedProp("author").c_str() ));
77 cJSON_AddItemToObject(json, "ratingFdm", cJSON_CreateString( p->getLocalisedProp("rating/FDM").c_str() ));
78 cJSON_AddItemToObject(json, "ratingCockpit", cJSON_CreateString( p->getLocalisedProp("rating/cockpit").c_str() ));
79 cJSON_AddItemToObject(json, "ratingModel", cJSON_CreateString( p->getLocalisedProp("rating/model").c_str() ));
80 cJSON_AddItemToObject(json, "ratingSystems", cJSON_CreateString( p->getLocalisedProp("rating/systems").c_str() ));
85 static cJSON * PackageListToJson( const simgear::pkg::PackageList & l )
87 cJSON * jsonArray = cJSON_CreateArray();
88 for( simgear::pkg::PackageList::const_iterator it = l.begin(); it != l.end(); ++it ) {
89 cJSON_AddItemToArray(jsonArray, PackageToJson(*it) );
94 static cJSON * CatalogToJson( simgear::pkg::Catalog * c )
96 cJSON * json = cJSON_CreateObject();
98 cJSON_AddItemToObject(json, "id", cJSON_CreateString( c->id().c_str() ));
99 cJSON_AddItemToObject(json, "installRoot", cJSON_CreateString( c->installRoot().str().c_str() ));
100 cJSON_AddItemToObject(json, "url", cJSON_CreateString( c->url().c_str() ));
101 cJSON_AddItemToObject(json, "description", cJSON_CreateString( c->description().c_str() ));
102 cJSON_AddItemToObject(json, "packages", PackageListToJson(c->packages()) );
103 cJSON_AddItemToObject(json, "needingUpdate", PackageListToJson(c->packagesNeedingUpdate()) );
104 cJSON_AddItemToObject(json, "installed", PackageListToJson(c->installedPackages()) );
110 static string PackageRootCommand( simgear::pkg::Root* packageRoot, const string & command, const string & args )
112 cJSON * json = cJSON_CreateObject();
114 if( command == "path" ) {
116 cJSON_AddItemToObject(json, "path", cJSON_CreateString( packageRoot->path().str().c_str() ));
118 } else if( command == "version" ) {
120 cJSON_AddItemToObject(json, "version", cJSON_CreateString( packageRoot->applicationVersion().c_str() ));
122 } else if( command == "refresh" ) {
123 packageRoot->refresh(true);
124 cJSON_AddItemToObject(json, "refresh", cJSON_CreateString( "OK" ));
126 } else if( command == "catalogs" ) {
128 cJSON * jsonArray = cJSON_CreateArray();
129 simgear::pkg::CatalogList catalogList = packageRoot->catalogs();
130 for( simgear::pkg::CatalogList::iterator it = catalogList.begin(); it != catalogList.end(); ++it ) {
131 cJSON_AddItemToArray(jsonArray, CatalogToJson(*it) );
133 cJSON_AddItemToObject(json, "catalogs", jsonArray );
135 } else if( command == "packageById" ) {
137 simgear::pkg::PackageRef p = packageRoot->getPackageById(args);
138 cJSON_AddItemToObject(json, "package", PackageToJson( p ));
140 } else if( command == "catalogById" ) {
142 simgear::pkg::CatalogRef p = packageRoot->getCatalogById(args);
143 cJSON_AddItemToObject(json, "catalog", CatalogToJson( p ));
145 } else if( command == "search" ) {
147 SGPropertyNode_ptr query(new SGPropertyNode);
148 simgear::pkg::PackageList packageList = packageRoot->packagesMatching(query);
149 cJSON_AddItemToObject(json, "packages", PackageListToJson(packageList) );
151 } else if( command == "install" ) {
153 simgear::pkg::PackageRef package = packageRoot->getPackageById(args);
154 if( NULL == package ) {
155 SG_LOG(SG_NETWORK,SG_WARN,"Can't install package '" << args << "', package not found" );
156 cJSON_Delete( json );
159 package->existingInstall();
162 SG_LOG( SG_NETWORK,SG_WARN, "Unhandled pkg command : '" << command << "'" );
163 cJSON_Delete( json );
167 char * jsonString = cJSON_PrintUnformatted( json );
168 string reply(jsonString);
170 cJSON_Delete( json );
174 static string findCommand( const string & uri, string & outArgs )
176 size_t n = uri.find_first_of('/');
177 if( n == string::npos ) outArgs = string("");
178 else outArgs = uri.substr( n+1 );
179 return uri.substr( 0, n );
182 bool PkgUriHandler::handleRequest( const HTTPRequest & request, HTTPResponse & response, Connection * connection )
184 response.Header["Content-Type"] = "application/json; charset=UTF-8";
185 response.Header["Access-Control-Allow-Origin"] = "*";
186 response.Header["Access-Control-Allow-Methods"] = "OPTIONS, GET, POST";
187 response.Header["Access-Control-Allow-Headers"] = "Origin, Accept, Content-Type, X-Requested-With, X-CSRF-Token";
189 if( request.Method == "OPTIONS" ){
190 return true; // OPTIONS only needs the headers
193 simgear::pkg::Root* packageRoot = globals->packageRoot();
194 if( NULL == packageRoot ) {
195 SG_LOG( SG_NETWORK,SG_WARN, "NO PackageRoot" );
196 response.StatusCode = 500;
197 response.Content = "{}";
202 string command = findCommand( string(request.Uri).substr(getUri().size()), argString );
205 SG_LOG(SG_NETWORK,SG_INFO, "Request is for command '" << command << "' with arg='" << argString << "'" );
207 if( request.Method == "GET" ){
208 } else if( request.Method == "POST" ) {
210 SG_LOG(SG_NETWORK,SG_INFO, "PkgUriHandler: invalid request method '" << request.Method << "'" );
211 response.Header["Allow"] = "OPTIONS, GET, POST";
212 response.StatusCode = 405;
213 response.Content = "{}";
217 response.Content = PackageRootCommand( packageRoot, command, argString );
218 if( response.Content.empty() ) {
219 response.StatusCode = 404;
220 response.Content = "{}";
226 } // namespace flightgear