mirror of
https://github.com/esp8266/Arduino.git
synced 2025-06-12 01:53:07 +03:00
Migrate from astyle to clang-format (#8464)
This commit is contained in:
committed by
Max Prokhorov
parent
46190b61f1
commit
19b7a29720
@ -9,10 +9,10 @@
|
||||
#include <Arduino.h>
|
||||
#include <ESP8266WebServer.h>
|
||||
|
||||
#include "secrets.h" // add WLAN Credentials in here.
|
||||
#include "secrets.h" // add WLAN Credentials in here.
|
||||
|
||||
#include <FS.h> // File System for Web Server Files
|
||||
#include <LittleFS.h> // This file system is used.
|
||||
#include <FS.h> // File System for Web Server Files
|
||||
#include <LittleFS.h> // This file system is used.
|
||||
|
||||
// mark parameters not used in example
|
||||
#define UNUSED __attribute__((unused))
|
||||
@ -41,13 +41,11 @@ void handleRedirect() {
|
||||
TRACE("Redirect...");
|
||||
String url = "/index.htm";
|
||||
|
||||
if (!LittleFS.exists(url)) {
|
||||
url = "/$update.htm";
|
||||
}
|
||||
if (!LittleFS.exists(url)) { url = "/$update.htm"; }
|
||||
|
||||
server.sendHeader("Location", url, true);
|
||||
server.send(302);
|
||||
} // handleRedirect()
|
||||
} // handleRedirect()
|
||||
|
||||
|
||||
// This function is called when the WebServer was requested to list all existing files in the filesystem.
|
||||
@ -58,20 +56,18 @@ void handleListFiles() {
|
||||
|
||||
result += "[\n";
|
||||
while (dir.next()) {
|
||||
if (result.length() > 4) {
|
||||
result += ",";
|
||||
}
|
||||
if (result.length() > 4) { result += ","; }
|
||||
result += " {";
|
||||
result += " \"name\": \"" + dir.fileName() + "\", ";
|
||||
result += " \"size\": " + String(dir.fileSize()) + ", ";
|
||||
result += " \"time\": " + String(dir.fileTime());
|
||||
result += " }\n";
|
||||
// jc.addProperty("size", dir.fileSize());
|
||||
} // while
|
||||
} // while
|
||||
result += "]";
|
||||
server.sendHeader("Cache-Control", "no-cache");
|
||||
server.send(200, "text/javascript; charset=utf-8", result);
|
||||
} // handleListFiles()
|
||||
} // handleListFiles()
|
||||
|
||||
|
||||
// This function is called when the sysInfo service was requested.
|
||||
@ -90,96 +86,84 @@ void handleSysInfo() {
|
||||
|
||||
server.sendHeader("Cache-Control", "no-cache");
|
||||
server.send(200, "text/javascript; charset=utf-8", result);
|
||||
} // handleSysInfo()
|
||||
} // handleSysInfo()
|
||||
|
||||
|
||||
// ===== Request Handler class used to answer more complex requests =====
|
||||
|
||||
// The FileServerHandler is registered to the web server to support DELETE and UPLOAD of files into the filesystem.
|
||||
class FileServerHandler : public RequestHandler {
|
||||
public:
|
||||
// @brief Construct a new File Server Handler object
|
||||
// @param fs The file system to be used.
|
||||
// @param path Path to the root folder in the file system that is used for serving static data down and upload.
|
||||
// @param cache_header Cache Header to be used in replies.
|
||||
FileServerHandler() {
|
||||
TRACE("FileServerHandler is registered\n");
|
||||
}
|
||||
public:
|
||||
// @brief Construct a new File Server Handler object
|
||||
// @param fs The file system to be used.
|
||||
// @param path Path to the root folder in the file system that is used for serving static data down and upload.
|
||||
// @param cache_header Cache Header to be used in replies.
|
||||
FileServerHandler() {
|
||||
TRACE("FileServerHandler is registered\n");
|
||||
}
|
||||
|
||||
|
||||
// @brief check incoming request. Can handle POST for uploads and DELETE.
|
||||
// @param requestMethod method of the http request line.
|
||||
// @param requestUri request ressource from the http request line.
|
||||
// @return true when method can be handled.
|
||||
bool canHandle(HTTPMethod requestMethod, const String UNUSED &_uri) override {
|
||||
return ((requestMethod == HTTP_POST) || (requestMethod == HTTP_DELETE));
|
||||
} // canHandle()
|
||||
// @brief check incoming request. Can handle POST for uploads and DELETE.
|
||||
// @param requestMethod method of the http request line.
|
||||
// @param requestUri request ressource from the http request line.
|
||||
// @return true when method can be handled.
|
||||
bool canHandle(HTTPMethod requestMethod, const String UNUSED &_uri) override {
|
||||
return ((requestMethod == HTTP_POST) || (requestMethod == HTTP_DELETE));
|
||||
} // canHandle()
|
||||
|
||||
|
||||
bool canUpload(const String &uri) override {
|
||||
// only allow upload on root fs level.
|
||||
return (uri == "/");
|
||||
} // canUpload()
|
||||
bool canUpload(const String &uri) override {
|
||||
// only allow upload on root fs level.
|
||||
return (uri == "/");
|
||||
} // canUpload()
|
||||
|
||||
|
||||
bool handle(ESP8266WebServer &server, HTTPMethod requestMethod, const String &requestUri) override {
|
||||
// ensure that filename starts with '/'
|
||||
String fName = requestUri;
|
||||
if (!fName.startsWith("/")) {
|
||||
fName = "/" + fName;
|
||||
}
|
||||
bool handle(ESP8266WebServer &server, HTTPMethod requestMethod, const String &requestUri) override {
|
||||
// ensure that filename starts with '/'
|
||||
String fName = requestUri;
|
||||
if (!fName.startsWith("/")) { fName = "/" + fName; }
|
||||
|
||||
if (requestMethod == HTTP_POST) {
|
||||
// all done in upload. no other forms.
|
||||
if (requestMethod == HTTP_POST) {
|
||||
// all done in upload. no other forms.
|
||||
|
||||
} else if (requestMethod == HTTP_DELETE) {
|
||||
if (LittleFS.exists(fName)) {
|
||||
LittleFS.remove(fName);
|
||||
}
|
||||
} // if
|
||||
} else if (requestMethod == HTTP_DELETE) {
|
||||
if (LittleFS.exists(fName)) { LittleFS.remove(fName); }
|
||||
} // if
|
||||
|
||||
server.send(200); // all done.
|
||||
return (true);
|
||||
} // handle()
|
||||
server.send(200); // all done.
|
||||
return (true);
|
||||
} // handle()
|
||||
|
||||
|
||||
// uploading process
|
||||
void upload(ESP8266WebServer UNUSED &server, const String UNUSED &_requestUri, HTTPUpload &upload) override {
|
||||
// ensure that filename starts with '/'
|
||||
String fName = upload.filename;
|
||||
if (!fName.startsWith("/")) {
|
||||
fName = "/" + fName;
|
||||
}
|
||||
// uploading process
|
||||
void upload(ESP8266WebServer UNUSED &server, const String UNUSED &_requestUri, HTTPUpload &upload) override {
|
||||
// ensure that filename starts with '/'
|
||||
String fName = upload.filename;
|
||||
if (!fName.startsWith("/")) { fName = "/" + fName; }
|
||||
|
||||
if (upload.status == UPLOAD_FILE_START) {
|
||||
// Open the file
|
||||
if (LittleFS.exists(fName)) {
|
||||
LittleFS.remove(fName);
|
||||
} // if
|
||||
_fsUploadFile = LittleFS.open(fName, "w");
|
||||
if (upload.status == UPLOAD_FILE_START) {
|
||||
// Open the file
|
||||
if (LittleFS.exists(fName)) { LittleFS.remove(fName); } // if
|
||||
_fsUploadFile = LittleFS.open(fName, "w");
|
||||
|
||||
} else if (upload.status == UPLOAD_FILE_WRITE) {
|
||||
// Write received bytes
|
||||
if (_fsUploadFile) {
|
||||
_fsUploadFile.write(upload.buf, upload.currentSize);
|
||||
}
|
||||
} else if (upload.status == UPLOAD_FILE_WRITE) {
|
||||
// Write received bytes
|
||||
if (_fsUploadFile) { _fsUploadFile.write(upload.buf, upload.currentSize); }
|
||||
|
||||
} else if (upload.status == UPLOAD_FILE_END) {
|
||||
// Close the file
|
||||
if (_fsUploadFile) {
|
||||
_fsUploadFile.close();
|
||||
}
|
||||
} // if
|
||||
} // upload()
|
||||
} else if (upload.status == UPLOAD_FILE_END) {
|
||||
// Close the file
|
||||
if (_fsUploadFile) { _fsUploadFile.close(); }
|
||||
} // if
|
||||
} // upload()
|
||||
|
||||
protected:
|
||||
File _fsUploadFile;
|
||||
protected:
|
||||
File _fsUploadFile;
|
||||
};
|
||||
|
||||
|
||||
// Setup everything to make the webserver work.
|
||||
void setup(void) {
|
||||
delay(3000); // wait for serial monitor to start completely.
|
||||
delay(3000); // wait for serial monitor to start completely.
|
||||
|
||||
// Use Serial port for some trace information from the example
|
||||
Serial.begin(115200);
|
||||
@ -250,12 +234,12 @@ void setup(void) {
|
||||
|
||||
server.begin();
|
||||
TRACE("hostname=%s\n", WiFi.getHostname());
|
||||
} // setup
|
||||
} // setup
|
||||
|
||||
|
||||
// run the server...
|
||||
void loop(void) {
|
||||
server.handleClient();
|
||||
} // loop()
|
||||
} // loop()
|
||||
|
||||
// end.
|
||||
|
Reference in New Issue
Block a user