1515//
1616#include " Firebase.h"
1717
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
1924
2025namespace {
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+
2129String makeFirebaseURL (const String& path, const String& auth) {
2230 String url;
2331 if (path[0 ] != ' /' ) {
@@ -33,75 +41,45 @@ String makeFirebaseURL(const String& path, const String& auth) {
3341} // namespace
3442
3543Firebase::Firebase (const String& host) : host_(host) {
36- http_.reset (FirebaseHttpClient::create ());
37- http_->setReuseConnection (true );
44+ http_.setReuse (true );
3845}
3946
4047Firebase& Firebase::auth (const String& auth) {
4148 auth_ = auth;
4249 return *this ;
4350}
4451
45- const String& Firebase::auth () {
46- return auth_;
47- }
48-
4952FirebaseGet 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_);
5554}
5655
5756FirebaseSet 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_);
6558}
6659
6760FirebasePush 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_);
7362}
7463
7564FirebaseRemove 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_);
8266}
8367
8468FirebaseStream Firebase::stream (const String& path) {
8569 // 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_);
9371}
9472
9573// FirebaseCall
9674FirebaseCall::FirebaseCall (const String& host, const String& auth,
9775 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 );
10280
10381 bool followRedirect = false ;
104- if (String ( method) == " STREAM" ) {
82+ if (method == " STREAM" ) {
10583 method = " GET" ;
10684 http_->addHeader (" Accept" , " text/event-stream" );
10785 followRedirect = true ;
@@ -112,24 +90,26 @@ FirebaseCall::FirebaseCall(const String& host, const String& auth,
11290 http_->collectHeaders (headers, 1 );
11391 }
11492
115- int status = http_->sendRequest (method, data);
93+ int status = http_->sendRequest (method, ( uint8_t *) data. c_str (), data. length () );
11694
11795 // TODO: Add a max redirect check
11896 if (followRedirect) {
119- while (status == HttpStatus::TEMPORARY_REDIRECT ) {
97+ while (status == HTTP_CODE_TEMPORARY_REDIRECT ) {
12098 String location = http_->header (" Location" );
121- http_->setReuseConnection (false );
99+ http_->setReuse (false );
122100 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 );
126104 }
127105 }
128106
129107 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
133113 }
134114
135115 // if not streaming.
@@ -148,14 +128,14 @@ const JsonObject& FirebaseCall::json() {
148128// FirebaseGet
149129FirebaseGet::FirebaseGet (const String& host, const String& auth,
150130 const String& path,
151- FirebaseHttpClient * http)
131+ HTTPClient * http)
152132 : FirebaseCall(host, auth, " GET" , path, " " , http) {
153133}
154134
155135// FirebaseSet
156136FirebaseSet::FirebaseSet (const String& host, const String& auth,
157137 const String& path, const String& value,
158- FirebaseHttpClient * http)
138+ HTTPClient * http)
159139 : FirebaseCall(host, auth, " PUT" , path, value, http) {
160140 if (!error ()) {
161141 // TODO: parse json
@@ -165,24 +145,24 @@ FirebaseSet::FirebaseSet(const String& host, const String& auth,
165145// FirebasePush
166146FirebasePush::FirebasePush (const String& host, const String& auth,
167147 const String& path, const String& value,
168- FirebaseHttpClient * http)
148+ HTTPClient * http)
169149 : FirebaseCall(host, auth, " POST" , path, value, http) {
170150 if (!error ()) {
171- // name_ = json()["name"].as<const char*>();
151+ name_ = json ()[" name" ].as <const char *>();
172152 }
173153}
174154
175155// FirebasePush
176156FirebaseRemove::FirebaseRemove (const String& host, const String& auth,
177157 const String& path,
178- FirebaseHttpClient * http)
158+ HTTPClient * http)
179159 : FirebaseCall(host, auth, " DELETE" , path, " " , http) {
180160}
181161
182162// FirebaseStream
183163FirebaseStream::FirebaseStream (const String& host, const String& auth,
184164 const String& path,
185- FirebaseHttpClient * http)
165+ HTTPClient * http)
186166 : FirebaseCall(host, auth, " STREAM" , path, " " , http) {
187167}
188168
0 commit comments