]> git.mxchange.org Git - flightgear.git/blob - src/Scripting/NasalHTTP.cxx
VS2015 compatability fixes.
[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 = globals->get_subsystem<FGHTTPClient>();
43   if( !http )
44     naRuntimeError(c, "Failed to get HTTP subsystem");
45
46   return *http;
47 }
48
49 /**
50  * http.save(url, filename)
51  */
52 static naRef f_http_save(const nasal::CallContext& ctx)
53 {
54   const std::string url = ctx.requireArg<std::string>(0);
55
56   // Check for write access to target file
57   const std::string filename = ctx.requireArg<std::string>(1);
58   const std::string validated_path = fgValidatePath(filename, true);
59
60   if( validated_path.empty() )
61     naRuntimeError( ctx.c,
62                     "Access denied: can not write to %s",
63                     filename.c_str() );
64
65   return ctx.to_nasal
66   (
67     requireHTTPClient(ctx.c).client()->save(url, validated_path)
68   );
69 }
70
71 /**
72  * http.load(url)
73  */
74 static naRef f_http_load(const nasal::CallContext& ctx)
75 {
76   const std::string url = ctx.requireArg<std::string>(0);
77   return ctx.to_nasal( requireHTTPClient(ctx.c).client()->load(url) );
78 }
79
80 static naRef f_request_abort(simgear::HTTP::Request&, const nasal::CallContext& ctx)
81 {
82     // we need a request_ptr for cancel, not a reference. So extract
83     // the me object from the context directly.
84     simgear::HTTP::Request_ptr req = ctx.from_nasal<simgear::HTTP::Request_ptr>(ctx.me);
85     requireHTTPClient(ctx.c).client()->cancelRequest(req);
86     return naNil();
87 }
88
89 //------------------------------------------------------------------------------
90 naRef initNasalHTTP(naRef globals, naContext c)
91 {
92   using simgear::HTTP::Request;
93   typedef Request* (Request::*HTTPCallback)(const Request::Callback&);
94   NasalRequest::init("http.Request")
95     .member("url", &Request::url)
96     .member("method", &Request::method)
97     .member("scheme", &Request::scheme)
98     .member("path", &Request::path)
99     .member("host", &Request::host)
100     .member("port", &Request::port)
101     .member("query", &Request::query)
102     .member("status", &Request::responseCode)
103     .member("reason", &Request::responseReason)
104     .member("readyState", &Request::readyState)
105     .method("abort", f_request_abort)
106     .method("done", static_cast<HTTPCallback>(&Request::done))
107     .method("fail", static_cast<HTTPCallback>(&Request::fail))
108     .method("always", static_cast<HTTPCallback>(&Request::always));
109
110   using simgear::HTTP::FileRequest;
111   NasalFileRequest::init("http.FileRequest")
112     .bases<NasalRequest>();
113
114   using simgear::HTTP::MemoryRequest;
115   NasalMemoryRequest::init("http.MemoryRequest")
116     .bases<NasalRequest>()
117     .member("response", &MemoryRequest::responseBody);
118     
119   nasal::Hash globals_module(globals, c),
120               http = globals_module.createHash("http");
121
122   http.set("save", f_http_save);
123   http.set("load", f_http_load);
124
125   return naNil();
126 }