SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
HttpUtils.h
Go to the documentation of this file.
1 /**
2  * \file HttpUtils.h
3  * \date 2020
4  * \authors Luc Girod
5  * \copyright http://opensource.org/licenses/GPL-3.0
6  * \remarks Please use clangformat to format the code. See more code style on
7  * https://github.com/cpvrlab/SLProject4/wiki/SLProject-Coding-Style
8 */
9 
10 #ifndef HTTP_UTILS_H
11 #define HTTP_UTILS_H
12 
13 #ifdef SL_BUILD_WITH_OPENSSL
14 
15 # ifdef _WINDOWS
16 # include <winsock2.h>
17 # include <ws2tcpip.h>
18 # else
19 # include <sys/socket.h>
20 # include <netinet/in.h>
21 # include <arpa/inet.h>
22 # endif
23 
24 # include <vector>
25 # include <string>
26 # include <openssl/ssl.h>
27 # include <openssl/err.h>
28 # include <functional>
29 # include <atomic>
30 
31 using std::function;
32 using std::string;
33 using std::vector;
34 
35 # define SERVER_NOT_REACHABLE 1
36 # define CANT_CREATE_DIR 2
37 # define CANT_CREATE_FILE 3
38 # define CONNECTION_CLOSED 4
39 
40 //------------------------------------------------------------------------------
41 //! Multiplatform socket helper
42 class Socket
43 {
44 public:
45  int fd;
46  union
47  {
48  struct sockaddr_in sa;
49  struct sockaddr_in6 sa6;
50  };
51 
52  socklen_t addrlen;
53 # ifdef _WINDOWS
54  static WSADATA wsadata;
55  static bool initialized;
56 # endif
57 
58  static bool SocketEnable()
59  {
60 # ifdef _WINDOWS
61  int ret;
62  if (!initialized)
63  {
64  ret = WSAStartup(MAKEWORD(2, 2), &wsadata);
65  if (ret == 0)
66  initialized = true;
67  else
68  return false;
69  }
70 # endif
71  return true;
72  }
73 
74  static void SocketDisable()
75  {
76 # ifdef _WINDOWS
77  if (initialized)
78  {
79  initialized = false;
80  WSACleanup();
81  }
82 # endif
83  }
84 
85  Socket() { reset(); }
86  virtual ~Socket() = default;
87 
88  virtual void reset();
89  virtual int connectTo(string ip, int port);
90  virtual int sendData(const char* data, size_t size);
91  virtual int receive(function<int(char* data, int size)> dataCB, int max = 0);
92  virtual void disconnect();
93  void interrupt() { _interrupt = true; };
94 
95 protected:
96  std::atomic_bool _interrupt{false};
97 };
98 //------------------------------------------------------------------------------
99 //! Multiplatform socket helper with encryption
100 class SecureSocket : public Socket
101 {
102 public:
103  SecureSocket()
104  {
105  ssl = nullptr;
106  Socket::reset();
107  }
108 
109  SSL* ssl;
110 
111  virtual int connectTo(string ip, int port);
112  virtual int sendData(const char* data, size_t size);
113  virtual int receive(function<int(char* data, int size)> dataCB, int max = 0);
114  virtual void disconnect();
115 };
116 //------------------------------------------------------------------------------
117 //! helper struct to get DNS from ip and ip from DNS
118 struct DNSRequest
119 {
120  string addr;
121  string hostname;
122  DNSRequest(string host);
123  string getAddr();
124  string getHostname();
125 };
126 //------------------------------------------------------------------------------
127 //! HttpUtils provides networking functionality via the HTTP and HTTPS protocols
128 namespace HttpUtils
129 {
130 //! Class to make http get request
131 class GetRequest
132 {
133 public:
134  Socket* s;
135  vector<char> firstBytes;
136  int contentOffset;
137 
138  string request;
139  string host;
140  string addr;
141  int port;
142 
143  string headers;
144  string version;
145  string status;
146  string contentType;
147  size_t contentLength;
148 
149  GetRequest(string url, string user = "", string pwd = "");
150  ~GetRequest()
151  {
152  if (s) { delete s; }
153  }
154 
155  // Read http header
156  int processHttpHeaders(vector<char>& data);
157 
158  // Send Http request to server
159  int send();
160 
161  // Start fetching the content (must be called after send)
162  int getContent(function<int(char* data, int size)> contentCB);
163 
164  // Parse http listing and return list of directory and files
165  vector<string> getListing();
166 };
167 //------------------------------------------------------------------------------
168 //! HTTPUtils::download provides download function for https/http with/without auth.
169 
170 // Download a file chunk by chunk of 1kB.
171 // download is interrupt if any of the callback return non-zero value.
172 /*!
173  * \param url url to the file / listing to download
174  * \param processFile A callback which is called when a new file will be downloaded.
175  * \param writeChunk A callback which is called for every new chunk of the current
176  * file being downloaded.
177  * \param processDir A callback which is called when a new directory is being downloaded.
178  * \param user Username (optional) when site require http auth.
179  * \param pwd Password (optional) when site require http auth.
180  * \param base Path where the files should be saved.
181  */
182 int download(string url,
183  function<int(string path, string file, size_t size)> processFile,
184  function<int(char* data, int size)> writeChunk,
185  function<int(string)> processDir,
186  string user = "",
187  string pwd = "",
188  string base = "./");
189 //------------------------------------------------------------------------------
190 //! HTTP download function with login credentials
191 // download is interrupt if progress callback return non zero value.
192 /*!
193  * \param url url to the file / listing to download
194  * \param dst Path where the files should be saved.
195  * \param user Username (optional) when site require http auth.
196  * \param pwd Password (optional) when site require http auth.
197  * \param progress A callback which is called each 1kB downloaded for the current file.
198  * The download stop if the returned value is not zero.
199  */
200 int download(string url,
201  string dst,
202  string user,
203  string pwd,
204  function<int(size_t curr, size_t filesize)> progress = nullptr);
205 //------------------------------------------------------------------------------
206 //! HTTP download function without login credentials
207 int download(string url,
208  string dst,
209  function<int(size_t curr, size_t filesize)> progress = nullptr);
210 
211 //-- return content Length of the HttpGet request
212 int length(string url, string user = "", string pwd = "");
213 
214 }; // namespace HttpUtils
215 //------------------------------------------------------------------------------
216 
217 #endif // SL_BUILD_WITH_OPENSSL
218 #endif // HTTP_UTILS_H
SLScene * s
Definition: SLScene.h:31
The SLScene class represents the top level instance holding the scene structure.
Definition: SLScene.h:47