DSL
download.h
1 //@AUTOHEADER@BEGIN@
2 /***********************************************************************\
3 | Drift Standard Libraries v1.01 |
4 | Copyright 2010-2023 Drift Solutions / Indy Sams |
5 | Docs and more information available at https://www.driftsolutions.dev |
6 | This file released under the 3-clause BSD license, |
7 | see included DSL.LICENSE.TXT file for details. |
8 \***********************************************************************/
9 //@AUTOHEADER@END@
10 
11 #ifndef __DSL_DOWNLOAD_H__
12 #define __DSL_DOWNLOAD_H__
13 
14 #include <drift/rwops.h>
15 #include <drift/sockets3.h>
16 
31 typedef bool (*DSL_Download_Callback)(uint64 got, uint64 fullsize, void * user_ptr);
32 
36 enum DSL_Download_Errors : uint8 {
37  TD_NO_ERROR,
38 
39  TD_FILE_ACCESS,
40  TD_ERROR_CONNECTING,
41  TD_INVALID_RESPONSE,
42  TD_ERROR_CREATING_SOCKET,
43  TD_ERROR_INITIALIZING_LIB,
44 
45  TD_FILE_WRITE_ERROR,
46  TD_INVALID_URL,
47  TD_CALLBACK_ABORT, // when the callback returns false, Download returns false and sets error to this
48  TD_INVALID_PROTOCOL,
49  TD_FILE_NOT_FOUND,
50  TD_REDIRECT,
51  TD_TIMEOUT,
52  TD_BAD_USER_PASS,
53  TD_NUM_ERRORS
54 };
55 
61 class DSL_API_CLASS DSL_Download_Base {
62 protected:
63  DSL_Download_Errors error = TD_INVALID_PROTOCOL;
64 public:
65 #ifndef DOXYGEN_SKIP
66  virtual ~DSL_Download_Base();
67 #endif
68 
69  virtual bool SetURL(const string& url) = 0;
70  virtual void SetCallback(DSL_Download_Callback callback, void * user_ptr = NULL) = 0;
71  virtual void SetUserPass(const string& user, const string& pass) = 0;
72 
73  virtual bool Download(const string& SaveAs);
74  virtual bool Download(FILE * fWriteTo);
79  virtual bool Download(DSL_FILE * fWriteTo) = 0;
86  virtual void SetTimeout(uint32 millisec) = 0;
87  virtual void SetUserAgent(const string& ua) = 0;
88  virtual void FollowRedirects(bool follow) = 0;
89 
90  virtual DSL_Download_Errors GetError();
91  virtual const char * GetErrorString();
92 };
93 
94 class DSL_API_CLASS DSL_Download_NoCurl: public DSL_Download_Base {
95 private:
96  string host, path, user_agent;
97  uint16 port = 0;
98  bool followRedirects = false;
99  uint32 timeo = 0;
100  string user, pass;
101  DSL_Sockets3_Base * socks = NULL;
102  DSL_Download_Callback callback = NULL;
103  void * u_ptr = NULL;
104 public:
105  DSL_Download_NoCurl(const string& url = "", DSL_Download_Callback callback = NULL, const string& user = "", const string& pass = "", void * user_ptr = NULL);
106  virtual ~DSL_Download_NoCurl();
107 
108  virtual bool SetURL(const string& url);
109  virtual bool GetURL(string& str);
110  virtual void SetCallback(DSL_Download_Callback callback, void * user_ptr = NULL);
111  virtual void SetUserPass(const string& user, const string& pass);
112 
117  virtual bool Download(DSL_FILE * fWriteTo);
118  virtual bool Download(FILE * fWriteTo) { return DSL_Download_Base::Download(fWriteTo); }
119  virtual bool Download(const string& fSaveAs) { return DSL_Download_Base::Download(fSaveAs); }
120 
121  virtual void SetTimeout(uint32 millisec);
122  virtual void SetUserAgent(const string& ua);
123  virtual void FollowRedirects(bool follow = true);
124 };
125 
126 #if defined(ENABLE_CURL) || defined(DOXYGEN_SKIP)
127 #include <drift/curl/curl.h>
128 #define DSL_Download DSL_Download_Curl
129 #else
130 #define DSL_Download DSL_Download_NoCurl
131 #endif
132 
135 #endif // __DSL_DOWNLOAD_H__
virtual bool Download(DSL_FILE *fWriteTo)=0
virtual void SetTimeout(uint32 millisec)=0
virtual bool Download(const string &SaveAs)
Begin download saving to file SaveAs.
Definition: download.cpp:46
virtual bool Download(FILE *fWriteTo)
Begin download saving to FILE stream.
Definition: download.h:118
virtual bool Download(const string &fSaveAs)
Begin download saving to file SaveAs.
Definition: download.h:119
bool(* DSL_Download_Callback)(uint64 got, uint64 fullsize, void *user_ptr)
Definition: download.h:31
DSL_Download_Errors
Definition: download.h:36
Definition: rwops.h:24