Skip to content

Commit 42f824b

Browse files
earlephilhowerdevyte
authored andcommitted
Fix WebServerSecure streamFile() (esp8266#4545)
* Fix WebServerSecure streamFile() ESP8266WebServerSecure's streamFile was using the base class' method which did not use SSL encrypt before transmitting, leading to failure. Add a new template method and required support for WiFiClientSecure::write(Stream&) (using a local temp buffer since the SSL libs do not grok Arduino Streams at all). Fixes esp8266#4544 * Match ClientContext buffer and yield() behavior ClientContext sends out 256 bytes at a time and gives a yield after each chunk to ensure the WDT doesn't fire. Mimic that behavior in WiFiClientSecure::write(Stream&).
1 parent 3267443 commit 42f824b

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

libraries/ESP8266WebServer/src/ESP8266WebServerSecure.h

+6
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ class ESP8266WebServerSecure : public ESP8266WebServer
4242
void handleClient() override;
4343
void close() override;
4444

45+
template<typename T>
46+
size_t streamFile(T &file, const String& contentType) {
47+
_streamFileCore(file.size(), file.name(), contentType);
48+
return _currentClientSecure.write(file);
49+
}
50+
4551
private:
4652
size_t _currentClientWrite (const char *bytes, size_t len) override { return _currentClientSecure.write((const uint8_t *)bytes, len); }
4753
size_t _currentClientWrite_P (PGM_P bytes, size_t len) override { return _currentClientSecure.write_P(bytes, len); }

libraries/ESP8266WiFi/src/WiFiClientSecure.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,30 @@ size_t WiFiClientSecure::write_P(PGM_P buf, size_t size)
564564
return write(copy, size);
565565
}
566566

567+
// The axTLS bare libs don't understand anything about Arduino Streams,
568+
// so we have to manually read and send individual chunks.
569+
size_t WiFiClientSecure::write(Stream& stream)
570+
{
571+
size_t totalSent = 0;
572+
size_t countRead;
573+
size_t countSent;
574+
if (!_ssl)
575+
{
576+
return 0;
577+
}
578+
do {
579+
uint8_t temp[256]; // Temporary chunk size same as ClientContext
580+
countSent = 0;
581+
countRead = stream.readBytes(temp, sizeof(temp));
582+
if (countRead) {
583+
countSent = write(temp, countRead);
584+
totalSent += countSent;
585+
}
586+
yield(); // Feed the WDT
587+
} while ( (countSent == countRead) && (countSent > 0) );
588+
return totalSent;
589+
}
590+
567591
int WiFiClientSecure::read(uint8_t *buf, size_t size)
568592
{
569593
if (!_ssl) {

libraries/ESP8266WiFi/src/WiFiClientSecure.h

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class WiFiClientSecure : public WiFiClient {
4343
uint8_t connected() override;
4444
size_t write(const uint8_t *buf, size_t size) override;
4545
size_t write_P(PGM_P buf, size_t size) override;
46+
size_t write(Stream& stream); // Note this is not virtual
4647
int read(uint8_t *buf, size_t size) override;
4748
int available() override;
4849
int read() override;

0 commit comments

Comments
 (0)