|
15 | 15 | #define USE_ESP_ARDUINO_CORE_2_0_0
|
16 | 16 | #endif
|
17 | 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(connected()) { |
| 26 | + if(_tcp->available() > 0) { |
| 27 | + DEBUG_HTTPCLIENT("[HTTP-Client][end] still data in buffer (%d), clean up.\n", _tcp->available()); |
| 28 | + while(_tcp->available() > 0) { |
| 29 | + _tcp->read(); |
| 30 | + } |
| 31 | + } |
| 32 | + if(_reuse && (_canReuse || _forceReuse)) { |
| 33 | + DEBUG_HTTPCLIENT("[HTTP-Client][end] tcp keep open for reuse\n"); |
| 34 | + } else { |
| 35 | + DEBUG_HTTPCLIENT("[HTTP-Client][end] tcp stop\n"); |
| 36 | + _tcp->stop(); |
| 37 | + } |
| 38 | + } else { |
| 39 | + DEBUG_HTTPCLIENT("[HTTP-Client][end] tcp is closed\n"); |
| 40 | + } |
| 41 | + } |
| 42 | + void forceReuse(bool forceReuse) { |
| 43 | + _forceReuse = forceReuse; |
| 44 | + } |
| 45 | +protected: |
| 46 | + bool _forceReuse = false; |
| 47 | +}; |
| 48 | + |
18 | 49 | class FirebaseHttpClientEsp8266 : public FirebaseHttpClient {
|
19 | 50 | public:
|
20 | 51 | FirebaseHttpClientEsp8266() {}
|
21 | 52 |
|
22 |
| - void setReuseConnection(bool reuse) override { |
| 53 | + void setReuseConnection(bool reuse) override { |
23 | 54 | http_.setReuse(reuse);
|
| 55 | + http_.forceReuse(reuse); |
24 | 56 | }
|
25 | 57 |
|
26 | 58 | void begin(const std::string& url) override {
|
@@ -64,7 +96,7 @@ class FirebaseHttpClientEsp8266 : public FirebaseHttpClient {
|
64 | 96 | }
|
65 | 97 |
|
66 | 98 | private:
|
67 |
| - HTTPClient http_; |
| 99 | + ForceReuseHTTPClient http_; |
68 | 100 | };
|
69 | 101 |
|
70 | 102 | FirebaseHttpClient* FirebaseHttpClient::create() {
|
|
0 commit comments