Skip to content
This repository was archived by the owner on Mar 17, 2025. It is now read-only.

FirebaseHttpClient: add forceReuse #260

Merged
merged 3 commits into from
Oct 27, 2017
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion src/FirebaseHttpClient_Esp8266.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,44 @@
#define USE_ESP_ARDUINO_CORE_2_0_0
#endif

// Firebase now returns `Connection: close` after REST streaming redirection.
//
// Override the built-in ESP8266HTTPClient to *not* close the
// connection if forceReuse it set to `true`.
class ForceReuseHTTPClient : public HTTPClient {
public:
void end() {
if(connected()) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

another possibility is to call HTTPClient::end here with _canReuse set to true before it?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that better only because it deviates less from the base so as the base changes we can merge with impunity.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

if(_tcp->available() > 0) {
DEBUG_HTTPCLIENT("[HTTP-Client][end] still data in buffer (%d), clean up.\n", _tcp->available());
while(_tcp->available() > 0) {
_tcp->read();
}
}
if(_reuse && (_canReuse || _forceReuse)) {
DEBUG_HTTPCLIENT("[HTTP-Client][end] tcp keep open for reuse\n");
} else {
DEBUG_HTTPCLIENT("[HTTP-Client][end] tcp stop\n");
_tcp->stop();
}
} else {
DEBUG_HTTPCLIENT("[HTTP-Client][end] tcp is closed\n");
}
}
void forceReuse(bool forceReuse) {
_forceReuse = forceReuse;
}
protected:
bool _forceReuse = false;
};

class FirebaseHttpClientEsp8266 : public FirebaseHttpClient {
public:
FirebaseHttpClientEsp8266() {}

void setReuseConnection(bool reuse) override {
http_.setReuse(reuse);
http_.forceReuse(reuse);
}

void begin(const std::string& url) override {
Expand Down Expand Up @@ -64,7 +96,7 @@ class FirebaseHttpClientEsp8266 : public FirebaseHttpClient {
}

private:
HTTPClient http_;
ForceReuseHTTPClient http_;
};

FirebaseHttpClient* FirebaseHttpClient::create() {
Expand Down