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 
28 {
29 public:
37  virtual ~HttpClient()
38  {
39  }
40 
41  /* High-Level Methods */
42 
43  bool sendRequest(const Url& url, RequestCompletedDelegate requestComplete)
44  {
45  return send(createRequest(url)->setMethod(HTTP_GET)->onRequestComplete(requestComplete));
46  }
47 
48  bool sendRequest(const HttpMethod method, const Url& url, const HttpHeaders& headers,
49  RequestCompletedDelegate requestComplete)
50  {
51  return send(createRequest(url)->setMethod(method)->setHeaders(headers)->onRequestComplete(requestComplete));
52  }
53 
54  bool sendRequest(const HttpMethod method, const Url& url, const HttpHeaders& headers, const String& body,
55  RequestCompletedDelegate requestComplete)
56  {
57  return send(createRequest(url)->setMethod(method)->setHeaders(headers)->setBody(body)->onRequestComplete(
58  requestComplete));
59  }
60 
69  bool downloadString(const Url& url, RequestCompletedDelegate requestComplete,
70  size_t maxLength = NETWORK_SEND_BUFFER_SIZE)
71  {
72  return send(createRequest(url)
73  ->setMethod(HTTP_GET)
74  ->setResponseStream(new LimitedMemoryStream(maxLength))
75  ->onRequestComplete(requestComplete));
76  }
77 
78  bool downloadFile(const Url& url, RequestCompletedDelegate requestComplete = nullptr)
79  {
80  return downloadFile(url, nullptr, requestComplete);
81  }
82 
89  bool downloadFile(const Url& url, const String& saveFileName, RequestCompletedDelegate requestComplete = nullptr);
90 
91  /* Low Level Methods */
92 
93  /*
94  * @brief This method queues a request and sends it, once it is connected to the remote server.
95  * @param HttpRequest* request The request object will be freed inside of the method.
96  * Do not try to reuse it outside of the send method as it will lead to unpredicted results
97  *
98  * @retval bool true if the request was queued, false otherwise.
99  *
100  */
101  bool send(HttpRequest* request);
102 
105  {
106  return createRequest(url);
107  }
108 
114  {
115  return new HttpRequest(url);
116  }
117 
121  static void cleanup()
122  {
124  }
125 
126 protected:
127  String getCacheKey(const Url& url)
128  {
129  return url.Host + ':' + url.getPort();
130  }
131 
132 protected:
134  static HttpConnectionPool httpConnectionPool;
135 };
136 
static void cleanup()
Definition: HttpClient.h:121
bool sendRequest(const HttpMethod method, const Url &url, const HttpHeaders &headers, RequestCompletedDelegate requestComplete)
Definition: HttpClient.h:48
Implementation of a HashMap for owned objects, i.e. anything created with new().
Definition: ObjectMap.h:47
Class to manage URL instance.
Definition: Url.h:66
bool sendRequest(const HttpMethod method, const Url &url, const HttpHeaders &headers, const String &body, RequestCompletedDelegate requestComplete)
Definition: HttpClient.h:54
bool send(HttpRequest *request)
Definition: HttpClient.h:27
The String class.
Definition: WString.h:136
enum http_method HttpMethod
Definition: HttpCommon.h:34
Encapsulates a set of HTTP header information.
Definition: HttpHeaders.h:96
ObjectMap< String, HttpClientConnection > HttpConnectionPool
Definition: HttpClient.h:133
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:69
int getPort() const
Obtain the actual port number to be used.
Definition: Url.h:133
HttpRequest * request(const String &url)
Definition: HttpClient.h:104
#define SMING_DEPRECATED
Definition: sming_attr.h:30
virtual ~HttpClient()
HttpClient destructor.
Definition: HttpClient.h:37
bool downloadFile(const Url &url, RequestCompletedDelegate requestComplete=nullptr)
Definition: HttpClient.h:78
#define NETWORK_SEND_BUFFER_SIZE
Definition: TcpConnection.h:23
void clear()
Clear the map of all entries.
Definition: ObjectMap.h:313
HttpRequest * createRequest(const Url &url)
Helper function to create a new request on a URL.
Definition: HttpClient.h:113
Definition: HttpRequest.h:35
bool sendRequest(const Url &url, RequestCompletedDelegate requestComplete)
Definition: HttpClient.h:43
static HttpConnectionPool httpConnectionPool
Definition: HttpClient.h:134
Memory stream that stores limited number of bytes Once the limit is reached the stream will discard i...
Definition: LimitedMemoryStream.h:23
String getCacheKey(const Url &url)
Definition: HttpClient.h:127
String Host
hostname or IP address
Definition: Url.h:173