1 // Expose HTTP module to Nasal
3 // Copyright (C) 2013 Thomas Geymayer
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 2 of the
8 // License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #include "NasalHTTP.hxx"
24 #include <Main/globals.hxx>
25 #include <Main/util.hxx>
26 #include <Network/HTTPClient.hxx>
28 #include <simgear/io/HTTPFileRequest.hxx>
29 #include <simgear/io/HTTPMemoryRequest.hxx>
31 #include <simgear/nasal/cppbind/from_nasal.hxx>
32 #include <simgear/nasal/cppbind/to_nasal.hxx>
33 #include <simgear/nasal/cppbind/NasalHash.hxx>
34 #include <simgear/nasal/cppbind/Ghost.hxx>
36 typedef nasal::Ghost<simgear::HTTP::Request_ptr> NasalRequest;
37 typedef nasal::Ghost<simgear::HTTP::FileRequestRef> NasalFileRequest;
38 typedef nasal::Ghost<simgear::HTTP::MemoryRequestRef> NasalMemoryRequest;
40 FGHTTPClient& requireHTTPClient(naContext c)
43 static_cast<FGHTTPClient*>(globals->get_subsystem("http"));
45 naRuntimeError(c, "Failed to get HTTP subsystem");
51 * urlretrieve(url, filename)
53 static naRef f_urlretrieve(const nasal::CallContext& ctx)
55 // Check for write access to target file
56 const std::string filename = ctx.requireArg<std::string>(1);
57 const std::string validated_path = fgValidatePath(filename, true);
59 if( validated_path.empty() )
60 naRuntimeError( ctx.c,
61 "Access denied: can not write to %s",
66 requireHTTPClient(ctx.c).client()
69 ctx.requireArg<std::string>(0), // url
70 validated_path // filename
78 static naRef f_urlload(const nasal::CallContext& ctx)
82 requireHTTPClient(ctx.c).client()
85 ctx.requireArg<std::string>(0) // url
90 naRef initNasalHTTP(naRef globals, naContext c)
92 using simgear::HTTP::Request;
93 NasalRequest::init("http.Request")
94 .member("url", &Request::url)
95 .member("method", &Request::method)
96 .member("scheme", &Request::scheme)
97 .member("path", &Request::path)
98 .member("host", &Request::host)
99 .member("port", &Request::port)
100 .member("query", &Request::query)
101 .member("status", &Request::responseCode)
102 .member("reason", &Request::responseReason)
103 .member("readyState", &Request::readyState)
104 .method("abort", static_cast<void (Request::*)()>(&Request::abort))
105 .method("done", &Request::done)
106 .method("fail", &Request::fail)
107 .method("always", &Request::always);
109 using simgear::HTTP::FileRequest;
110 NasalFileRequest::init("http.FileRequest")
111 .bases<NasalRequest>();
113 using simgear::HTTP::MemoryRequest;
114 NasalMemoryRequest::init("http.MemoryRequest")
115 .bases<NasalRequest>()
116 .member("response", &MemoryRequest::responseBody);
118 nasal::Hash globals_module(globals, c),
119 http = globals_module.createHash("http");
121 http.set("urlretrieve", f_urlretrieve);
122 http.set("urlload", f_urlload);