OpenZWave Library  1.6.1914
HttpClient.h
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2 //
3 // HttpClient.h
4 //
5 // Cross-platform HttpClient
6 //
7 // Originally based upon minihttp client
8 // Copyright (c) 2016 Justin Hammond <Justin@dynam.ac>
9 //
10 // SOFTWARE NOTICE AND LICENSE
11 //
12 // This file is part of OpenZWave.
13 //
14 // OpenZWave is free software: you can redistribute it and/or modify
15 // it under the terms of the GNU Lesser General Public License as published
16 // by the Free Software Foundation, either version 3 of the License,
17 // or (at your option) any later version.
18 //
19 // OpenZWave is distributed in the hope that it will be useful,
20 // but WITHOUT ANY WARRANTY; without even the implied warranty of
21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 // GNU Lesser General Public License for more details.
23 //
24 // You should have received a copy of the GNU Lesser General Public License
25 // along with OpenZWave. If not, see <http://www.gnu.org/licenses/>.
26 //
27 //-----------------------------------------------------------------------------
28 
29 // Original License Text:
30 /* This program is free software. It comes without any warranty, to
31  * the extent permitted by applicable law. You can redistribute it
32  * and/or modify it under the terms of the Do What The Fuck You Want
33  * To Public License, Version 2, as published by Sam Hocevar.
34  * See http://sam.zoy.org/wtfpl/COPYING for more details. */
35 
36 #ifndef MINIHTTPSOCKET_H
37 #define MINIHTTPSOCKET_H
38 
39 // ---- Compile config -----
40 #define MINIHTTP_SUPPORT_HTTP
41 #define MINIHTTP_SUPPORT_SOCKET_SET
42 // -------------------------
43 
44 // Intentionally avoid pulling in any other headers
45 
46 #include <string>
47 #include <map>
48 #include <queue>
49 
50 namespace OpenZWave
51 {
57  namespace Internal
58  {
59  namespace Platform
60  {
68  bool InitNetwork();
69 
75  void StopNetwork();
76 
85  bool HasSSL();
86 
99  bool SplitURI(const std::string& uri, std::string& host, std::string& file, int& port);
100 
110  void URLEncode(const std::string& s, std::string& enc);
111 
117  {
118  SSLR_OK = 0x0,
119  SSLR_NO_SSL = 0x1,
120  SSLR_FAIL = 0x2,
129  _SSLR_FORCE32BIT = 0x7fffffff
130  };
131 
138  class TcpSocket
139  {
140  public:
141  TcpSocket();
142  virtual ~TcpSocket();
143 
144  virtual bool HasPendingTask() const
145  {
146  return false;
147  }
148 
149  bool open(const char *addr = NULL, unsigned int port = 0);
150  void close();
151  bool update(); // returns true if something interesting happened (incoming data, closed connection, etc)
152 
153  bool isOpen(void);
154 
155  void SetBufsizeIn(unsigned int s);
156  bool SetNonBlocking(bool nonblock);
157  unsigned int GetBufSize()
158  {
159  return _inbufSize;
160  }
161  const char *GetHost(void)
162  {
163  return _host.c_str();
164  }
165  bool SendBytes(const void *buf, unsigned int len);
166 
167  // SSL related
168  bool initSSL(const char *certs);
169  bool hasSSL() const
170  {
171  return !!_sslctx;
172  }
173  void shutdownSSL();
175 
176  protected:
177  virtual void _OnCloseInternal();
178  virtual void _OnData(); // data received callback. Internal, should only be overloaded to call _OnRecv()
179 
180  virtual void _OnRecv(void *buf, unsigned int size) = 0;
181  virtual void _OnClose()
182  {
183  }
184  ; // close callback
185  virtual void _OnOpen()
186  {
187  } // called when opened
188  virtual bool _OnUpdate()
189  {
190  return true;
191  } // called before reading from the socket
192 
193  void _ShiftBuffer();
194 
195  char *_inbuf;
196  char *_readptr; // part of inbuf, optionally skipped header
197  char *_writeptr; // passed to recv(). usually equal to _inbuf, but may point inside the buffer in case of a partial transfer.
198 
199  unsigned int _inbufSize; // size of internal buffer
200  unsigned int _writeSize; // how many bytes can be written to _writeptr;
201  unsigned int _recvSize; // incoming size, max _inbufSize - 1
202 
203  unsigned int _lastport; // port used in last open() call
204 
205  bool _nonblocking; // Default true. If false, the current thread is blocked while waiting for input.
206 
207 #ifdef _WIN32
208  intptr_t _s; // socket handle. really an int, but to be sure its 64 bit compatible as it seems required on windows, we use this.
209 #else
210  long _s;
211 #endif
212 
213  std::string _host;
214 
215  private:
216  int _writeBytes(const unsigned char *buf, size_t len);
217  int _readBytes(unsigned char *buf, size_t maxlen);
218  void *_sslctx;
219  };
220 
221 #ifdef MINIHTTP_SUPPORT_HTTP
222 
223  enum HttpCode
224  {
225  HTTP_OK = 200,
227  };
228 
235  class POST
236  {
237  public:
238  void reserve(size_t res)
239  {
240  data.reserve(res);
241  }
242  POST& add(const char *key, const char *value);
243  const char *c_str() const
244  {
245  return data.c_str();
246  }
247  const std::string& str() const
248  {
249  return data;
250  }
251  bool empty() const
252  {
253  return data.empty();
254  }
255  size_t length() const
256  {
257  return data.length();
258  }
259  private:
260  std::string data;
261  };
262 
269  struct Request
270  {
272  port(80), user(NULL)
273  {
274  }
275  Request(const std::string& h, const std::string& res, int p = 80, void *u = NULL) :
276  host(h), resource(res), port(80), user(u), useSSL(false)
277  {
278  }
279 
280  std::string protocol;
281  std::string host;
282  std::string header; // set by socket
283  std::string resource;
284  std::string extraGetHeaders;
285  int port;
286  void *user;
287  bool useSSL;
288  POST post; // if this is empty, it's a GET request, otherwise a POST request
289  };
290 
298  {
299  public:
300 
302  virtual ~HttpSocket();
303 
304  virtual bool HasPendingTask() const
305  {
306  return ExpectMoreData() || _requestQ.size();
307  }
308 
309  void SetKeepAlive(unsigned int secs)
310  {
311  _keep_alive = secs;
312  }
313  void SetUserAgent(const std::string &s)
314  {
315  _user_agent = s;
316  }
317  void SetAcceptEncoding(const std::string& s)
318  {
319  _accept_encoding = s;
320  }
321  void SetFollowRedirect(bool follow)
322  {
323  _followRedir = follow;
324  }
325  void SetAlwaysHandle(bool h)
326  {
327  _alwaysHandle = h;
328  }
329  void SetDownloadFile(std::string filename)
330  {
331  _filename = filename;
332  }
333 
334  bool Download(const std::string& url, const char *extraRequest = NULL, void *user = NULL, const POST *post = NULL);
335  bool SendRequest(Request& what, bool enqueue);
336  bool SendRequest(const std::string what, const char *extraRequest = NULL, void *user = NULL);
337  bool QueueRequest(const std::string what, const char *extraRequest = NULL, void *user = NULL);
338 
339  unsigned int GetRemaining() const
340  {
341  return _remaining;
342  }
343 
344  unsigned int GetStatusCode() const
345  {
346  return _status;
347  }
348  unsigned int GetContentLen() const
349  {
350  return _contentLen;
351  }
352  bool ChunkedTransfer() const
353  {
354  return _chunkedTransfer;
355  }
356  bool ExpectMoreData() const
357  {
358  return _remaining || _chunkedTransfer;
359  }
360 
361  const Request &GetCurrentRequest() const
362  {
363  return _curRequest;
364  }
365  const char *Hdr(const char *h) const;
366 
367  bool IsRedirecting() const;
368  bool IsSuccess() const;
369 
370  protected:
371  virtual void _OnCloseInternal();
372  virtual void _OnClose();
373  virtual void _OnData(); // data received callback. Internal, should only be overloaded to call _OnRecv()
374  virtual void _OnRecv(void *buf, unsigned int size);
375  virtual void _OnOpen(); // called when opene
376  virtual bool _OnUpdate(); // called before reading from the socket
377 
378  // new ones:
379  virtual void _OnRequestDone()
380  {
381  }
382 
383  bool _Redirect(std::string loc, bool forceGET);
384 
386  bool _EnqueueOrSend(const Request& req, bool forceQueue = false);
387  void _DequeueMore();
388  bool _OpenRequest(const Request& req);
389  void _ParseHeader();
390  void _ParseHeaderFields(const char *s, size_t size);
391  bool _HandleStatus(); // Returns whether the processed request was successful, or not
393  void _OnRecvInternal(void *buf, unsigned int size);
394 
395  std::string _user_agent;
396  std::string _accept_encoding; // Default empty.
397  std::string _tmpHdr; // used to save the http header if the incoming buffer was not large enough
398 
399  unsigned int _keep_alive; // http related
400  unsigned int _remaining; // http "Content-Length: X" - already recvd. 0 if ready for next packet.
401  // For chunked transfer encoding, this holds the remaining size of the current chunk
402  unsigned int _contentLen; // as reported by server
403  unsigned int _status; // http status code, HTTP_OK if things are good
404 
405  std::queue<Request> _requestQ;
406  std::map<std::string, std::string> _hdrs; // Maps HTTP header fields to their values
407 
409 
412  bool _mustClose; // keep-alive specified, or not
413  bool _followRedir; // Default true. Follow 3xx redirects if this is set.
414  bool _alwaysHandle; // Also deliver to _OnRecv() if a non-success code was received.
415  std::string _filename;
416  FILE* _pFile;
417  };
418 
419 #endif
420 
421 // ------------------------------------------------------------------------
422 
423 #ifdef MINIHTTP_SUPPORT_SOCKET_SET
424 
431  class SocketSet
432  {
433  public:
434  virtual ~SocketSet();
435  void deleteAll();
436  bool update();
437  void add(TcpSocket *s, bool deleteWhenDone = true);
438  bool has(TcpSocket *s);
439  void remove(TcpSocket *s);
440  inline size_t size()
441  {
442  return _store.size();
443  }
444 
445 //protected:
446 
448  {
450  // To be extended
451  };
452 
453  typedef std::map<TcpSocket*, SocketSetData> Store;
454 
456  };
457 
458 #endif
459  } // namespace Platform
460  } // namespace Internal
461 } // namespace OpenZWave
462 
463 #endif
#define NULL
Definition: Defs.h:81
a Socket that speaks HTTP protocol.
Definition: HttpClient.h:298
FILE * _pFile
Definition: HttpClient.h:416
void SetFollowRedirect(bool follow)
Definition: HttpClient.h:321
unsigned int _keep_alive
Definition: HttpClient.h:399
void SetDownloadFile(std::string filename)
Definition: HttpClient.h:329
bool _mustClose
Definition: HttpClient.h:412
void SetKeepAlive(unsigned int secs)
Definition: HttpClient.h:309
bool _Redirect(std::string loc, bool forceGET)
bool _followRedir
Definition: HttpClient.h:413
unsigned int GetStatusCode() const
Definition: HttpClient.h:344
void _ParseHeaderFields(const char *s, size_t size)
void SetUserAgent(const std::string &s)
Definition: HttpClient.h:313
unsigned int _contentLen
Definition: HttpClient.h:402
bool _alwaysHandle
Definition: HttpClient.h:414
virtual bool HasPendingTask() const
Definition: HttpClient.h:304
virtual void _OnRecv(void *buf, unsigned int size)
bool _OpenRequest(const Request &req)
std::string _tmpHdr
Definition: HttpClient.h:397
bool ChunkedTransfer() const
Definition: HttpClient.h:352
bool Download(const std::string &url, const char *extraRequest=NULL, void *user=NULL, const POST *post=NULL)
bool QueueRequest(const std::string what, const char *extraRequest=NULL, void *user=NULL)
unsigned int GetRemaining() const
Definition: HttpClient.h:339
unsigned int GetContentLen() const
Definition: HttpClient.h:348
const Request & GetCurrentRequest() const
Definition: HttpClient.h:361
bool ExpectMoreData() const
Definition: HttpClient.h:356
const char * Hdr(const char *h) const
bool SendRequest(Request &what, bool enqueue)
bool _inProgress
Definition: HttpClient.h:410
std::queue< Request > _requestQ
Definition: HttpClient.h:405
unsigned int _status
Definition: HttpClient.h:403
void _OnRecvInternal(void *buf, unsigned int size)
std::string _filename
Definition: HttpClient.h:415
unsigned int _remaining
Definition: HttpClient.h:400
void SetAcceptEncoding(const std::string &s)
Definition: HttpClient.h:317
bool SendRequest(const std::string what, const char *extraRequest=NULL, void *user=NULL)
std::string _user_agent
Definition: HttpClient.h:395
virtual void _OnRequestDone()
Definition: HttpClient.h:379
Request _curRequest
Definition: HttpClient.h:408
void SetAlwaysHandle(bool h)
Definition: HttpClient.h:325
std::string _accept_encoding
Definition: HttpClient.h:396
bool _EnqueueOrSend(const Request &req, bool forceQueue=false)
std::map< std::string, std::string > _hdrs
Definition: HttpClient.h:406
bool _chunkedTransfer
Definition: HttpClient.h:411
This class is used for Posting data to a HTTP(s) server.
Definition: HttpClient.h:236
bool empty() const
Definition: HttpClient.h:251
POST & add(const char *key, const char *value)
const char * c_str() const
Definition: HttpClient.h:243
void reserve(size_t res)
Definition: HttpClient.h:238
size_t length() const
Definition: HttpClient.h:255
const std::string & str() const
Definition: HttpClient.h:247
Support Multiple TCP Socket connections.
Definition: HttpClient.h:432
std::map< TcpSocket *, SocketSetData > Store
Definition: HttpClient.h:453
size_t size()
Definition: HttpClient.h:440
Store _store
Definition: HttpClient.h:455
void add(TcpSocket *s, bool deleteWhenDone=true)
a TCP Socket that can optionally be protected via SSL
Definition: HttpClient.h:139
bool hasSSL() const
Definition: HttpClient.h:169
TcpSocket()
Definition: HttpClient.cpp:356
bool open(const char *addr=NULL, unsigned int port=0)
Definition: HttpClient.cpp:508
virtual bool _OnUpdate()
Definition: HttpClient.h:188
virtual void _OnOpen()
Definition: HttpClient.h:185
const char * GetHost(void)
Definition: HttpClient.h:161
virtual void _OnData()
Definition: HttpClient.cpp:746
void _ShiftBuffer()
Definition: HttpClient.cpp:737
std::string _host
Definition: HttpClient.h:213
virtual bool HasPendingTask() const
Definition: HttpClient.h:144
char * _writeptr
Definition: HttpClient.h:197
SSLResult verifySSL()
Definition: HttpClient.cpp:667
bool SetNonBlocking(bool nonblock)
Definition: HttpClient.cpp:404
unsigned int GetBufSize()
Definition: HttpClient.h:157
bool _nonblocking
Definition: HttpClient.h:205
void SetBufsizeIn(unsigned int s)
Definition: HttpClient.cpp:410
unsigned int _recvSize
Definition: HttpClient.h:201
bool initSSL(const char *certs)
Definition: HttpClient.cpp:661
virtual ~TcpSocket()
Definition: HttpClient.cpp:361
virtual void _OnClose()
Definition: HttpClient.h:181
virtual void _OnRecv(void *buf, unsigned int size)=0
void close()
Definition: HttpClient.cpp:373
char * _inbuf
Definition: HttpClient.h:195
bool update()
Definition: HttpClient.cpp:763
unsigned int _lastport
Definition: HttpClient.h:203
void shutdownSSL()
Definition: HttpClient.cpp:657
char * _readptr
Definition: HttpClient.h:196
virtual void _OnCloseInternal()
Definition: HttpClient.cpp:399
unsigned int _writeSize
Definition: HttpClient.h:200
bool SendBytes(const void *buf, unsigned int len)
Definition: HttpClient.cpp:673
unsigned int _inbufSize
Definition: HttpClient.h:199
long _s
Definition: HttpClient.h:210
bool isOpen(void)
Definition: HttpClient.cpp:368
bool HasSSL()
Indicates if we support HTTPS requests.
Definition: HttpClient.cpp:165
void StopNetwork()
Stop the Network for HTTP requests.
Definition: HttpClient.cpp:219
void URLEncode(const std::string &s, std::string &enc)
Encode a String suitable for sending as a URL request (eg Get)
Definition: HttpClient.cpp:308
SSLResult
Result Codes for SSL operations.
Definition: HttpClient.h:117
bool InitNetwork()
Initialize the Network for HTTP requests.
Definition: HttpClient.cpp:206
@ SSLR_NO_SSL
Definition: HttpClient.h:119
@ SSLR_FAIL
Definition: HttpClient.h:120
@ SSLR_CERT_MISSING
Definition: HttpClient.h:125
@ SSLR_CERT_CN_MISMATCH
Definition: HttpClient.h:123
@ _SSLR_FORCE32BIT
Definition: HttpClient.h:129
@ SSLR_CERT_EXPIRED
Definition: HttpClient.h:121
@ SSLR_OK
Definition: HttpClient.h:118
@ SSLR_CERT_REVOKED
Definition: HttpClient.h:122
@ SSLR_CERT_FUTURE
Definition: HttpClient.h:127
@ SSLR_CERT_SKIP_VERIFY
Definition: HttpClient.h:126
@ SSLR_CERT_NOT_TRUSTED
Definition: HttpClient.h:124
HttpCode
Definition: HttpClient.h:224
@ HTTP_NOTFOUND
Definition: HttpClient.h:226
@ HTTP_OK
Definition: HttpClient.h:225
bool SplitURI(const std::string &uri, std::string &protocol, std::string &host, std::string &file, int &port, bool &useSSL)
Definition: HttpClient.cpp:258
Definition: Bitfield.cpp:31
Main class for making a HTTP request to a HTTP(s) server.
Definition: HttpClient.h:270
bool useSSL
Definition: HttpClient.h:287
std::string host
Definition: HttpClient.h:281
int port
Definition: HttpClient.h:285
std::string extraGetHeaders
Definition: HttpClient.h:284
std::string protocol
Definition: HttpClient.h:280
std::string header
Definition: HttpClient.h:282
Request()
Definition: HttpClient.h:271
POST post
Definition: HttpClient.h:288
std::string resource
Definition: HttpClient.h:283
void * user
Definition: HttpClient.h:286
Request(const std::string &h, const std::string &res, int p=80, void *u=NULL)
Definition: HttpClient.h:275