This repository was archived by the owner on Mar 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 492
/
Copy pathFirebaseHttpClient_Esp8266.cpp
105 lines (85 loc) · 2.84 KB
/
FirebaseHttpClient_Esp8266.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "FirebaseHttpClient.h"
#include <stdio.h>
// The ordering of these includes matters greatly.
#include <WiFiClientSecure.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
// Detect whether stable version of HTTP library is installed instead of
// master branch and patch in missing status and methods.
#ifndef HTTP_CODE_TEMPORARY_REDIRECT
#define HTTP_CODE_TEMPORARY_REDIRECT 307
#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()) {
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 {
http_.begin(url.c_str(), kFirebaseFingerprint);
}
void begin(const std::string& host, const std::string& path) override {
http_.begin(host.c_str(), kFirebasePort, path.c_str(), kFirebaseFingerprint);
}
void end() override {
http_.end();
}
void addHeader(const std::string& name, const std::string& value) override {
http_.addHeader(name.c_str(), value.c_str());
}
void collectHeaders(const char* header_keys[], const int count) override {
http_.collectHeaders(header_keys, count);
}
std::string header(const std::string& name) override {
return http_.header(name.c_str()).c_str();
}
int sendRequest(const std::string& method, const std::string& data) override {
return http_.sendRequest(method.c_str(), (uint8_t*)data.c_str(), data.length());
}
std::string getString() override {
return http_.getString().c_str();
}
Stream* getStreamPtr() override {
return http_.getStreamPtr();
}
std::string errorToString(int error_code) override {
return HTTPClient::errorToString(error_code).c_str();
}
private:
ForceReuseHTTPClient http_;
};
FirebaseHttpClient* FirebaseHttpClient::create() {
return new FirebaseHttpClientEsp8266();
}