OpenClonk
C4HTTP.h
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 2017, The OpenClonk Team and contributors
5  *
6  * Distributed under the terms of the ISC license; see accompanying file
7  * "COPYING" for details.
8  *
9  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
10  * See accompanying file "TRADEMARK" for details.
11  *
12  * To redistribute this file separately, substitute the full license texts
13  * for the above references.
14  */
15 #ifndef C4HTTP_H
16 #define C4HTTP_H
17 
18 #include "platform/StdScheduler.h"
19 #include "network/C4NetIO.h"
20 
21 #include <map>
22 
23 #define CURL_STRICTER
24 #include <curl/curl.h>
25 // TODO: Replace the include with forward declaration once we stop supporting
26 // ancient curl that typedefs these to void
27 //typedef struct Curl_multi CURLM;
28 //typedef struct Curl_easy CURL;
29 
30 const int C4HTTPQueryTimeout = 10; // (s)
31 
32 // mini HTTP client
34 {
35 public:
36  C4HTTPClient();
37  ~C4HTTPClient() override;
38  bool Init();
39 
40 private:
41 
42  CURLM *MultiHandle{nullptr};
43  CURL *CurlHandle{nullptr};
44 
45 #ifdef STDSCHEDULER_USE_EVENTS
46  // event indicating network activity
47  HANDLE Event{nullptr};
48 #endif
49  std::map<SOCKET, int> sockets;
50 
51  // Address information
52  StdCopyStrBuf URL, ServerName;
53  C4NetIO::addr_t ServerAddr;
54  std::string headerAcceptedResponseType = "";
55 
56  StdCopyBuf RequestData;
57 
58  bool fBinary{false};
59  bool fSuccess{false};
60 
61  // Response header data
62  size_t iDownloadedSize{0}, iTotalSize{0};
63  bool fCompressed;
64 
65  // Event queue to use for notify when something happens
66  class C4InteractiveThread *pNotify{nullptr};
67 
68  // CURL callbacks
69  static size_t SWriteCallback(char *ptr, size_t size, size_t nmemb, void *userdata);
70  size_t WriteCallback(char *ptr, size_t realsize);
71  static int SProgressCallback(void *clientp, int64_t dltotal, int64_t dlnow, int64_t ultotal, int64_t ulnow);
72  int ProgressCallback(int64_t dltotal, int64_t dlnow, int64_t ultotal, int64_t ulnow);
73  static int SSocketCallback(CURL *easy, SOCKET s, int what, void *userp, void *socketp);
74  int SocketCallback(CURL *easy, SOCKET s, int what, void *socketp);
75 
76 protected:
77  StdCopyBuf ResultBin; // set if fBinary
78  StdCopyStrBuf ResultString; // set if !fBinary
80  void SetError(const char *strnError) { Error = strnError; }
81 
82 public:
83  bool Query(const StdBuf &Data, bool fBinary);
84  bool Query(const char *szData, bool fBinary) { return Query(StdBuf(szData, SLen(szData)), fBinary); }
85 
86  bool isBusy() const { return !!CurlHandle; }
87  bool isConnected() const { return iDownloadedSize + iTotalSize != 0; }
88  bool isSuccess() const { return fSuccess; }
89  size_t getTotalSize() const { return iTotalSize; }
90  size_t getDownloadedSize() const { return iDownloadedSize; }
91  const StdBuf &getResultBin() const { assert(fBinary); return ResultBin; }
92  const char *getResultString() const { assert(!fBinary); return ResultString.getData(); }
93  const char *getURL() const { return URL.getData(); }
94  const char *getServerName() const { return ServerName.getData(); }
95  const C4NetIO::addr_t &getServerAddress() const { return ServerAddr; }
96  virtual const char *GetError() const { return Error.getData(); }
97  void ResetError() { Error.Clear(); }
98 
99  void Cancel(const char *szReason);
100  void Clear();
101 
102  bool SetServer(const char *szServerAddress);
105 
106  void SetNotify(class C4InteractiveThread *pnNotify) { pNotify = pnNotify; }
107 
108  // StdScheduler interface
109  bool Execute(int iMaxTime = -1, pollfd * readyfds = nullptr) override;
111 #ifdef STDSCHEDULER_USE_EVENTS
112  HANDLE GetEvent() override { return Event; }
113 #else
114  void GetFDs(std::vector<struct pollfd> &) override;
115 #endif
116 
117 };
118 
119 #endif
#define s
const int C4HTTPQueryTimeout
Definition: C4HTTP.h:30
#define SOCKET
Definition: C4NetIO.h:35
size_t SLen(const char *sptr)
Definition: Standard.h:74
StdCopyStrBuf Error
Definition: C4HTTP.h:79
void SetError(const char *strnError)
Definition: C4HTTP.h:80
void SetExpectedResponseType(ResponseType type)
Definition: C4HTTP.cpp:315
bool isSuccess() const
Definition: C4HTTP.h:88
size_t getTotalSize() const
Definition: C4HTTP.h:89
bool Execute(int iMaxTime=-1, pollfd *readyfds=nullptr) override
Definition: C4HTTP.cpp:59
bool Init()
Definition: C4HTTP.cpp:42
void ResetError()
Definition: C4HTTP.h:97
virtual const char * GetError() const
Definition: C4HTTP.h:96
bool SetServer(const char *szServerAddress)
Definition: C4HTTP.cpp:298
bool Query(const StdBuf &Data, bool fBinary)
Definition: C4HTTP.cpp:158
const C4NetIO::addr_t & getServerAddress() const
Definition: C4HTTP.h:95
void GetFDs(std::vector< struct pollfd > &) override
Definition: C4HTTP.cpp:135
~C4HTTPClient() override
Definition: C4HTTP.cpp:31
bool Query(const char *szData, bool fBinary)
Definition: C4HTTP.h:84
bool isConnected() const
Definition: C4HTTP.h:87
void Clear()
Definition: C4HTTP.cpp:290
const char * getResultString() const
Definition: C4HTTP.h:92
C4TimeMilliseconds GetNextTick(C4TimeMilliseconds tNow) override
Definition: C4HTTP.cpp:125
@ NoPreference
Definition: C4HTTP.h:103
size_t getDownloadedSize() const
Definition: C4HTTP.h:90
const StdBuf & getResultBin() const
Definition: C4HTTP.h:91
StdCopyStrBuf ResultString
Definition: C4HTTP.h:78
StdCopyBuf ResultBin
Definition: C4HTTP.h:77
const char * getURL() const
Definition: C4HTTP.h:93
const char * getServerName() const
Definition: C4HTTP.h:94
void SetNotify(class C4InteractiveThread *pnNotify)
Definition: C4HTTP.h:106
bool isBusy() const
Definition: C4HTTP.h:86
void Cancel(const char *szReason)
Definition: C4HTTP.cpp:277
C4HTTPClient()
Definition: C4HTTP.cpp:27
Definition: StdBuf.h:30
const char * getData() const
Definition: StdBuf.h:442
void Clear()
Definition: StdBuf.h:466