6043
|
1 // urlwrite and urlread, a curl front-end for octave |
|
2 /* |
|
3 |
|
4 Copyright (C) 2006 Alexander Barth |
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
21 02110-1301, USA. |
|
22 |
|
23 */ |
|
24 |
|
25 // Author: Alexander Barth <abarth@marine.usf.edu> |
|
26 // Adapted-By: jwe |
|
27 |
|
28 #ifdef HAVE_CONFIG_H |
|
29 #include <config.h> |
|
30 #endif |
|
31 |
|
32 #include <string> |
|
33 #include <fstream> |
|
34 #include <iomanip> |
|
35 |
|
36 #include "oct-env.h" |
|
37 |
|
38 #include "defun-dld.h" |
|
39 #include "error.h" |
|
40 #include "oct-obj.h" |
|
41 #include "ov-cell.h" |
|
42 #include "pager.h" |
|
43 |
|
44 #if defined (HAVE_CURL) |
|
45 |
|
46 #include <curl/curl.h> |
|
47 #include <curl/types.h> |
|
48 #include <curl/easy.h> |
|
49 |
|
50 // Write callback function for curl. |
|
51 |
6986
|
52 static int |
6043
|
53 write_data (void *buffer, size_t size, size_t nmemb, void *streamp) |
|
54 { |
|
55 // *stream is actually an ostream object. |
|
56 std::ostream& stream = *(static_cast<std::ostream*> (streamp)); |
|
57 stream.write (static_cast<const char*> (buffer), size*nmemb); |
|
58 return (stream.fail () ? 0 : size * nmemb); |
|
59 } |
|
60 |
|
61 // Form the query string based on param. |
|
62 |
6986
|
63 static std::string |
6043
|
64 form_query_string (CURL *curl, const Cell& param) |
|
65 { |
|
66 std::ostringstream query; |
|
67 |
|
68 for (int i = 0; i < param.numel (); i += 2) |
|
69 { |
|
70 std::string name = param(i).string_value (); |
|
71 std::string text = param(i+1).string_value (); |
|
72 |
|
73 // Encode strings. |
|
74 char *enc_name = curl_easy_escape (curl, name.c_str (), name.length ()); |
|
75 char *enc_text = curl_easy_escape (curl, text.c_str (), text.length ()); |
|
76 |
|
77 query << enc_name << "=" << enc_text; |
|
78 |
|
79 curl_free (enc_name); |
|
80 curl_free (enc_text); |
|
81 |
|
82 if (i < param.numel()-1) |
|
83 query << "&"; |
|
84 } |
|
85 |
|
86 query.flush (); |
|
87 |
|
88 return query.str (); |
|
89 } |
|
90 |
|
91 // curl front-end |
|
92 |
6992
|
93 static void |
|
94 urlget_cleanup (CURL *curl) |
|
95 { |
|
96 curl_easy_cleanup (curl); |
|
97 curl_global_cleanup (); |
|
98 } |
|
99 |
6986
|
100 static CURLcode |
6043
|
101 urlget (const std::string& url, const std::string& method, |
|
102 const Cell& param, std::ostream& stream) |
|
103 { |
|
104 CURL *curl; |
|
105 |
|
106 curl_global_init(CURL_GLOBAL_DEFAULT); |
|
107 |
|
108 curl = curl_easy_init(); |
|
109 |
|
110 if (! curl) |
|
111 return CURLE_FAILED_INIT; |
|
112 |
|
113 // handle paramters of GET or POST request |
|
114 |
|
115 std::string query_string = form_query_string (curl,param); |
|
116 //octave_stdout << "query_string " << query_string << std::endl; |
|
117 |
|
118 if (method == "get") |
|
119 { |
|
120 query_string = url + "?" + query_string; |
|
121 curl_easy_setopt (curl, CURLOPT_URL, query_string.c_str ()); |
|
122 } |
|
123 else if (method == "post") |
|
124 { |
|
125 curl_easy_setopt (curl, CURLOPT_URL, url.c_str ()); |
|
126 curl_easy_setopt (curl, CURLOPT_POSTFIELDS, query_string.c_str ()); |
|
127 } |
|
128 else |
6986
|
129 curl_easy_setopt (curl, CURLOPT_URL,url.c_str()); |
6043
|
130 |
|
131 // Define our callback to get called when there's data to be written. |
|
132 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, write_data); |
|
133 |
|
134 // Set a pointer to our struct to pass to the callback. |
|
135 curl_easy_setopt (curl, CURLOPT_WRITEDATA, static_cast<void*> (&stream)); |
|
136 |
6390
|
137 // Follow redirects. |
|
138 curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 1); |
|
139 |
6992
|
140 curl_easy_setopt (curl, CURLOPT_NOPROGRESS, true); |
6043
|
141 curl_easy_setopt (curl, CURLOPT_PROGRESSDATA, url.c_str ()); |
|
142 curl_easy_setopt (curl, CURLOPT_FAILONERROR, true); |
|
143 |
|
144 // Switch on full protocol/debug output |
|
145 // curl_easy_setopt(curl, CURLOPT_VERBOSE, true); |
|
146 |
6992
|
147 CURLcode res = CURLE_OK; |
|
148 |
|
149 // To understand the following, see the definitions of these macros |
|
150 // in libcruft/misc/quit.h. The idea is that we call sigsetjmp here |
|
151 // then the signal handler calls siglongjmp to get back here |
|
152 // immediately. Then we perform some cleanup and throw an interrupt |
|
153 // exception which will get us back to the top level, cleaning up |
|
154 // any local C++ objects on the stack as we go. |
|
155 |
|
156 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_1; |
|
157 |
|
158 // We were interrupted (this code is inside a block that is only |
|
159 // called when siglongjmp is called from a signal handler). |
6043
|
160 |
6992
|
161 // Is there a better error code to use? Maybe it doesn't matter |
|
162 // because we are about to throw an execption. |
|
163 |
|
164 res = CURLE_ABORTED_BY_CALLBACK; |
|
165 urlget_cleanup (curl); |
|
166 octave_throw_interrupt_exception (); |
|
167 |
|
168 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_2; |
6043
|
169 |
6992
|
170 res = curl_easy_perform (curl); |
|
171 |
|
172 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
173 |
|
174 // If we are not interuppted, we will end up here, so we still need |
|
175 // to clean up. |
|
176 |
|
177 urlget_cleanup (curl); |
6043
|
178 |
|
179 return res; |
|
180 } |
|
181 |
|
182 #endif |
|
183 |
|
184 DEFUN_DLD (urlwrite, args, nargout, |
|
185 "-*- texinfo -*-\n\ |
|
186 @deftypefn {Loadable Function} {} urlwrite (@var{URL}, @var{localfile})\n\ |
|
187 @deftypefnx {Loadable Function} {@var{f} =} urlwrite (@var{url}, @var{localfile})\n\ |
|
188 @deftypefnx {Loadable Function} {[@var{f}, @var{success}] =} urlwrite (@var{url}, @var{localfile})\n\ |
|
189 @deftypefnx {Loadable Function} {[@var{f}, @var{success}, @var{message}] =} urlwrite (@var{url}, @var{localfile})\n\ |
|
190 Download a remote file specified by its @var{URL} and save it as\n\ |
|
191 @var{localfile}. For example,\n\ |
|
192 \n\ |
|
193 @example\n\ |
6588
|
194 urlwrite (\"ftp://ftp.octave.org/pub/octave/README\", \"README.txt\");\n\ |
6043
|
195 @end example\n\ |
|
196 \n\ |
|
197 The full path of the downloaded file is returned in @var{f}. The\n\ |
|
198 variable @var{success} is 1 if the download was successful,\n\ |
|
199 otherwise it is 0 in which case @var{message} contains an error\n\ |
|
200 message. If no output argument is specified and if an error occurs,\n\ |
6588
|
201 then the error is signaled through Octave's error handling mechanism.\n\ |
6043
|
202 \n\ |
|
203 This function uses libcurl. Curl supports, among others, the HTTP,\n\ |
|
204 FTP and FILE protocols. Username and password may be specified in\n\ |
|
205 the URL, for example:\n\ |
|
206 \n\ |
|
207 @example\n\ |
6588
|
208 urlwrite (\"http://username:password@@example.com/file.txt\",\n\ |
|
209 \"file.txt\");\n\ |
6043
|
210 @end example\n\ |
|
211 \n\ |
|
212 GET and POST requests can be specified by @var{method} and @var{param}.\n\ |
6589
|
213 The parameter @var{method} is either @samp{get} or @samp{post}\n\ |
6588
|
214 and @var{param} is a cell array of parameter and value pairs.\n\ |
|
215 For example:\n\ |
6043
|
216 \n\ |
|
217 @example\n\ |
6588
|
218 urlwrite (\"http://www.google.com/search\", \"search.html\",\n\ |
|
219 \"get\", @{\"query\", \"octave\"@});\n\ |
6043
|
220 @end example\n\ |
|
221 @seealso{urlread}\n\ |
|
222 @end deftypefn") |
|
223 { |
|
224 octave_value_list retval; |
|
225 |
|
226 #if defined (HAVE_CURL) |
|
227 |
|
228 int nargin = args.length (); |
|
229 |
|
230 // verify arguments |
|
231 if (nargin != 2 && nargin != 4) |
|
232 { |
|
233 print_usage (); |
|
234 return retval; |
|
235 } |
|
236 |
|
237 std::string url = args(0).string_value(); |
|
238 |
|
239 if (error_state) |
|
240 { |
|
241 error ("urlwrite: url must be a character string"); |
|
242 return retval; |
|
243 } |
|
244 |
|
245 // name to store the file if download is succesful |
|
246 std::string filename = args(1).string_value(); |
|
247 |
|
248 if (error_state) |
|
249 { |
|
250 error ("urlwrite: localfile must be a character string"); |
|
251 return retval; |
|
252 } |
|
253 |
|
254 std::string method; |
|
255 Cell param; // empty cell array |
|
256 |
|
257 if (nargin == 4) |
|
258 { |
|
259 method = args(2).string_value(); |
|
260 |
|
261 if (error_state) |
|
262 { |
|
263 error ("urlwrite: method can only be \"get\" or \"post\""); |
|
264 return retval; |
|
265 } |
|
266 |
|
267 if (method != "get" && method != "post") |
|
268 { |
|
269 error ("urlwrite: method can only be \"get\" or \"post\""); |
|
270 return retval; |
|
271 } |
|
272 |
|
273 param = args(3).cell_value(); |
|
274 |
|
275 if (error_state) |
|
276 { |
|
277 error ("urlwrite: parameters for get and post requests must be given as a cell"); |
|
278 return retval; |
|
279 } |
|
280 |
|
281 |
|
282 if (param.numel () % 2 == 1 ) |
|
283 { |
|
284 error ("urlwrite: number of elements in param must be even"); |
|
285 return retval; |
|
286 } |
|
287 } |
|
288 |
6986
|
289 std::ofstream ofile (filename.c_str(), std::ios::out | std::ios::binary); |
6043
|
290 |
6986
|
291 if (! ofile.is_open ()) |
6043
|
292 { |
|
293 error ("urlwrite: unable to open file"); |
|
294 return retval; |
|
295 } |
|
296 |
6986
|
297 CURLcode res = urlget (url, method, param, ofile); |
6043
|
298 |
6986
|
299 ofile.close (); |
6043
|
300 |
|
301 if (nargout > 0) |
|
302 { |
|
303 retval(0) = octave_env::make_absolute (filename, octave_env::getcwd ()); |
|
304 retval(1) = res == CURLE_OK; |
|
305 retval(2) = std::string (res == CURLE_OK ? "" : curl_easy_strerror (res)); |
|
306 } |
|
307 |
6484
|
308 if (nargout < 2 && res != CURLE_OK) |
6043
|
309 error ("urlwrite: curl: %s", curl_easy_strerror (res)); |
|
310 |
|
311 #else |
6981
|
312 error ("urlwrite: not available in this version of Octave"); |
6043
|
313 #endif |
|
314 |
|
315 return retval; |
|
316 } |
|
317 |
|
318 DEFUN_DLD (urlread, args, nargout, |
|
319 "-*- texinfo -*-\n\ |
6549
|
320 @deftypefn {Loadable Function} {@var{s} =} urlread (@var{url})\n\ |
6043
|
321 @deftypefnx {Loadable Function} {[@var{s}, @var{success}] =} urlread (@var{url})\n\ |
6549
|
322 @deftypefnx {Loadable Function} {[@var{s}, @var{success}, @var{message}] =} urlread (@var{url})\n\ |
6547
|
323 @deftypefnx {Loadable Function} {[@dots{}] =} urlread (@var{url}, @var{method}, @var{param})\n\ |
6043
|
324 Download a remote file specified by its @var{URL} and return its content\n\ |
|
325 in string @var{s}. For example,\n\ |
|
326 \n\ |
|
327 @example\n\ |
6588
|
328 s = urlread (\"ftp://ftp.octave.org/pub/octave/README\");\n\ |
6043
|
329 @end example\n\ |
|
330 \n\ |
|
331 The variable @var{success} is 1 if the download was successful,\n\ |
|
332 otherwise it is 0 in which case @var{message} contains an error\n\ |
|
333 message. If no output argument is specified and if an error occurs,\n\ |
6588
|
334 then the error is signaled through Octave's error handling mechanism.\n\ |
6043
|
335 \n\ |
|
336 This function uses libcurl. Curl supports, among others, the HTTP,\n\ |
|
337 FTP and FILE protocols. Username and password may be specified in the\n\ |
|
338 URL. For example,\n\ |
|
339 \n\ |
|
340 @example\n\ |
6588
|
341 s = urlread (\"http://username:password@@example.com/file.txt\");\n\ |
6043
|
342 @end example\n\ |
|
343 \n\ |
|
344 GET and POST requests can be specified by @var{method} and @var{param}.\n\ |
6588
|
345 The parameter @var{method} is either @samp{get} or @samp{post}\n\ |
|
346 and @var{param} is a cell array of parameter and value pairs.\n\ |
6650
|
347 For example,\n\ |
6043
|
348 \n\ |
|
349 @example\n\ |
6588
|
350 s = urlread (\"http://www.google.com/search\", \"get\",\n\ |
|
351 @{\"query\", \"octave\"@});\n\ |
6043
|
352 @end example\n\ |
|
353 @seealso{urlwrite}\n\ |
|
354 @end deftypefn") |
|
355 { |
6588
|
356 // Octave's return value |
6043
|
357 octave_value_list retval; |
|
358 |
|
359 #if defined (HAVE_CURL) |
|
360 |
|
361 int nargin = args.length (); |
|
362 |
|
363 // verify arguments |
|
364 if (nargin != 1 && nargin != 3) |
|
365 { |
|
366 print_usage (); |
|
367 return retval; |
|
368 } |
|
369 |
|
370 std::string url = args(0).string_value(); |
|
371 |
|
372 if (error_state) |
|
373 { |
|
374 error ("urlread: url must be a character string"); |
|
375 return retval; |
|
376 } |
|
377 |
|
378 std::string method; |
|
379 Cell param; // empty cell array |
|
380 |
|
381 if (nargin == 3) |
|
382 { |
|
383 method = args(1).string_value(); |
|
384 |
|
385 if (error_state) |
|
386 { |
|
387 error ("urlread: method can only be \"get\" or \"post\""); |
|
388 return retval; |
|
389 } |
|
390 |
|
391 if (method != "get" && method != "post") |
|
392 { |
|
393 error ("urlread: method can only be \"get\" or \"post\""); |
|
394 return retval; |
|
395 } |
|
396 |
|
397 param = args(2).cell_value(); |
|
398 |
|
399 if (error_state) |
|
400 { |
|
401 error ("urlread: parameters for get and post requests must be given as a cell"); |
|
402 return retval; |
|
403 } |
|
404 |
|
405 if (param.numel () % 2 == 1 ) |
|
406 { |
|
407 error ("urlread: number of elements in param must be even"); |
|
408 return retval; |
|
409 } |
|
410 } |
|
411 |
6986
|
412 std::ostringstream buf; |
6043
|
413 |
6986
|
414 CURLcode res = urlget (url, method, param, buf); |
6043
|
415 |
|
416 if (nargout > 0) |
|
417 { |
6986
|
418 retval(0) = buf.str (); |
6043
|
419 retval(1) = res == CURLE_OK; |
6986
|
420 // Return empty string if no error occured. |
6043
|
421 retval(2) = std::string (res == CURLE_OK ? "" : curl_easy_strerror (res)); |
|
422 } |
|
423 |
6484
|
424 if (nargout < 2 && res != CURLE_OK) |
6043
|
425 error ("urlread: curl: %s", curl_easy_strerror (res)); |
|
426 |
|
427 #else |
6981
|
428 error ("urlread: not available in this version of Octave"); |
6043
|
429 #endif |
|
430 |
|
431 return retval; |
|
432 } |