GNU Octave  6.2.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
url-transfer.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2006-2021 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if ! defined (octave_url_transfer_h)
27 #define octave_url_transfer_h 1
28 
29 #include "octave-config.h"
30 
31 #include <istream>
32 #include <memory>
33 #include <ostream>
34 #include <string>
35 
36 #include "str-vec.h"
37 
38 template <typename T> class Array;
39 
40 namespace octave
41 {
42  struct weboptions
43  {
44  std::string UserAgent;
45  long Timeout;
46  std::string Username;
47  std::string Password;
49  std::string ContentReader;
50  std::string RequestMethod;
51  std::string ArrayFormat;
52  std::string CertificateFilename;
53  };
54 
55  class
56  OCTAVE_API
58  {
59  private:
60 
61  static void delete_file (const std::string& file);
62 
63  static void reset_path (base_url_transfer *curl_xfer)
64  {
65  curl_xfer->cwd ("..");
66  }
67 
68  public:
69 
70  friend class url_transfer;
71 
72  base_url_transfer (void);
73 
74  base_url_transfer (const std::string& host,
75  const std::string& /* user_arg */,
76  const std::string& /* passwd */,
77  std::ostream& os);
78 
79  base_url_transfer (const std::string& url, std::ostream& os);
80 
81  // No copying!
82 
84 
85  base_url_transfer& operator = (const base_url_transfer&) = delete;
86 
87  virtual ~base_url_transfer (void) = default;
88 
89  bool is_valid (void) const { return m_valid; }
90 
91  bool good (void) const { return m_valid && m_ok; }
92 
93  virtual void perform (void) { }
94 
95  virtual std::string lasterror (void) const { return m_errmsg; }
96 
97  virtual std::ostream& set_ostream (std::ostream& /* os */)
98  {
99  return *m_curr_ostream;
100  }
101 
102  virtual std::istream& set_istream (std::istream& /* is */)
103  {
104  return *m_curr_istream;
105  }
106 
107  virtual void ascii (void) { }
108 
109  virtual void binary (void) { }
110 
111  bool is_ascii (void) const { return m_ascii_mode; }
112 
113  bool is_binary (void) const { return ! m_ascii_mode; }
114 
115  virtual void cwd (const std::string& /* path */) { }
116 
117  virtual void del (const std::string& /* file */) { }
118 
119  virtual void rmdir (const std::string& /* path */) { }
120 
121  virtual void mkdir (const std::string& /* path */) { }
122 
123  virtual void rename (const std::string& /* oldname */,
124  const std::string& /* newname */) { }
125 
126  virtual void put (const std::string& /* file */,
127  std::istream& /* is */) { }
128 
129  virtual void get (const std::string& /* file */,
130  std::ostream& /* os */) { }
131 
132  void mget_directory (const std::string& directory,
133  const std::string& target);
134 
135  string_vector mput_directory (const std::string& base,
136  const std::string& directory);
137 
138  virtual void dir (void) { }
139 
140  virtual string_vector list (void) { return string_vector (); }
141 
142  virtual void get_fileinfo (const std::string& /* filename */,
143  double& /* filesize */,
144  time_t& /* filetime */,
145  bool& /* fileisdir */) { }
146 
147  virtual std::string pwd (void) { return ""; }
148 
149  virtual void http_get (const Array<std::string>& /* param */) { }
150 
151  virtual void http_post (const Array<std::string>& /* param */) { }
152 
153  virtual void http_action (const Array<std::string>& /* param */,
154  const std::string& /* action */) { }
155 
156  virtual void cookie_jar (const std::string& /* filename */) { }
157 
158  virtual void set_header_fields (const Array<std::string>& /* param */) { }
159 
160  virtual void form_data_post (const Array<std::string>& /* param */) { }
161 
162  virtual void set_weboptions (const struct weboptions& /* param */) { }
163 
164  protected:
165 
166  // Host for ftp transfers or full URL for http requests.
167  std::string m_host_or_url;
168  bool m_valid;
169  bool m_ftp;
171  bool m_ok;
172  std::string m_errmsg;
173  std::istream *m_curr_istream;
174  std::ostream *m_curr_ostream;
175  };
176 
177  class
178  OCTAVE_API
180  {
181  public:
182 
183  url_transfer (void);
184 
185  url_transfer (const std::string& host, const std::string& user,
186  const std::string& passwd, std::ostream& os);
187 
188  url_transfer (const std::string& url, std::ostream& os);
189 
190  url_transfer (const url_transfer&) = default;
191 
192  url_transfer& operator = (const url_transfer&) = default;
193 
194  ~url_transfer (void) = default;
195 
196  bool is_valid (void) const { return m_rep->is_valid (); }
197 
198  bool good (void) const { return m_rep->good (); }
199 
200  std::string lasterror (void) const { return m_rep->lasterror (); }
201 
202  std::ostream& set_ostream (std::ostream& os)
203  {
204  return m_rep->set_ostream (os);
205  }
206 
207  std::istream& set_istream (std::istream& is)
208  {
209  return m_rep->set_istream (is);
210  }
211 
212  void ascii (void) { m_rep->ascii (); }
213 
214  void binary (void) { m_rep->binary (); }
215 
216  bool is_ascii (void) const { return m_rep->is_ascii (); }
217 
218  bool is_binary (void) const { return m_rep->is_binary (); }
219 
220  void cwd (const std::string& path) { m_rep->cwd (path); }
221 
222  void del (const std::string& file) { m_rep->del (file); }
223 
224  void rmdir (const std::string& path) { m_rep->rmdir (path); }
225 
226  void mkdir (const std::string& path) { m_rep->mkdir (path); }
227 
228  void rename (const std::string& oldname, const std::string& newname)
229  {
230  m_rep->rename (oldname, newname);
231  }
232 
233  void put (const std::string& file, std::istream& is)
234  {
235  m_rep->put (file, is);
236  }
237 
238  void get (const std::string& file, std::ostream& os)
239  {
240  m_rep->get (file, os);
241  }
242 
243  void mget_directory (const std::string& directory,
244  const std::string& target)
245  {
246  m_rep->mget_directory (directory, target);
247  }
248 
249  string_vector mput_directory (const std::string& base,
250  const std::string& directory)
251  {
252  return m_rep->mput_directory (base, directory);
253  }
254 
255  void dir (void) { m_rep->dir (); }
256 
257  string_vector list (void) { return m_rep->list (); }
258 
259  void get_fileinfo (const std::string& filename, double& filesize,
260  time_t& filetime, bool& fileisdir)
261  {
262  m_rep->get_fileinfo (filename, filesize, filetime, fileisdir);
263  }
264 
265  std::string pwd (void) { return m_rep->pwd (); }
266 
267  void http_get (const Array<std::string>& param)
268  {
269  m_rep->http_get (param);
270  }
271 
272  void http_post (const Array<std::string>& param)
273  {
274  m_rep->http_post (param);
275  }
276 
277  void http_action (const Array<std::string>& param,
278  const std::string& action)
279  {
280  m_rep->http_action (param, action);
281  }
282 
283  void cookie_jar (const std::string& filename)
284  {
285  m_rep->cookie_jar (filename);
286  }
287 
289  {
290  m_rep->set_header_fields (param);
291  }
292 
293  void form_data_post (const Array<std::string>& param)
294  {
295  m_rep->form_data_post (param);
296  }
297 
298  void set_weboptions (const struct weboptions& param)
299  {
300  m_rep->set_weboptions (param);
301  }
302 
303  private:
304 
305  std::shared_ptr<base_url_transfer> m_rep;
306  };
307 }
308 
309 #endif
static void delete_file(const std::string &file)
Definition: __ftp__.cc:56
N Dimensional Array with copy-on-write semantics.
Definition: Array.h:128
virtual void http_get(const Array< std::string > &)
Definition: url-transfer.h:149
virtual void http_action(const Array< std::string > &, const std::string &)
Definition: url-transfer.h:153
virtual void mkdir(const std::string &)
Definition: url-transfer.h:121
virtual void del(const std::string &)
Definition: url-transfer.h:117
static void reset_path(base_url_transfer *curl_xfer)
Definition: url-transfer.h:63
virtual void binary(void)
Definition: url-transfer.h:109
virtual std::ostream & set_ostream(std::ostream &)
Definition: url-transfer.h:97
virtual void ascii(void)
Definition: url-transfer.h:107
virtual void rename(const std::string &, const std::string &)
Definition: url-transfer.h:123
virtual std::string lasterror(void) const
Definition: url-transfer.h:95
bool is_valid(void) const
Definition: url-transfer.h:89
std::ostream * m_curr_ostream
Definition: url-transfer.h:174
bool is_binary(void) const
Definition: url-transfer.h:113
virtual void cookie_jar(const std::string &)
Definition: url-transfer.h:156
virtual std::string pwd(void)
Definition: url-transfer.h:147
virtual void put(const std::string &, std::istream &)
Definition: url-transfer.h:126
virtual void set_weboptions(const struct weboptions &)
Definition: url-transfer.h:162
virtual void http_post(const Array< std::string > &)
Definition: url-transfer.h:151
base_url_transfer(const base_url_transfer &)=delete
std::istream * m_curr_istream
Definition: url-transfer.h:173
virtual void form_data_post(const Array< std::string > &)
Definition: url-transfer.h:160
virtual void get(const std::string &, std::ostream &)
Definition: url-transfer.h:129
virtual void perform(void)
Definition: url-transfer.h:93
virtual void cwd(const std::string &)
Definition: url-transfer.h:115
virtual void get_fileinfo(const std::string &, double &, time_t &, bool &)
Definition: url-transfer.h:142
bool is_ascii(void) const
Definition: url-transfer.h:111
virtual string_vector list(void)
Definition: url-transfer.h:140
virtual void dir(void)
Definition: url-transfer.h:138
virtual void rmdir(const std::string &)
Definition: url-transfer.h:119
virtual ~base_url_transfer(void)=default
virtual std::istream & set_istream(std::istream &)
Definition: url-transfer.h:102
virtual void set_header_fields(const Array< std::string > &)
Definition: url-transfer.h:158
bool good(void) const
Definition: url-transfer.h:91
void mget_directory(const std::string &directory, const std::string &target)
Definition: url-transfer.h:243
std::string lasterror(void) const
Definition: url-transfer.h:200
bool good(void) const
Definition: url-transfer.h:198
std::istream & set_istream(std::istream &is)
Definition: url-transfer.h:207
void cwd(const std::string &path)
Definition: url-transfer.h:220
string_vector mput_directory(const std::string &base, const std::string &directory)
Definition: url-transfer.h:249
void set_header_fields(const Array< std::string > &param)
Definition: url-transfer.h:288
void http_get(const Array< std::string > &param)
Definition: url-transfer.h:267
bool is_binary(void) const
Definition: url-transfer.h:218
void form_data_post(const Array< std::string > &param)
Definition: url-transfer.h:293
string_vector list(void)
Definition: url-transfer.h:257
std::shared_ptr< base_url_transfer > m_rep
Definition: url-transfer.h:305
void rename(const std::string &oldname, const std::string &newname)
Definition: url-transfer.h:228
void http_action(const Array< std::string > &param, const std::string &action)
Definition: url-transfer.h:277
bool is_valid(void) const
Definition: url-transfer.h:196
void mkdir(const std::string &path)
Definition: url-transfer.h:226
bool is_ascii(void) const
Definition: url-transfer.h:216
void rmdir(const std::string &path)
Definition: url-transfer.h:224
std::ostream & set_ostream(std::ostream &os)
Definition: url-transfer.h:202
void put(const std::string &file, std::istream &is)
Definition: url-transfer.h:233
~url_transfer(void)=default
std::string pwd(void)
Definition: url-transfer.h:265
void cookie_jar(const std::string &filename)
Definition: url-transfer.h:283
void del(const std::string &file)
Definition: url-transfer.h:222
void get(const std::string &file, std::ostream &os)
Definition: url-transfer.h:238
void http_post(const Array< std::string > &param)
Definition: url-transfer.h:272
void get_fileinfo(const std::string &filename, double &filesize, time_t &filetime, bool &fileisdir)
Definition: url-transfer.h:259
void set_weboptions(const struct weboptions &param)
Definition: url-transfer.h:298
url_transfer(const url_transfer &)=default
QString path
std::string RequestMethod
Definition: url-transfer.h:50
std::string UserAgent
Definition: url-transfer.h:44
std::string Password
Definition: url-transfer.h:47
std::string ArrayFormat
Definition: url-transfer.h:51
std::string ContentReader
Definition: url-transfer.h:49
std::string Username
Definition: url-transfer.h:46
Array< std::string > HeaderFields
Definition: url-transfer.h:48
std::string CertificateFilename
Definition: url-transfer.h:52