]> git.mxchange.org Git - flightgear.git/blob - src/Network/http/PkgUriHandler.cxx
Interim windows build fix
[flightgear.git] / src / Network / http / PkgUriHandler.cxx
1 // PkgUriHandler.cxx -- service for the package system
2 //
3 // Written by Torsten Dreyer, started February 2015.
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 "PkgUriHandler.hxx"
23 #include <3rdparty/cjson/cJSON.h>
24
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>
30
31 #include <Main/fg_props.hxx>
32
33 using std::string;
34
35 namespace flightgear {
36 namespace http {
37
38 /*
39 url: /pkg/command/args
40
41 Examples:
42 /pkg/path
43
44 Input:
45 {
46   command: "command",
47   args: {
48   }
49 }
50
51 Output:
52 {
53 }
54 */
55
56 static cJSON * StringListToJson( const string_list & l )
57 {
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()) );
61   return jsonArray;
62 }
63
64 static cJSON * PackageToJson( simgear::pkg::Package * p )
65 {
66   cJSON * json = cJSON_CreateObject();
67   if( p ) {
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() ));
81   }
82   return json;
83 }
84
85 static cJSON * PackageListToJson( const simgear::pkg::PackageList & l )
86 {
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) );
90   }
91   return jsonArray;
92 }
93
94 static cJSON * CatalogToJson( simgear::pkg::Catalog * c )
95 {
96   cJSON * json = cJSON_CreateObject();
97   if( c ) {
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()) );
105   }
106   return json;
107 }
108
109
110 static string PackageRootCommand( simgear::pkg::Root* packageRoot, const string & command, const string & args )
111 {
112   cJSON * json = cJSON_CreateObject();
113
114   if( command == "path" ) {
115
116     cJSON_AddItemToObject(json, "path", cJSON_CreateString( packageRoot->path().str().c_str() ));
117
118   } else if( command == "version" ) {
119
120     cJSON_AddItemToObject(json, "version", cJSON_CreateString( packageRoot->applicationVersion().c_str() ));
121
122   } else if( command == "refresh" ) {
123     packageRoot->refresh(true);
124     cJSON_AddItemToObject(json, "refresh", cJSON_CreateString( "OK" ));
125
126   } else if( command == "catalogs" ) {
127
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) );
132     }
133     cJSON_AddItemToObject(json, "catalogs", jsonArray );
134
135   } else if( command == "packageById" ) {
136
137     simgear::pkg::PackageRef p = packageRoot->getPackageById(args);
138     cJSON_AddItemToObject(json, "package", PackageToJson( p ));
139
140   } else if( command == "catalogById" ) {
141
142     simgear::pkg::CatalogRef p = packageRoot->getCatalogById(args);
143     cJSON_AddItemToObject(json, "catalog", CatalogToJson( p ));
144
145   } else if( command == "search" ) {
146
147     SGPropertyNode_ptr query(new SGPropertyNode);
148     simgear::pkg::PackageList packageList = packageRoot->packagesMatching(query);
149     cJSON_AddItemToObject(json, "packages", PackageListToJson(packageList) );
150
151   } else if( command == "install" ) {
152
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 );
157                     return string("");
158           }
159           package->existingInstall();
160
161   } else {
162     SG_LOG( SG_NETWORK,SG_WARN, "Unhandled pkg command : '" << command << "'" );
163     cJSON_Delete( json );
164     return string("");
165   }
166
167   char * jsonString = cJSON_PrintUnformatted( json );
168   string reply(jsonString);
169   free( jsonString );
170   cJSON_Delete( json );
171   return reply;
172 }
173
174 static  string findCommand( const string & uri, string & outArgs )
175 {
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 );
180 }
181
182 bool PkgUriHandler::handleRequest( const HTTPRequest & request, HTTPResponse & response, Connection * connection )
183 {
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";
188
189   if( request.Method == "OPTIONS" ){
190       return true; // OPTIONS only needs the headers
191   }
192
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 = "{}";
198     return true; 
199   }
200
201   string argString;
202   string command = findCommand( string(request.Uri).substr(getUri().size()), argString );
203   
204
205   SG_LOG(SG_NETWORK,SG_INFO, "Request is for command '"  << command << "' with arg='" << argString << "'" );
206
207   if( request.Method == "GET" ){
208   } else if( request.Method == "POST" ) {
209   } else {
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 = "{}";
214     return true; 
215   }
216
217   response.Content = PackageRootCommand( packageRoot, command, argString );
218   if( response.Content.empty() ) {
219     response.StatusCode = 404;
220     response.Content = "{}";
221   }
222   return true; 
223 }
224
225 } // namespace http
226 } // namespace flightgear
227