]> git.mxchange.org Git - flightgear.git/blob - src/Scripting/NasalHTTP.cxx
Remove debug console output in FGApproachController
[flightgear.git] / src / Scripting / NasalHTTP.cxx
1 // Expose HTTP module to Nasal
2 //
3 // Copyright (C) 2013 Thomas Geymayer
4 //
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.
9 //
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.
14 //
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.
18
19 #ifdef HAVE_CONFIG_H
20 #  include "config.h"
21 #endif
22
23 #include "NasalHTTP.hxx"
24 #include <Main/globals.hxx>
25 #include <Main/util.hxx>
26 #include <Network/HTTPClient.hxx>
27
28 #include <simgear/io/HTTPFileRequest.hxx>
29 #include <simgear/io/HTTPMemoryRequest.hxx>
30
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>
35
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;
39
40 FGHTTPClient& requireHTTPClient(naContext c)
41 {
42   FGHTTPClient* http =
43     static_cast<FGHTTPClient*>(globals->get_subsystem("http"));
44   if( !http )
45     naRuntimeError(c, "Failed to get HTTP subsystem");
46
47   return *http;
48 }
49
50 /**
51  * http.save(url, filename)
52  */
53 static naRef f_http_save(const nasal::CallContext& ctx)
54 {
55   const std::string url = ctx.requireArg<std::string>(0);
56
57   // Check for write access to target file
58   const std::string filename = ctx.requireArg<std::string>(1);
59   const std::string validated_path = fgValidatePath(filename, true);
60
61   if( validated_path.empty() )
62     naRuntimeError( ctx.c,
63                     "Access denied: can not write to %s",
64                     filename.c_str() );
65
66   return ctx.to_nasal
67   (
68     requireHTTPClient(ctx.c).client()->save(url, validated_path)
69   );
70 }
71
72 /**
73  * http.load(url)
74  */
75 static naRef f_http_load(const nasal::CallContext& ctx)
76 {
77   const std::string url = ctx.requireArg<std::string>(0);
78   return ctx.to_nasal( requireHTTPClient(ctx.c).client()->load(url) );
79 }
80
81 //------------------------------------------------------------------------------
82 naRef initNasalHTTP(naRef globals, naContext c)
83 {
84   using simgear::HTTP::Request;
85   typedef Request* (Request::*HTTPCallback)(const Request::Callback&);
86   NasalRequest::init("http.Request")
87     .member("url", &Request::url)
88     .member("method", &Request::method)
89     .member("scheme", &Request::scheme)
90     .member("path", &Request::path)
91     .member("host", &Request::host)
92     .member("port", &Request::port)
93     .member("query", &Request::query)
94     .member("status", &Request::responseCode)
95     .member("reason", &Request::responseReason)
96     .member("readyState", &Request::readyState)
97     .method("abort", static_cast<void (Request::*)()>(&Request::abort))
98     .method("done", static_cast<HTTPCallback>(&Request::done))
99     .method("fail", static_cast<HTTPCallback>(&Request::fail))
100     .method("always", static_cast<HTTPCallback>(&Request::always));
101
102   using simgear::HTTP::FileRequest;
103   NasalFileRequest::init("http.FileRequest")
104     .bases<NasalRequest>();
105
106   using simgear::HTTP::MemoryRequest;
107   NasalMemoryRequest::init("http.MemoryRequest")
108     .bases<NasalRequest>()
109     .member("response", &MemoryRequest::responseBody);
110     
111   nasal::Hash globals_module(globals, c),
112               http = globals_module.createHash("http");
113
114   http.set("save", f_http_save);
115   http.set("load", f_http_load);
116
117   return naNil();
118 }