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