|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "filesys.h" |
|
|
#include "filesys.h" |
|
|
#include <ESP8266WebServer.h> |
|
|
#include <ESP8266WebServer.h> |
|
|
|
|
|
#include <stdarg.h> |
|
|
|
|
|
|
|
|
|
|
|
#define LOG_STR "[HTTPServer]" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class HTTPServer : public ESP8266WebServer { |
|
|
class HTTPServer : public ESP8266WebServer { |
|
|
private: |
|
|
private: |
|
|
const char* rootDir; |
|
|
|
|
|
|
|
|
const char* rootDir = "/"; |
|
|
|
|
|
|
|
|
bool addRootFileHandler(); |
|
|
bool addRootFileHandler(); |
|
|
|
|
|
bool addRootFileHandler_ls(); |
|
|
bool formatFS() { |
|
|
bool formatFS() { |
|
|
return format_fs(); |
|
|
return format_fs(); |
|
|
} |
|
|
} |
|
|
void listRoot() { |
|
|
void listRoot() { |
|
|
ls(rootDir); |
|
|
ls(rootDir); |
|
|
} |
|
|
} |
|
|
|
|
|
void logf(const char *format, ...) { |
|
|
|
|
|
va_list args; |
|
|
|
|
|
va_start(args, format); |
|
|
|
|
|
Serial.print(LOG_STR); |
|
|
|
|
|
Serial.printf(format, args); |
|
|
|
|
|
va_end(args); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
public: |
|
|
public: |
|
|
HTTPServer(const int _port, const char* _rootDir) : |
|
|
HTTPServer(const int _port, const char* _rootDir) : |
|
|
|
|
|
|
|
|
this->listRoot(); |
|
|
this->listRoot(); |
|
|
Serial.printf("\n\r"); |
|
|
Serial.printf("\n\r"); |
|
|
|
|
|
|
|
|
// https://stackoverflow.com/questions/7627098/what-is-a-lambda-expression-in-c11 |
|
|
|
|
|
on("/compress.html", [this]() { |
|
|
|
|
|
File f = open("/compress.html.gz"); |
|
|
|
|
|
if (!f) { |
|
|
|
|
|
Serial.println("Failed to open file for reading"); |
|
|
|
|
|
send(404, "text/plain", "Failed to open file for reading"); |
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
|
|
|
Serial.println("Streaming /compress.html.gz to client"); |
|
|
|
|
|
const String cont = "text/html"; |
|
|
|
|
|
streamFile(f, cont); |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
on("/comp.html", [this]() { |
|
|
|
|
|
File f = open("/comp.html.gz"); |
|
|
|
|
|
if (!f) { |
|
|
|
|
|
Serial.println("Failed to open file for reading"); |
|
|
|
|
|
send(404, "text/plain", "Failed to open file for reading"); |
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
|
|
|
Serial.println("Streaming /comp.html.gz to client"); |
|
|
|
|
|
const String cont = "text/html"; |
|
|
|
|
|
streamFile(f, cont); |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
// on("/compress.html", [this]() { |
|
|
|
|
|
// File f = open("/compress.html.gz"); |
|
|
|
|
|
// if (!f) { |
|
|
|
|
|
// Serial.println("Failed to open file for reading"); |
|
|
|
|
|
// send(404, "text/plain", "Failed to open file for reading"); |
|
|
|
|
|
// } |
|
|
|
|
|
// else |
|
|
|
|
|
// { |
|
|
|
|
|
// Serial.println("Streaming /compress.html.gz to client"); |
|
|
|
|
|
// const String cont = "text/html"; |
|
|
|
|
|
// streamFile(f, cont); |
|
|
|
|
|
// } |
|
|
|
|
|
// f.close(); |
|
|
|
|
|
// }); |
|
|
|
|
|
// on("/comp.html", [this]() { |
|
|
|
|
|
// File f = open("/comp.html.gz"); |
|
|
|
|
|
// if (!f) { |
|
|
|
|
|
// Serial.println("Failed to open file for reading"); |
|
|
|
|
|
// send(404, "text/plain", "Failed to open file for reading"); |
|
|
|
|
|
// } |
|
|
|
|
|
// else |
|
|
|
|
|
// { |
|
|
|
|
|
// Serial.println("Streaming /comp.html.gz to client"); |
|
|
|
|
|
// const String cont = "text/html"; |
|
|
|
|
|
// streamFile(f, cont); |
|
|
|
|
|
// } |
|
|
|
|
|
// f.close(); |
|
|
|
|
|
// }); |
|
|
|
|
|
|
|
|
on("/terminal", [this]() { |
|
|
on("/terminal", [this]() { |
|
|
String log_msg = "terminal: millis: "; |
|
|
String log_msg = "terminal: millis: "; |
|
|
|
|
|
|
|
|
send(200, "text/plain", log_msg); |
|
|
send(200, "text/plain", log_msg); |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
// default handler |
|
|
onNotFound([this]() { |
|
|
onNotFound([this]() { |
|
|
String message = "File Not Found\n\n"; |
|
|
String message = "File Not Found\n\n"; |
|
|
message += "URI: "; |
|
|
message += "URI: "; |
|
|
|
|
|
|
|
|
send(404, "text/plain", message); |
|
|
send(404, "text/plain", message); |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
if( this->addRootFileHandler() ){ |
|
|
|
|
|
|
|
|
if( this->addRootFileHandler_ls() ){ |
|
|
this->begin(); |
|
|
this->begin(); |
|
|
Serial.printf("[HTTPServer] Server active on Port 80 !\n\r"); |
|
|
Serial.printf("[HTTPServer] Server active on Port 80 !\n\r"); |
|
|
return true; |
|
|
return true; |