Expose HTTP module to Nasal.
This commit is contained in:
parent
8a582e6376
commit
cf270bde22
6 changed files with 185 additions and 26 deletions
|
@ -91,5 +91,12 @@ const char *fgValidatePath (const char *str, bool write)
|
||||||
return result[0] ? result : 0;
|
return result[0] ? result : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//------------------------------------------------------------------------------
|
||||||
|
std::string fgValidatePath(const std::string& path, bool write)
|
||||||
|
{
|
||||||
|
const char* validate_path = fgValidatePath(path.c_str(), write);
|
||||||
|
return std::string(validate_path ? validate_path : "");
|
||||||
|
}
|
||||||
|
|
||||||
// end of util.cxx
|
// end of util.cxx
|
||||||
|
|
||||||
|
|
|
@ -17,14 +17,10 @@
|
||||||
//
|
//
|
||||||
// $Id$
|
// $Id$
|
||||||
|
|
||||||
|
|
||||||
#ifndef __UTIL_HXX
|
#ifndef __UTIL_HXX
|
||||||
#define __UTIL_HXX 1
|
#define __UTIL_HXX 1
|
||||||
|
|
||||||
#ifndef __cplusplus
|
#include <string>
|
||||||
# error This library requires C++
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Move a value towards a target.
|
* Move a value towards a target.
|
||||||
|
@ -37,7 +33,7 @@
|
||||||
* (elapsed time/smoothing time)
|
* (elapsed time/smoothing time)
|
||||||
* @return The new value.
|
* @return The new value.
|
||||||
*/
|
*/
|
||||||
extern double fgGetLowPass (double current, double target, double timeratio);
|
double fgGetLowPass (double current, double target, double timeratio);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Validation listener interface for io.nas, used by fgcommands.
|
* Validation listener interface for io.nas, used by fgcommands.
|
||||||
|
@ -45,6 +41,7 @@ extern double fgGetLowPass (double current, double target, double timeratio);
|
||||||
* @param write True for write operations and false for read operations.
|
* @param write True for write operations and false for read operations.
|
||||||
* @return The validated path on success or 0 if access denied.
|
* @return The validated path on success or 0 if access denied.
|
||||||
*/
|
*/
|
||||||
extern const char *fgValidatePath (const char *path, bool write);
|
const char *fgValidatePath (const char *path, bool write);
|
||||||
|
std::string fgValidatePath(const std::string& path, bool write);
|
||||||
|
|
||||||
#endif // __UTIL_HXX
|
#endif // __UTIL_HXX
|
||||||
|
|
|
@ -8,9 +8,10 @@ set(SOURCES
|
||||||
NasalCanvas.cxx
|
NasalCanvas.cxx
|
||||||
NasalClipboard.cxx
|
NasalClipboard.cxx
|
||||||
NasalCondition.cxx
|
NasalCondition.cxx
|
||||||
|
NasalHTTP.cxx
|
||||||
NasalString.cxx
|
NasalString.cxx
|
||||||
NasalModelData.cxx
|
NasalModelData.cxx
|
||||||
)
|
)
|
||||||
|
|
||||||
set(HEADERS
|
set(HEADERS
|
||||||
NasalSys.hxx
|
NasalSys.hxx
|
||||||
|
@ -19,9 +20,10 @@ set(HEADERS
|
||||||
NasalCanvas.hxx
|
NasalCanvas.hxx
|
||||||
NasalClipboard.hxx
|
NasalClipboard.hxx
|
||||||
NasalCondition.hxx
|
NasalCondition.hxx
|
||||||
|
NasalHTTP.hxx
|
||||||
NasalString.hxx
|
NasalString.hxx
|
||||||
NasalModelData.hxx
|
NasalModelData.hxx
|
||||||
)
|
)
|
||||||
|
|
||||||
if(WIN32)
|
if(WIN32)
|
||||||
list(APPEND SOURCES ClipboardWindows.cxx)
|
list(APPEND SOURCES ClipboardWindows.cxx)
|
||||||
|
|
125
src/Scripting/NasalHTTP.cxx
Normal file
125
src/Scripting/NasalHTTP.cxx
Normal file
|
@ -0,0 +1,125 @@
|
||||||
|
// Expose HTTP module to Nasal
|
||||||
|
//
|
||||||
|
// Copyright (C) 2013 Thomas Geymayer
|
||||||
|
//
|
||||||
|
// This program is free software; you can redistribute it and/or
|
||||||
|
// modify it under the terms of the GNU General Public License as
|
||||||
|
// published by the Free Software Foundation; either version 2 of the
|
||||||
|
// License, or (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful, but
|
||||||
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
// General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with this program; if not, write to the Free Software
|
||||||
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||||
|
|
||||||
|
#ifdef HAVE_CONFIG_H
|
||||||
|
# include "config.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include "NasalHTTP.hxx"
|
||||||
|
#include <Main/globals.hxx>
|
||||||
|
#include <Main/util.hxx>
|
||||||
|
#include <Network/HTTPClient.hxx>
|
||||||
|
|
||||||
|
#include <simgear/io/HTTPFileRequest.hxx>
|
||||||
|
#include <simgear/io/HTTPMemoryRequest.hxx>
|
||||||
|
|
||||||
|
#include <simgear/nasal/cppbind/from_nasal.hxx>
|
||||||
|
#include <simgear/nasal/cppbind/to_nasal.hxx>
|
||||||
|
#include <simgear/nasal/cppbind/NasalHash.hxx>
|
||||||
|
#include <simgear/nasal/cppbind/Ghost.hxx>
|
||||||
|
|
||||||
|
typedef nasal::Ghost<simgear::HTTP::Request_ptr> NasalRequest;
|
||||||
|
typedef nasal::Ghost<simgear::HTTP::FileRequestRef> NasalFileRequest;
|
||||||
|
typedef nasal::Ghost<simgear::HTTP::MemoryRequestRef> NasalMemoryRequest;
|
||||||
|
|
||||||
|
FGHTTPClient& requireHTTPClient(naContext c)
|
||||||
|
{
|
||||||
|
FGHTTPClient* http =
|
||||||
|
static_cast<FGHTTPClient*>(globals->get_subsystem("http"));
|
||||||
|
if( !http )
|
||||||
|
naRuntimeError(c, "Failed to get HTTP subsystem");
|
||||||
|
|
||||||
|
return *http;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* urlretrieve(url, filename)
|
||||||
|
*/
|
||||||
|
static naRef f_urlretrieve(const nasal::CallContext& ctx)
|
||||||
|
{
|
||||||
|
// Check for write access to target file
|
||||||
|
const std::string filename = ctx.requireArg<std::string>(1);
|
||||||
|
const std::string validated_path = fgValidatePath(filename, true);
|
||||||
|
|
||||||
|
if( validated_path.empty() )
|
||||||
|
naRuntimeError( ctx.c,
|
||||||
|
"Access denied: can not write to %s",
|
||||||
|
filename.c_str() );
|
||||||
|
|
||||||
|
return ctx.to_nasal
|
||||||
|
(
|
||||||
|
requireHTTPClient(ctx.c).client()
|
||||||
|
->urlretrieve
|
||||||
|
(
|
||||||
|
ctx.requireArg<std::string>(0), // url
|
||||||
|
validated_path // filename
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* urlload(url)
|
||||||
|
*/
|
||||||
|
static naRef f_urlload(const nasal::CallContext& ctx)
|
||||||
|
{
|
||||||
|
return ctx.to_nasal
|
||||||
|
(
|
||||||
|
requireHTTPClient(ctx.c).client()
|
||||||
|
->urlload
|
||||||
|
(
|
||||||
|
ctx.requireArg<std::string>(0) // url
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
naRef initNasalHTTP(naRef globals, naContext c)
|
||||||
|
{
|
||||||
|
using simgear::HTTP::Request;
|
||||||
|
NasalRequest::init("http.Request")
|
||||||
|
.member("url", &Request::url)
|
||||||
|
.member("method", &Request::method)
|
||||||
|
.member("scheme", &Request::scheme)
|
||||||
|
.member("path", &Request::path)
|
||||||
|
.member("host", &Request::host)
|
||||||
|
.member("port", &Request::port)
|
||||||
|
.member("query", &Request::query)
|
||||||
|
.member("status", &Request::responseCode)
|
||||||
|
.member("reason", &Request::responseReason)
|
||||||
|
.member("readyState", &Request::readyState)
|
||||||
|
.method("abort", static_cast<void (Request::*)()>(&Request::abort))
|
||||||
|
.method("done", &Request::done)
|
||||||
|
.method("fail", &Request::fail)
|
||||||
|
.method("always", &Request::always);
|
||||||
|
|
||||||
|
using simgear::HTTP::FileRequest;
|
||||||
|
NasalFileRequest::init("http.FileRequest")
|
||||||
|
.bases<NasalRequest>();
|
||||||
|
|
||||||
|
using simgear::HTTP::MemoryRequest;
|
||||||
|
NasalMemoryRequest::init("http.MemoryRequest")
|
||||||
|
.bases<NasalRequest>()
|
||||||
|
.member("response", &MemoryRequest::responseBody);
|
||||||
|
|
||||||
|
nasal::Hash globals_module(globals, c),
|
||||||
|
http = globals_module.createHash("http");
|
||||||
|
|
||||||
|
http.set("urlretrieve", f_urlretrieve);
|
||||||
|
http.set("urlload", f_urlload);
|
||||||
|
|
||||||
|
return naNil();
|
||||||
|
}
|
26
src/Scripting/NasalHTTP.hxx
Normal file
26
src/Scripting/NasalHTTP.hxx
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
//@file Expose HTTP module to Nasal
|
||||||
|
//
|
||||||
|
// Copyright (C) 2013 Thomas Geymayer
|
||||||
|
//
|
||||||
|
// This program is free software; you can redistribute it and/or
|
||||||
|
// modify it under the terms of the GNU General Public License as
|
||||||
|
// published by the Free Software Foundation; either version 2 of the
|
||||||
|
// License, or (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful, but
|
||||||
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
// General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with this program; if not, write to the Free Software
|
||||||
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||||
|
|
||||||
|
#ifndef SCRIPTING_NASAL_HTTP_HXX
|
||||||
|
#define SCRIPTING_NASAL_HTTP_HXX
|
||||||
|
|
||||||
|
#include <simgear/nasal/nasal.h>
|
||||||
|
|
||||||
|
naRef initNasalHTTP(naRef globals, naContext c);
|
||||||
|
|
||||||
|
#endif // of SCRIPTING_NASAL_HTTP_HXX
|
|
@ -40,6 +40,7 @@
|
||||||
#include "NasalCanvas.hxx"
|
#include "NasalCanvas.hxx"
|
||||||
#include "NasalClipboard.hxx"
|
#include "NasalClipboard.hxx"
|
||||||
#include "NasalCondition.hxx"
|
#include "NasalCondition.hxx"
|
||||||
|
#include "NasalHTTP.hxx"
|
||||||
#include "NasalString.hxx"
|
#include "NasalString.hxx"
|
||||||
|
|
||||||
#include <Main/globals.hxx>
|
#include <Main/globals.hxx>
|
||||||
|
@ -778,6 +779,7 @@ void FGNasalSys::init()
|
||||||
NasalClipboard::init(this);
|
NasalClipboard::init(this);
|
||||||
initNasalCanvas(_globals, _context);
|
initNasalCanvas(_globals, _context);
|
||||||
initNasalCondition(_globals, _context);
|
initNasalCondition(_globals, _context);
|
||||||
|
initNasalHTTP(_globals, _context);
|
||||||
|
|
||||||
NasalTimerObj::init("Timer")
|
NasalTimerObj::init("Timer")
|
||||||
.method("start", &TimerObj::start)
|
.method("start", &TimerObj::start)
|
||||||
|
|
Loading…
Add table
Reference in a new issue