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 |
|
52 int |
|
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 // Progress callback function for curl. |
|
62 |
|
63 int |
|
64 progress_func (const char *url, double dltotal, double dlnow, |
|
65 double /*ultotal*/, double /*ulnow*/) |
|
66 { |
|
67 // macro that picks up Ctrl-C signalling |
|
68 OCTAVE_QUIT; |
|
69 |
|
70 #if !defined (URL_QUITE_DOWNLOAD) |
|
71 if (dltotal != 0) |
|
72 { |
|
73 // Is the carriage return character "\r" portable? |
|
74 octave_stdout << "\r" << url << ": " |
|
75 << std::fixed << std::setw(5) << std::setprecision(1) |
|
76 << dlnow*100.0/dltotal << " %"; |
|
77 |
|
78 octave_stdout.flush (); |
|
79 } |
|
80 #endif |
|
81 |
|
82 return 0; |
|
83 } |
|
84 |
|
85 // Form the query string based on param. |
|
86 |
|
87 std::string |
|
88 form_query_string (CURL *curl, const Cell& param) |
|
89 { |
|
90 std::ostringstream query; |
|
91 |
|
92 for (int i = 0; i < param.numel (); i += 2) |
|
93 { |
|
94 std::string name = param(i).string_value (); |
|
95 std::string text = param(i+1).string_value (); |
|
96 |
|
97 // Encode strings. |
|
98 char *enc_name = curl_easy_escape (curl, name.c_str (), name.length ()); |
|
99 char *enc_text = curl_easy_escape (curl, text.c_str (), text.length ()); |
|
100 |
|
101 query << enc_name << "=" << enc_text; |
|
102 |
|
103 curl_free (enc_name); |
|
104 curl_free (enc_text); |
|
105 |
|
106 if (i < param.numel()-1) |
|
107 query << "&"; |
|
108 } |
|
109 |
|
110 query.flush (); |
|
111 |
|
112 return query.str (); |
|
113 } |
|
114 |
|
115 // curl front-end |
|
116 |
|
117 CURLcode |
|
118 urlget (const std::string& url, const std::string& method, |
|
119 const Cell& param, std::ostream& stream) |
|
120 { |
|
121 CURL *curl; |
|
122 |
|
123 curl_global_init(CURL_GLOBAL_DEFAULT); |
|
124 |
|
125 curl = curl_easy_init(); |
|
126 |
|
127 if (! curl) |
|
128 return CURLE_FAILED_INIT; |
|
129 |
|
130 // handle paramters of GET or POST request |
|
131 |
|
132 std::string query_string = form_query_string (curl,param); |
|
133 //octave_stdout << "query_string " << query_string << std::endl; |
|
134 |
|
135 if (method == "get") |
|
136 { |
|
137 query_string = url + "?" + query_string; |
|
138 curl_easy_setopt (curl, CURLOPT_URL, query_string.c_str ()); |
|
139 } |
|
140 else if (method == "post") |
|
141 { |
|
142 curl_easy_setopt (curl, CURLOPT_URL, url.c_str ()); |
|
143 curl_easy_setopt (curl, CURLOPT_POSTFIELDS, query_string.c_str ()); |
|
144 } |
|
145 else |
|
146 { |
|
147 curl_easy_setopt (curl, CURLOPT_URL,url.c_str()); |
|
148 } |
|
149 |
|
150 // Define our callback to get called when there's data to be written. |
|
151 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, write_data); |
|
152 |
|
153 // Set a pointer to our struct to pass to the callback. |
|
154 curl_easy_setopt (curl, CURLOPT_WRITEDATA, static_cast<void*> (&stream)); |
|
155 |
6390
|
156 // Follow redirects. |
|
157 curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 1); |
|
158 |
6043
|
159 curl_easy_setopt (curl, CURLOPT_NOPROGRESS, false); |
|
160 curl_easy_setopt (curl, CURLOPT_PROGRESSFUNCTION, progress_func); |
|
161 curl_easy_setopt (curl, CURLOPT_PROGRESSDATA, url.c_str ()); |
|
162 curl_easy_setopt (curl, CURLOPT_FAILONERROR, true); |
|
163 |
|
164 // Switch on full protocol/debug output |
|
165 // curl_easy_setopt(curl, CURLOPT_VERBOSE, true); |
|
166 |
|
167 CURLcode res = curl_easy_perform(curl); |
|
168 |
|
169 #if !defined (URL_QUITE_DOWNLOAD) |
|
170 if (res == CURLE_OK) |
|
171 { |
|
172 // download is complete |
|
173 progress_func (url.c_str (), 1, 1, 0, 0); |
|
174 // new line after progress_func |
|
175 octave_stdout << std::endl; |
|
176 } |
|
177 #endif |
|
178 |
|
179 // always cleanup |
|
180 curl_easy_cleanup (curl); |
|
181 |
|
182 curl_global_cleanup (); |
|
183 |
|
184 return res; |
|
185 } |
|
186 |
|
187 #endif |
|
188 |
|
189 DEFUN_DLD (urlwrite, args, nargout, |
|
190 "-*- texinfo -*-\n\ |
|
191 @deftypefn {Loadable Function} {} urlwrite (@var{URL}, @var{localfile})\n\ |
|
192 @deftypefnx {Loadable Function} {@var{f} =} urlwrite (@var{url}, @var{localfile})\n\ |
|
193 @deftypefnx {Loadable Function} {[@var{f}, @var{success}] =} urlwrite (@var{url}, @var{localfile})\n\ |
|
194 @deftypefnx {Loadable Function} {[@var{f}, @var{success}, @var{message}] =} urlwrite (@var{url}, @var{localfile})\n\ |
|
195 Download a remote file specified by its @var{URL} and save it as\n\ |
|
196 @var{localfile}. For example,\n\ |
|
197 \n\ |
|
198 @example\n\ |
6588
|
199 urlwrite (\"ftp://ftp.octave.org/pub/octave/README\", \"README.txt\");\n\ |
6043
|
200 @end example\n\ |
|
201 \n\ |
|
202 The full path of the downloaded file is returned in @var{f}. The\n\ |
|
203 variable @var{success} is 1 if the download was successful,\n\ |
|
204 otherwise it is 0 in which case @var{message} contains an error\n\ |
|
205 message. If no output argument is specified and if an error occurs,\n\ |
6588
|
206 then the error is signaled through Octave's error handling mechanism.\n\ |
6043
|
207 \n\ |
|
208 This function uses libcurl. Curl supports, among others, the HTTP,\n\ |
|
209 FTP and FILE protocols. Username and password may be specified in\n\ |
|
210 the URL, for example:\n\ |
|
211 \n\ |
|
212 @example\n\ |
6588
|
213 urlwrite (\"http://username:password@@example.com/file.txt\",\n\ |
|
214 \"file.txt\");\n\ |
6043
|
215 @end example\n\ |
|
216 \n\ |
|
217 GET and POST requests can be specified by @var{method} and @var{param}.\n\ |
6589
|
218 The parameter @var{method} is either @samp{get} or @samp{post}\n\ |
6588
|
219 and @var{param} is a cell array of parameter and value pairs.\n\ |
|
220 For example:\n\ |
6043
|
221 \n\ |
|
222 @example\n\ |
6588
|
223 urlwrite (\"http://www.google.com/search\", \"search.html\",\n\ |
|
224 \"get\", @{\"query\", \"octave\"@});\n\ |
6043
|
225 @end example\n\ |
|
226 @seealso{urlread}\n\ |
|
227 @end deftypefn") |
|
228 { |
|
229 octave_value_list retval; |
|
230 |
|
231 #if defined (HAVE_CURL) |
|
232 |
|
233 int nargin = args.length (); |
|
234 |
|
235 // verify arguments |
|
236 if (nargin != 2 && nargin != 4) |
|
237 { |
|
238 print_usage (); |
|
239 return retval; |
|
240 } |
|
241 |
|
242 std::string url = args(0).string_value(); |
|
243 |
|
244 if (error_state) |
|
245 { |
|
246 error ("urlwrite: url must be a character string"); |
|
247 return retval; |
|
248 } |
|
249 |
|
250 // name to store the file if download is succesful |
|
251 std::string filename = args(1).string_value(); |
|
252 |
|
253 if (error_state) |
|
254 { |
|
255 error ("urlwrite: localfile must be a character string"); |
|
256 return retval; |
|
257 } |
|
258 |
|
259 std::string method; |
|
260 Cell param; // empty cell array |
|
261 |
|
262 if (nargin == 4) |
|
263 { |
|
264 method = args(2).string_value(); |
|
265 |
|
266 if (error_state) |
|
267 { |
|
268 error ("urlwrite: method can only be \"get\" or \"post\""); |
|
269 return retval; |
|
270 } |
|
271 |
|
272 if (method != "get" && method != "post") |
|
273 { |
|
274 error ("urlwrite: method can only be \"get\" or \"post\""); |
|
275 return retval; |
|
276 } |
|
277 |
|
278 param = args(3).cell_value(); |
|
279 |
|
280 if (error_state) |
|
281 { |
|
282 error ("urlwrite: parameters for get and post requests must be given as a cell"); |
|
283 return retval; |
|
284 } |
|
285 |
|
286 |
|
287 if (param.numel () % 2 == 1 ) |
|
288 { |
|
289 error ("urlwrite: number of elements in param must be even"); |
|
290 return retval; |
|
291 } |
|
292 } |
|
293 |
|
294 // create and open file stream |
|
295 |
|
296 std::ofstream stream (filename.c_str(), std::ios::out | std::ios::binary); |
|
297 |
|
298 if (! stream.is_open ()) |
|
299 { |
|
300 error ("urlwrite: unable to open file"); |
|
301 return retval; |
|
302 } |
|
303 |
|
304 CURLcode res = urlget (url, method, param, stream); |
|
305 |
|
306 // close the local file |
|
307 stream.close (); |
|
308 |
|
309 if (nargout > 0) |
|
310 { |
|
311 // FIXME: urlwrite should return full file path |
|
312 retval(0) = octave_env::make_absolute (filename, octave_env::getcwd ()); |
|
313 // retval(0) = filename; |
|
314 retval(1) = res == CURLE_OK; |
|
315 // return empty string if no error occured |
|
316 retval(2) = std::string (res == CURLE_OK ? "" : curl_easy_strerror (res)); |
|
317 } |
|
318 |
6484
|
319 if (nargout < 2 && res != CURLE_OK) |
6043
|
320 error ("urlwrite: curl: %s", curl_easy_strerror (res)); |
|
321 |
|
322 #else |
6335
|
323 retval(2) = "urlwrite: not available in this version of Octave"; |
|
324 retval(1) = 0; |
|
325 retval(0) = ""; |
6043
|
326 #endif |
|
327 |
|
328 return retval; |
|
329 } |
|
330 |
|
331 DEFUN_DLD (urlread, args, nargout, |
|
332 "-*- texinfo -*-\n\ |
6549
|
333 @deftypefn {Loadable Function} {@var{s} =} urlread (@var{url})\n\ |
6043
|
334 @deftypefnx {Loadable Function} {[@var{s}, @var{success}] =} urlread (@var{url})\n\ |
6549
|
335 @deftypefnx {Loadable Function} {[@var{s}, @var{success}, @var{message}] =} urlread (@var{url})\n\ |
6547
|
336 @deftypefnx {Loadable Function} {[@dots{}] =} urlread (@var{url}, @var{method}, @var{param})\n\ |
6043
|
337 Download a remote file specified by its @var{URL} and return its content\n\ |
|
338 in string @var{s}. For example,\n\ |
|
339 \n\ |
|
340 @example\n\ |
6588
|
341 s = urlread (\"ftp://ftp.octave.org/pub/octave/README\");\n\ |
6043
|
342 @end example\n\ |
|
343 \n\ |
|
344 The variable @var{success} is 1 if the download was successful,\n\ |
|
345 otherwise it is 0 in which case @var{message} contains an error\n\ |
|
346 message. If no output argument is specified and if an error occurs,\n\ |
6588
|
347 then the error is signaled through Octave's error handling mechanism.\n\ |
6043
|
348 \n\ |
|
349 This function uses libcurl. Curl supports, among others, the HTTP,\n\ |
|
350 FTP and FILE protocols. Username and password may be specified in the\n\ |
|
351 URL. For example,\n\ |
|
352 \n\ |
|
353 @example\n\ |
6588
|
354 s = urlread (\"http://username:password@@example.com/file.txt\");\n\ |
6043
|
355 @end example\n\ |
|
356 \n\ |
|
357 GET and POST requests can be specified by @var{method} and @var{param}.\n\ |
6588
|
358 The parameter @var{method} is either @samp{get} or @samp{post}\n\ |
|
359 and @var{param} is a cell array of parameter and value pairs.\n\ |
6650
|
360 For example,\n\ |
6043
|
361 \n\ |
|
362 @example\n\ |
6588
|
363 s = urlread (\"http://www.google.com/search\", \"get\",\n\ |
|
364 @{\"query\", \"octave\"@});\n\ |
6043
|
365 @end example\n\ |
|
366 @seealso{urlwrite}\n\ |
|
367 @end deftypefn") |
|
368 { |
6588
|
369 // Octave's return value |
6043
|
370 octave_value_list retval; |
|
371 |
|
372 #if defined (HAVE_CURL) |
|
373 |
|
374 int nargin = args.length (); |
|
375 |
|
376 // verify arguments |
|
377 if (nargin != 1 && nargin != 3) |
|
378 { |
|
379 print_usage (); |
|
380 return retval; |
|
381 } |
|
382 |
|
383 std::string url = args(0).string_value(); |
|
384 |
|
385 if (error_state) |
|
386 { |
|
387 error ("urlread: url must be a character string"); |
|
388 return retval; |
|
389 } |
|
390 |
|
391 std::string method; |
|
392 Cell param; // empty cell array |
|
393 |
|
394 if (nargin == 3) |
|
395 { |
|
396 method = args(1).string_value(); |
|
397 |
|
398 if (error_state) |
|
399 { |
|
400 error ("urlread: method can only be \"get\" or \"post\""); |
|
401 return retval; |
|
402 } |
|
403 |
|
404 if (method != "get" && method != "post") |
|
405 { |
|
406 error ("urlread: method can only be \"get\" or \"post\""); |
|
407 return retval; |
|
408 } |
|
409 |
|
410 param = args(2).cell_value(); |
|
411 |
|
412 if (error_state) |
|
413 { |
|
414 error ("urlread: parameters for get and post requests must be given as a cell"); |
|
415 return retval; |
|
416 } |
|
417 |
|
418 if (param.numel () % 2 == 1 ) |
|
419 { |
|
420 error ("urlread: number of elements in param must be even"); |
|
421 return retval; |
|
422 } |
|
423 } |
|
424 |
|
425 // string stream for output |
|
426 std::ostringstream stream; |
|
427 |
|
428 CURLcode res = urlget (url, method, param, stream); |
|
429 |
|
430 if (nargout > 0) |
|
431 { |
|
432 retval(0) = stream.str (); |
|
433 retval(1) = res == CURLE_OK; |
|
434 // return empty string if no error occured |
|
435 retval(2) = std::string (res == CURLE_OK ? "" : curl_easy_strerror (res)); |
|
436 } |
|
437 |
6484
|
438 if (nargout < 2 && res != CURLE_OK) |
6043
|
439 error ("urlread: curl: %s", curl_easy_strerror (res)); |
|
440 |
|
441 #else |
6335
|
442 retval(2) = "urlread: not available in this version of Octave"; |
|
443 retval(1) = 0; |
|
444 retval(0) = ""; |
6043
|
445 #endif |
|
446 |
|
447 return retval; |
|
448 } |