|
| 1 | + |
| 2 | +#include "FirebaseHttpClient.h" |
| 3 | + |
| 4 | +#include <stdio.h> |
| 5 | + |
| 6 | +// The ordering of these includes matters greatly. |
| 7 | +#include <WiFiClientSecure.h> |
| 8 | +#include <ESP8266WiFi.h> |
| 9 | +#include <ESP8266HTTPClient.h> |
| 10 | + |
| 11 | +// Detect whether stable version of HTTP library is installed instead of |
| 12 | +// master branch and patch in missing status and methods. |
| 13 | +#ifndef HTTP_CODE_TEMPORARY_REDIRECT |
| 14 | +#define HTTP_CODE_TEMPORARY_REDIRECT 307 |
| 15 | +#define USE_ESP_ARDUINO_CORE_2_0_0 |
| 16 | +#endif |
| 17 | + |
| 18 | +// Firebase now returns `Connection: close` after REST streaming redirection. |
| 19 | +// |
| 20 | +// Override the built-in ESP8266HTTPClient to *not* close the |
| 21 | +// connection if forceReuse it set to `true`. |
| 22 | +class ForceReuseHTTPClient : public HTTPClient { |
| 23 | +public: |
| 24 | + void end() { |
| 25 | + if (_forceReuse) { |
| 26 | + _canReuse = true; |
| 27 | + } |
| 28 | + HTTPClient::end(); |
| 29 | + } |
| 30 | + void forceReuse(bool forceReuse) { |
| 31 | + _forceReuse = forceReuse; |
| 32 | + } |
| 33 | +protected: |
| 34 | + bool _forceReuse = false; |
| 35 | +}; |
| 36 | + |
| 37 | +class FirebaseHttpClientEsp8266 : public FirebaseHttpClient { |
| 38 | + public: |
| 39 | + FirebaseHttpClientEsp8266() {} |
| 40 | + |
| 41 | + void setReuseConnection(bool reuse) override { |
| 42 | + http_.setReuse(reuse); |
| 43 | + http_.forceReuse(reuse); |
| 44 | + } |
| 45 | + |
| 46 | + void begin(const std::string& url) override { |
| 47 | + http_.begin(url.c_str(), kFirebaseFingerprint); |
| 48 | + } |
| 49 | + |
| 50 | + void begin(const std::string& host, const std::string& path) override { |
| 51 | + http_.begin(host.c_str(), kFirebasePort, path.c_str(), kFirebaseFingerprint); |
| 52 | + } |
| 53 | + |
| 54 | + void end() override { |
| 55 | + http_.end(); |
| 56 | + } |
| 57 | + |
| 58 | + void addHeader(const std::string& name, const std::string& value) override { |
| 59 | + http_.addHeader(name.c_str(), value.c_str()); |
| 60 | + } |
| 61 | + |
| 62 | + void collectHeaders(const char* header_keys[], const int count) override { |
| 63 | + http_.collectHeaders(header_keys, count); |
| 64 | + } |
| 65 | + |
| 66 | + std::string header(const std::string& name) override { |
| 67 | + return http_.header(name.c_str()).c_str(); |
| 68 | + } |
| 69 | + |
| 70 | + int sendRequest(const std::string& method, const std::string& data) override { |
| 71 | + return http_.sendRequest(method.c_str(), (uint8_t*)data.c_str(), data.length()); |
| 72 | + } |
| 73 | + |
| 74 | + std::string getString() override { |
| 75 | + return http_.getString().c_str(); |
| 76 | + } |
| 77 | + |
| 78 | + Stream* getStreamPtr() override { |
| 79 | + return http_.getStreamPtr(); |
| 80 | + } |
| 81 | + |
| 82 | + std::string errorToString(int error_code) override { |
| 83 | + return HTTPClient::errorToString(error_code).c_str(); |
| 84 | + } |
| 85 | + |
| 86 | + bool connected() override { |
| 87 | + return http_.connected(); |
| 88 | + } |
| 89 | + |
| 90 | + private: |
| 91 | + ForceReuseHTTPClient http_; |
| 92 | +}; |
| 93 | + |
| 94 | +FirebaseHttpClient* FirebaseHttpClient::create() { |
| 95 | + return new FirebaseHttpClientEsp8266(); |
| 96 | +} |
| 97 | + |