Skip to content

Serve LittleFS files with ethernet W5500 and esp8266webserver (ERR_CONTENT_LENGTH_MISMATCH)  #8855

Closed
@infrafast

Description

@infrafast

Basic Infos

  • [X ] This issue complies with the issue POLICY doc.
  • [ X] I have read the documentation at readthedocs and the issue is not addressed there.
  • [X ] I have tested that the issue is present in current master branch (aka latest git).
  • [X ] I have searched the issue tracker for a similar issue.
  • If there is a stack dump, I have decoded it.
  • [ X] I have filled out all fields below.

Platform

  • Hardware: ESP8266
  • Core Version: framework-arduinoespressif8266 @ 3.30101.0 (3.1.1)
  • Development Env: PlatformIO
  • Operating System: Windows

Settings in IDE

  • Module: Espressif 8266 (4.1.0) > WeMos D1 R2 and mini
  • Flash Mode:
  • Flash Size: 4M
  • lwip Variant: default
  • Reset Method:
  • Flash Frequency: [40Mhz]
  • CPU Frequency: 80Mhz
  • Upload Using: serial + OTA
  • Upload Speed: 115200

Problem Description

Detailed problem description goes here.

I have a device that works both with WiFi and Ethernet using W5500 library and esp8266webserver.
Serving page works ok with WiFI but when using W5500 I have the similar problem as the one described in #2121
This happens randomly n some page, one time it can load, the other it will not and another page will fail.

image

I follow all threads about this issue both in github and other places and I made test with and without gziping the document, with and without merging my javascript files, with small (2/3k) and bigger file (30k).
I monitored the freeheap before and after pages are served and always have a reasonnable amount of memory available

The problem happens both when using "server.serveStatic" and "server.on"

I presume that the fix that was apply for the WiFi core has not been done for the W5500 part.

Hence I suspect the streamFile method template to be the cause.

I provide some extract of the code which are representative of the function calls (not the full one has it is too big).

MCVE Sketch

#include <Arduino.h>
#include <ESP8266WebServer.h>
#include <W5500lwIP.h>        

ESP8266WebServer server;
Wiznet5500lwIP ethDriver(D2);   

void streamJavascript(String path) {
  streamFile(path,"application/javascript");
}

void streamCSS(String path){
  streamFile(path,"text/css");
}

void handleIndexJS() {
  streamJavascript("/index.min.js.gz");
}

void handleStyleCSS() {
  streamCSS("/style.min.css.gz");
}

bool streamFile(String path, String dataType) {
  DEBUG_PRINT("\nFree heap() before call: %i",ESP.getFreeHeap());
  DEBUG_PRINT("\nRequested page -> %S",path.c_str());
  if (LittleFS.exists(path)){
      File dataFile = LittleFS.open(path, "r");
      if (!dataFile) {
          DEBUG_PRINT("\nNot found");
          return false;
      }
      uint16_t dataSentSize = server.streamFile(dataFile, dataType);
      if (dataSentSize != dataFile.size()) {
        DEBUG_PRINT("\nSent less data (%i) than expected! (%i)",dataSentSize,dataFile.size());

      }else{
          DEBUG_PRINT("\nPage served!");
      }
      DEBUG_PRINT("\nFree heap() after call: %i",ESP.getFreeHeap());
      dataFile.close();
  }else{
      DEBUG_PRINT("\nNot found");
      return false;
  }
  return true;
}

void setup() {
      ethDriver.begin(mac);         
      loopElapsedTime = millis();
      while (!ethDriver.connected() && (millis() - loopElapsedTime < ETHCONNECTTIMEOUT)) {
        //DEBUG_PRINT(".");          
      }
    server.on("/index.min.js", handleIndexJS);
    server.on("/style.min.css", handleStyleCSS);
}

void loop() {
   server.handleClient();
}

Debug Messages

Free heap() before call: 11856
Requested page -> /style.min.css.gz
Page served!
Free heap() after call: 10232
Free heap() before call: 12504
Requested page -> /index.min.js.gz
Sent less data (4166) than expected! (6808)
Free heap() after call: 9832

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions