~ubuntu-branches/debian/sid/flightgear/sid

« back to all changes in this revision

Viewing changes to src/Scripting/NasalHTTP.cxx

  • Committer: Package Import Robot
  • Author(s): Markus Wanner, Markus Wanner, Rebecca Palmer
  • Date: 2014-01-21 22:31:02 UTC
  • mfrom: (1.3.1) (15.1.2 experimental)
  • Revision ID: package-import@ubuntu.com-20140121223102-cjw7g9le25acd119
Tags: 3.0.0~git20140204+c99ea4-1
[ Markus Wanner ]
* Upload to unstable.
* Adjust B-D to allow building on kfreebsd-*. Closes: #724686.
* Add a lintian-overrides on autotools; we use cmake.
* Upstream corrected the fgfs manpage. Closes: #556362.
* Drop unnecessary man page for gl-info. Closes: #698308.
* Drop README.Linux: it's outdated to the point of uselessness.
  Closes: #574173.
* Add an upper limit of libsimgear-dev versions that flightgear can be
  built with. Closes: #738436.
* Drop the libsvn-dev dependency, neither flightgear nor simgear depend
  on libsvn, anymore. Closes: #682947.
* List icons in debian/install rather than copying around from rules.
* Update menu entry for flightgear, add one for fgcom; add .xpm icons.
  Closes: #713924.
* flightgear.desktop: add German translation
* Bump Standards-Version to 3.9.5; no changes needed.

[ Rebecca Palmer ]
* New upstream release.
* Install the icons (based on code by Saikrishna Arcot).  (Not a
  complete fix for LP908153 as it only sets the menu/Dash icon, not the
  running window's icon, but better than nothing).
* Disable screensaver while running. Closes: LP#793599. Add required
  libdbus-1-dev dependency.
* Remove outdated README.Debian.
* Terrasync now works after just ticking the box. Closes: #252899.
* Always set Terrasync directory.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
  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);
 
100
 
 
101
  using simgear::HTTP::FileRequest;
 
102
  NasalFileRequest::init("http.FileRequest")
 
103
    .bases<NasalRequest>();
 
104
 
 
105
  using simgear::HTTP::MemoryRequest;
 
106
  NasalMemoryRequest::init("http.MemoryRequest")
 
107
    .bases<NasalRequest>()
 
108
    .member("response", &MemoryRequest::responseBody);
 
109
    
 
110
  nasal::Hash globals_module(globals, c),
 
111
              http = globals_module.createHash("http");
 
112
 
 
113
  http.set("save", f_http_save);
 
114
  http.set("load", f_http_load);
 
115
 
 
116
  return naNil();
 
117
}