Skip to content

Pass Client parameter to httpUpdate to use any BearSSL security method for a firmware update (with example) #4832

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 24 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
8c3edfe
Make HTTPClient suitabe for every Client
Jun 18, 2018
1bf21cd
Removed deprecated marking of the current api
Jun 18, 2018
7535a40
Removed deprecated marking of the current api
Jun 18, 2018
0a644f2
Updated examples to work with new API and added a https example
Jun 20, 2018
3cfa6e9
Merge branch 'master' into feature/httpclient_client_parameter
Jun 20, 2018
3ad2b07
Corrected minor formatting issues
Jun 20, 2018
57b3a9f
Merge branch 'master' of https://github.com/esp8266/Arduino into feat…
Jun 20, 2018
8e23576
Merge branch 'feature/httpclient_client_parameter' of https://github.…
Jun 20, 2018
a068dca
Corrected minor formatting issues
Jun 20, 2018
bf1188e
Corrected minor formatting issues
Jun 20, 2018
deb4a5b
Corrected minor formatting issues
Jun 20, 2018
b34228f
Include support for getStreamPtr
Jun 20, 2018
6088be9
TLS/SSL httpUpdate passing Client& parameter and with WiFiClientSecur…
Jun 20, 2018
86a2d12
Remove #include <Time.h>
Jun 20, 2018
1ed6f4a
Remove #include <Arduino.h>
Jun 21, 2018
6cc188e
Merge branch 'master' of https://github.com/esp8266/Arduino
Jul 2, 2018
48ac7ca
Merge branch 'master' into feature/httpupdate_client_parameter
Jul 3, 2018
2c00e09
Merge branch 'master' into feature/httpupdate_client_parameter
earlephilhower Jul 6, 2018
e136972
Merge branch 'master' of https://github.com/esp8266/Arduino
Jul 15, 2018
1101809
Merge branch 'master' of https://github.com/esp8266/Arduino into feat…
Jul 15, 2018
0fb35d1
Merge branch 'master' of https://github.com/Jeroen88/Arduino into fea…
Jul 15, 2018
f100573
Merge branch 'feature/httpupdate_client_parameter' of https://github.…
Jul 15, 2018
e216ce8
Merge branch 'master' into feature/httpupdate_client_parameter
Jul 20, 2018
45eb148
Merge branch 'master' into feature/httpupdate_client_parameter
Jul 23, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

#include <ESP8266HTTPClient.h>

#include <WiFiClient.h>

#define USE_SERIAL Serial

