Skip to content

Commit 88170c8

Browse files
committed
Manually merge: "Report error when streaming loses connection. FirebaseExtended#179"
1 parent 2f76fae commit 88170c8

File tree

7 files changed

+165
-0
lines changed

7 files changed

+165
-0
lines changed

contrib/test/dummies/FirebaseHttpClient_dummy.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ class FirebaseHttpClientDummy : public FirebaseHttpClient {
2323
void addHeader(const std::string& UNUSED_ARG(name), const std::string& UNUSED_ARG(value)) override {
2424
}
2525

26+
bool connected() override {
27+
return true;
28+
}
29+
2630
void collectHeaders(const char* UNUSED_ARG(header_keys[]), const int UNUSED_ARG(count)) override {
2731
}
2832

src/FirebaseArduino.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,11 @@ void FirebaseArduino::stream(const String& path) {
156156

157157
bool FirebaseArduino::available() {
158158
if (stream_http_.get() == nullptr) {
159+
error_ = FirebaseError(FIREBASE_ERROR_CODES::STREAM_NOT_INITIALIZED, "HTTP stream is not initialized");
160+
return 0;
161+
}
162+
if (!stream_http_.get()->connected()) {
163+
error_ = FirebaseError(FIREBASE_ERROR_CODES::HTTP_CONNECTION_LOST, "Connection Lost");
159164
return 0;
160165
}
161166
auto client = stream_http_.get()->getStreamPtr();

src/FirebaseError.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
#ifndef firebase_error_h
22
#define firebase_error_h
33

4+
5+
// These error codes are used in addition to regular HTTP error codes.
6+
// Same error space is shared between HTTP errors and these values.
7+
enum FIREBASE_ERROR_CODES {
8+
HTTP_CONNECTION_LOST = -5,
9+
STREAM_NOT_INITIALIZED = -6
10+
};
11+
412
class FirebaseError {
513
public:
614
// Make it explicit that the empty constructor mean no error.

src/FirebaseHttpClient.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ class FirebaseHttpClient {
3333

3434
virtual std::string errorToString(int error_code) = 0;
3535

36+
virtual bool connected() = 0;
37+
3638
protected:
3739
static const uint16_t kFirebasePort = 443;
3840
};

src/FirebaseHttpClient.h~

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#ifndef FIREBASE_HTTP_CLIENT_H
2+
#define FIREBASE_HTTP_CLIENT_H
3+
4+
#include <string>
5+
6+
#include "Arduino.h"
7+
#include "Stream.h"
8+
9+
struct HttpStatus {
10+
static const int TEMPORARY_REDIRECT = 307;
11+
};
12+
13+
class FirebaseHttpClient {
14+
public:
15+
static FirebaseHttpClient* create();
16+
17+
virtual void setReuseConnection(bool reuse) = 0;
18+
virtual void begin(const std::string& url) = 0;
19+
virtual void begin(const std::string& host, const std::string& path) = 0;
20+
21+
virtual void end() = 0;
22+
23+
virtual void addHeader(const std::string& name, const std::string& value) = 0;
24+
virtual void collectHeaders(const char* header_keys[],
25+
const int header_key_count) = 0;
26+
virtual std::string header(const std::string& name) = 0;
27+
28+
virtual int sendRequest(const std::string& method, const std::string& data) = 0;
29+
30+
virtual std::string getString() = 0;
31+
32+
virtual Stream* getStreamPtr() = 0;
33+
34+
virtual std::string errorToString(int error_code) = 0;
35+
36+
virtual bool connected() = 0;
37+
38+
protected:
39+
static const uint16_t kFirebasePort = 443;
40+
};
41+
42+
static const char kFirebaseFingerprint[] =
43+
"B8 4F 40 70 0C 63 90 E0 07 E8 7D BD B4 11 D0 4A EA 9C 90 F6";
44+
45+
#endif // FIREBASE_HTTP_CLIENT_H

src/FirebaseHttpClient_Esp8266.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ class FirebaseHttpClientEsp8266 : public FirebaseHttpClient {
8383
return HTTPClient::errorToString(error_code).c_str();
8484
}
8585

86+
bool connected() override {
87+
return http_.connected();
88+
}
89+
8690
private:
8791
ForceReuseHTTPClient http_;
8892
};

src/FirebaseHttpClient_Esp8266.cpp~

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
2+
#include "FirebaseHttpClient.h"
3+
4+
#include <stdio.h>
5+
6+
// The ordering of these includes matters greatly.
7+
#include <WiFiClientSecure.h>
8+
#include <ESP8266WiFi.h>
9+
#include <ESP8266HTTPClient.h>
10+
11+
// Detect whether stable version of HTTP library is installed instead of
12+
// master branch and patch in missing status and methods.
13+
#ifndef HTTP_CODE_TEMPORARY_REDIRECT
14+
#define HTTP_CODE_TEMPORARY_REDIRECT 307
15+
#define USE_ESP_ARDUINO_CORE_2_0_0
16+
#endif
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 (_forceReuse) {
26+
_canReuse = true;
27+
}
28+
HTTPClient::end();
29+
}
30+
void forceReuse(bool forceReuse) {
31+
_forceReuse = forceReuse;
32+
}
33+
protected:
34+
bool _forceReuse = false;
35+
};
36+
37+
class FirebaseHttpClientEsp8266 : public FirebaseHttpClient {
38+
public:
39+
FirebaseHttpClientEsp8266() {}
40+
41+
void setReuseConnection(bool reuse) override {
42+
http_.setReuse(reuse);
43+
http_.forceReuse(reuse);
44+
}
45+
46+
void begin(const std::string& url) override {
47+
http_.begin(url.c_str(), kFirebaseFingerprint);
48+
}
49+
50+
void begin(const std::string& host, const std::string& path) override {
51+
http_.begin(host.c_str(), kFirebasePort, path.c_str(), kFirebaseFingerprint);
52+
}
53+
54+
void end() override {
55+
http_.end();
56+
}
57+
58+
void addHeader(const std::string& name, const std::string& value) override {
59+
http_.addHeader(name.c_str(), value.c_str());
60+
}
61+
62+
void collectHeaders(const char* header_keys[], const int count) override {
63+
http_.collectHeaders(header_keys, count);
64+
}
65+
66+
std::string header(const std::string& name) override {
67+
return http_.header(name.c_str()).c_str();
68+
}
69+
70+
int sendRequest(const std::string& method, const std::string& data) override {
71+
return http_.sendRequest(method.c_str(), (uint8_t*)data.c_str(), data.length());
72+
}
73+
74+
std::string getString() override {
75+
return http_.getString().c_str();
76+
}
77+
78+
Stream* getStreamPtr() override {
79+
return http_.getStreamPtr();
80+
}
81+
82+
std::string errorToString(int error_code) override {
83+
return HTTPClient::errorToString(error_code).c_str();
84+
}
85+
86+
bool connected() override {
87+
return http_.connected();
88+
}
89+
90+
private:
91+
ForceReuseHTTPClient http_;
92+
};
93+
94+
FirebaseHttpClient* FirebaseHttpClient::create() {
95+
return new FirebaseHttpClientEsp8266();
96+
}
97+

0 commit comments

Comments
 (0)