Http/HttpClient.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * HttpClient.h
8  *
9  * Modified: 2017 - Slavey Karadzhov <slav@attachix.com>
10  *
11  ****/
12 
19 #pragma once
20 
21 #include "../TcpClient.h"
22 #include "HttpCommon.h"
23 #include "HttpRequest.h"
24 #include "HttpClientConnection.h"
26 #include <SimpleTimer.h>
27 
29 {
30 public:
38  virtual ~HttpClient() = default;
39 
40  /* High-Level Methods */
41 
42  bool sendRequest(const Url& url, RequestCompletedDelegate requestComplete)
43  {
44  return send(createRequest(url)->setMethod(HTTP_GET)->onRequestComplete(requestComplete));
45  }
46 
47  bool sendRequest(const HttpMethod method, const Url& url, const HttpHeaders& headers,
48  RequestCompletedDelegate requestComplete)
49  {
50  return send(createRequest(url)->setMethod(method)->setHeaders(headers)->onRequestComplete(requestComplete));
51  }
52 
53  bool sendRequest(const HttpMethod method, const Url& url, const HttpHeaders& headers, const String& body,
54  RequestCompletedDelegate requestComplete)
55  {
56  return send(createRequest(url)->setMethod(method)->setHeaders(headers)->setBody(body)->onRequestComplete(
57  requestComplete));
58  }
59 
60  bool sendRequest(const HttpMethod method, const Url& url, const HttpHeaders& headers, String&& body,
61  RequestCompletedDelegate requestComplete) noexcept
62  {
63  return send(createRequest(url)
64  ->setMethod(method)
65  ->setHeaders(headers)
66  ->setBody(std::move(body))
67  ->onRequestComplete(requestComplete));
68  }
69 
78  bool downloadString(const Url& url, RequestCompletedDelegate requestComplete,
79  size_t maxLength = NETWORK_SEND_BUFFER_SIZE)
80  {
81  return send(createRequest(url)
82  ->setMethod(HTTP_GET)
83  ->setResponseStream(new LimitedMemoryStream(maxLength))
84  ->onRequestComplete(requestComplete));
85  }
86 
87  bool downloadFile(const Url& url, RequestCompletedDelegate requestComplete = nullptr)
88  {
89  return downloadFile(url, nullptr, requestComplete);
90  }
91 
98  bool downloadFile(const Url& url, const String& saveFileName, RequestCompletedDelegate requestComplete = nullptr);
99 
100  /* Low Level Methods */
101 
102  /*
103  * @brief This method queues a request and sends it, once it is connected to the remote server.
104  * @param HttpRequest* request The request object will be freed inside of the method.
105  * Do not try to reuse it outside of the send method as it will lead to unpredicted results
106  *
107  * @retval bool true if the request was queued, false otherwise.
108  *
109  */
110  bool send(HttpRequest* request);
111 
117  {
118  return new HttpRequest(url);
119  }
120 
124  static void cleanup()
125  {
127  }
128 
129 protected:
130  String getCacheKey(const Url& url)
131  {
132  return url.Host + ':' + url.getPort();
133  }
134 
135 protected:
138 
139 private:
140  static SimpleTimer cleanUpTimer;
141  static void cleanInactive();
142 };
143 
HttpMethod
Strongly-typed enum which shadows http_method from http_parser library.
Definition: HttpCommon.h:42
Definition: Http/HttpClient.h:29
bool sendRequest(const HttpMethod method, const Url &url, const HttpHeaders &headers, String &&body, RequestCompletedDelegate requestComplete) noexcept
Definition: Http/HttpClient.h:60
bool send(HttpRequest *request)
bool sendRequest(const HttpMethod method, const Url &url, const HttpHeaders &headers, const String &body, RequestCompletedDelegate requestComplete)
Definition: Http/HttpClient.h:53
virtual ~HttpClient()=default
HttpClient destructor.
bool downloadFile(const Url &url, const String &saveFileName, RequestCompletedDelegate requestComplete=nullptr)
Queue request to download a file.
bool downloadString(const Url &url, RequestCompletedDelegate requestComplete, size_t maxLength=NETWORK_SEND_BUFFER_SIZE)
Queue request to download content as string (in memory)
Definition: Http/HttpClient.h:78
HttpRequest * createRequest(const Url &url)
Helper function to create a new request on a URL.
Definition: Http/HttpClient.h:116
bool downloadFile(const Url &url, RequestCompletedDelegate requestComplete=nullptr)
Definition: Http/HttpClient.h:87
bool sendRequest(const HttpMethod method, const Url &url, const HttpHeaders &headers, RequestCompletedDelegate requestComplete)
Definition: Http/HttpClient.h:47
static HttpConnectionPool httpConnectionPool
Definition: Http/HttpClient.h:137
bool sendRequest(const Url &url, RequestCompletedDelegate requestComplete)
Definition: Http/HttpClient.h:42
String getCacheKey(const Url &url)
Definition: Http/HttpClient.h:130
static void cleanup()
Use this method to clean all object pools.
Definition: Http/HttpClient.h:124
Encapsulates a set of HTTP header information.
Definition: HttpHeaders.h:35
Encapsulates an incoming or outgoing request.
Definition: HttpRequest.h:37
Memory stream operating on fixed-size buffer Once the limit is reached the stream will discard incomi...
Definition: LimitedMemoryStream.h:22
Implementation of a HashMap for owned objects, i.e. anything created with new().
Definition: ObjectMap.h:49
void clear()
Clear the map of all entries.
Definition: ObjectMap.h:304
The String class.
Definition: WString.h:133
Class to manage URL instance.
Definition: Url.h:67
String Host
hostname or IP address
Definition: Url.h:193
int getPort() const
Obtain the actual port number to be used.
Definition: Url.h:139
#define NETWORK_SEND_BUFFER_SIZE
Definition: TcpConnection.h:24