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