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 "Http/HttpCommon.h"
23 #include "Http/HttpRequest.h"
26 #include <SimpleTimer.h>
27 
29 {
30 public:
38  virtual ~HttpClient()
39  {
40  }
41 
42  /* High-Level Methods */
43 
44  bool sendRequest(const Url& url, RequestCompletedDelegate requestComplete)
45  {
46  return send(createRequest(url)->setMethod(HTTP_GET)->onRequestComplete(requestComplete));
47  }
48 
49  bool sendRequest(const HttpMethod method, const Url& url, const HttpHeaders& headers,
50  RequestCompletedDelegate requestComplete)
51  {
52  return send(createRequest(url)->setMethod(method)->setHeaders(headers)->onRequestComplete(requestComplete));
53  }
54 
55  bool sendRequest(const HttpMethod method, const Url& url, const HttpHeaders& headers, const String& body,
56  RequestCompletedDelegate requestComplete)
57  {
58  return send(createRequest(url)->setMethod(method)->setHeaders(headers)->setBody(body)->onRequestComplete(
59  requestComplete));
60  }
61 
62  bool sendRequest(const HttpMethod method, const Url& url, const HttpHeaders& headers, String&& body,
63  RequestCompletedDelegate requestComplete) noexcept
64  {
65  return send(createRequest(url)
66  ->setMethod(method)
67  ->setHeaders(headers)
68  ->setBody(std::move(body))
69  ->onRequestComplete(requestComplete));
70  }
71 
80  bool downloadString(const Url& url, RequestCompletedDelegate requestComplete,
81  size_t maxLength = NETWORK_SEND_BUFFER_SIZE)
82  {
83  return send(createRequest(url)
84  ->setMethod(HTTP_GET)
85  ->setResponseStream(new LimitedMemoryStream(maxLength))
86  ->onRequestComplete(requestComplete));
87  }
88 
89  bool downloadFile(const Url& url, RequestCompletedDelegate requestComplete = nullptr)
90  {
91  return downloadFile(url, nullptr, requestComplete);
92  }
93 
100  bool downloadFile(const Url& url, const String& saveFileName, RequestCompletedDelegate requestComplete = nullptr);
101 
102  /* Low Level Methods */
103 
104  /*
105  * @brief This method queues a request and sends it, once it is connected to the remote server.
106  * @param HttpRequest* request The request object will be freed inside of the method.
107  * Do not try to reuse it outside of the send method as it will lead to unpredicted results
108  *
109  * @retval bool true if the request was queued, false otherwise.
110  *
111  */
112  bool send(HttpRequest* request);
113 
119  {
120  return new HttpRequest(url);
121  }
122 
126  static void cleanup()
127  {
129  }
130 
131 protected:
132  String getCacheKey(const Url& url)
133  {
134  return url.Host + ':' + url.getPort();
135  }
136 
137 protected:
140 
141 private:
142  static SimpleTimer cleanUpTimer;
143  static void cleanInactive();
144 };
145 
bool downloadString(const Url &url, RequestCompletedDelegate requestComplete, size_t maxLength=NETWORK_SEND_BUFFER_SIZE)
Queue request to download content as string (in memory)
Definition: HttpClient.h:80
HttpRequest * createRequest(const Url &url)
Helper function to create a new request on a URL.
Definition: HttpClient.h:118
bool sendRequest(const Url &url, RequestCompletedDelegate requestComplete)
Definition: HttpClient.h:44
Class to manage URL instance.
Definition: Url.h:66
Definition: HttpClient.h:28
The String class.
Definition: WString.h:136
bool downloadFile(const Url &url, RequestCompletedDelegate requestComplete=nullptr)
Definition: HttpClient.h:89
Encapsulates a set of HTTP header information.
Definition: HttpHeaders.h:34
String getCacheKey(const Url &url)
Definition: HttpClient.h:132
String Host
hostname or IP address
Definition: Url.h:173
Memory stream operating on fixed-size buffer Once the limit is reached the stream will discard incomi...
Definition: LimitedMemoryStream.h:21
void clear()
Clear the map of all entries.
Definition: ObjectMap.h:322
static HttpConnectionPool httpConnectionPool
Definition: HttpClient.h:139
bool sendRequest(const HttpMethod method, const Url &url, const HttpHeaders &headers, const String &body, RequestCompletedDelegate requestComplete)
Definition: HttpClient.h:55
static void cleanup()
Use this method to clean all object pools.
Definition: HttpClient.h:126
bool sendRequest(const HttpMethod method, const Url &url, const HttpHeaders &headers, String &&body, RequestCompletedDelegate requestComplete) noexcept
Definition: HttpClient.h:62
bool sendRequest(const HttpMethod method, const Url &url, const HttpHeaders &headers, RequestCompletedDelegate requestComplete)
Definition: HttpClient.h:49
#define NETWORK_SEND_BUFFER_SIZE
Definition: TcpConnection.h:24
bool send(HttpRequest *request)
Implementation of a HashMap for owned objects, i.e. anything created with new().
Definition: ObjectMap.h:48
Encapsulates an incoming or outgoing request.
Definition: HttpRequest.h:36
HttpMethod
Strongly-typed enum which shadows http_method from http_parser library.
Definition: HttpCommon.h:42
virtual ~HttpClient()
HttpClient destructor.
Definition: HttpClient.h:38
int getPort() const
Obtain the actual port number to be used.
Definition: Url.h:133