15
15
//
16
16
#include " Firebase.h"
17
17
18
- using std::unique_ptr;
18
+ // Detect whether stable version of HTTP library is installed instead of
19
+ // master branch and patch in missing status and methods.
20
+ #ifndef HTTP_CODE_TEMPORARY_REDIRECT
21
+ #define HTTP_CODE_TEMPORARY_REDIRECT 307
22
+ #define USE_ESP_ARDUINO_CORE_2_0_0
23
+ #endif
19
24
20
25
namespace {
26
+ const char * kFirebaseFingerprint = " 7A 54 06 9B DC 7A 25 B3 86 8D 66 53 48 2C 0B 96 42 C7 B3 0A" ;
27
+ const uint16_t kFirebasePort = 443 ;
28
+
21
29
String makeFirebaseURL (const String& path, const String& auth) {
22
30
String url;
23
31
if (path[0 ] != ' /' ) {
@@ -33,75 +41,45 @@ String makeFirebaseURL(const String& path, const String& auth) {
33
41
} // namespace
34
42
35
43
Firebase::Firebase (const String& host) : host_(host) {
36
- http_.reset (FirebaseHttpClient::create ());
37
- http_->setReuseConnection (true );
44
+ http_.setReuse (true );
38
45
}
39
46
40
47
Firebase& Firebase::auth (const String& auth) {
41
48
auth_ = auth;
42
49
return *this ;
43
50
}
44
51
45
- const String& Firebase::auth () {
46
- return auth_;
47
- }
48
-
49
52
FirebaseGet Firebase::get (const String& path) {
50
- return FirebaseGet (host_, auth_, path, http_.get ());
51
- }
52
-
53
- unique_ptr<FirebaseGet> Firebase::getPtr (const String& path) {
54
- return unique_ptr<FirebaseGet>(new FirebaseGet (host_, auth_, path, http_.get ()));
53
+ return FirebaseGet (host_, auth_, path, &http_);
55
54
}
56
55
57
56
FirebaseSet Firebase::set (const String& path, const String& value) {
58
- return FirebaseSet (host_, auth_, path, value, http_.get ());
59
- }
60
-
61
- unique_ptr<FirebaseSet> Firebase::setPtr (const String& path,
62
- const String& value) {
63
- return unique_ptr<FirebaseSet>(
64
- new FirebaseSet (host_, auth_, path, value, http_.get ()));
57
+ return FirebaseSet (host_, auth_, path, value, &http_);
65
58
}
66
59
67
60
FirebasePush Firebase::push (const String& path, const String& value) {
68
- return FirebasePush (host_, auth_, path, value, http_.get ());
69
- }
70
- unique_ptr<FirebasePush> Firebase::pushPtr (const String& path, const String& value) {
71
- return unique_ptr<FirebasePush>(
72
- new FirebasePush (host_, auth_, path, value, http_.get ()));
61
+ return FirebasePush (host_, auth_, path, value, &http_);
73
62
}
74
63
75
64
FirebaseRemove Firebase::remove (const String& path) {
76
- return FirebaseRemove (host_, auth_, path, http_.get ());
77
- }
78
-
79
- unique_ptr<FirebaseRemove> Firebase::removePtr (const String& path) {
80
- return unique_ptr<FirebaseRemove>(
81
- new FirebaseRemove (host_, auth_, path, http_.get ()));
65
+ return FirebaseRemove (host_, auth_, path, &http_);
82
66
}
83
67
84
68
FirebaseStream Firebase::stream (const String& path) {
85
69
// TODO: create new client dedicated to stream.
86
- return FirebaseStream (host_, auth_, path, http_.get ());
87
- }
88
-
89
- unique_ptr<FirebaseStream> Firebase::streamPtr (const String& path) {
90
- // TODO: create new client dedicated to stream.
91
- return unique_ptr<FirebaseStream>(
92
- new FirebaseStream (host_, auth_, path, http_.get ()));
70
+ return FirebaseStream (host_, auth_, path, &http_);
93
71
}
94
72
95
73
// FirebaseCall
96
74
FirebaseCall::FirebaseCall (const String& host, const String& auth,
97
75
const char * method, const String& path,
98
- const String& data, FirebaseHttpClient * http) : http_(http) {
99
- String path_with_auth = makeFirebaseURL (path, auth);
100
- http_->setReuseConnection (true );
101
- http_->begin (host, path_with_auth );
76
+ const String& data, HTTPClient * http) : http_(http) {
77
+ String url = makeFirebaseURL (path, auth);
78
+ http_->setReuse (true );
79
+ http_->begin (host, kFirebasePort , url, true , kFirebaseFingerprint );
102
80
103
81
bool followRedirect = false ;
104
- if (String ( method) == " STREAM" ) {
82
+ if (method == " STREAM" ) {
105
83
method = " GET" ;
106
84
http_->addHeader (" Accept" , " text/event-stream" );
107
85
followRedirect = true ;
@@ -112,24 +90,26 @@ FirebaseCall::FirebaseCall(const String& host, const String& auth,
112
90
http_->collectHeaders (headers, 1 );
113
91
}
114
92
115
- int status = http_->sendRequest (method, data);
93
+ int status = http_->sendRequest (method, ( uint8_t *) data. c_str (), data. length () );
116
94
117
95
// TODO: Add a max redirect check
118
96
if (followRedirect) {
119
- while (status == HttpStatus::TEMPORARY_REDIRECT ) {
97
+ while (status == HTTP_CODE_TEMPORARY_REDIRECT ) {
120
98
String location = http_->header (" Location" );
121
- http_->setReuseConnection (false );
99
+ http_->setReuse (false );
122
100
http_->end ();
123
- http_->setReuseConnection (true );
124
- http_->begin (location);
125
- status = http_->sendRequest (" GET" , String () );
101
+ http_->setReuse (true );
102
+ http_->begin (location, kFirebaseFingerprint );
103
+ status = http_->sendRequest (" GET" , ( uint8_t *) NULL , 0 );
126
104
}
127
105
}
128
106
129
107
if (status != 200 ) {
130
- error_ = FirebaseError (status,
131
- String (method) + " " + path_with_auth +
132
- " : " + http_->errorToString (status));
108
+ #ifdef USE_ESP_ARDUINO_CORE_2_0_0
109
+ error_ = FirebaseError (status, String (method) + " " + url + " : " + status);
110
+ #else
111
+ error_ = FirebaseError (status, String (method) + " " + url + " : " + HTTPClient::errorToString (status));
112
+ #endif
133
113
}
134
114
135
115
// if not streaming.
@@ -148,14 +128,14 @@ const JsonObject& FirebaseCall::json() {
148
128
// FirebaseGet
149
129
FirebaseGet::FirebaseGet (const String& host, const String& auth,
150
130
const String& path,
151
- FirebaseHttpClient * http)
131
+ HTTPClient * http)
152
132
: FirebaseCall(host, auth, " GET" , path, " " , http) {
153
133
}
154
134
155
135
// FirebaseSet
156
136
FirebaseSet::FirebaseSet (const String& host, const String& auth,
157
137
const String& path, const String& value,
158
- FirebaseHttpClient * http)
138
+ HTTPClient * http)
159
139
: FirebaseCall(host, auth, " PUT" , path, value, http) {
160
140
if (!error ()) {
161
141
// TODO: parse json
@@ -165,24 +145,24 @@ FirebaseSet::FirebaseSet(const String& host, const String& auth,
165
145
// FirebasePush
166
146
FirebasePush::FirebasePush (const String& host, const String& auth,
167
147
const String& path, const String& value,
168
- FirebaseHttpClient * http)
148
+ HTTPClient * http)
169
149
: FirebaseCall(host, auth, " POST" , path, value, http) {
170
150
if (!error ()) {
171
- // name_ = json()["name"].as<const char*>();
151
+ name_ = json ()[" name" ].as <const char *>();
172
152
}
173
153
}
174
154
175
155
// FirebasePush
176
156
FirebaseRemove::FirebaseRemove (const String& host, const String& auth,
177
157
const String& path,
178
- FirebaseHttpClient * http)
158
+ HTTPClient * http)
179
159
: FirebaseCall(host, auth, " DELETE" , path, " " , http) {
180
160
}
181
161
182
162
// FirebaseStream
183
163
FirebaseStream::FirebaseStream (const String& host, const String& auth,
184
164
const String& path,
185
- FirebaseHttpClient * http)
165
+ HTTPClient * http)
186
166
: FirebaseCall(host, auth, " STREAM" , path, " " , http) {
187
167
}
188
168
0 commit comments