ESP8266WiFiMulti WiFiMulti;
Expand Down Expand Up @@ -40,22 +42,24 @@ void loop() {
// wait for WiFi connection
if ((WiFiMulti.run() == WL_CONNECTED)) {

WiFiClient client;

HTTPClient http;

USE_SERIAL.print("[HTTP] begin...\n");
// configure traged server and url


http.begin("http://user:[email protected]/test.html");
http.begin((Client&)client, "http://guest:[email protected]/HTTP/Basic/");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't there no need for the cast (Client&) here and in the other examples? Your prototype will make the C++ compiler pass the pointer no matter what, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You will be right. I'll check it and then change it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cast has been removed


/*
// or
http.begin("http://192.168.1.12/test.html");
http.setAuthorization("user", "password");
http.begin("http://jigsaw.w3.org/HTTP/Basic/");
http.setAuthorization("guest", "guest");

// or
http.begin("http://192.168.1.12/test.html");
http.setAuthorization("dXNlcjpwYXN3b3Jk");
http.begin("http://jigsaw.w3.org/HTTP/Basic/");
http.setAuthorization("Z3Vlc3Q6Z3Vlc3Q=");
*/


Expand All @@ -82,4 +86,3 @@ void loop() {

delay(10000);
}

Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

#include <ESP8266HTTPClient.h>

#include <WiFiClient.h>

#define USE_SERIAL Serial

ESP8266WiFiMulti WiFiMulti;
Expand Down Expand Up @@ -40,34 +42,39 @@ void loop() {
// wait for WiFi connection
if ((WiFiMulti.run() == WL_CONNECTED)) {

WiFiClient client;

HTTPClient http;

USE_SERIAL.print("[HTTP] begin...\n");
// configure traged server and url
//http.begin("https://192.168.1.12/test.html", "7a 9c f4 db 40 d3 62 5a 6e 21 bc 5c cc 66 c8 3e a1 45 59 38"); //HTTPS
http.begin("http://192.168.1.12/test.html"); //HTTP
if (http.begin((Client &)client, "http://tls.mbed.org/")) { // HTTP
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason this one picked? Isn't tls.mbed.org https-only?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, thanks for checking return codes!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.



USE_SERIAL.print("[HTTP] GET...\n");
// start connection and send HTTP header
int httpCode = http.GET();
USE_SERIAL.print("[HTTP] GET...\n");
// start connection and send HTTP header
int httpCode = http.GET();

// httpCode will be negative on error
if (httpCode > 0) {
// HTTP header has been send and Server response header has been handled
USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);
// httpCode will be negative on error
if (httpCode > 0) {
// HTTP header has been send and Server response header has been handled
USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);

// file found at server
if (httpCode == HTTP_CODE_OK) {
String payload = http.getString();
USE_SERIAL.println(payload);
// file found at server
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
String payload = http.getString();
USE_SERIAL.println(payload);
}
} else {
USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}

http.end();
} else {
USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
USE_SERIAL.printf("Unable to connect\n");
}

http.end();
}

delay(10000);
}

Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

#include <ESP8266HTTPClient.h>

const char* ssid = "........";
const char* ssidPassword = "........";
const char* ssid = "SSID";
const char* ssidPassword = "PASSWORD";

const char *username = "admin";
const char *password = "admin";
Expand Down Expand Up @@ -76,7 +76,7 @@ String getDigestAuth(String& authReq, const String& username, const String& pass
}

void setup() {
Serial.begin(9600);
Serial.begin(115200);

WiFi.mode(WIFI_STA);
WiFi.begin(ssid, ssidPassword);
Expand All @@ -95,10 +95,12 @@ void setup() {
void loop() {
HTTPClient http;

WiFiClient client;

Serial.print("[HTTP] begin...\n");

// configure traged server and url
http.begin(String(server) + String(uri));
http.begin((Client&) client, String(server) + String(uri));


const char *keys[] = {"WWW-Authenticate"};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ void loop() {
// wait for WiFi connection
if ((WiFiMulti.run() == WL_CONNECTED)) {

http.begin("http://192.168.1.12/test.html");
//http.begin("192.168.1.12", 80, "/test.html");
WiFiClient client;

http.begin((Client&) client, "http://192.168.1.12/test.html");
//http.begin((Client&) client, "192.168.1.12", 80, "/test.html");

int httpCode = http.GET();
if (httpCode > 0) {
Expand All @@ -65,6 +67,3 @@ void loop() {

delay(1000);
}



Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@ void loop() {

HTTPClient http;

WiFiClient client;

USE_SERIAL.print("[HTTP] begin...\n");

// configure server and url
http.begin("http://192.168.1.12/test.html");
//http.begin("192.168.1.12", 80, "/test.html");
http.begin((Client&) client, "http://jigsaw.w3.org/HTTP/connection.html");
//http.begin("jigsaw.w3.org", 80, "/HTTP/connection.html");

USE_SERIAL.print("[HTTP] GET...\n");
// start connection and send HTTP header
Expand All @@ -65,7 +67,7 @@ void loop() {
uint8_t buff[128] = { 0 };

// get tcp stream
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we get rid of the stream temp variable completely since it's no longer needed with your fix? i.e. there is no getStreamPtr() needed anymore?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I see later on that you still have a "getStreamPtr()" method with the new one, too. Any reason not to just keep using it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not quite understand you here.
The getStreamPtr() is embedded in the #ifdef KEEP_PRESENT_API, so it's just there for backward compatibility.
I added the streaming example on purpose, because a server response may be too large to fit into a String, making it impossible to use getString(), but still possible to stream it. Moreover, since it is String getString() instead of String &getString() the response might even be copied (allthough the compiler may optimize this, I don't know)
Could you explain what you are suggesting?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand you now. The getStreamPtr() is kept as part of the existing API, however in the examples the unneeded call to getStreamPtr() has been removed.

WiFiClient * stream = http.getStreamPtr();
WiFiClient * stream = &client;

// read all data from server
while (http.connected() && (len > 0 || len == -1)) {
Expand Down Expand Up @@ -99,4 +101,3 @@ void loop() {

delay(10000);
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/**
StreamHTTPClient.ino

Created on: 24.05.2015

*/

#include <Arduino.h>

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>

#include <ESP8266HTTPClient.h>

#define USE_SERIAL Serial

ESP8266WiFiMulti WiFiMulti;

void setup() {

USE_SERIAL.begin(115200);
// USE_SERIAL.setDebugOutput(true);

USE_SERIAL.println();
USE_SERIAL.println();
USE_SERIAL.println();

for (uint8_t t = 4; t > 0; t--) {
USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
USE_SERIAL.flush();
delay(1000);
}

WiFi.mode(WIFI_STA);
WiFiMulti.addAP("SSID", "PASSWORD");

}

void loop() {
// wait for WiFi connection
if ((WiFiMulti.run() == WL_CONNECTED)) {

HTTPClient http;

BearSSL::WiFiClientSecure client;

bool mfln = client.probeMaxFragmentLength("tls.mbed.org", 443, 1024);
USE_SERIAL.printf("\nConnecting to https://tls.mbed.org\n");
USE_SERIAL.printf("MFLN supported: %s\n", mfln ? "yes" : "no");
if (mfln) {
client.setBufferSizes(1024, 1024);
}

USE_SERIAL.print("[HTTP] begin...\n");

// configure server and url
const uint8_t fingerprint[20] = {0xEB, 0xD9, 0xDF, 0x37, 0xC2, 0xCC, 0x84, 0x89, 0x00, 0xA0, 0x58, 0x52, 0x24, 0x04, 0xE4, 0x37, 0x3E, 0x2B, 0xF1, 0x41};
client.setFingerprint(fingerprint);

//if (http.begin("jigsaw.w3.org", 443, "/HTTP/connection.html", true)) {
if (http.begin((Client&) client, "https://tls.mbed.org/")) {

USE_SERIAL.print("[HTTP] GET...\n");
// start connection and send HTTP header
int httpCode = http.GET();
if (httpCode > 0) {
// HTTP header has been send and Server response header has been handled
USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);

// file found at server
if (httpCode == HTTP_CODE_OK) {

// get lenght of document (is -1 when Server sends no Content-Length header)
int len = http.getSize();

// create buffer for read
uint8_t buff[128] = { 0 };

// get tcp stream
WiFiClient * stream = &client;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above, no more "get tcp stream" with your change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getStreamPtr() has been removed from the example


// read all data from server
while (http.connected() && (len > 0 || len == -1)) {
// get available data size
size_t size = stream->available();

if (size) {
// read up to 128 byte
int c = stream->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size));

// write it to Serial
USE_SERIAL.write(buff, c);

if (len > 0) {
len -= c;
}
}
delay(1);
}

USE_SERIAL.println();
USE_SERIAL.print("[HTTP] connection closed or file end.\n");

}
} else {
USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}

http.end();
} else {
USE_SERIAL.printf("Unable to connect\n");
}
}

delay(10000);
}
Loading