Debian Codegraph @

src/Network/HTTPClient.cxx

raw symbols SCIP
1 // HTTPClient.cxx -- Singleton HTTP client object
2 //
3 // Written by James Turner, started April 2012.
4 //
5 // Copyright (C) 2012 James Turner
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 #include "config.h"
22
23 #include "HTTPClient.hxx"
24
25 #include <cassert>
26
27 #include <Main/fg_props.hxx>
28
29 #include <simgear/sg_inlines.h>
30
31 #include <simgear/package/Root.hxx>
32 #include <simgear/package/Catalog.hxx>
33 #include <simgear/package/Delegate.hxx>
34 #include <simgear/package/Install.hxx>
35 #include <simgear/package/Package.hxx>
36
37 #include <simgear/nasal/cppbind/from_nasal.hxx>
38 #include <simgear/nasal/cppbind/to_nasal.hxx>
39 #include <simgear/nasal/cppbind/NasalHash.hxx>
40 #include <simgear/nasal/cppbind/Ghost.hxx>
41
42 #include <Scripting/NasalSys.hxx>
43
44 using namespace simgear;
45
46 typedef nasal::Ghost<pkg::RootRef> NasalPackageRoot;
47 typedef nasal::Ghost<pkg::PackageRef> NasalPackage;
48 typedef nasal::Ghost<pkg::CatalogRef> NasalCatalog;
49 typedef nasal::Ghost<pkg::InstallRef> NasalInstall;
50
51 static const char* OFFICIAL_CATALOG_ID = "org.flightgear.fgaddon.stable_2024";
52
53 // fallback URL is used when looking up a version-specific catalog fails
54 static const char* FALLBACK_CATALOG_URL = "https://mirrors.ibiblio.org/flightgear/ftp/Aircraft-2024/catalog.xml";
55
56 namespace {
57
58 std::string _getDefaultCatalogId()
59 {
60 return fgGetString("/sim/package-system/default-catalog/id",
61 OFFICIAL_CATALOG_ID);
62 }
63
64 pkg::CatalogRef getDefaultCatalog()
65 {
66 if (!globals->packageRoot())
67 return pkg::CatalogRef();
68
69 return globals->packageRoot()->getCatalogById(_getDefaultCatalogId());
70 }
71
72 std::string _getDefaultCatalogUrl()
73 {
74 return fgGetString("/sim/package-system/default-catalog/url",
75 "https://mirrors.ibiblio.org/flightgear/ftp/Aircraft-2024/catalog.xml");
76 }
77 } // of anonymous namespace
78
79
80 FGHTTPClient::FGHTTPClient() :
81 _inited(false)
82 {
83 }
84
85 FGHTTPClient::~FGHTTPClient()
86 {
87 }
88
89 FGHTTPClient* FGHTTPClient::getOrCreate()
90 {
91 auto ext = globals->get_subsystem<FGHTTPClient>();
92 if (ext) {
93 return ext;
94 }
95
96 ext = globals->get_subsystem_mgr()->add<FGHTTPClient>();
97 ext->init();
98 return ext;
99 }
100
101
102 void FGHTTPClient::init()
103 {
104 // launcher may need to setup HTTP access abnormally early, so
105 // guard against duplicate inits
106 if (_inited) {
107 return;
108 }
109
110 _http.reset(new simgear::HTTP::Client);
111
112 std::string proxyHost(fgGetString("/sim/presets/proxy/host"));
113 int proxyPort(fgGetInt("/sim/presets/proxy/port"));
114 std::string proxyAuth(fgGetString("/sim/presets/proxy/auth"));
115
116 if (!proxyHost.empty()) {
117 _http->setProxy(proxyHost, proxyPort, proxyAuth);
118 }
119
120 pkg::Root* packageRoot = globals->packageRoot();
121 if (packageRoot) {
122 // package system needs access to the HTTP engine too
123 packageRoot->setHTTPClient(_http.get());
124 }
125
126 _inited = true;
127 }
128
129 bool FGHTTPClient::isDefaultCatalogInstalled() const
130 {
131 return getDefaultCatalog().valid();
132 }
133
134 pkg::CatalogRef FGHTTPClient::addDefaultCatalog()
135 {
136 pkg::CatalogRef defaultCatalog = getDefaultCatalog();
137 if (!defaultCatalog.valid()) {
138 auto cat = pkg::Catalog::createFromUrl(globals->packageRoot(), getDefaultCatalogUrl());
139 return cat;
140 }
141
142 return defaultCatalog;
143 }
144
145 std::string FGHTTPClient::getDefaultCatalogId() const
146 {
147 return _getDefaultCatalogId();
148 }
149
150 std::string FGHTTPClient::getDefaultCatalogUrl() const
151 {
152 return _getDefaultCatalogUrl();
153 }
154
155 std::string FGHTTPClient::getDefaultCatalogFallbackUrl() const
156 {
157 return std::string(FALLBACK_CATALOG_URL);
158 }
159
160 static naRef f_package_existingInstall( pkg::Package& pkg,
161 const nasal::CallContext& ctx )
162 {
163 return ctx.to_nasal(
164 pkg.existingInstall( ctx.getArg<pkg::Package::InstallCallback>(0) )
165 );
166 }
167
168 static naRef f_package_uninstall(pkg::Package& pkg, const nasal::CallContext& ctx)
169 {
170 pkg::InstallRef ins = pkg.existingInstall();
171 if (ins) {
172 ins->uninstall();
173 }
174
175 return naNil();
176 }
177
178 static SGPropertyNode_ptr queryPropsFromHash(const nasal::Hash& h)
179 {
180 SGPropertyNode_ptr props(new SGPropertyNode);
181
182 for (nasal::Hash::const_iterator it = h.begin(); it != h.end(); ++it) {
183 std::string const key = it->getKey();
184 if ((key == "name") || (key == "description")) {
185 props->setStringValue(key, it->getValue<std::string>());
186 } else if (strutils::starts_with(key, "rating-")) {
187 props->setIntValue(key, it->getValue<int>());
188 } else if (key == "tags") {
189 string_list tags = it->getValue<string_list>();
190 string_list::const_iterator tagIt;
191 int tagCount = 0;
192 for (tagIt = tags.begin(); tagIt != tags.end(); ++tagIt) {
193 SGPropertyNode_ptr tag = props->getChild("tag", tagCount++, true);
194 tag->setStringValue(*tagIt);
195 }
196 } else if (key == "installed") {
197 props->setBoolValue(key, it->getValue<bool>());
198 } else {
199 SG_LOG(SG_GENERAL, SG_WARN, "unknown filter term in hash:" << key);
200 }
201 }
202
203 return props;
204 }
205
206 static naRef f_root_search(pkg::Root& root, const nasal::CallContext& ctx)
207 {
208 SGPropertyNode_ptr query = queryPropsFromHash(ctx.requireArg<nasal::Hash>(0));
209 pkg::PackageList result = root.packagesMatching(query);
210 return ctx.to_nasal(result);
211 }
212
213 static naRef f_catalog_search(pkg::Catalog& cat, const nasal::CallContext& ctx)
214 {
215 SGPropertyNode_ptr query = queryPropsFromHash(ctx.requireArg<nasal::Hash>(0));
216 pkg::PackageList result = cat.packagesMatching(query);
217 return ctx.to_nasal(result);
218 }
219
220 static naRef f_catalog_packages(pkg::Catalog& cat, const nasal::CallContext& ctx)
221 {
222 // TODO: support package types
223 pkg::PackageList result = cat.packages();
224 return ctx.to_nasal(result);
225 }
226
227
228 static naRef f_catalog_installedPackages(pkg::Catalog& cat, naContext c)
229 {
230 // TODO: support package types
231 pkg::PackageList result = cat.installedPackages();
232 return nasal::to_nasal(c, result);
233 }
234
235 static naRef f_package_variants(pkg::Package& pack, naContext c)
236 {
237 nasal::Hash h(c);
238 string_list vars(pack.variants());
239 for (string_list_iterator it = vars.begin(); it != vars.end(); ++it) {
240 h.set(*it, pack.nameForVariant(*it));
241 }
242
243 return h.get_naRef();
244 }
245
246 void FGHTTPClient::postinit()
247 {
248 NasalPackageRoot::init("PackageRoot")
249 .member("path", &pkg::Root::path)
250 .member("version", &pkg::Root::catalogVersion)
251 .method("refresh", &pkg::Root::refresh)
252 .method("catalogs", &pkg::Root::catalogs)
253 .method("packageById", &pkg::Root::getPackageById)
254 .method("catalogById", &pkg::Root::getCatalogById)
255 .method("search", &f_root_search);
256
257 NasalCatalog::init("Catalog")
258 .member("installRoot", &pkg::Catalog::installRoot)
259 .member("id", &pkg::Catalog::id)
260 .member("url", &pkg::Catalog::url)
261 .member("description", &pkg::Catalog::description)
262 .method("packages", &f_catalog_packages)
263 .method("packageById", &pkg::Catalog::getPackageById)
264 .method("refresh", &pkg::Catalog::refresh)
265 .method("needingUpdate", &pkg::Catalog::packagesNeedingUpdate)
266 .member("installed", &f_catalog_installedPackages)
267 .method("search", &f_catalog_search)
268 .member("enabled", &pkg::Catalog::isEnabled);
269
270 NasalPackage::init("Package")
271 .member("id", &pkg::Package::id)
272 .member("name", &pkg::Package::name)
273 .member("description", &pkg::Package::description)
274 .member("installed", &pkg::Package::isInstalled)
275 .member("thumbnails", &pkg::Package::thumbnailUrls)
276 .member("variants", &f_package_variants)
277 .member("revision", &pkg::Package::revision)
278 .member("catalog", &pkg::Package::catalog)
279 .method("install", &pkg::Package::install)
280 .method("uninstall", &f_package_uninstall)
281 .method("existingInstall", &f_package_existingInstall)
282 .method("lprop", &pkg::Package::getLocalisedProp)
283 .member("fileSize", &pkg::Package::fileSizeBytes);
284
285 typedef pkg::Install* (pkg::Install::*InstallCallback)
286 (const pkg::Install::Callback&);
287 typedef pkg::Install* (pkg::Install::*ProgressCallback)
288 (const pkg::Install::ProgressCallback&);
289 NasalInstall::init("Install")
290 .member("revision", &pkg::Install::revsion)
291 .member("pkg", &pkg::Install::package)
292 .member("path", &pkg::Install::path)
293 .member("hasUpdate", &pkg::Install::hasUpdate)
294 .method("startUpdate", &pkg::Install::startUpdate)
295 .method("uninstall", &pkg::Install::uninstall)
296 .method("done", static_cast<InstallCallback>(&pkg::Install::done))
297 .method("fail", static_cast<InstallCallback>(&pkg::Install::fail))
298 .method("always", static_cast<InstallCallback>(&pkg::Install::always))
299 .method("progress", static_cast<ProgressCallback>(&pkg::Install::progress));
300
301 pkg::Root* packageRoot = globals->packageRoot();
302 if (packageRoot) {
303 auto nasalSys = globals->get_subsystem<FGNasalSys>();
304 nasal::Hash nasalGlobals = nasalSys->getGlobals();
305 nasal::Hash nasalPkg = nasalGlobals.createHash("pkg"); // module
306 nasalPkg.set("root", packageRoot);
307 }
308 }
309
310 void FGHTTPClient::shutdown()
311 {
312 _http.reset();
313
314 _inited = false;
315 }
316
317 void FGHTTPClient::update(double)
318 {
319 _http->update();
320 }
321
322 void FGHTTPClient::makeRequest(const simgear::HTTP::Request_ptr& req)
323 {
324 _http->makeRequest(req);
325 }
326
327
328 // Register the subsystem.
329 SGSubsystemMgr::Registrant<FGHTTPClient> registrantFGHTTPClient(
330 SGSubsystemMgr::GENERAL,
331 {{"nasal", SGSubsystemMgr::Dependency::HARD}});