Last active
January 8, 2017 10:12
-
-
Save robertoostenveld/6939f0b51f83da47fe3339c9dad84c1a to your computer and use it in GitHub Desktop.
ESP8266WebServer handler that serves static files from SPIFFS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// this serves all URLs that can be resolved to a file on the SPIFFS filesystem | |
// server.onNotFound(handleNotFound); | |
static String getContentType(const String& path) { | |
if (path.endsWith(".html")) return "text/html"; | |
else if (path.endsWith(".htm")) return "text/html"; | |
else if (path.endsWith(".css")) return "text/css"; | |
else if (path.endsWith(".txt")) return "text/plain"; | |
else if (path.endsWith(".js")) return "application/javascript"; | |
else if (path.endsWith(".png")) return "image/png"; | |
else if (path.endsWith(".gif")) return "image/gif"; | |
else if (path.endsWith(".jpg")) return "image/jpeg"; | |
else if (path.endsWith(".jpeg")) return "image/jpeg"; | |
else if (path.endsWith(".ico")) return "image/x-icon"; | |
else if (path.endsWith(".svg")) return "image/svg+xml"; | |
else if (path.endsWith(".xml")) return "text/xml"; | |
else if (path.endsWith(".pdf")) return "application/pdf"; | |
else if (path.endsWith(".zip")) return "application/zip"; | |
else if (path.endsWith(".gz")) return "application/x-gzip"; | |
return "application/octet-stream"; | |
} | |
void sendRedirect(String filename) { | |
char buf[64]; | |
filename.toCharArray(buf, 64); | |
sendRedirect(filename); | |
} | |
void sendRedirect(const char * filename) { | |
server.sendHeader("Location", String(filename), true); | |
server.send(302, "text/plain", ""); | |
} | |
void sendStaticFile(String filename) { | |
char buf[64]; | |
filename.toCharArray(buf, 64); | |
sendStaticFile(buf); | |
} | |
void sendStaticFile(const char * filename) { | |
SPIFFS.begin(); | |
File f = SPIFFS.open(filename, "r"); | |
Serial.print("Opening file "); | |
Serial.print(filename); | |
if (!f) { | |
Serial.println(" FAILED"); | |
server.send(500, "text/html", "File not found"); | |
} else { | |
Serial.println(" OK"); | |
String content = f.readString(); | |
f.close(); | |
server.sendHeader("Connection", "close"); | |
server.sendHeader("Access-Control-Allow-Origin", "*"); | |
server.send(200, getContentType(String(filename)), content); | |
} | |
} | |
void handleNotFound() { | |
SPIFFS.begin(); | |
if (SPIFFS.exists(server.uri())) { | |
sendStaticFile(server.uri()); | |
} | |
else { | |
String message = "File Not Found\n\n"; | |
message += "URI: "; | |
message += server.uri(); | |
message += "\nMethod: "; | |
message += (server.method() == HTTP_GET) ? "GET" : "POST"; | |
message += "\nArguments: "; | |
message += server.args(); | |
message += "\n"; | |
for (uint8_t i = 0; i < server.args(); i++) { | |
message += " " + server.argName(i) + ": " + server.arg(i) + "\n"; | |
} | |
server.send(404, "text/plain", message); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment