Ein Script für den automatischen Login in Wohnheimen, um das lästige Anmelden auf der Loginseite zu umgehen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ESP8266HTTPClient.h 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. #ifndef ESP8266HTTPClient_H_
  26. #define ESP8266HTTPClient_H_
  27. #ifndef HTTPCLIENT_1_1_COMPATIBLE
  28. #define HTTPCLIENT_1_1_COMPATIBLE 1
  29. #endif
  30. #include <memory>
  31. #include <Arduino.h>
  32. #include <WiFiClient.h>
  33. #ifdef DEBUG_ESP_HTTP_CLIENT
  34. #ifdef DEBUG_ESP_PORT
  35. #define DEBUG_HTTPCLIENT(fmt, ...) DEBUG_ESP_PORT.printf_P( (PGM_P)PSTR(fmt), ## __VA_ARGS__ )
  36. #endif
  37. #endif
  38. #ifndef DEBUG_HTTPCLIENT
  39. #define DEBUG_HTTPCLIENT(...) do { (void)0; } while (0)
  40. #endif
  41. #define HTTPCLIENT_DEFAULT_TCP_TIMEOUT (5000)
  42. /// HTTP client errors
  43. #define HTTPC_ERROR_CONNECTION_REFUSED (-1)
  44. #define HTTPC_ERROR_SEND_HEADER_FAILED (-2)
  45. #define HTTPC_ERROR_SEND_PAYLOAD_FAILED (-3)
  46. #define HTTPC_ERROR_NOT_CONNECTED (-4)
  47. #define HTTPC_ERROR_CONNECTION_LOST (-5)
  48. #define HTTPC_ERROR_NO_STREAM (-6)
  49. #define HTTPC_ERROR_NO_HTTP_SERVER (-7)
  50. #define HTTPC_ERROR_TOO_LESS_RAM (-8)
  51. #define HTTPC_ERROR_ENCODING (-9)
  52. #define HTTPC_ERROR_STREAM_WRITE (-10)
  53. #define HTTPC_ERROR_READ_TIMEOUT (-11)
  54. /// size for the stream handling
  55. #define HTTP_TCP_BUFFER_SIZE (1460)
  56. /// HTTP codes see RFC7231
  57. typedef enum {
  58. HTTP_CODE_CONTINUE = 100,
  59. HTTP_CODE_SWITCHING_PROTOCOLS = 101,
  60. HTTP_CODE_PROCESSING = 102,
  61. HTTP_CODE_OK = 200,
  62. HTTP_CODE_CREATED = 201,
  63. HTTP_CODE_ACCEPTED = 202,
  64. HTTP_CODE_NON_AUTHORITATIVE_INFORMATION = 203,
  65. HTTP_CODE_NO_CONTENT = 204,
  66. HTTP_CODE_RESET_CONTENT = 205,
  67. HTTP_CODE_PARTIAL_CONTENT = 206,
  68. HTTP_CODE_MULTI_STATUS = 207,
  69. HTTP_CODE_ALREADY_REPORTED = 208,
  70. HTTP_CODE_IM_USED = 226,
  71. HTTP_CODE_MULTIPLE_CHOICES = 300,
  72. HTTP_CODE_MOVED_PERMANENTLY = 301,
  73. HTTP_CODE_FOUND = 302,
  74. HTTP_CODE_SEE_OTHER = 303,
  75. HTTP_CODE_NOT_MODIFIED = 304,
  76. HTTP_CODE_USE_PROXY = 305,
  77. HTTP_CODE_TEMPORARY_REDIRECT = 307,
  78. HTTP_CODE_PERMANENT_REDIRECT = 308,
  79. HTTP_CODE_BAD_REQUEST = 400,
  80. HTTP_CODE_UNAUTHORIZED = 401,
  81. HTTP_CODE_PAYMENT_REQUIRED = 402,
  82. HTTP_CODE_FORBIDDEN = 403,
  83. HTTP_CODE_NOT_FOUND = 404,
  84. HTTP_CODE_METHOD_NOT_ALLOWED = 405,
  85. HTTP_CODE_NOT_ACCEPTABLE = 406,
  86. HTTP_CODE_PROXY_AUTHENTICATION_REQUIRED = 407,
  87. HTTP_CODE_REQUEST_TIMEOUT = 408,
  88. HTTP_CODE_CONFLICT = 409,
  89. HTTP_CODE_GONE = 410,
  90. HTTP_CODE_LENGTH_REQUIRED = 411,
  91. HTTP_CODE_PRECONDITION_FAILED = 412,
  92. HTTP_CODE_PAYLOAD_TOO_LARGE = 413,
  93. HTTP_CODE_URI_TOO_LONG = 414,
  94. HTTP_CODE_UNSUPPORTED_MEDIA_TYPE = 415,
  95. HTTP_CODE_RANGE_NOT_SATISFIABLE = 416,
  96. HTTP_CODE_EXPECTATION_FAILED = 417,
  97. HTTP_CODE_MISDIRECTED_REQUEST = 421,
  98. HTTP_CODE_UNPROCESSABLE_ENTITY = 422,
  99. HTTP_CODE_LOCKED = 423,
  100. HTTP_CODE_FAILED_DEPENDENCY = 424,
  101. HTTP_CODE_UPGRADE_REQUIRED = 426,
  102. HTTP_CODE_PRECONDITION_REQUIRED = 428,
  103. HTTP_CODE_TOO_MANY_REQUESTS = 429,
  104. HTTP_CODE_REQUEST_HEADER_FIELDS_TOO_LARGE = 431,
  105. HTTP_CODE_INTERNAL_SERVER_ERROR = 500,
  106. HTTP_CODE_NOT_IMPLEMENTED = 501,
  107. HTTP_CODE_BAD_GATEWAY = 502,
  108. HTTP_CODE_SERVICE_UNAVAILABLE = 503,
  109. HTTP_CODE_GATEWAY_TIMEOUT = 504,
  110. HTTP_CODE_HTTP_VERSION_NOT_SUPPORTED = 505,
  111. HTTP_CODE_VARIANT_ALSO_NEGOTIATES = 506,
  112. HTTP_CODE_INSUFFICIENT_STORAGE = 507,
  113. HTTP_CODE_LOOP_DETECTED = 508,
  114. HTTP_CODE_NOT_EXTENDED = 510,
  115. HTTP_CODE_NETWORK_AUTHENTICATION_REQUIRED = 511
  116. } t_http_codes;
  117. typedef enum {
  118. HTTPC_TE_IDENTITY,
  119. HTTPC_TE_CHUNKED
  120. } transferEncoding_t;
  121. #if HTTPCLIENT_1_1_COMPATIBLE
  122. class TransportTraits;
  123. typedef std::unique_ptr<TransportTraits> TransportTraitsPtr;
  124. #endif
  125. class StreamString;
  126. class HTTPClient
  127. {
  128. public:
  129. HTTPClient();
  130. ~HTTPClient();
  131. /*
  132. * Since both begin() functions take a reference to client as a parameter, you need to
  133. * ensure the client object lives the entire time of the HTTPClient
  134. */
  135. bool begin(WiFiClient &client, const String& url);
  136. bool begin(WiFiClient &client, const String& host, uint16_t port, const String& uri = "/", bool https = false);
  137. #if HTTPCLIENT_1_1_COMPATIBLE
  138. // Plain HTTP connection, unencrypted
  139. bool begin(String url) __attribute__ ((deprecated));
  140. bool begin(String host, uint16_t port, String uri = "/") __attribute__ ((deprecated));
  141. // Use axTLS for secure HTTPS connection
  142. bool begin(String url, String httpsFingerprint) __attribute__ ((deprecated));
  143. bool begin(String host, uint16_t port, String uri, String httpsFingerprint) __attribute__ ((deprecated));
  144. // Use BearSSL for secure HTTPS connection
  145. bool begin(String url, const uint8_t httpsFingerprint[20]) __attribute__ ((deprecated));
  146. bool begin(String host, uint16_t port, String uri, const uint8_t httpsFingerprint[20]) __attribute__ ((deprecated));
  147. // deprecated, use the overload above instead
  148. bool begin(String host, uint16_t port, String uri, bool https, String httpsFingerprint) __attribute__ ((deprecated));
  149. #endif
  150. void end(void);
  151. bool connected(void);
  152. void setReuse(bool reuse); /// keep-alive
  153. void setUserAgent(const String& userAgent);
  154. void setAuthorization(const char * user, const char * password);
  155. void setAuthorization(const char * auth);
  156. void setTimeout(uint16_t timeout);
  157. void setFollowRedirects(bool follow);
  158. void setRedirectLimit(uint16_t limit); // max redirects to follow for a single request
  159. bool setURL(const String& url); // handy for handling redirects
  160. void useHTTP10(bool usehttp10 = true);
  161. /// request handling
  162. int GET();
  163. int POST(const uint8_t* payload, size_t size);
  164. int POST(const String& payload);
  165. int PUT(const uint8_t* payload, size_t size);
  166. int PUT(const String& payload);
  167. int PATCH(const uint8_t* payload, size_t size);
  168. int PATCH(const String& payload);
  169. int sendRequest(const char* type, const String& payload);
  170. int sendRequest(const char* type, const uint8_t* payload = NULL, size_t size = 0);
  171. int sendRequest(const char* type, Stream * stream, size_t size = 0);
  172. void addHeader(const String& name, const String& value, bool first = false, bool replace = true);
  173. /// Response handling
  174. void collectHeaders(const char* headerKeys[], const size_t headerKeysCount);
  175. String header(const char* name); // get request header value by name
  176. String header(size_t i); // get request header value by number
  177. String headerName(size_t i); // get request header name by number
  178. int headers(); // get header count
  179. bool hasHeader(const char* name); // check if header exists
  180. int getSize(void);
  181. const String& getLocation(void); // Location header from redirect if 3XX
  182. WiFiClient& getStream(void);
  183. WiFiClient* getStreamPtr(void);
  184. int writeToStream(Stream* stream);
  185. const String& getString(void);
  186. static String errorToString(int error);
  187. protected:
  188. struct RequestArgument {
  189. String key;
  190. String value;
  191. };
  192. bool beginInternal(const String& url, const char* expectedProtocol);
  193. void disconnect(bool preserveClient = false);
  194. void clear();
  195. int returnError(int error);
  196. bool connect(void);
  197. bool sendHeader(const char * type);
  198. int handleHeaderResponse();
  199. int writeToStreamDataBlock(Stream * stream, int len);
  200. #if HTTPCLIENT_1_1_COMPATIBLE
  201. TransportTraitsPtr _transportTraits;
  202. std::unique_ptr<WiFiClient> _tcpDeprecated;
  203. #endif
  204. WiFiClient* _client;
  205. /// request handling
  206. String _host;
  207. uint16_t _port = 0;
  208. bool _reuse = true;
  209. uint16_t _tcpTimeout = HTTPCLIENT_DEFAULT_TCP_TIMEOUT;
  210. bool _useHTTP10 = false;
  211. String _uri;
  212. String _protocol;
  213. String _headers;
  214. String _userAgent;
  215. String _base64Authorization;
  216. /// Response handling
  217. RequestArgument* _currentHeaders = nullptr;
  218. size_t _headerKeysCount = 0;
  219. int _returnCode = 0;
  220. int _size = -1;
  221. bool _canReuse = false;
  222. bool _followRedirects = false;
  223. uint16_t _redirectCount = 0;
  224. uint16_t _redirectLimit = 10;
  225. String _location;
  226. transferEncoding_t _transferEncoding = HTTPC_TE_IDENTITY;
  227. std::unique_ptr<StreamString> _payload;
  228. };
  229. #endif /* ESP8266HTTPClient_H_ */