Guest User

ESP8266HTTPClient.h

a guest
Jul 22nd, 2022
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.10 KB | None | 0 0
  1. /**
  2. * ESP8266HTTPClient.h
  3. *
  4. * Created on: 02.11.2015
  5. *
  6. * Copyright (c) 2015 Markus Sattler. All rights reserved.
  7. * This file is part of the ESP8266HTTPClient for Arduino.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Modified by Jeroen Döll, June 2018
  24. */
  25.  
  26. #ifndef ESP8266HTTPClient_H_
  27. #define ESP8266HTTPClient_H_
  28.  
  29. #ifndef HTTPCLIENT_1_1_COMPATIBLE
  30. #define HTTPCLIENT_1_1_COMPATIBLE 1
  31. #endif
  32.  
  33. #include <memory>
  34. #include <Arduino.h>
  35.  
  36. #include <WiFiClient.h>
  37.  
  38. #ifdef DEBUG_ESP_HTTP_CLIENT
  39. #ifdef DEBUG_ESP_PORT
  40. #define DEBUG_HTTPCLIENT(fmt, ...) DEBUG_ESP_PORT.printf_P( (PGM_P)PSTR(fmt), ## __VA_ARGS__ )
  41. #endif
  42. #endif
  43.  
  44. #ifndef DEBUG_HTTPCLIENT
  45. #define DEBUG_HTTPCLIENT(...) do { (void)0; } while (0)
  46. #endif
  47.  
  48. #define HTTPCLIENT_DEFAULT_TCP_TIMEOUT (5000)
  49.  
  50. /// HTTP client errors
  51. #define HTTPC_ERROR_CONNECTION_REFUSED (-1)
  52. #define HTTPC_ERROR_SEND_HEADER_FAILED (-2)
  53. #define HTTPC_ERROR_SEND_PAYLOAD_FAILED (-3)
  54. #define HTTPC_ERROR_NOT_CONNECTED (-4)
  55. #define HTTPC_ERROR_CONNECTION_LOST (-5)
  56. #define HTTPC_ERROR_NO_STREAM (-6)
  57. #define HTTPC_ERROR_NO_HTTP_SERVER (-7)
  58. #define HTTPC_ERROR_TOO_LESS_RAM (-8)
  59. #define HTTPC_ERROR_ENCODING (-9)
  60. #define HTTPC_ERROR_STREAM_WRITE (-10)
  61. #define HTTPC_ERROR_READ_TIMEOUT (-11)
  62.  
  63. /// size for the stream handling
  64. #define HTTP_TCP_BUFFER_SIZE (1460)
  65.  
  66. /// HTTP codes see RFC7231
  67. typedef enum {
  68. HTTP_CODE_CONTINUE = 100,
  69. HTTP_CODE_SWITCHING_PROTOCOLS = 101,
  70. HTTP_CODE_PROCESSING = 102,
  71. HTTP_CODE_OK = 200,
  72. HTTP_CODE_CREATED = 201,
  73. HTTP_CODE_ACCEPTED = 202,
  74. HTTP_CODE_NON_AUTHORITATIVE_INFORMATION = 203,
  75. HTTP_CODE_NO_CONTENT = 204,
  76. HTTP_CODE_RESET_CONTENT = 205,
  77. HTTP_CODE_PARTIAL_CONTENT = 206,
  78. HTTP_CODE_MULTI_STATUS = 207,
  79. HTTP_CODE_ALREADY_REPORTED = 208,
  80. HTTP_CODE_IM_USED = 226,
  81. HTTP_CODE_MULTIPLE_CHOICES = 300,
  82. HTTP_CODE_MOVED_PERMANENTLY = 301,
  83. HTTP_CODE_FOUND = 302,
  84. HTTP_CODE_SEE_OTHER = 303,
  85. HTTP_CODE_NOT_MODIFIED = 304,
  86. HTTP_CODE_USE_PROXY = 305,
  87. HTTP_CODE_TEMPORARY_REDIRECT = 307,
  88. HTTP_CODE_PERMANENT_REDIRECT = 308,
  89. HTTP_CODE_BAD_REQUEST = 400,
  90. HTTP_CODE_UNAUTHORIZED = 401,
  91. HTTP_CODE_PAYMENT_REQUIRED = 402,
  92. HTTP_CODE_FORBIDDEN = 403,
  93. HTTP_CODE_NOT_FOUND = 404,
  94. HTTP_CODE_METHOD_NOT_ALLOWED = 405,
  95. HTTP_CODE_NOT_ACCEPTABLE = 406,
  96. HTTP_CODE_PROXY_AUTHENTICATION_REQUIRED = 407,
  97. HTTP_CODE_REQUEST_TIMEOUT = 408,
  98. HTTP_CODE_CONFLICT = 409,
  99. HTTP_CODE_GONE = 410,
  100. HTTP_CODE_LENGTH_REQUIRED = 411,
  101. HTTP_CODE_PRECONDITION_FAILED = 412,
  102. HTTP_CODE_PAYLOAD_TOO_LARGE = 413,
  103. HTTP_CODE_URI_TOO_LONG = 414,
  104. HTTP_CODE_UNSUPPORTED_MEDIA_TYPE = 415,
  105. HTTP_CODE_RANGE_NOT_SATISFIABLE = 416,
  106. HTTP_CODE_EXPECTATION_FAILED = 417,
  107. HTTP_CODE_MISDIRECTED_REQUEST = 421,
  108. HTTP_CODE_UNPROCESSABLE_ENTITY = 422,
  109. HTTP_CODE_LOCKED = 423,
  110. HTTP_CODE_FAILED_DEPENDENCY = 424,
  111. HTTP_CODE_UPGRADE_REQUIRED = 426,
  112. HTTP_CODE_PRECONDITION_REQUIRED = 428,
  113. HTTP_CODE_TOO_MANY_REQUESTS = 429,
  114. HTTP_CODE_REQUEST_HEADER_FIELDS_TOO_LARGE = 431,
  115. HTTP_CODE_INTERNAL_SERVER_ERROR = 500,
  116. HTTP_CODE_NOT_IMPLEMENTED = 501,
  117. HTTP_CODE_BAD_GATEWAY = 502,
  118. HTTP_CODE_SERVICE_UNAVAILABLE = 503,
  119. HTTP_CODE_GATEWAY_TIMEOUT = 504,
  120. HTTP_CODE_HTTP_VERSION_NOT_SUPPORTED = 505,
  121. HTTP_CODE_VARIANT_ALSO_NEGOTIATES = 506,
  122. HTTP_CODE_INSUFFICIENT_STORAGE = 507,
  123. HTTP_CODE_LOOP_DETECTED = 508,
  124. HTTP_CODE_NOT_EXTENDED = 510,
  125. HTTP_CODE_NETWORK_AUTHENTICATION_REQUIRED = 511
  126. } t_http_codes;
  127.  
  128. typedef enum {
  129. HTTPC_TE_IDENTITY,
  130. HTTPC_TE_CHUNKED
  131. } transferEncoding_t;
  132.  
  133. #if HTTPCLIENT_1_1_COMPATIBLE
  134. class TransportTraits;
  135. typedef std::unique_ptr<TransportTraits> TransportTraitsPtr;
  136. #endif
  137.  
  138. class StreamString;
  139.  
  140. class HTTPClient
  141. {
  142. public:
  143. HTTPClient();
  144. ~HTTPClient();
  145.  
  146. /*
  147. * Since both begin() functions take a reference to client as a parameter, you need to
  148. * ensure the client object lives the entire time of the HTTPClient
  149. */
  150. bool begin(WiFiClient &client, const String& url);
  151. bool begin(WiFiClient &client, const String& host, uint16_t port, const String& uri = "/", bool https = false);
  152.  
  153. #if HTTPCLIENT_1_1_COMPATIBLE
  154. // Plain HTTP connection, unencrypted
  155. bool begin(String url) __attribute__ ((deprecated));
  156. bool begin(String host, uint16_t port, String uri = "/") __attribute__ ((deprecated));
  157. // Use axTLS for secure HTTPS connection
  158. bool begin(String url, String httpsFingerprint) __attribute__ ((deprecated));
  159. bool begin(String host, uint16_t port, String uri, String httpsFingerprint) __attribute__ ((deprecated));
  160. // Use BearSSL for secure HTTPS connection
  161. bool begin(String url, const uint8_t httpsFingerprint[20]) __attribute__ ((deprecated));
  162. bool begin(String host, uint16_t port, String uri, const uint8_t httpsFingerprint[20]) __attribute__ ((deprecated));
  163. // deprecated, use the overload above instead
  164. bool begin(String host, uint16_t port, String uri, bool https, String httpsFingerprint) __attribute__ ((deprecated));
  165. #endif
  166.  
  167. void end(void);
  168.  
  169. bool connected(void);
  170.  
  171. void setReuse(bool reuse); /// keep-alive
  172. void setUserAgent(const String& userAgent);
  173. void setAuthorization(const char * user, const char * password);
  174. void setAuthorization(const char * auth);
  175. void setTimeout(uint16_t timeout);
  176. void setFollowRedirects(bool follow);
  177. void setRedirectLimit(uint16_t limit); // max redirects to follow for a single request
  178. bool setURL(const String& url); // handy for handling redirects
  179. void useHTTP10(bool usehttp10 = true);
  180.  
  181. /// request handling
  182. int GET();
  183. int POST(const uint8_t* payload, size_t size);
  184. int POST(const String& payload);
  185. int PUT(const uint8_t* payload, size_t size);
  186. int PUT(const String& payload);
  187. int PATCH(const uint8_t* payload, size_t size);
  188. int PATCH(const String& payload);
  189. int sendRequest(const char* type, const String& payload);
  190. int sendRequest(const char* type, const uint8_t* payload = NULL, size_t size = 0);
  191. int sendRequest(const char* type, Stream * stream, size_t size = 0);
  192.  
  193. void addHeader(const String& name, const String& value, bool first = false, bool replace = true);
  194.  
  195. /// Response handling
  196. void collectHeaders(const char* headerKeys[], const size_t headerKeysCount);
  197. String header(const char* name); // get request header value by name
  198. String header(size_t i); // get request header value by number
  199. String headerName(size_t i); // get request header name by number
  200. int headers(); // get header count
  201. bool hasHeader(const char* name); // check if header exists
  202.  
  203.  
  204. int getSize(void);
  205. const String& getLocation(void); // Location header from redirect if 3XX
  206.  
  207. WiFiClient& getStream(void);
  208. WiFiClient* getStreamPtr(void);
  209. int writeToStream(Stream* stream);
  210. const String& getString(void);
  211. static String errorToString(int error);
  212.  
  213. protected:
  214. struct RequestArgument {
  215. String key;
  216. String value;
  217. };
  218.  
  219. bool beginInternal(const String& url, const char* expectedProtocol);
  220. void disconnect(bool preserveClient = false);
  221. void clear();
  222. int returnError(int error);
  223. bool connect(void);
  224. bool sendHeader(const char * type);
  225. int handleHeaderResponse();
  226. int writeToStreamDataBlock(Stream * stream, int len);
  227.  
  228.  
  229. #if HTTPCLIENT_1_1_COMPATIBLE
  230. TransportTraitsPtr _transportTraits;
  231. std::unique_ptr<WiFiClient> _tcpDeprecated;
  232. #endif
  233. WiFiClient* _client;
  234.  
  235. /// request handling
  236. String _host;
  237. uint16_t _port = 0;
  238. bool _reuse = true;
  239. uint16_t _tcpTimeout = HTTPCLIENT_DEFAULT_TCP_TIMEOUT;
  240. bool _useHTTP10 = false;
  241.  
  242. String _uri;
  243. String _protocol;
  244. String _headers;
  245. String _userAgent;
  246. String _base64Authorization;
  247.  
  248. /// Response handling
  249. RequestArgument* _currentHeaders = nullptr;
  250. size_t _headerKeysCount = 0;
  251.  
  252. int _returnCode = 0;
  253. int _size = -1;
  254. bool _canReuse = false;
  255. bool _followRedirects = false;
  256. uint16_t _redirectCount = 0;
  257. uint16_t _redirectLimit = 10;
  258. String _location;
  259. transferEncoding_t _transferEncoding = HTTPC_TE_IDENTITY;
  260. std::unique_ptr<StreamString> _payload;
  261. };
  262.  
  263.  
  264.  
  265. #endif /* ESP8266HTTPClient_H_ */
  266.  
Add Comment
Please, Sign In to add comment