26 #if defined (HAVE_CONFIG_H)
43 #if defined (HAVE_CURL)
44 # include <curl/curl.h>
45 # include <curl/curlver.h>
46 # include <curl/easy.h>
52 : m_host_or_url (), m_valid (false), m_ftp (false),
53 m_ascii_mode (false), m_ok (true), m_errmsg (),
54 m_curr_istream (&std::cin), m_curr_ostream (&std::cout)
61 : m_host_or_url (host), m_valid (false), m_ftp (true),
62 m_ascii_mode (false), m_ok (true), m_errmsg (),
63 m_curr_istream (&std::cin), m_curr_ostream (&os)
68 : m_host_or_url (url), m_valid (false), m_ftp (false),
69 m_ascii_mode (false), m_ok (true), m_errmsg (),
70 m_curr_istream (&std::cin), m_curr_ostream (&os)
81 const std::string& target)
86 if (! fs || ! fs.
is_dir ())
89 int status =
sys::mkdir (directory, 0777, msg);
94 m_errmsg =
"__ftp_mget__: can not create directory '"
95 + target + sep + directory +
"': " + msg;
122 std::string realfile = target + directory + sep + sv(i);
126 std::ios::out | std::ios::binary);
128 if (! ofile.is_open ())
131 m_errmsg =
"__ftp_mget__: unable to open file";
155 const std::string& directory)
182 std::string file = files (i);
184 if (file ==
"." || file ==
"..")
195 m_errmsg =
"__ftp__mput: file '" + realfile
196 +
"' does not exist";
212 std::ios::in | std::ios::binary);
214 if (! ifile.is_open ())
217 m_errmsg =
"__ftp_mput__: unable to open file '"
229 file_list.
append (realfile);
235 m_errmsg =
"__ftp_mput__: can not read the directory '"
243 #if defined (HAVE_CURL)
246 write_data (
void *buffer,
size_t size,
size_t nmemb,
void *streamp)
248 std::ostream&
stream = *(
static_cast<std::ostream *
> (streamp));
249 stream.
write (
static_cast<const char *
> (buffer), size*nmemb);
250 return (
stream.fail () ? 0 : size * nmemb);
254 read_data (
void *buffer,
size_t size,
size_t nmemb,
void *streamp)
256 std::istream&
stream = *(
static_cast<std::istream *
> (streamp));
257 stream.
read (
static_cast<char *
> (buffer), size*nmemb);
261 return (
stream.fail () ? 0 : size * nmemb);
267 return static_cast<size_t> (size * nmemb);
273 #define SETOPT(option, parameter) \
276 CURLcode res = curl_easy_setopt (m_curl, option, parameter); \
277 if (res != CURLE_OK) \
280 m_errmsg = curl_easy_strerror (res); \
287 #define SETOPTR(option, parameter) \
290 CURLcode res = curl_easy_setopt (m_curl, option, parameter); \
291 if (res != CURLE_OK) \
294 m_errmsg = curl_easy_strerror (res); \
311 m_errmsg =
"can not create curl object";
315 const std::string& passwd, std::ostream& os)
323 m_errmsg =
"can not create curl object";
327 init (user_arg, passwd, std::cin, os);
329 m_url =
"ftp://" + host;
344 m_errmsg =
"can not create curl object";
348 init (
"",
"", std::cin, os);
351 if (! cainfo.empty ())
352 SETOPT (CURLOPT_CAINFO, cainfo.c_str ());
355 if (! capath.empty ())
356 SETOPT (CURLOPT_CAPATH, capath.c_str ());
358 SETOPT (CURLOPT_NOBODY, 0);
364 SETOPT (CURLOPT_HTTPGET, 1);
376 curl_easy_cleanup (
m_curl);
396 return std::string (curl_easy_strerror (
m_errnum));
418 SETOPT (CURLOPT_TRANSFERTEXT, 1);
424 SETOPT (CURLOPT_TRANSFERTEXT, 0);
432 void del (
const std::string& file)
447 void rename (
const std::string& oldname,
const std::string& newname)
449 struct curl_slist *slist =
nullptr;
452 frame.
add_fcn (curl_slist_free_all, slist);
454 std::string cmd =
"rnfr " + oldname;
455 slist = curl_slist_append (slist, cmd.c_str ());
456 cmd =
"rnto " + newname;
457 slist = curl_slist_append (slist, cmd.c_str ());
458 SETOPT (CURLOPT_POSTQUOTE, slist);
464 SETOPT (CURLOPT_POSTQUOTE, 0);
467 void put (
const std::string& file, std::istream& is)
471 SETOPT (CURLOPT_UPLOAD, 1);
472 SETOPT (CURLOPT_NOBODY, 0);
480 SETOPT (CURLOPT_NOBODY, 1);
481 SETOPT (CURLOPT_UPLOAD, 0);
486 void get (
const std::string& file, std::ostream& os)
490 SETOPT (CURLOPT_NOBODY, 0);
498 SETOPT (CURLOPT_NOBODY, 1);
507 SETOPT (CURLOPT_NOBODY, 0);
513 SETOPT (CURLOPT_NOBODY, 1);
522 std::ostringstream buf;
524 SETOPTR (CURLOPT_WRITEDATA,
static_cast<void *
> (&buf));
526 SETOPTR (CURLOPT_DIRLISTONLY, 1);
536 SETOPTR (CURLOPT_DIRLISTONLY, 0);
540 std::string str = buf.str ();
545 pos = str.find_first_of (
'\n', pos);
546 if (pos == std::string::npos)
555 size_t newpos = str.find_first_of (
'\n', pos);
556 if (newpos == std::string::npos)
559 retval(i) = str.substr(pos, newpos - pos);
567 time_t& filetime,
bool& fileisdir)
573 SETOPT (CURLOPT_FILETIME, 1);
594 curl_easy_getinfo (
m_curl, CURLINFO_FILETIME, &ft);
597 curl_easy_getinfo (
m_curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &fs);
601 SETOPT (CURLOPT_HEADERFUNCTION, 0);
602 SETOPT (CURLOPT_FILETIME, 0);
617 struct curl_slist *slist =
nullptr;
620 frame.
add_fcn (curl_slist_free_all, slist);
622 slist = curl_slist_append (slist,
"pwd");
623 SETOPTR (CURLOPT_POSTQUOTE, slist);
626 std::ostringstream buf;
627 SETOPTR (CURLOPT_WRITEHEADER,
static_cast<void *
>(&buf));
636 size_t pos2 =
retval.rfind (
'"');
637 size_t pos1 =
retval.rfind (
'"', pos2 - 1);
640 SETOPTR (CURLOPT_HEADERFUNCTION, 0);
641 SETOPTR (CURLOPT_WRITEHEADER, 0);
642 SETOPTR (CURLOPT_POSTQUOTE, 0);
661 std::string query_string;
665 if (action.empty () || action ==
"get")
667 if (! query_string.empty ())
668 m_url +=
'?' + query_string;
672 else if (action ==
"post" || action ==
"put" || action ==
"delete")
674 SETOPT (CURLOPT_POSTFIELDS, query_string.c_str ());
678 SETOPT (CURLOPT_CUSTOMREQUEST,
"PUT");
681 if (action ==
"delete")
683 SETOPT (CURLOPT_CUSTOMREQUEST,
"DELETE");
691 m_errmsg =
"curl_transfer: unknown http action";
700 SETOPT (CURLOPT_COOKIEJAR, filename.c_str ());
702 SETOPT (CURLOPT_COOKIEFILE, filename.c_str ());
709 struct curl_slist *slist =
nullptr;
713 frame.
add_fcn (curl_slist_free_all, slist);
715 if (param.
numel () >= 2)
717 for (
int i = 0; i < param.
numel (); i += 2)
719 std::string header = param(i) +
": " + param(i+1);
721 slist = curl_slist_append (slist, header.c_str ());
724 SETOPT (CURLOPT_HTTPHEADER, slist);
735 struct curl_httppost *post =
nullptr, *last =
nullptr;
741 frame.
add_fcn (curl_formfree, post);
743 if (param.
numel () >= 2)
745 for (
int i = 0; i < param.
numel (); i += 2)
747 std::string
name = param(i);
748 std::string data = param(i+1);
751 curl_formadd (&post, &last, CURLFORM_COPYNAME,
name.c_str (),
752 CURLFORM_FILE, data.c_str (), CURLFORM_END);
754 curl_formadd(&post, &last, CURLFORM_COPYNAME,
name.c_str (),
755 CURLFORM_COPYCONTENTS, data.c_str (), CURLFORM_END);
758 SETOPT (CURLOPT_HTTPPOST, post);
768 std::string temp =
"";
782 SETOPT (CURLOPT_USERPWD, tmp.c_str ());
786 std::string tmp = options.
Username +
":";
787 SETOPT (CURLOPT_USERPWD, tmp.c_str ());
828 void init (
const std::string& user,
const std::string& passwd,
829 std::istream& is, std::ostream& os)
832 SETOPT (CURLOPT_NOBODY, 1);
836 if (! passwd.empty ())
845 SETOPT (CURLOPT_WRITEDATA,
static_cast<void *
> (&os));
851 SETOPT (CURLOPT_READDATA,
static_cast<void *
> (&is));
854 SETOPT (CURLOPT_FOLLOWLOCATION,
true);
859 SETOPT (CURLOPT_FTP_USE_EPSV,
false);
863 curl_version_info_data * data = curl_version_info(CURLVERSION_NOW);
864 const char *lib_ver = data->version;
865 std::string user_agent
868 +
" (https://www.gnu.org/software/octave/ ; help@octave.org) libcurl/"
869 + std::string (lib_ver));
871 SETOPT (CURLOPT_USERAGENT, user_agent.c_str ());
873 SETOPT (CURLOPT_NOPROGRESS,
true);
874 SETOPT (CURLOPT_FAILONERROR,
true);
876 SETOPT (CURLOPT_POSTQUOTE, 0);
877 SETOPT (CURLOPT_QUOTE, 0);
882 std::ostringstream query;
884 if (param.
numel () >= 2)
885 for (
int i = 0; i < param.
numel (); i += 2)
887 std::string
name = param(i);
888 std::string
text = param(i+1);
891 char *enc_name = curl_easy_escape (
m_curl,
name.c_str (),
893 char *enc_text = curl_easy_escape (
m_curl,
text.c_str (),
896 query << enc_name <<
'=' << enc_text;
898 curl_free (enc_name);
899 curl_free (enc_text);
901 if (i < param.
numel ()-2)
911 const std::string& action)
913 struct curl_slist *slist =
nullptr;
917 frame.
add_fcn (curl_slist_free_all, slist);
919 std::string cmd = action +
' ' + file_or_dir;
921 slist = curl_slist_append (slist, cmd.c_str ());
923 SETOPT (CURLOPT_POSTQUOTE, slist);
930 SETOPT (CURLOPT_POSTQUOTE, 0);
938 #if defined (HAVE_CURL)
939 # define REP_CLASS curl_transfer
941 # define REP_CLASS base_url_transfer
948 const std::string& passwd, std::ostream& os)
949 : m_rep (new
REP_CLASS (host, user, passwd, os))
void resize(const dim_vector &dv, const T &rfv)
Size of the specified dimension.
octave_idx_type numel(void) const
Number of elements in the array.
void add_fcn(void(*fcn)(Params...), Args &&... args)
static void delete_file(const std::string &file)
virtual void mkdir(const std::string &)
static void reset_path(base_url_transfer *curl_xfer)
std::string m_host_or_url
std::ostream * m_curr_ostream
string_vector mput_directory(const std::string &base, const std::string &directory)
virtual void put(const std::string &, std::istream &)
std::istream * m_curr_istream
virtual void get(const std::string &, std::ostream &)
virtual void cwd(const std::string &)
virtual void get_fileinfo(const std::string &, double &, time_t &, bool &)
virtual string_vector list(void)
void mget_directory(const std::string &directory, const std::string &target)
void set_header_fields(const Array< std::string > ¶m)
void set_weboptions(const struct weboptions &options)
void http_action(const Array< std::string > ¶m, const std::string &action)
void put(const std::string &file, std::istream &is)
void cookie_jar(const std::string &filename)
std::string lasterror(void) const
void get(const std::string &file, std::ostream &os)
curl_transfer & operator=(const curl_transfer &)=delete
void http_get(const Array< std::string > ¶m)
void init(const std::string &user, const std::string &passwd, std::istream &is, std::ostream &os)
void mkdir(const std::string &path)
void get_fileinfo(const std::string &filename, double &filesize, time_t &filetime, bool &fileisdir)
void ftp_file_or_dir_action(const std::string &file_or_dir, const std::string &action)
curl_transfer(const std::string &host, const std::string &user_arg, const std::string &passwd, std::ostream &os)
std::istream & set_istream(std::istream &is)
void cwd(const std::string &path)
void rename(const std::string &oldname, const std::string &newname)
std::ostream & set_ostream(std::ostream &os)
void http_post(const Array< std::string > ¶m)
void rmdir(const std::string &path)
curl_transfer(const std::string &url_str, std::ostream &os)
void form_data_post(const Array< std::string > ¶m)
void del(const std::string &file)
std::string form_query_string(const Array< std::string > ¶m)
curl_transfer(const curl_transfer &)=delete
octave_value read(const Array< double > &size, octave_idx_type block_size, oct_data_conv::data_type input_type, oct_data_conv::data_type output_type, octave_idx_type skip, mach_info::float_format flt_fmt, octave_idx_type &count)
octave_idx_type write(const octave_value &data, octave_idx_type block_size, oct_data_conv::data_type output_type, octave_idx_type skip, mach_info::float_format flt_fmt)
static std::string getenv(const std::string &name)
string_vector & append(const std::string &s)
octave_idx_type numel(void) const
std::string dir_sep_str(void)
int mkdir(const std::string &nm, mode_t md)
std::ifstream ifstream(const std::string &filename, const std::ios::openmode mode)
int unlink(const std::string &name)
std::ofstream ofstream(const std::string &filename, const std::ios::openmode mode)
bool get_dirlist(const std::string &dirname, string_vector &dirlist, std::string &msg)
static size_t throw_away(void *, size_t size, size_t nmemb, void *)
static int write_data(void *buffer, size_t size, size_t nmemb, void *streamp)
static int read_data(void *buffer, size_t size, size_t nmemb, void *streamp)
octave_value::octave_value(const Array< char > &chm, char type) return retval
#define BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE
#define END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE
std::string ContentReader
Array< std::string > HeaderFields
std::string CertificateFilename
#define SETOPT(option, parameter)
#define SETOPTR(option, parameter)