Skip to content

Instantly share code, notes, and snippets.

@salqadri
Created November 11, 2017 03:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save salqadri/5c833dd091c3309adc1008bea87632c4 to your computer and use it in GitHub Desktop.
Save salqadri/5c833dd091c3309adc1008bea87632c4 to your computer and use it in GitHub Desktop.
// Copyright 2017 Google Inc.
// Though this is NOT an officially supported Google product!
//
// 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.
#pragma once
// Must define a constant FIREBASE_ENDPOINT to be the database url
// static const char* FIREBASE_ENDPOINT = "https://myproject.firebaseio.com";
const uint8_t GeoTrustCA_cer[] PROGMEM = {
0x30, 0x82, 0x03, 0x54, 0x30, 0x82, 0x02, 0x3c, 0xa0, 0x03, 0x02, 0x01,
0x02, 0x02, 0x03, 0x02, 0x34, 0x56, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86,
0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00, 0x30, 0x42, 0x31,
0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53,
0x31, 0x16, 0x30, 0x14, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x13, 0x0d, 0x47,
0x65, 0x6f, 0x54, 0x72, 0x75, 0x73, 0x74, 0x20, 0x49, 0x6e, 0x63, 0x2e,
0x31, 0x1b, 0x30, 0x19, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x12, 0x47,
0x65, 0x6f, 0x54, 0x72, 0x75, 0x73, 0x74, 0x20, 0x47, 0x6c, 0x6f, 0x62,
0x61, 0x6c, 0x20, 0x43, 0x41, 0x30, 0x1e, 0x17, 0x0d, 0x30, 0x32, 0x30,
0x35, 0x32, 0x31, 0x30, 0x34, 0x30, 0x30, 0x30, 0x30, 0x5a, 0x17, 0x0d,
0x32, 0x32, 0x30, 0x35, 0x32, 0x31, 0x30, 0x34, 0x30, 0x30, 0x30, 0x30,
0x5a, 0x30, 0x42, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06,
0x13, 0x02, 0x55, 0x53, 0x31, 0x16, 0x30, 0x14, 0x06, 0x03, 0x55, 0x04,
0x0a, 0x13, 0x0d, 0x47, 0x65, 0x6f, 0x54, 0x72, 0x75, 0x73, 0x74, 0x20,
0x49, 0x6e, 0x63, 0x2e, 0x31, 0x1b, 0x30, 0x19, 0x06, 0x03, 0x55, 0x04,
0x03, 0x13, 0x12, 0x47, 0x65, 0x6f, 0x54, 0x72, 0x75, 0x73, 0x74, 0x20,
0x47, 0x6c, 0x6f, 0x62, 0x61, 0x6c, 0x20, 0x43, 0x41, 0x30, 0x82, 0x01,
0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01,
0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00, 0x30, 0x82, 0x01,
0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xda, 0xcc, 0x18, 0x63, 0x30, 0xfd,
0xf4, 0x17, 0x23, 0x1a, 0x56, 0x7e, 0x5b, 0xdf, 0x3c, 0x6c, 0x38, 0xe4,
0x71, 0xb7, 0x78, 0x91, 0xd4, 0xbc, 0xa1, 0xd8, 0x4c, 0xf8, 0xa8, 0x43,
0xb6, 0x03, 0xe9, 0x4d, 0x21, 0x07, 0x08, 0x88, 0xda, 0x58, 0x2f, 0x66,
0x39, 0x29, 0xbd, 0x05, 0x78, 0x8b, 0x9d, 0x38, 0xe8, 0x05, 0xb7, 0x6a,
0x7e, 0x71, 0xa4, 0xe6, 0xc4, 0x60,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0x30, 0xf4, 0x5f, 0xc0, 0x2b, 0xe8, 0x71, 0x6b, 0xe4, 0xf9, 0x02, 0x03,
0x01, 0x00, 0x01, 0xa3, 0x53, 0x30, 0x51, 0x30, 0x0f, 0x06, 0x03, 0x55,
0x1d, 0x13, 0x01, 0x01, 0xff, 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xff,
0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14, 0xc0,
0x7a, 0x98, 0x68, 0x8d, 0x89, 0xfb, 0xab, 0x05, 0x64, 0x0c, 0x11, 0x7d,
0xaa, 0x7d, 0x65, 0xb8, 0xca, 0xcc, 0x4e, 0x30, 0x1f, 0x06, 0x03, 0x55,
0x1d, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80, 0x14, 0xc0, 0x7a, 0x98, 0x68,
0x8d, 0x89, 0xfb, 0xab, 0x05, 0x64, 0x0c, 0x11, 0x7d, 0xaa, 0x7d, 0x65,
0xb8, 0xca, 0xcc, 0x4e, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00,
0x35, 0xe3, 0x29, 0x6a, 0xe5, 0x2f, 0x5d, 0x54, 0x8e, 0x29, 0x50, 0x94,
0x9f, 0x99, 0x1a, 0x14, 0xe4, 0x8f, 0x78, 0x2a, 0x62, 0x94, 0xa2, 0x27,
0x67, 0x9e, 0xd0, 0xcf, 0x1a, 0x5e, 0x47, 0xe9, 0xc1, 0xb2, 0xa4, 0xcf,
0xdd, 0x41, 0x1a, 0x05, 0x4e, 0x9b, 0x4b, 0xee, 0x4a, 0x6f, 0x55, 0x52,
0xb3, 0x24, 0xa1, 0x37, 0x0a, 0xeb, 0x64, 0x76, 0x2a, 0x2e, 0x2c, 0xf3,
0xfd, 0x3b, 0x75, 0x90, 0xbf, 0xfa, 0x71, 0xd8, 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a1, 0xcb, 0xe6, 0x33
};
#ifdef ESP32
#include <HTTPClient.h>
#else
#include <ESP8266HTTPClient.h>
#endif
// method must be either "PUT", "POST" or "PATCH". "GET" will not end up returning what is got.
bool FirebaseWrite(HTTPClient* http, const String& method, const String& auth_token, const String& path, const String& json)
{
String url = FIREBASE_ENDPOINT;
url += path + F(".json?print=silent&auth=");
url += auth_token;
#ifdef ESP32
http->setReuse(true);
http->useHTTP10(false);
#else
http->setReuse(false);
http->useHTTP10(true);
#endif
Serial.print(F("curl -X "));
Serial.print(method);
#ifdef ESP32
Serial.print(F(" -H 'User-Agent: ESP32HTTPClient' -d '"));
Serial.print(json);
Serial.print(F("' "));
Serial.println(url);
bool result = http->begin(url, GeoTrustCA_cer, sizeof(GeoTrustCA_cer));
if (!result) {
Serial.print(F("HTTP begin failed\n"));
return false;
}
http->addHeader(F("User-Agent"), F("ESP32HTTPClient"));
#else
Serial.print(F(" -H 'User-Agent: ESP8266HTTPClient' -d '"));
Serial.print(json);
Serial.print(F("' "));
Serial.println(url);
if (!http->begin(url)) {
Serial.print(F("HTTP begin failed\n"));
return false;
}
if (!http->setRootCA_P((PGM_VOID_P*) GeoTrustCA_cer, sizeof(GeoTrustCA_cer)))
{
Serial.print(F("Failed to load cert\n"));
http->end();
return false;
}
http->addHeader(F("User-Agent"), F("ESP8266HTTPClient"));
#endif
uint16_t httpCode = http->sendRequest(method.c_str(), json);
if (httpCode / 100 == 2)
{
http->end();
return true;
}
else
{
Serial.print(F("HTTP Failure code: "));
Serial.println(httpCode);
}
http->end();
return false;
}
bool FirebaseStream(HTTPClient** http, const String& auth_token, const String& path)
{
HTTPClient* firestream = *http;
String url = FIREBASE_ENDPOINT;
url += path + F(".json?auth=");
url += auth_token;
if (firestream != 0) {
#ifdef ESP32
firestream->setReuse(true);
#else
firestream->setReuse(false);
#endif
firestream->end();
}
#ifdef ESP32
bool result = firestream->begin(url, GeoTrustCA_cer, sizeof(GeoTrustCA_cer));
if (!result) {
Serial.print(F("HTTP begin failed\n"));
return false;
}
#else
if (!firestream->begin(url)) {
Serial.print(F("HTTP begin failed\n"));
return false;
}
if (!firestream->setRootCA_P((PGM_VOID_P*) GeoTrustCA_cer, sizeof(GeoTrustCA_cer)))
{
Serial.print(F("Failed to load cert\n"));
firestream->end();
return false;
}
#endif
firestream->addHeader(F("Accept"), F("text/event-stream"));
const char* headers[] = {"Location"};
firestream->collectHeaders(headers, 1);
int status = firestream->GET();
while (status == 307) {
String location = firestream->header("Location");
#ifdef ESP32
firestream->setReuse(true);
#else
firestream->setReuse(false);
#endif
firestream->end();
#ifndef ESP32
delete firestream;
*http = firestream = new HTTPClient();
firestream->setReuse(false);
firestream->useHTTP10(true);
#endif
#ifdef ESP32
bool result = firestream->begin(url, GeoTrustCA_cer, sizeof(GeoTrustCA_cer));
if (!result) {
Serial.print(F("HTTP begin failed\n"));
return false;
}
#else
firestream->begin(location);
if (!firestream->setRootCA_P((PGM_VOID_P*) GeoTrustCA_cer, sizeof(GeoTrustCA_cer)))
{
Serial.print(F("Failed to load cert\n"));
firestream->end();
return false;
}
#endif
status = firestream->GET();
}
if (status / 100 != 2) {
Serial.print(F("ERROR: Failed to stream "));
Serial.println(url);
Serial.print(F("Http result: "));
Serial.println(status);
firestream->end();
#ifndef ESP32
delete firestream;
*http = firestream = new HTTPClient();
firestream->setReuse(false);
firestream->useHTTP10(true);
#endif
return false;
}
if (firestream->getStreamPtr() == 0) {
Serial.print(F("Stream not initialized for some reason\n"));
firestream->end();
return false;
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment