43 #include <curl/curl.h>
44 #include <curl/curlver.h>
45 #include <curl/easy.h>
56 const std::string& target)
69 errmsg =
"__ftp_mget__: can not create directory '"
70 + target + sep + directory +
"': " + msg;
97 std::string realfile = target + directory + sep + sv(i);
99 std::ofstream ofile (realfile.c_str (),
102 if (! ofile.is_open ())
105 errmsg =
"__ftp_mget__: unable to open file";
129 const std::string& directory)
134 = (base.length () == 0
158 std::string file = files (i);
160 if (file ==
"." || file ==
"..")
169 errmsg =
"__ftp__mput: file '" + realfile
170 +
"' does not exist";
184 std::ifstream ifile (realfile.c_str (), std::ios::in |
187 if (! ifile.is_open ())
190 errmsg =
"__ftp_mput__: unable to open file '"
202 file_list.
append (realfile);
209 errmsg =
"__ftp_mput__: can not read the directory '"
217 #if defined (HAVE_CURL)
222 std::ostream& stream = *(
static_cast<std::ostream*
> (streamp));
223 stream.write (static_cast<const char*> (buffer), size*nmemb);
224 return (stream.fail () ? 0 : size * nmemb);
230 std::istream& stream = *(
static_cast<std::istream*
> (streamp));
231 stream.read (static_cast<char*> (buffer), size*nmemb);
233 return stream.gcount ();
235 return (stream.fail () ? 0 : size * nmemb);
241 return static_cast<size_t>(size * nmemb);
247 #define SETOPT(option, parameter) \
250 CURLcode res = curl_easy_setopt (curl, option, parameter); \
251 if (res != CURLE_OK) \
254 errmsg = curl_easy_strerror (res); \
261 #define SETOPTR(option, parameter) \
264 CURLcode res = curl_easy_setopt (curl, option, parameter); \
265 if (res != CURLE_OK) \
268 errmsg = curl_easy_strerror (res); \
285 errmsg =
"can not create curl object";
289 const std::string& passwd, std::ostream& os)
297 errmsg =
"can not create curl object";
301 init (user_arg, passwd, std::cin, os);
303 url =
"ftp://" + host;
318 errmsg =
"can not create curl object";
322 init (
"",
"", std::cin, os);
324 SETOPT (CURLOPT_NOBODY, 0);
330 SETOPT (CURLOPT_HTTPGET, 1);
336 curl_easy_cleanup (
curl);
356 return std::string (curl_easy_strerror (
errnum));
378 SETOPT (CURLOPT_TRANSFERTEXT, 1);
384 SETOPT (CURLOPT_TRANSFERTEXT, 0);
387 void cwd (
const std::string& path)
392 void del (
const std::string& file)
397 void rmdir (
const std::string& path)
402 void mkdir (
const std::string& path)
407 void rename (
const std::string& oldname,
const std::string& newname)
409 struct curl_slist *slist = 0;
412 frame.
add_fcn (curl_slist_free_all, slist);
414 std::string cmd =
"rnfr " + oldname;
415 slist = curl_slist_append (slist, cmd.c_str ());
416 cmd =
"rnto " + newname;
417 slist = curl_slist_append (slist, cmd.c_str ());
418 SETOPT (CURLOPT_POSTQUOTE, slist);
424 SETOPT (CURLOPT_POSTQUOTE, 0);
427 void put (
const std::string& file, std::istream& is)
431 SETOPT (CURLOPT_UPLOAD, 1);
432 SETOPT (CURLOPT_NOBODY, 0);
440 SETOPT (CURLOPT_NOBODY, 1);
441 SETOPT (CURLOPT_UPLOAD, 0);
446 void get (
const std::string& file, std::ostream& os)
450 SETOPT (CURLOPT_NOBODY, 0);
458 SETOPT (CURLOPT_NOBODY, 1);
467 SETOPT (CURLOPT_NOBODY, 0);
473 SETOPT (CURLOPT_NOBODY, 1);
482 std::ostringstream buf;
484 SETOPTR (CURLOPT_WRITEDATA, static_cast<void*> (&buf));
486 SETOPTR (CURLOPT_DIRLISTONLY, 1);
496 SETOPTR (CURLOPT_DIRLISTONLY, 0);
500 std::string str = buf.str ();
505 pos = str.find_first_of (
'\n', pos);
506 if (pos == std::string::npos)
515 size_t newpos = str.find_first_of (
'\n', pos);
516 if (newpos == std::string::npos)
519 retval(i) = str.substr(pos, newpos - pos);
527 time_t& filetime,
bool& fileisdir)
529 std::string path =
pwd ();
533 SETOPT (CURLOPT_FILETIME, 1);
554 curl_easy_getinfo (
curl, CURLINFO_FILETIME, &ft);
557 curl_easy_getinfo (
curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &fs);
561 SETOPT (CURLOPT_HEADERFUNCTION, 0);
562 SETOPT (CURLOPT_FILETIME, 0);
577 struct curl_slist *slist = 0;
580 frame.
add_fcn (curl_slist_free_all, slist);
582 slist = curl_slist_append (slist,
"pwd");
583 SETOPTR (CURLOPT_POSTQUOTE, slist);
586 std::ostringstream buf;
587 SETOPTR (CURLOPT_WRITEHEADER, static_cast<void *>(&buf));
596 size_t pos2 = retval.rfind (
'"');
597 size_t pos1 = retval.rfind (
'"', pos2 - 1);
598 retval = retval.substr (pos1 + 1, pos2 - pos1 - 1);
600 SETOPTR (CURLOPT_HEADERFUNCTION, 0);
601 SETOPTR (CURLOPT_WRITEHEADER, 0);
602 SETOPTR (CURLOPT_POSTQUOTE, 0);
613 if (! query_string.empty ())
614 url +=
"?" + query_string;
627 SETOPT (CURLOPT_POSTFIELDS, query_string.c_str ());
634 if (action.empty () || action ==
"get")
636 else if (action ==
"post")
641 errmsg =
"curl_transfer: unknown http action";
675 void init (
const std::string& user,
const std::string& passwd,
676 std::istream& is, std::ostream& os)
679 SETOPT (CURLOPT_NOBODY, 1);
683 if (! passwd.empty ())
692 SETOPT (CURLOPT_WRITEDATA, static_cast<void*> (&os));
698 SETOPT (CURLOPT_READDATA, static_cast<void*> (&is));
701 SETOPT (CURLOPT_FOLLOWLOCATION,
true);
706 SETOPT (CURLOPT_FTP_USE_EPSV,
false);
708 SETOPT (CURLOPT_NOPROGRESS,
true);
709 SETOPT (CURLOPT_FAILONERROR,
true);
711 SETOPT (CURLOPT_POSTQUOTE, 0);
712 SETOPT (CURLOPT_QUOTE, 0);
717 std::ostringstream query;
719 for (
int i = 0; i < param.
numel (); i += 2)
721 std::string name = param(i);
722 std::string
text = param(i+1);
725 char *enc_name = curl_easy_escape (
curl, name.c_str (),
727 char *enc_text = curl_easy_escape (
curl, text.c_str (),
730 query << enc_name <<
"=" << enc_text;
732 curl_free (enc_name);
733 curl_free (enc_text);
735 if (i < param.
numel ()-1)
745 const std::string& action)
747 struct curl_slist *slist = 0;
751 frame.
add_fcn (curl_slist_free_all, slist);
753 std::string cmd = action +
" " + file_or_dir;
755 slist = curl_slist_append (slist, cmd.c_str ());
757 SETOPT (CURLOPT_POSTQUOTE, slist);
764 SETOPT (CURLOPT_POSTQUOTE, 0);
773 disabled_error (
void)
775 (*current_liboctave_error_handler)
776 (
"support for url transfers was disabled when Octave was built");
781 #if defined (HAVE_CURL)
782 # define REP_CLASS curl_transfer
784 # define REP_CLASS base_url_transfer
789 #if !defined (HAVE_CURL)
795 const std::string& passwd, std::ostream& os)
796 : rep (new
REP_CLASS (host, user, passwd, os))
798 #if !defined (HAVE_CURL)
806 #if !defined (HAVE_CURL)