forked from FirebaseExtended/firebase-arduino
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFirebase.cpp
157 lines (130 loc) · 4.42 KB
/
Firebase.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
//
// Copyright 2015 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include "Firebase.h"
namespace {
const char* kFirebaseFingerprint = "7A 54 06 9B DC 7A 25 B3 86 8D 66 53 48 2C 0B 96 42 C7 B3 0A";
const uint16_t kFirebasePort = 443;
String makeUrl(const String& path, const String& auth) {
String url;
if (path[0] != '/') {
url = "/";
}
url += path + ".json";
if (auth.length() > 0) {
url += "?auth=" + auth;
}
return url;
}
} // namespace
Firebase::Firebase(const String& host) : host_(host) {
http_.setReuse(true);
}
Firebase& Firebase::auth(const String& auth) {
auth_ = auth;
return *this;
}
FirebaseCall Firebase::get(const String& path) {
return FirebaseCall(host_, auth_, "GET", path, &http_);
}
FirebaseCall Firebase::push(const String& path, const String& value) {
return FirebaseCall(host_, auth_, "POST", path, value, &http_);
}
FirebaseCall Firebase::remove(const String& path) {
return FirebaseCall(host_, auth_, "DELETE", path, &http_);
}
FirebaseEventStream Firebase::stream(const String& path) {
return FirebaseEventStream(host_, auth_, path);
}
/* FirebaseCall */
FirebaseCall::FirebaseCall(const String& host, const String& auth,
const char* method, const String& path, const String& value,
HTTPClient* http) : http_(http) {
const String url = makeUrl(path, auth);
http_->begin(host.c_str(), kFirebasePort, url.c_str(), true, kFirebaseFingerprint);
status_ = http_->sendRequest(method, (uint8_t*)value.c_str(), value.length());
if (isError()) {
error_message_ = String(method) + " " + url + ": " + HTTPClient::errorToString(status_);
}
}
FirebaseCall::FirebaseCall(const String& host, const String& auth,
const char* method, const String& path,
HTTPClient* http) : FirebaseCall(host, auth, method, path, "", http) {
}
bool FirebaseCall::isOk() const {
return status_ == HTTP_CODE_OK;
}
bool FirebaseCall::isError() const {
return status_ < 0;
}
String FirebaseCall::errorMessage() const {
return error_message_;
}
String FirebaseCall::rawResponse() {
return http_->getString();
}
/* FirebaseEventStream */
FirebaseEventStream::FirebaseEventStream(const String& host, const String& auth,
const String& path) {
const String url = makeUrl(path, auth);
http_.setReuse(true);
http_.begin(host.c_str(), kFirebasePort, url.c_str(), true,
kFirebaseFingerprint);
const char* headers[] = {"Location"};
http_.collectHeaders(headers, 1);
http_.addHeader("Accept", "text/event-stream");
status_ = http_.sendRequest("GET", (uint8_t*)NULL, 0);
String location;
// TODO(proppy): Add a max redirect check
while (status_ == HTTP_CODE_TEMPORARY_REDIRECT) {
location = http_.header("Location");
http_.setReuse(false);
http_.end();
http_.setReuse(true);
http_.begin(location, kFirebaseFingerprint);
status_ = http_.sendRequest("GET", (uint8_t*)NULL, 0);
}
if (status_ != 200) {
error_message_ = "stream " + location + ": "
+ HTTPClient::errorToString(status_);
}
}
bool FirebaseEventStream::connected() {
return http_.connected();
}
bool FirebaseEventStream::available() {
return http_.getStreamPtr()->available();
}
FirebaseEventStream::Event FirebaseEventStream::read(String& event) {
auto client = http_.getStreamPtr();
Event type;
String typeStr = client->readStringUntil('\n').substring(7);
if (typeStr == "put") {
type = FirebaseEventStream::Event::PUT;
} else if (typeStr == "patch") {
type = FirebaseEventStream::Event::PATCH;
} else {
type = FirebaseEventStream::Event::UNKNOWN;
}
event = client->readStringUntil('\n').substring(6);
client->readStringUntil('\n'); // consume separator
return type;
}
bool FirebaseEventStream::isError() const {
return status_ < 0;
}
String FirebaseEventStream::errorMessage() const {
return error_message_;
}