Arduino icon indicating copy to clipboard operation
Arduino copied to clipboard

Using serveStatic() to serve custom page for ESP8266HTTPUpdateServer does not work

Open chess-levin opened this issue 3 years ago • 0 comments

Hi folks, I'm trying to serve my own customized upload page for ESP8266HTTPUpdateServer ( https://github.com/esp8266/Arduino/tree/master/libraries/ESP8266HTTPUpdateServer).

But it seems, that the GET Request /update does not hit

 httpServer.serveStatic("/update", LittleFS, "/update.html");

because, the default upload page from ESP8266HTTPUpdateServer is sent back to the browser instead of my custom page.

May be it's because serverStatic() does not check the HTTP method of the incoming request? For ESP8266HTTPUpdateServer you have to distinct between GET and POST requests.

My workaround (based on https://www.mischianti.org/2021/11/11/esp8266-ota-update-with-web-browser-custom-web-interface-3/) looks like this. Actually it's rebuilding the serverStatic() method, but I can check whether it's a GET or POST request.

bool sendFile(String path, String dataType) {
  PRINTLN("Requested page -> ", path);
  
  if (LittleFS.exists(path)) {
    File dataFile = LittleFS.open(path, "r");
    if (!dataFile) {
      httpServer.send(404, F(MIMETYPE_TEXT), F("404: Not Found"));
      return false;
    }
    if (httpServer.streamFile(dataFile, dataType) != dataFile.size()) {
      PRINTSLN("Sent less data than expected!");
    } else {
      PRINTSLN("Page served!");
    }

    dataFile.close();
  } else {
    httpServer.send(404, F(MIMETYPE_TEXT), F("404: Not Found"));
    return false;
  }
  
  return true;
}

void handleUpdate() {
  sendFile("/updatefw.html", F(MIMETYPE_HTML));
}

httpServer.on("/myupdate", HTTP_GET, handleUpdate);

chess-levin avatar Apr 11 '22 14:04 chess-levin