GNU Octave  9.1.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-2024 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 "Array-fwd.h"
37 #include "str-vec.h"
38 
40 
41 struct weboptions
42 {
43  std::string UserAgent;
44  long Timeout;
45  std::string Username;
46  std::string Password;
48  std::string ContentReader;
49  std::string RequestMethod;
50  std::string ArrayFormat;
51  std::string CertificateFilename;
52 };
53 
54 class
57 {
58 public:
59 
60  friend class url_transfer;
61 
63 
64  base_url_transfer (const std::string& host,
65  const std::string& /* user_arg */,
66  const std::string& /* passwd */,
67  std::ostream& os);
68 
69  base_url_transfer (const std::string& url, std::ostream& os);
70 
71  OCTAVE_DISABLE_COPY_MOVE (base_url_transfer)
72 
73  virtual ~base_url_transfer () = default;
74 
75  bool is_valid () const { return m_valid; }
76 
77  bool good () const { return m_valid && m_ok; }
78 
79  virtual void perform () { }
80 
81  virtual std::string lasterror () const { return m_errmsg; }
82 
83  virtual std::ostream& set_ostream (std::ostream& /* os */)
84  {
85  return *m_curr_ostream;
86  }
87 
88  virtual std::istream& set_istream (std::istream& /* is */)
89  {
90  return *m_curr_istream;
91  }
92 
93  virtual void ascii () { }
94 
95  virtual void binary () { }
96 
97  bool is_ascii () const { return m_ascii_mode; }
98 
99  bool is_binary () const { return ! m_ascii_mode; }
100 
101  virtual void cwd (const std::string& /* path */) { }
102 
103  virtual void del (const std::string& /* file */) { }
104 
105  virtual void rmdir (const std::string& /* path */) { }
106 
107  virtual void mkdir (const std::string& /* path */) { }
108 
109  virtual void rename (const std::string& /* oldname */,
110  const std::string& /* newname */) { }
111 
112  virtual void put (const std::string& /* file */,
113  std::istream& /* is */) { }
114 
115  virtual void get (const std::string& /* file */,
116  std::ostream& /* os */) { }
117 
118  void mget_directory (const std::string& directory,
119  const std::string& target);
120 
121  string_vector mput_directory (const std::string& base,
122  const std::string& directory);
123 
124  virtual void dir () { }
125 
126  virtual string_vector list () { return string_vector (); }
127 
128  virtual void get_fileinfo (const std::string& /* filename */,
129  double& /* filesize */,
130  OCTAVE_TIME_T& /* filetime */,
131  bool& /* fileisdir */) { }
132 
133  virtual std::string pwd () { return ""; }
134 
135  virtual void http_get (const Array<std::string>& /* param */) { }
136 
137  virtual void http_post (const Array<std::string>& /* param */) { }
138 
139  virtual void http_action (const Array<std::string>& /* param */,
140  const std::string& /* action */) { }
141 
142  virtual void cookie_jar (const std::string& /* filename */) { }
143 
144  virtual void set_header_fields (const Array<std::string>& /* param */) { }
145 
146  virtual void form_data_post (const Array<std::string>& /* param */) { }
147 
148  virtual void set_weboptions (const struct weboptions& /* param */) { }
149 
150 protected:
151 
152  // Host for ftp transfers or full URL for http requests.
153  std::string m_host_or_url;
154  bool m_valid;
155  bool m_ftp;
157  bool m_ok;
158  std::string m_errmsg;
159  std::istream *m_curr_istream;
160  std::ostream *m_curr_ostream;
161 };
162 
163 class
166 {
167 public:
168 
169  url_transfer ();
170 
171  url_transfer (const std::string& host, const std::string& user,
172  const std::string& passwd, std::ostream& os);
173 
174  url_transfer (const std::string& url, std::ostream& os);
175 
176  url_transfer (const url_transfer&) = default;
177 
179 
180  ~url_transfer () = default;
181 
182  bool is_valid () const { return m_rep->is_valid (); }
183 
184  bool good () const { return m_rep->good (); }
185 
186  std::string lasterror () const { return m_rep->lasterror (); }
187 
188  std::ostream& set_ostream (std::ostream& os)
189  {
190  return m_rep->set_ostream (os);
191  }
192 
193  std::istream& set_istream (std::istream& is)
194  {
195  return m_rep->set_istream (is);
196  }
197 
198  void ascii () { m_rep->ascii (); }
199 
200  void binary () { m_rep->binary (); }
201 
202  bool is_ascii () const { return m_rep->is_ascii (); }
203 
204  bool is_binary () const { return m_rep->is_binary (); }
205 
206  void cwd (const std::string& path) { m_rep->cwd (path); }
207 
208  void del (const std::string& file) { m_rep->del (file); }
209 
210  void rmdir (const std::string& path) { m_rep->rmdir (path); }
211 
212  void mkdir (const std::string& path) { m_rep->mkdir (path); }
213 
214  void rename (const std::string& oldname, const std::string& newname)
215  {
216  m_rep->rename (oldname, newname);
217  }
218 
219  void put (const std::string& file, std::istream& is)
220  {
221  m_rep->put (file, is);
222  }
223 
224  void get (const std::string& file, std::ostream& os)
225  {
226  m_rep->get (file, os);
227  }
228 
229  void mget_directory (const std::string& directory,
230  const std::string& target)
231  {
232  m_rep->mget_directory (directory, target);
233  }
234 
235  string_vector mput_directory (const std::string& base,
236  const std::string& directory)
237  {
238  return m_rep->mput_directory (base, directory);
239  }
240 
241  void dir () { m_rep->dir (); }
242 
243  string_vector list () { return m_rep->list (); }
244 
245  void get_fileinfo (const std::string& filename, double& filesize,
246  OCTAVE_TIME_T& filetime, bool& fileisdir)
247  {
248  m_rep->get_fileinfo (filename, filesize, filetime, fileisdir);
249  }
250 
251  std::string pwd () { return m_rep->pwd (); }
252 
253  void http_get (const Array<std::string>& param)
254  {
255  m_rep->http_get (param);
256  }
257 
258  void http_post (const Array<std::string>& param)
259  {
260  m_rep->http_post (param);
261  }
262 
263  void http_action (const Array<std::string>& param,
264  const std::string& action)
265  {
266  m_rep->http_action (param, action);
267  }
268 
269  void cookie_jar (const std::string& filename)
270  {
271  m_rep->cookie_jar (filename);
272  }
273 
275  {
276  m_rep->set_header_fields (param);
277  }
278 
279  void form_data_post (const Array<std::string>& param)
280  {
281  m_rep->form_data_post (param);
282  }
283 
284  void set_weboptions (const struct weboptions& param)
285  {
286  m_rep->set_weboptions (param);
287  }
288 
289 private:
290 
291  std::shared_ptr<base_url_transfer> m_rep;
292 };
293 
294 OCTAVE_END_NAMESPACE(octave)
295 
296 #endif
virtual void http_get(const Array< std::string > &)
Definition: url-transfer.h:135
virtual void rename(const std::string &, const std::string &)
Definition: url-transfer.h:109
std::string m_host_or_url
Definition: url-transfer.h:153
bool good() const
Definition: url-transfer.h:77
virtual void dir()
Definition: url-transfer.h:124
virtual void form_data_post(const Array< std::string > &)
Definition: url-transfer.h:146
bool is_binary() const
Definition: url-transfer.h:99
std::string m_errmsg
Definition: url-transfer.h:158
virtual void get(const std::string &, std::ostream &)
Definition: url-transfer.h:115
virtual void ascii()
Definition: url-transfer.h:93
virtual void set_weboptions(const struct weboptions &)
Definition: url-transfer.h:148
virtual void binary()
Definition: url-transfer.h:95
virtual void put(const std::string &, std::istream &)
Definition: url-transfer.h:112
virtual void perform()
Definition: url-transfer.h:79
virtual void set_header_fields(const Array< std::string > &)
Definition: url-transfer.h:144
virtual void cwd(const std::string &)
Definition: url-transfer.h:101
virtual std::string pwd()
Definition: url-transfer.h:133
virtual void cookie_jar(const std::string &)
Definition: url-transfer.h:142
virtual void del(const std::string &)
Definition: url-transfer.h:103
std::istream * m_curr_istream
Definition: url-transfer.h:159
virtual string_vector list()
Definition: url-transfer.h:126
virtual std::istream & set_istream(std::istream &)
Definition: url-transfer.h:88
bool is_ascii() const
Definition: url-transfer.h:97
virtual void http_post(const Array< std::string > &)
Definition: url-transfer.h:137
virtual void get_fileinfo(const std::string &, double &, OCTAVE_TIME_T &, bool &)
Definition: url-transfer.h:128
virtual std::ostream & set_ostream(std::ostream &)
Definition: url-transfer.h:83
std::ostream * m_curr_ostream
Definition: url-transfer.h:160
virtual void http_action(const Array< std::string > &, const std::string &)
Definition: url-transfer.h:139
virtual void rmdir(const std::string &)
Definition: url-transfer.h:105
virtual void mkdir(const std::string &)
Definition: url-transfer.h:107
virtual std::string lasterror() const
Definition: url-transfer.h:81
void put(const std::string &file, std::istream &is)
Definition: url-transfer.h:219
void get_fileinfo(const std::string &filename, double &filesize, OCTAVE_TIME_T &filetime, bool &fileisdir)
Definition: url-transfer.h:245
std::ostream & set_ostream(std::ostream &os)
Definition: url-transfer.h:188
string_vector list()
Definition: url-transfer.h:243
~url_transfer()=default
void get(const std::string &file, std::ostream &os)
Definition: url-transfer.h:224
bool is_ascii() const
Definition: url-transfer.h:202
void http_post(const Array< std::string > &param)
Definition: url-transfer.h:258
void cwd(const std::string &path)
Definition: url-transfer.h:206
void set_header_fields(const Array< std::string > &param)
Definition: url-transfer.h:274
void set_weboptions(const struct weboptions &param)
Definition: url-transfer.h:284
std::istream & set_istream(std::istream &is)
Definition: url-transfer.h:193
bool is_valid() const
Definition: url-transfer.h:182
void http_get(const Array< std::string > &param)
Definition: url-transfer.h:253
void mkdir(const std::string &path)
Definition: url-transfer.h:212
string_vector mput_directory(const std::string &base, const std::string &directory)
Definition: url-transfer.h:235
std::string lasterror() const
Definition: url-transfer.h:186
void http_action(const Array< std::string > &param, const std::string &action)
Definition: url-transfer.h:263
url_transfer & operator=(const url_transfer &)=default
void mget_directory(const std::string &directory, const std::string &target)
Definition: url-transfer.h:229
std::string pwd()
Definition: url-transfer.h:251
url_transfer(const url_transfer &)=default
void cookie_jar(const std::string &filename)
Definition: url-transfer.h:269
void rmdir(const std::string &path)
Definition: url-transfer.h:210
bool is_binary() const
Definition: url-transfer.h:204
void del(const std::string &file)
Definition: url-transfer.h:208
void rename(const std::string &oldname, const std::string &newname)
Definition: url-transfer.h:214
bool good() const
Definition: url-transfer.h:184
void form_data_post(const Array< std::string > &param)
Definition: url-transfer.h:279
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
#define OCTAVE_API
Definition: main.cc:55
std::string UserAgent
Definition: url-transfer.h:43
std::string ArrayFormat
Definition: url-transfer.h:50
std::string ContentReader
Definition: url-transfer.h:48
std::string RequestMethod
Definition: url-transfer.h:49
Array< std::string > HeaderFields
Definition: url-transfer.h:47
std::string Password
Definition: url-transfer.h:46
long Timeout
Definition: url-transfer.h:44
std::string CertificateFilename
Definition: url-transfer.h:51
std::string Username
Definition: url-transfer.h:45