26#if defined (HAVE_CONFIG_H)
63 const std::string& who)
65 int nargin = args.
length ();
67 if ((nargin - start_idx) % 2 != 0)
68 error (
"%s: property/value arguments must occur in pairs", who.c_str ());
71 for (
int i = start_idx; i < nargin; i += 2)
73 if (! args(i).is_string ())
74 error (
"%s: property names must be strings", who.c_str ());
76 std::string
property = args(i).string_value ();
79 std::transform (property.begin (), property.end (), property.begin (),
80 [] (
unsigned char c) { return std::tolower(c); });
82 if (property ==
"get")
85 std::string err_msg = who +
": 'Get' value must be a cell array of strings";
86 param = args(i+1).xcellstr_value (err_msg.c_str ());
87 if (param.
numel () % 2 == 1)
88 error (
"%s: number of elements in 'Get' cell array must be even", who.c_str ());
90 else if (property ==
"post")
93 std::string err_msg = who +
": 'Post' value must be a cell array of strings";
94 param = args(i+1).xcellstr_value (err_msg.c_str ());
95 if (param.
numel () % 2 == 1)
96 error (
"%s: number of elements in 'Post' cell array must be even", who.c_str ());
98 else if (property ==
"timeout")
100 std::string err_msg = who +
": 'Timeout' value must be numeric";
101 double timeout = args(i+1).xdouble_value (err_msg.c_str ());
103 error (
"%s: 'Timeout' value must be positive", who.c_str ());
105 options.
Timeout =
static_cast<long> (timeout * 1000);
107 else if (property ==
"useragent")
109 std::string err_msg = who +
": 'UserAgent' value must be a string";
110 options.
UserAgent = args(i+1).xstring_value (err_msg.c_str ());
112 else if (property ==
"username")
114 std::string err_msg = who +
": 'Username' value must be a string";
115 options.
Username = args(i+1).xstring_value (err_msg.c_str ());
117 else if (property ==
"password")
119 std::string err_msg = who +
": 'Password' value must be a string";
120 options.
Password = args(i+1).xstring_value (err_msg.c_str ());
122 else if (property ==
"charset")
125 std::string err_msg = who +
": 'Charset' value must be a string";
130 error (
"%s: unknown property '%s'", who.c_str (), args(i).string_value ().c_str ());
135DEFUN (urlwrite, args, nargout,
215 int nargin = args.
length ();
221 std::string url = args(0).xstring_value (
"urlwrite: URL must be a string");
224 std::string filename = args(1).xstring_value (
"urlwrite: LOCALFILE must be a string");
226 std::string method =
"get";
235 if (nargin == 4 && args(2).is_string ()
236 && (args(2).string_value () ==
"get" || args(2).string_value () ==
"post"))
239 method = args(2).xstring_value (
"urlwrite: METHOD must be a string");
241 if (method !=
"get" && method !=
"post")
242 error (R
"(urlwrite: METHOD must be "get" or "post")");
244 param = args(3).xcellstr_value ("urlwrite: parameters (PARAM) for get and post requests must be given as a cell array of strings");
246 if (param.
numel () % 2 == 1)
247 error (
"urlwrite: number of elements in PARAM must be even");
252 parse_property_value_pairs (args, 2, options, method, param,
"urlwrite");
259 std::ofstream ofile =
260 sys::ofstream (filename.c_str (), std::ios::out | std::ios::binary);
262 if (! ofile.is_open ())
263 error (
"urlwrite: unable to open file");
265 int(*unlink_fptr)(
const std::string&) = sys::unlink;
273 error (
"support for URL transfers was disabled when Octave was built");
283 if (url_xfer.
good ())
288 if (url_xfer.
good ())
289 retval =
ovl (sys::env::make_absolute (filename),
true,
"");
294 if (nargout < 2 && ! url_xfer.
good ())
300DEFUN (urlread, args, nargout,
375 int nargin = args.
length ();
381 std::string url = args(0).xstring_value (
"urlread: URL must be a string");
383 std::string method =
"get";
392 if (nargin == 3 && args(1).is_string ()
393 && (args(1).string_value () ==
"get" || args(1).string_value () ==
"post"))
396 method = args(1).xstring_value (
"urlread: METHOD must be a string");
398 if (method !=
"get" && method !=
"post")
399 error (R
"(urlread: METHOD must be "get" or "post")");
401 param = args(2).xcellstr_value ("urlread: parameters (PARAM) for get and post requests must be given as a cell array of strings");
403 if (param.
numel () % 2 == 1)
404 error (
"urlread: number of elements in PARAM must be even");
409 parse_property_value_pairs (args, 1, options, method, param,
"urlread");
412 std::ostringstream buf;
417 error (
"support for URL transfers was disabled when Octave was built");
425 if (nargout < 2 && ! url_xfer.
good ())
430 retval(0) = buf.str ();
432 retval(1) = url_xfer.
good ();
439DEFUN (__restful_service__, args, nargout,
445 int nargin = args.
length ();
450 std::string url = args(0).xstring_value (
"__restful_service__: URL must be a string");
452 std::ostringstream content;
457 error (
"support for URL transfers was disabled when Octave was built");
461 std::string data, method;
466 = args (nargin - 1).classdef_object_value () -> get_object ();
474 for (
int i = 0; i < keys.
numel (); i++)
476 if (keys(i) ==
"Timeout")
478 float timeout =
object.get (keys(i)).float_value ();
479 options.
Timeout =
static_cast<long> (timeout * 1000);
482 if (keys(i) ==
"HeaderFields")
484 options.
HeaderFields =
object.get (keys(i)).cellstr_value ();
490 if (keys(i) !=
"Timeout" && keys(i) !=
"HeaderFields"
491 && keys(i) !=
"delete" && keys(i) !=
"display")
493 std::string value =
object.get (keys(i)).string_value ();
495 if (keys(i) ==
"UserAgent")
498 if (keys(i) ==
"Username")
501 if (keys(i) ==
"Password")
504 if (keys(i) ==
"ContentReader")
508 if (keys(i) ==
"RequestMethod")
511 if (keys(i) ==
"ArrayFormat")
514 if (keys(i) ==
"CertificateFilename")
523 if (nargout < 2 && ! url_xfer.
good ())
524 error (
"__restful_service__: %s", url_xfer.
lasterror ().c_str ());
526 return ovl (content.str ());
529OCTAVE_END_NAMESPACE(octave)
N Dimensional Array with copy-on-write semantics.
octave_idx_type numel() const
Number of elements in the array.
octave_idx_type length() const
octave_idx_type numel() const
void set_weboptions(const struct weboptions ¶m)
std::string lasterror() const
void http_action(const Array< std::string > ¶m, const std::string &action)
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
#define DEFUN(name, args_name, nargout_name, doc)
Macro to define a builtin function.
void error(const char *fmt,...)
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.
std::string ContentReader
Array< std::string > HeaderFields
std::string CertificateFilename
std::string CharacterEncoding