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
* http.save(url, filename)
53
static naRef f_http_save(const nasal::CallContext& ctx)
55
const std::string url = ctx.requireArg<std::string>(0);
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);
61
if( validated_path.empty() )
62
naRuntimeError( ctx.c,
63
"Access denied: can not write to %s",
68
requireHTTPClient(ctx.c).client()->save(url, validated_path)
75
static naRef f_http_load(const nasal::CallContext& ctx)
77
const std::string url = ctx.requireArg<std::string>(0);
78
return ctx.to_nasal( requireHTTPClient(ctx.c).client()->load(url) );
81
//------------------------------------------------------------------------------
82
naRef initNasalHTTP(naRef globals, naContext c)
84
using simgear::HTTP::Request;
85
NasalRequest::init("http.Request")
86
.member("url", &Request::url)
87
.member("method", &Request::method)
88
.member("scheme", &Request::scheme)
89
.member("path", &Request::path)
90
.member("host", &Request::host)
91
.member("port", &Request::port)
92
.member("query", &Request::query)
93
.member("status", &Request::responseCode)
94
.member("reason", &Request::responseReason)
95
.member("readyState", &Request::readyState)
96
.method("abort", static_cast<void (Request::*)()>(&Request::abort))
97
.method("done", &Request::done)
98
.method("fail", &Request::fail)
99
.method("always", &Request::always);
101
using simgear::HTTP::FileRequest;
102
NasalFileRequest::init("http.FileRequest")
103
.bases<NasalRequest>();
105
using simgear::HTTP::MemoryRequest;
106
NasalMemoryRequest::init("http.MemoryRequest")
107
.bases<NasalRequest>()
108
.member("response", &MemoryRequest::responseBody);
110
nasal::Hash globals_module(globals, c),
111
http = globals_module.createHash("http");
113
http.set("save", f_http_save);
114
http.set("load", f_http_load);