arduino-pico/libraries/HTTPClient/examples/StreamHttpsClient/StreamHttpsClient.ino
Earle F. Philhower, III f4de3cbd6d
Rewrite HTTPClient chunked transfer-encoding (#3034)
* Rewrite HTTPClient chunked transfer-encoding

Fixes #3029

The chunked decoder seems to have had some race conditions resulting in
lost data.

The HTTPClient::getStreamPtr returned the raw WiFiClient which included
all of the chunked block size markers, requiring the user to manually
parse the chunked encoding.

Remove the existing chunked handling, add a HTTPStream as a WiFiClient
subclass.  The HTTPClient will return this HTTPStream which will always
properly cut out chunk markers when present (and pass things raw for
non-encoded streams).  Use this new HTTPStream class to replace the
existing handling in HTTPClient

Update the fingerprint in the StreamHTTPSClient example because their
cert was updated.

* Astyle

* Add block write passthru

* Add other WiFiClient passthru calls
2025-07-11 12:12:46 -07:00

105 lines
2.4 KiB
C++

/**
StreamHTTPClient.ino
Created on: 24.05.2015
*/
#include <Arduino.h>
#include <WiFi.h>
#include <HTTPClient.h>
#ifndef STASSID
#define STASSID "your-ssid"
#define STAPSK "your-password"
#endif
const char *ssid = STASSID;
const char *pass = STAPSK;
WiFiMulti WiFiMulti;
void setup() {
Serial.begin(115200);
// Serial.setDebugOutput(true);
Serial.println();
Serial.println();
Serial.println();
for (uint8_t t = 4; t > 0; t--) {
Serial.printf("[SETUP] WAIT %d...\n", t);
Serial.flush();
delay(1000);
}
WiFi.mode(WIFI_STA);
WiFiMulti.addAP(ssid, pass);
}
void loop() {
// wait for WiFi connection
if ((WiFiMulti.run() == WL_CONNECTED)) {
Serial.print("[HTTPS] begin...\n");
// configure server and url
const char *fp = "e5:e8:05:7d:85:70:b0:db:d9:5a:b6:a6:bb:2b:29:ae:d9:b1:b7:f9";
HTTPClient https;
https.setFingerprint(fp);
if (https.begin("https://www.trustedfirmware.org/projects/mbed-tls")) {
Serial.print("[HTTPS] GET...\n");
// start connection and send HTTP header
int httpCode = https.GET();
if (httpCode > 0) {
// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTPS] GET... code: %d\n", httpCode);
// file found at server
if (httpCode == HTTP_CODE_OK) {
// get length of document (is -1 when Server sends no Content-Length header)
int len = https.getSize();
// create buffer for read
static uint8_t buff[128] = { 0 };
// read all data from server
while (https.connected() && (len > 0 || len == -1)) {
// get available data size
size_t size = https.getStreamPtr()->available();
if (size) {
// read up to 128 byte
int c = https.getStreamPtr()->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size));
// write it to Serial
Serial.write(buff, c);
if (len > 0) {
len -= c;
}
}
delay(1);
}
Serial.println();
Serial.print("[HTTPS] connection closed or file end.\n");
}
} else {
Serial.printf("[HTTPS] GET... failed, error: %s\n", https.errorToString(httpCode).c_str());
}
https.end();
} else {
Serial.printf("Unable to connect\n");
}
}
Serial.println("Wait 10s before the next round...");
delay(10000);
}