Ocean
HTTPSClient.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef FACEBOOK_NETWORK_HTTPS_CLIENT_H
9 #define FACEBOOK_NETWORK_HTTPS_CLIENT_H
10 
11 #include "ocean/network/Network.h"
12 #include "ocean/network/Port.h"
13 
14 #include "ocean/base/Callback.h"
15 
16 namespace Ocean
17 {
18 
19 namespace Network
20 {
21 
22 /**
23  * This class implements a HTTPS Client supporting GET and POST requests.
24  * @ingroup network
25  */
26 class OCEAN_NETWORK_EXPORT HTTPSClient
27 {
28  public:
29 
30  /**
31  * Definition of a vector holding characters.
32  */
33  typedef std::vector<uint8_t> Buffer;
34 
35  /**
36  * Definition of a vector holding strings.
37  */
38  typedef std::vector<std::string> Strings;
39 
40  /**
41  * Callback for receiving progress information when performing a HTTP request.
42  * The first parameter represents how many Bytes of the payload have been received so far, with range [0, infinity)
43  * The second parameter represents the total size of the payload in bytes, with range [0, infinity). If the total size is unknown this parameter is 0.
44  */
46 
47  protected:
48 
49  /**
50  * Forward declaration.
51  */
52  class CurlSessionData;
53 
54  public:
55 
56  /**
57  * Function to executes a HTTPS GET (site/file download) request.
58  * @param url The URL of the HTTPS site which is requested, beginning with "HTTPS://"
59  * @param data The resulting request data
60  * @param port The port of the HTTPS server
61  * @param timeout The timeout this function waits for the server's response, with range (0, infinity)
62  * @param abort Optional flag that may be set to True by another thread to abort the request
63  * @param progressCallback Optional callback for receiving progress information
64  * @return True, if succeeded
65  */
66  static bool httpsGetRequest(const std::string& url, Buffer& data, const Port& port = Port(443, Port::TYPE_READABLE), const double timeout = 5.0, bool* abort = nullptr, const ProgressCallback& progressCallback = ProgressCallback());
67 
68  /**
69  * Function to executes a HTTPS POST (site/file upload) request.
70  * @param url The URL of the HTTPS site which is requested, beginning with "HTTPS://"
71  * @param requestData The buffer of the request data
72  * @param requestDataSize The number of bytes of the request data
73  * @param data The resulting response data
74  * @param port The port of the HTTPS server
75  * @param timeout The timeout this function waits for the server's response, with range (0, infinity)
76  * @param additionalHeaders Optional additional HTTP headers
77  * @return True, if succeeded
78  */
79  static bool httpsPostRequest(const std::string& url, const uint8_t* requestData, const size_t requestDataSize, Buffer& data, const Port& port = Port(443, Port::TYPE_READABLE), const double timeout = 5.0, const Strings& additionalHeaders = Strings());
80 
81  /**
82  * Converts a URL to a URI.
83  * @param url The URL to convert, e.g., 'https://www.website.com/index.html"
84  * @param protocol The resulting protocol, will be 'https' if successful
85  * @param host The resulting host without ending '/'
86  * @param uri The resulting URI, can be empty if the URL does not include an URI
87  * @return True, if succeeded
88  */
89  static bool url2uri(const std::string& url, std::string& protocol, std::string& host, std::string& uri);
90 
91  protected:
92 
93 #if defined(__APPLE__)
94 
95  /**
96  * Function to executes a HTTPS GET (site/file download) request (specialized function for Apple platforms).
97  * @sa httpsGetRequest()
98  * @param url The URL of the HTTPS site which is requested, beginning with "HTTPS://"
99  * @param data The resulting request data
100  * @param port The port of the HTTPS server
101  * @param timeout The timeout this function waits for the server's response, with range (0, infinity)
102  * @param abort Optional flag that may be set to True by another thread to abort the request
103  * @param progressCallback Optional callback for receiving progress information
104  * @return True, if succeeded
105  */
106  static bool httpsGetRequestApple(const std::string& url, Buffer& data, const Port& port, const double timeout, bool* abort, const ProgressCallback& progressCallback);
107 
108  /**
109  * Function to executes a HTTPS POST (site/file upload) request (specialized function for Apple platforms).
110  * @sa httpsPostRequest()
111  * @param url The URL of the HTTPS site which is requested, beginning with "HTTPS://"
112  * @param requestData The buffer of the request data
113  * @param requestDataSize The number of bytes of the request data
114  * @param data The resulting response data
115  * @param port The port of the HTTPS server
116  * @param timeout The timeout this function waits for the server's response, with range (0, infinity)
117  * @param additionalHeaders Optional additional HTTP headers
118  * @return True, if succeeded
119  */
120  static bool httpsPostRequestApple(const std::string& url, const uint8_t* requestData, const size_t requestDataSize, Buffer& data, const Port& port, const double timeout, const Strings& additionalHeaders);
121 
122 #endif // defined(__APPLE__)
123 
124 };
125 
126 }
127 
128 }
129 
130 #endif // FACEBOOK_NETWORK_HTTPS_CLIENT_H
This class implements a container for callback functions.
Definition: Callback.h:3456
@ TYPE_READABLE
Readable endian type which is platform specific.
Definition: Data.h:37
This class implements a HTTPS Client supporting GET and POST requests.
Definition: HTTPSClient.h:27
static bool url2uri(const std::string &url, std::string &protocol, std::string &host, std::string &uri)
Converts a URL to a URI.
static bool httpsGetRequest(const std::string &url, Buffer &data, const Port &port=Port(443, Port::TYPE_READABLE), const double timeout=5.0, bool *abort=nullptr, const ProgressCallback &progressCallback=ProgressCallback())
Function to executes a HTTPS GET (site/file download) request.
Callback< void, size_t, size_t > ProgressCallback
Callback for receiving progress information when performing a HTTP request.
Definition: HTTPSClient.h:45
static bool httpsPostRequest(const std::string &url, const uint8_t *requestData, const size_t requestDataSize, Buffer &data, const Port &port=Port(443, Port::TYPE_READABLE), const double timeout=5.0, const Strings &additionalHeaders=Strings())
Function to executes a HTTPS POST (site/file upload) request.
std::vector< uint8_t > Buffer
Definition of a vector holding characters.
Definition: HTTPSClient.h:33
std::vector< std::string > Strings
Definition of a vector holding strings.
Definition: HTTPSClient.h:38
static bool httpsPostRequestApple(const std::string &url, const uint8_t *requestData, const size_t requestDataSize, Buffer &data, const Port &port, const double timeout, const Strings &additionalHeaders)
Function to executes a HTTPS POST (site/file upload) request (specialized function for Apple platform...
static bool httpsGetRequestApple(const std::string &url, Buffer &data, const Port &port, const double timeout, bool *abort, const ProgressCallback &progressCallback)
Function to executes a HTTPS GET (site/file download) request (specialized function for Apple platfor...
This class wraps a port number with 16 bits.
Definition: Port.h:26
std::vector< std::string > Strings
Definition of a vector holding strings.
Definition: Base.h:162
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15