|
| 1 | +/* |
| 2 | + * HTTP Client Wrapper based on WiFiNINA library, version 1.0.1 |
| 3 | + * |
| 4 | + * |
| 5 | + * This library required WiFiNINA Library to be installed. |
| 6 | + * https://github.com/arduino-libraries/WiFiNINA |
| 7 | + * |
| 8 | + * March 5, 2020 |
| 9 | + * |
| 10 | + * |
| 11 | + * |
| 12 | + * The MIT License (MIT) |
| 13 | + * Copyright (c) 2019 K. Suwatchai (Mobizt) |
| 14 | + * |
| 15 | + * |
| 16 | + * Permission is hereby granted, free of charge, to any person returning a copy of |
| 17 | + * this software and associated documentation files (the "Software"), to deal in |
| 18 | + * the Software without restriction, including without limitation the rights to |
| 19 | + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
| 20 | + * the Software, and to permit persons to whom the Software is furnished to do so, |
| 21 | + * subject to the following conditions: |
| 22 | + * |
| 23 | + * The above copyright notice and this permission notice shall be included in all |
| 24 | + * copies or substantial portions of the Software. |
| 25 | + * |
| 26 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 27 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
| 28 | + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 29 | + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
| 30 | + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 31 | + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 32 | +*/ |
| 33 | + |
| 34 | +#ifndef Firebase_Arduino_WiFiNINA_HTTPClient_H |
| 35 | +#define Firebase_Arduino_WiFiNINA_HTTPClient_H |
| 36 | + |
| 37 | +#include <Arduino.h> |
| 38 | +#include <SPI.h> |
| 39 | +#include <WiFiNINA.h> |
| 40 | + |
| 41 | + |
| 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 | +#define FIREBASE_ERROR_BUFFER_OVERFLOW (-13) |
| 55 | +#define FIREBASE_ERROR_DATA_TYPE_MISMATCH (-14) |
| 56 | +#define FIREBASE_ERROR_PATH_NOT_EXIST (-15) |
| 57 | + |
| 58 | +/// HTTP codes see RFC7231 |
| 59 | + |
| 60 | +#define _HTTP_CODE_OK 200 |
| 61 | +#define _HTTP_CODE_NON_AUTHORITATIVE_INFORMATION 203 |
| 62 | +#define _HTTP_CODE_NO_CONTENT 204 |
| 63 | +#define _HTTP_CODE_MOVED_PERMANENTLY 301 |
| 64 | +#define _HTTP_CODE_USE_PROXY 305 |
| 65 | +#define _HTTP_CODE_TEMPORARY_REDIRECT 307 |
| 66 | +#define _HTTP_CODE_PERMANENT_REDIRECT 308 |
| 67 | +#define _HTTP_CODE_BAD_REQUEST 400 |
| 68 | +#define _HTTP_CODE_UNAUTHORIZED 401 |
| 69 | +#define _HTTP_CODE_FORBIDDEN 403 |
| 70 | +#define _HTTP_CODE_NOT_FOUND 404 |
| 71 | +#define _HTTP_CODE_METHOD_NOT_ALLOWED 405 |
| 72 | +#define _HTTP_CODE_NOT_ACCEPTABLE 406 |
| 73 | +#define _HTTP_CODE_PROXY_AUTHENTICATION_REQUIRED 407 |
| 74 | +#define _HTTP_CODE_REQUEST_TIMEOUT 408 |
| 75 | +#define _HTTP_CODE_LENGTH_REQUIRED 411 |
| 76 | +#define _HTTP_CODE_PAYLOAD_TOO_LARGE 413 |
| 77 | +#define _HTTP_CODE_URI_TOO_LONG 414 |
| 78 | +#define _HTTP_CODE_MISDIRECTED_REQUEST 421 |
| 79 | +#define _HTTP_CODE_UNPROCESSABLE_ENTITY 422 |
| 80 | +#define _HTTP_CODE_TOO_MANY_REQUESTS 429 |
| 81 | +#define _HTTP_CODE_REQUEST_HEADER_FIELDS_TOO_LARGE 431 |
| 82 | +#define _HTTP_CODE_INTERNAL_SERVER_ERROR 500 |
| 83 | +#define _HTTP_CODE_NOT_IMPLEMENTED 501 |
| 84 | +#define _HTTP_CODE_BAD_GATEWAY 502 |
| 85 | +#define _HTTP_CODE_SERVICE_UNAVAILABLE 503 |
| 86 | +#define _HTTP_CODE_GATEWAY_TIMEOUT 504 |
| 87 | +#define _HTTP_CODE_HTTP_VERSION_NOT_SUPPORTED 505 |
| 88 | +#define _HTTP_CODE_LOOP_DETECTED 508 |
| 89 | +#define _HTTP_CODE_NETWORK_AUTHENTICATION_REQUIRED 511 |
| 90 | + |
| 91 | +#define _HOST_LENGTH 100 |
| 92 | + |
| 93 | +class FirebaseHTTPClient |
| 94 | +{ |
| 95 | + public: |
| 96 | + FirebaseHTTPClient(); |
| 97 | + ~FirebaseHTTPClient(); |
| 98 | + |
| 99 | + bool http_begin(const char *host, uint16_t port); |
| 100 | + bool http_connected(void); |
| 101 | + int http_sendRequest(const char *header, const char *payload); |
| 102 | + WiFiSSLClient client; |
| 103 | + uint16_t netClientTimeout = 5000; |
| 104 | + |
| 105 | + protected: |
| 106 | + bool http_connect(void); |
| 107 | + bool http_sendHeader(const char *header); |
| 108 | + String _host = ""; |
| 109 | + uint16_t _port = 0; |
| 110 | +}; |
| 111 | + |
| 112 | +#endif /* Firebase_Arduino_WiFiNINA_HTTPClient_H */ |
0 commit comments