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 (),
100 std::ios::out | std::ios::binary);
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 ())
684 userpwd +=
":" + passwd;
685 if (! userpwd.empty ())
686 SETOPT (CURLOPT_USERPWD, userpwd.c_str ());
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);
772 #if defined (HAVE_CURL)
773 # define REP_CLASS curl_transfer
775 # define REP_CLASS base_url_transfer
782 const std::string& passwd, std::ostream& os)
783 : rep (new
REP_CLASS (host, user, passwd, os))
std::string form_query_string(const Array< std::string > ¶m)
int octave_unlink(const std::string &name)
static int write_data(void *buffer, size_t size, size_t nmemb, void *streamp)
curl_transfer(const std::string &url_str, std::ostream &os)
static int read_data(void *buffer, size_t size, size_t nmemb, void *streamp)
virtual void cwd(const std::string &)
void rename(const std::string &oldname, const std::string &newname)
octave_idx_type numel(void) const
Number of elements in the array.
virtual void put(const std::string &, std::istream &)
void cwd(const std::string &path)
void init(const std::string &user, const std::string &passwd, std::istream &is, std::ostream &os)
void http_get(const Array< std::string > ¶m)
int octave_mkdir(const std::string &nm, mode_t md)
static size_t throw_away(void *, size_t size, size_t nmemb, void *)
#define SETOPTR(option, parameter)
void ftp_file_or_dir_action(const std::string &file_or_dir, const std::string &action)
std::string lasterror(void) const
void add_fcn(void(*fcn)(void))
#define SETOPT(option, parameter)
void resize(octave_idx_type n, const std::string &rfv=std::string())
string_vector & append(const std::string &s)
void get_fileinfo(const std::string &filename, double &filesize, time_t &filetime, bool &fileisdir)
virtual void mkdir(const std::string &)
size_t size(T const (&)[z])
std::istream & set_istream(std::istream &is)
void del(const std::string &file)
void mkdir(const std::string &path)
void mget_directory(const std::string &directory, const std::string &target)
void http_post(const Array< std::string > ¶m)
virtual void get_fileinfo(const std::string &, double &, time_t &, bool &)
octave_idx_type length(void) const
Number of elements in the array.
void rmdir(const std::string &path)
#define END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE
std::istream * curr_istream
void put(const std::string &file, std::istream &is)
void http_action(const Array< std::string > ¶m, const std::string &action)
static void delete_file(const std::string &file)
curl_transfer & operator=(const curl_transfer &)
static void reset_path(base_url_transfer *curl_xfer)
curl_transfer(const std::string &host, const std::string &user_arg, const std::string &passwd, std::ostream &os)
#define BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE
std::ostream & set_ostream(std::ostream &os)
static std::string dir_sep_str(void)
string_vector mput_directory(const std::string &base, const std::string &directory)
virtual string_vector list(void)
std::ostream * curr_ostream