Cuberite
A lightweight, fast and extensible game server for Minecraft
UrlClient.h
Go to the documentation of this file.
1 
2 // UrlClient.h
3 
4 // Declares the cUrlClient class for high-level URL interaction
5 
6 /*
7 Options that can be set via the Options parameter to the cUrlClient calls:
8 "MaxRedirects": The maximum number of allowed redirects before the client refuses a redirect with an error
9 "OwnCert": The client certificate to use, if requested by the server. Any string that can be parsed by cX509Cert.
10 "OwnPrivKey": The private key appropriate for OwnCert. Any string that can be parsed by cCryptoKey.
11 "OwnPrivKeyPassword": The password for OwnPrivKey. If not present or empty, no password is assumed.
12 "TrustedRootCAs": The trusted root CA certificates (\n-delimited concatenated PEM format) to be used for peer cert verification. If not present, peer cert is not verified.
13 
14 Behavior:
15 - If a redirect is received, and redirection is allowed, the redirection is reported via OnRedirecting() callback
16 and the request is restarted at the redirect URL, without reporting any of the redirect's headers nor body
17 - If a redirect is received and redirection is not allowed (maximum redirection attempts have been reached),
18 the OnRedirecting() callback is called with the redirect URL and then the request terminates with an OnError() callback,
19 without reporting the redirect's headers nor body.
20 */
21 
22 
23 
24 
25 
26 #pragma once
27 
28 #include "../OSSupport/Network.h"
29 
30 
31 
32 
33 
35 {
36 public:
38  class cCallbacks
39  {
40  public:
41  // Force a virtual destructor in descendants:
42  virtual ~cCallbacks() {}
43 
45  virtual void OnConnected(cTCPLink & a_Link) {}
46 
51  virtual bool OnCertificateReceived() { return true; }
52 
55  virtual void OnTlsHandshakeCompleted() { }
56 
58  virtual void OnRequestSent() {}
59 
61  virtual void OnStatusLine(const AString & a_HttpVersion, int a_StatusCode, const AString & a_Rest) {}
62 
65  virtual void OnHeader(const AString & a_Key, const AString & a_Value) {}
66 
69  virtual void OnHeadersFinished() {}
70 
73  virtual void OnBodyData(const void * a_Data, size_t a_Size) {}
74 
77  virtual void OnBodyFinished() {}
78 
80  virtual void OnError(const AString & a_ErrorMsg) {}
81 
88  virtual void OnRedirecting(const AString & a_NewLocation) {}
89  };
90  using cCallbacksPtr = std::shared_ptr<cCallbacks>;
91 
92 
95  {
97  HTTP_STATUS_MULTIPLE_CHOICES = 300, // MAY have a redirect using the "Location" header
98  HTTP_STATUS_MOVED_PERMANENTLY = 301, // redirect using the "Location" header
99  HTTP_STATUS_FOUND = 302, // redirect using the "Location" header
100  HTTP_STATUS_SEE_OTHER = 303, // redirect using the "Location" header
101  HTTP_STATUS_TEMPORARY_REDIRECT = 307, // redirect using the "Location" header
102  };
103 
104 
114  static std::pair<bool, AString> Request(
115  const AString & a_Method,
116  const AString & a_URL,
117  cCallbacksPtr && a_Callbacks,
118  AStringMap && a_Headers,
119  const AString & a_Body,
120  const AStringMap & a_Options
121  );
122 
124  static std::pair<bool, AString> Get(
125  const AString & a_URL,
126  cCallbacksPtr && a_Callbacks,
127  AStringMap && a_Headers = {},
128  const AString & a_Body = {},
129  const AStringMap & a_Options = {}
130  );
131 
133  static std::pair<bool, AString> Post(
134  const AString & a_URL,
135  cCallbacksPtr && a_Callbacks,
136  AStringMap && a_Headers,
137  const AString & a_Body,
138  const AStringMap & a_Options = {}
139  );
140 
142  static std::pair<bool, AString> Put(
143  const AString & a_URL,
144  cCallbacksPtr && a_Callbacks,
145  AStringMap && a_Headers,
146  const AString & a_Body,
147  const AStringMap & a_Options = {}
148  );
149 
153  static std::pair<bool, AString> BlockingRequest(
154  const AString & a_Method,
155  const AString & a_URL,
156  AStringMap && a_Headers = {},
157  const AString & a_Body = {},
158  const AStringMap & a_Options = {}
159  );
160 
162  static std::pair<bool, AString> BlockingGet(
163  const AString & a_URL,
164  AStringMap a_Headers = {},
165  const AString & a_Body = {},
166  const AStringMap & a_Options = {}
167  );
168 
170  static std::pair<bool, AString> BlockingPost(
171  const AString & a_URL,
172  AStringMap && a_Headers,
173  const AString & a_Body,
174  const AStringMap & a_Options = {}
175  );
176 
178  static std::pair<bool, AString> BlockingPut(
179  const AString & a_URL,
180  AStringMap && a_Headers,
181  const AString & a_Body,
182  const AStringMap & a_Options = {}
183  );
184 };
185 
186 
187 
188 
std::string AString
Definition: StringUtils.h:11
std::map< AString, AString > AStringMap
A string dictionary, used for key-value pairs.
Definition: StringUtils.h:16
static std::pair< bool, AString > BlockingPut(const AString &a_URL, AStringMap &&a_Headers, const AString &a_Body, const AStringMap &a_Options={})
Alias for BlockingRequest("PUT", ...)
Definition: UrlClient.cpp:807
static std::pair< bool, AString > BlockingRequest(const AString &a_Method, const AString &a_URL, AStringMap &&a_Headers={}, const AString &a_Body={}, const AStringMap &a_Options={})
Sends a generic request and block until a response is received or an error occurs.
Definition: UrlClient.cpp:744
std::shared_ptr< cCallbacks > cCallbacksPtr
Definition: UrlClient.h:90
static std::pair< bool, AString > Put(const AString &a_URL, cCallbacksPtr &&a_Callbacks, AStringMap &&a_Headers, const AString &a_Body, const AStringMap &a_Options={})
Alias for Request("PUT", ...)
Definition: UrlClient.cpp:727
static std::pair< bool, AString > Request(const AString &a_Method, const AString &a_URL, cCallbacksPtr &&a_Callbacks, AStringMap &&a_Headers, const AString &a_Body, const AStringMap &a_Options)
Makes a network request to the specified URL, using the specified method (if applicable).
Definition: UrlClient.cpp:675
eHTTPStatus
Used for HTTP status codes.
Definition: UrlClient.h:95
@ HTTP_STATUS_MULTIPLE_CHOICES
Definition: UrlClient.h:97
@ HTTP_STATUS_FOUND
Definition: UrlClient.h:99
@ HTTP_STATUS_MOVED_PERMANENTLY
Definition: UrlClient.h:98
@ HTTP_STATUS_OK
Definition: UrlClient.h:96
@ HTTP_STATUS_SEE_OTHER
Definition: UrlClient.h:100
@ HTTP_STATUS_TEMPORARY_REDIRECT
Definition: UrlClient.h:101
static std::pair< bool, AString > BlockingPost(const AString &a_URL, AStringMap &&a_Headers, const AString &a_Body, const AStringMap &a_Options={})
Alias for BlockingRequest("POST", ...)
Definition: UrlClient.cpp:793
static std::pair< bool, AString > BlockingGet(const AString &a_URL, AStringMap a_Headers={}, const AString &a_Body={}, const AStringMap &a_Options={})
Alias for BlockingRequest("GET", ...)
Definition: UrlClient.cpp:779
static std::pair< bool, AString > Post(const AString &a_URL, cCallbacksPtr &&a_Callbacks, AStringMap &&a_Headers, const AString &a_Body, const AStringMap &a_Options={})
Alias for Request("POST", ...)
Definition: UrlClient.cpp:710
static std::pair< bool, AString > Get(const AString &a_URL, cCallbacksPtr &&a_Callbacks, AStringMap &&a_Headers={}, const AString &a_Body={}, const AStringMap &a_Options={})
Alias for Request("GET", ...)
Definition: UrlClient.cpp:693
Callbacks that are used for progress and result reporting.
Definition: UrlClient.h:39
virtual void OnTlsHandshakeCompleted()
Called for TLS connections, when the TLS handshake has been completed.
Definition: UrlClient.h:55
virtual void OnHeadersFinished()
Called when the HTTP headers have been fully parsed, unless the response is an allowed redirect.
Definition: UrlClient.h:69
virtual void OnConnected(cTCPLink &a_Link)
Called when the TCP connection is established.
Definition: UrlClient.h:45
virtual void OnStatusLine(const AString &a_HttpVersion, int a_StatusCode, const AString &a_Rest)
Called after the first line of the response is parsed, unless the response is an allowed redirect.
Definition: UrlClient.h:61
virtual void OnRedirecting(const AString &a_NewLocation)
Called when a redirect is to be followed.
Definition: UrlClient.h:88
virtual bool OnCertificateReceived()
Called for TLS connections, when the server certificate is received.
Definition: UrlClient.h:51
virtual void OnRequestSent()
Called after the entire request has been sent to the remote peer.
Definition: UrlClient.h:58
virtual void OnHeader(const AString &a_Key, const AString &a_Value)
Called when a single HTTP header is received and parsed, unless the response is an allowed redirect C...
Definition: UrlClient.h:65
virtual void OnBodyData(const void *a_Data, size_t a_Size)
Called when the next fragment of the response body is received, unless the response is an allowed red...
Definition: UrlClient.h:73
virtual ~cCallbacks()
Definition: UrlClient.h:42
virtual void OnError(const AString &a_ErrorMsg)
Called when an asynchronous error is encountered.
Definition: UrlClient.h:80
virtual void OnBodyFinished()
Called after the response body has been fully reported by OnBody() calls, unless the response is an a...
Definition: UrlClient.h:77
Interface that provides the methods available on a single TCP connection.
Definition: Network.h:42