diff --git a/examples/Async-Server/Async-Server.ino b/examples/Async-Server/Async-Server.ino index d987a51..8fed51d 100644 --- a/examples/Async-Server/Async-Server.ino +++ b/examples/Async-Server/Async-Server.ino @@ -1,31 +1,31 @@ /** - * Example for the ESP32 HTTP(S) Webserver - * - * IMPORTANT NOTE: - * To run this script, your need to - * 1) Enter your WiFi SSID and PSK below this comment - * 2) Make sure to have certificate data available. You will find a - * shell script and instructions to do so in the library folder - * under extras/ - * - * This script will install an HTTPS Server on your ESP32 with the following - * functionalities: - * - Show simple page on web server root - * - 404 for everything else - * The server will be run in a separate task, so that you can do your own stuff - * in the loop() function. - * Everything else is just like the Static-Page example - */ + Example for the ESP32 HTTP(S) Webserver + + IMPORTANT NOTE: + To run this script, your need to + 1) Enter your WiFi SSID and PSK below this comment + 2) Make sure to have certificate data available. You will find a + shell script and instructions to do so in the library folder + under extras/ + + This script will install an HTTPS Server on your ESP32 with the following + functionalities: + - Show simple page on web server root + - 404 for everything else + The server will be run in a separate task, so that you can do your own stuff + in the loop() function. + Everything else is just like the Static-Page example +*/ // TODO: Configure your WiFi here -#define WIFI_SSID "" -#define WIFI_PSK "" +#define WIFI_SSID "your_ssid" +#define WIFI_PSK "12345678" /** Check if we have multiple cores */ #if CONFIG_FREERTOS_UNICORE -#define ARDUINO_RUNNING_CORE 0 + #define ARDUINO_RUNNING_CORE 0 #else -#define ARDUINO_RUNNING_CORE 1 + #define ARDUINO_RUNNING_CORE 1 #endif // Include certificate data (see note above) @@ -46,53 +46,15 @@ using namespace httpsserver; // Create an SSL certificate object from the files included above SSLCert cert = SSLCert( - example_crt_DER, example_crt_DER_len, - example_key_DER, example_key_DER_len -); + example_crt_DER, example_crt_DER_len, + example_key_DER, example_key_DER_len + ); // Create an SSL-enabled server that uses the certificate HTTPSServer secureServer = HTTPSServer(&cert); -// Declare some handler functions for the various URLs on the server -void handleRoot(HTTPRequest * req, HTTPResponse * res); -void handle404(HTTPRequest * req, HTTPResponse * res); - -// We declare a function that will be the entry-point for the task that is going to be -// created. -void serverTask(void *params); - -void setup() { - // For logging - Serial.begin(115200); - - // Connect to WiFi - Serial.println("Setting up WiFi"); - WiFi.begin(WIFI_SSID, WIFI_PSK); - while (WiFi.status() != WL_CONNECTED) { - Serial.print("."); - delay(500); - } - Serial.print("Connected. IP="); - Serial.println(WiFi.localIP()); - - // Setup the server as a separate task. - Serial.println("Creating server task... "); - // We pass: - // serverTask - the function that should be run as separate task - // "https443" - a name for the task (mainly used for logging) - // 6144 - stack size in byte. If you want up to four clients, you should - // not go below 6kB. If your stack is too small, you will encounter - // Panic and stack canary exceptions, usually during the call to - // SSL_accept. - xTaskCreatePinnedToCore(serverTask, "https443", 6144, NULL, 1, NULL, ARDUINO_RUNNING_CORE); -} - -void loop() { - Serial.println("loop()"); - delay(5000); -} - -void serverTask(void *params) { +void serverTask(void *params) +{ // In the separate task we first do everything that we would have done in the // setup() function, if we would run the server synchronously. @@ -112,11 +74,14 @@ void serverTask(void *params) { Serial.println("Starting server..."); secureServer.start(); - if (secureServer.isRunning()) { + + if (secureServer.isRunning()) + { Serial.println("Server ready."); // "loop()" function of the separate task - while(true) { + while (true) + { // This call will let the server do its work secureServer.loop(); @@ -126,7 +91,8 @@ void serverTask(void *params) { } } -void handleRoot(HTTPRequest * req, HTTPResponse * res) { +void handleRoot(HTTPRequest * req, HTTPResponse * res) +{ // Status code is 200 OK by default. // We want to deliver a simple HTML page, so we send a corresponding content type: res->setHeader("Content-Type", "text/html"); @@ -140,13 +106,14 @@ void handleRoot(HTTPRequest * req, HTTPResponse * res) { res->println("

Hello World!

"); res->print("

Your server is running for "); // A bit of dynamic data: Show the uptime - res->print((int)(millis()/1000), DEC); + res->print((int)(millis() / 1000), DEC); res->println(" seconds.

"); res->println(""); res->println(""); } -void handle404(HTTPRequest * req, HTTPResponse * res) { +void handle404(HTTPRequest * req, HTTPResponse * res) +{ // Discard request body, if we received any // We do this, as this is the default node and may also server POST/PUT requests req->discardRequestBody(); @@ -165,3 +132,44 @@ void handle404(HTTPRequest * req, HTTPResponse * res) { res->println("

404 Not Found

The requested resource was not found on this server.

"); res->println(""); } + +void setup() +{ + // For logging + Serial.begin(115200); + while (!Serial && millis() < 5000); + + /////////////////////////////////////////////// + + Serial.print("\nStarting Async_Server on "); Serial.println(ARDUINO_BOARD); + + // Connect to WiFi + Serial.println("Setting up WiFi"); + WiFi.begin(WIFI_SSID, WIFI_PSK); + + while (WiFi.status() != WL_CONNECTED) + { + Serial.print("."); + delay(500); + } + + Serial.print("Connected. IP="); + Serial.println(WiFi.localIP()); + + // Setup the server as a separate task. + Serial.println("Creating server task... "); + // We pass: + // serverTask - the function that should be run as separate task + // "https443" - a name for the task (mainly used for logging) + // 6144 - stack size in byte. If you want up to four clients, you should + // not go below 6kB. If your stack is too small, you will encounter + // Panic and stack canary exceptions, usually during the call to + // SSL_accept. + xTaskCreatePinnedToCore(serverTask, "https443", 6144, NULL, 1, NULL, ARDUINO_RUNNING_CORE); +} + +void loop() +{ + Serial.println("loop()"); + delay(5000); +} diff --git a/examples/Async-Server/cert.h b/examples/Async-Server/cert.h new file mode 100644 index 0000000..c4b5c43 --- /dev/null +++ b/examples/Async-Server/cert.h @@ -0,0 +1,4 @@ +#ifndef CERT_H_ +#define CERT_H_ + #error You have to run the srcipt extras/create_cert.sh to recreate these files +#endif diff --git a/examples/Async-Server/private_key.h b/examples/Async-Server/private_key.h new file mode 100644 index 0000000..ec2498e --- /dev/null +++ b/examples/Async-Server/private_key.h @@ -0,0 +1,4 @@ +#ifndef PRIVATE_KEY_H_ +#define PRIVATE_KEY_H_ + #error You have to run the srcipt extras/create_cert.sh to recreate these files +#endif diff --git a/examples/Authentication/Authentication.ino b/examples/Authentication/Authentication.ino index 0a1cb2d..a66506d 100644 --- a/examples/Authentication/Authentication.ino +++ b/examples/Authentication/Authentication.ino @@ -1,28 +1,28 @@ /** - * Example for the ESP32 HTTP(S) Webserver - * - * IMPORTANT NOTE: - * To run this script, your need to - * 1) Enter your WiFi SSID and PSK below this comment - * 2) Make sure to have certificate data available. You will find a - * shell script and instructions to do so in the library folder - * under extras/ - * - * This script will install an HTTPS Server on your ESP32 with the following - * functionalities: - * - Show simple page on web server root - * - Provide some "internal pages" that are protected by the server - * - Run a middleware that authenticates the user - * - Run a middleware that provides access control - * - 404 for everything else - * Authentication is done using HTTP Basic Auth, which is supported by the webserver, - * so you don't have to care about retrieving the login information from request - * headers. - */ + Example for the ESP32 HTTP(S) Webserver + + IMPORTANT NOTE: + To run this script, your need to + 1) Enter your WiFi SSID and PSK below this comment + 2) Make sure to have certificate data available. You will find a + shell script and instructions to do so in the library folder + under extras/ + + This script will install an HTTPS Server on your ESP32 with the following + functionalities: + - Show simple page on web server root + - Provide some "internal pages" that are protected by the server + - Run a middleware that authenticates the user + - Run a middleware that provides access control + - 404 for everything else + Authentication is done using HTTP Basic Auth, which is supported by the webserver, + so you don't have to care about retrieving the login information from request + headers. +*/ // TODO: Configure your WiFi here -#define WIFI_SSID "" -#define WIFI_PSK "" +#define WIFI_SSID "your_ssid" +#define WIFI_PSK "12345678" // Include certificate data (see note above) #include "cert.h" @@ -51,21 +51,14 @@ using namespace httpsserver; // Create an SSL certificate object from the files included above SSLCert cert = SSLCert( - example_crt_DER, example_crt_DER_len, - example_key_DER, example_key_DER_len -); + example_crt_DER, example_crt_DER_len, + example_key_DER, example_key_DER_len + ); // Create an SSL-enabled server that uses the certificate // The contstructor takes some more parameters, but we go for default values here. HTTPSServer secureServer = HTTPSServer(&cert); -// Declare some handler functions for the various URLs on the server -void handleRoot(HTTPRequest * req, HTTPResponse * res); -void handleInternalPage(HTTPRequest * req, HTTPResponse * res); -void handleAdminPage(HTTPRequest * req, HTTPResponse * res); -void handlePublicPage(HTTPRequest * req, HTTPResponse * res); -void handle404(HTTPRequest * req, HTTPResponse * res); - // Declare a middleware function. // Parameters: // req: Request data, can be used to access URL, HTTP Method, Headers, ... @@ -78,79 +71,23 @@ void handle404(HTTPRequest * req, HTTPResponse * res); // code 403 on the response to show that the user is not allowed to access a specific // resource. // For more details, see the definition below. -void middlewareAuthentication(HTTPRequest * req, HTTPResponse * res, std::function next); -void middlewareAuthorization(HTTPRequest * req, HTTPResponse * res, std::function next); - -void setup() { - // For logging - Serial.begin(115200); - - // Connect to WiFi - Serial.println("Setting up WiFi"); - WiFi.begin(WIFI_SSID, WIFI_PSK); - while (WiFi.status() != WL_CONNECTED) { - Serial.print("."); - delay(500); - } - Serial.print("Connected. IP="); - Serial.println(WiFi.localIP()); - - // For every resource available on the server, we need to create a ResourceNode - // The ResourceNode links URL and HTTP method to a handler function - ResourceNode * nodeRoot = new ResourceNode("/", "GET", &handleRoot); - ResourceNode * nodeInternal = new ResourceNode("/internal", "GET", &handleInternalPage); - ResourceNode * nodeAdmin = new ResourceNode("/internal/admin", "GET", &handleAdminPage); - ResourceNode * nodePublic = new ResourceNode("/public", "GET", &handlePublicPage); - ResourceNode * node404 = new ResourceNode("", "GET", &handle404); - - // Add the nodes to the server - secureServer.registerNode(nodeRoot); - secureServer.registerNode(nodeInternal); - secureServer.registerNode(nodeAdmin); - secureServer.registerNode(nodePublic); - - // Add the 404 not found node to the server. - // The path is ignored for the default node. - secureServer.setDefaultNode(node404); - - // Add the middleware. These functions will be called globally for every request - // Note: The functions are called in the order they are added to the server. - // This means, we need to add the authentication middleware first, because the - // authorization middleware needs the headers that will be set by the authentication - // middleware (First we check the identity, then we see what the user is allowed to do) - secureServer.addMiddleware(&middlewareAuthentication); - secureServer.addMiddleware(&middlewareAuthorization); - Serial.println("Starting server..."); - secureServer.start(); - if (secureServer.isRunning()) { - Serial.println("Server ready."); - } -} +/** + The following middleware function is one of two functions dealing with access control. The + middlewareAuthentication() will interpret the HTTP Basic Auth header, check usernames and password, + and if they are valid, set the X-USERNAME and X-GROUP header. -void loop() { - // This call will let the server do its work - secureServer.loop(); + If they are invalid, the X-USERNAME and X-GROUP header will be unset. This is important because + otherwise the client may manipulate those internal headers. - // Other code would go here... - delay(1); -} + Having that done, further middleware functions and the request handler functions will be able to just + use req->getHeader("X-USERNAME") to find out if the user is logged in correctly. -/** - * The following middleware function is one of two functions dealing with access control. The - * middlewareAuthentication() will interpret the HTTP Basic Auth header, check usernames and password, - * and if they are valid, set the X-USERNAME and X-GROUP header. - * - * If they are invalid, the X-USERNAME and X-GROUP header will be unset. This is important because - * otherwise the client may manipulate those internal headers. - * - * Having that done, further middleware functions and the request handler functions will be able to just - * use req->getHeader("X-USERNAME") to find out if the user is logged in correctly. - * - * Furthermore, if the user supplies credentials and they are invalid, he will receive an 401 response - * without any other functions being called. - */ -void middlewareAuthentication(HTTPRequest * req, HTTPResponse * res, std::function next) { + Furthermore, if the user supplies credentials and they are invalid, he will receive an 401 response + without any other functions being called. +*/ +void middlewareAuthentication(HTTPRequest * req, HTTPResponse * res, std::function next) +{ // Unset both headers to discard any value from the client // This prevents authentication bypass by a client that just sets X-USERNAME req->setHeader(HEADER_USERNAME, ""); @@ -165,21 +102,29 @@ void middlewareAuthentication(HTTPRequest * req, HTTPResponse * res, std::functi std::string reqPassword = req->getBasicAuthPassword(); // If the user entered login information, we will check it - if (reqUsername.length() > 0 && reqPassword.length() > 0) { + if (reqUsername.length() > 0 && reqPassword.length() > 0) + { // _Very_ simple hardcoded user database to check credentials and assign the group bool authValid = true; std::string group = ""; - if (reqUsername == "admin" && reqPassword == "secret") { + + if (reqUsername == "admin" && reqPassword == "secret") + { group = "ADMIN"; - } else if (reqUsername == "user" && reqPassword == "test") { + } + else if (reqUsername == "user" && reqPassword == "test") + { group = "USER"; - } else { + } + else + { authValid = false; } // If authentication was successful - if (authValid) { + if (authValid) + { // set custom headers and delegate control req->setHeader(HEADER_USERNAME, reqUsername); req->setHeader(HEADER_GROUP, group); @@ -187,7 +132,9 @@ void middlewareAuthentication(HTTPRequest * req, HTTPResponse * res, std::functi // The user tried to authenticate and was successful // -> We proceed with this request. next(); - } else { + } + else + { // Display error page res->setStatusCode(401); res->setStatusText("Unauthorized"); @@ -204,7 +151,9 @@ void middlewareAuthentication(HTTPRequest * req, HTTPResponse * res, std::functi // NO CALL TO next() here, as the authentication failed. // -> The code above did handle the request already. } - } else { + } + else + { // No attempt to authenticate // -> Let the request pass through by calling next() next(); @@ -212,19 +161,21 @@ void middlewareAuthentication(HTTPRequest * req, HTTPResponse * res, std::functi } /** - * This function plays together with the middlewareAuthentication(). While the first function checks the - * username/password combination and stores it in the request, this function makes use of this information - * to allow or deny access. - * - * This example only prevents unauthorized access to every ResourceNode stored under an /internal/... path. - */ -void middlewareAuthorization(HTTPRequest * req, HTTPResponse * res, std::function next) { + This function plays together with the middlewareAuthentication(). While the first function checks the + username/password combination and stores it in the request, this function makes use of this information + to allow or deny access. + + This example only prevents unauthorized access to every ResourceNode stored under an /internal/... path. +*/ +void middlewareAuthorization(HTTPRequest * req, HTTPResponse * res, std::function next) +{ // Get the username (if any) std::string username = req->getHeader(HEADER_USERNAME); // Check that only logged-in users may get to the internal area (All URLs starting with /internal) // Only a simple example, more complicated configuration is up to you. - if (username == "" && req->getRequestString().substr(0,9) == "/internal") { + if (username == "" && req->getRequestString().substr(0, 9) == "/internal") + { // Same as the deny-part in middlewareAuthentication() res->setStatusCode(401); res->setStatusText("Unauthorized"); @@ -233,7 +184,9 @@ void middlewareAuthorization(HTTPRequest * req, HTTPResponse * res, std::functio res->println("401. Unauthorized (try admin/secret or user/test)"); // No call denies access to protected handler function. - } else { + } + else + { // Everything else will be allowed, so we call next() next(); } @@ -242,7 +195,8 @@ void middlewareAuthorization(HTTPRequest * req, HTTPResponse * res, std::functio // This is the internal page. It will greet the user with // a personalized message and - if the user is in the ADMIN group - // provide a link to the admin interface. -void handleInternalPage(HTTPRequest * req, HTTPResponse * res) { +void handleInternalPage(HTTPRequest * req, HTTPResponse * res) +{ // Header res->setStatusCode(200); res->setStatusText("OK"); @@ -266,7 +220,8 @@ void handleInternalPage(HTTPRequest * req, HTTPResponse * res) { res->println("

Welcome to the internal area. Congratulations on successfully entering your password!

"); // The "admin area" will only be shown if the correct group has been assigned in the authenticationMiddleware - if (req->getHeader(HEADER_GROUP) == "ADMIN") { + if (req->getHeader(HEADER_GROUP) == "ADMIN") + { res->println("
"); res->println("

You are an administrator

"); res->println("

You are allowed to access the admin page:

"); @@ -280,7 +235,8 @@ void handleInternalPage(HTTPRequest * req, HTTPResponse * res) { res->println(""); } -void handleAdminPage(HTTPRequest * req, HTTPResponse * res) { +void handleAdminPage(HTTPRequest * req, HTTPResponse * res) +{ // Headers res->setHeader("Content-Type", "text/html; charset=utf8"); @@ -289,7 +245,8 @@ void handleAdminPage(HTTPRequest * req, HTTPResponse * res) { // Checking permissions can not only be done centrally in the middleware function but also in the actual request handler. // This would be handy if you provide an API with lists of resources, but access rights are defined object-based. - if (req->getHeader(HEADER_GROUP) == "ADMIN") { + if (req->getHeader(HEADER_GROUP) == "ADMIN") + { res->setStatusCode(200); res->setStatusText("OK"); res->printStd(header); @@ -298,7 +255,9 @@ void handleAdminPage(HTTPRequest * req, HTTPResponse * res) { res->println("

You found the secret administrator page!

"); res->println("

Go back

"); res->println("
"); - } else { + } + else + { res->printStd(header); res->setStatusCode(403); res->setStatusText("Unauthorized"); @@ -309,7 +268,8 @@ void handleAdminPage(HTTPRequest * req, HTTPResponse * res) { } // Just a simple page for demonstration, very similar to the root page. -void handlePublicPage(HTTPRequest * req, HTTPResponse * res) { +void handlePublicPage(HTTPRequest * req, HTTPResponse * res) +{ res->setHeader("Content-Type", "text/html"); res->println(""); res->println(""); @@ -317,7 +277,7 @@ void handlePublicPage(HTTPRequest * req, HTTPResponse * res) { res->println(""); res->println("

Hello World!

"); res->print("

Your server is running for "); - res->print((int)(millis()/1000), DEC); + res->print((int)(millis() / 1000), DEC); res->println(" seconds.

"); res->println("

Go back

"); res->println(""); @@ -325,7 +285,8 @@ void handlePublicPage(HTTPRequest * req, HTTPResponse * res) { } // For details on the implementation of the hanlder functions, refer to the Static-Page example. -void handleRoot(HTTPRequest * req, HTTPResponse * res) { +void handleRoot(HTTPRequest * req, HTTPResponse * res) +{ res->setHeader("Content-Type", "text/html"); res->println(""); res->println(""); @@ -333,13 +294,14 @@ void handleRoot(HTTPRequest * req, HTTPResponse * res) { res->println(""); res->println("

Hello World!

"); res->println("

This is the authentication and authorization example. When asked for login " - "information, try admin/secret or user/test.

"); + "information, try admin/secret or user/test.

"); res->println("

Go to: Internal Page | Public Page

"); res->println(""); res->println(""); } -void handle404(HTTPRequest * req, HTTPResponse * res) { +void handle404(HTTPRequest * req, HTTPResponse * res) +{ req->discardRequestBody(); res->setStatusCode(404); res->setStatusText("Not Found"); @@ -350,3 +312,70 @@ void handle404(HTTPRequest * req, HTTPResponse * res) { res->println("

404 Not Found

The requested resource was not found on this server.

"); res->println(""); } + +void setup() +{ + // For logging + Serial.begin(115200); + while (!Serial && millis() < 5000); + + /////////////////////////////////////////////// + + Serial.print("\nStarting Authentication on "); Serial.println(ARDUINO_BOARD); + + // Connect to WiFi + Serial.println("Setting up WiFi"); + WiFi.begin(WIFI_SSID, WIFI_PSK); + + while (WiFi.status() != WL_CONNECTED) + { + Serial.print("."); + delay(500); + } + + Serial.print("Connected. IP="); + Serial.println(WiFi.localIP()); + + // For every resource available on the server, we need to create a ResourceNode + // The ResourceNode links URL and HTTP method to a handler function + ResourceNode * nodeRoot = new ResourceNode("/", "GET", &handleRoot); + ResourceNode * nodeInternal = new ResourceNode("/internal", "GET", &handleInternalPage); + ResourceNode * nodeAdmin = new ResourceNode("/internal/admin", "GET", &handleAdminPage); + ResourceNode * nodePublic = new ResourceNode("/public", "GET", &handlePublicPage); + ResourceNode * node404 = new ResourceNode("", "GET", &handle404); + + // Add the nodes to the server + secureServer.registerNode(nodeRoot); + secureServer.registerNode(nodeInternal); + secureServer.registerNode(nodeAdmin); + secureServer.registerNode(nodePublic); + + // Add the 404 not found node to the server. + // The path is ignored for the default node. + secureServer.setDefaultNode(node404); + + // Add the middleware. These functions will be called globally for every request + // Note: The functions are called in the order they are added to the server. + // This means, we need to add the authentication middleware first, because the + // authorization middleware needs the headers that will be set by the authentication + // middleware (First we check the identity, then we see what the user is allowed to do) + secureServer.addMiddleware(&middlewareAuthentication); + secureServer.addMiddleware(&middlewareAuthorization); + + Serial.println("Starting server..."); + secureServer.start(); + + if (secureServer.isRunning()) + { + Serial.println("Server ready."); + } +} + +void loop() +{ + // This call will let the server do its work + secureServer.loop(); + + // Other code would go here... + delay(1); +} diff --git a/examples/Authentication/cert.h b/examples/Authentication/cert.h new file mode 100644 index 0000000..c4b5c43 --- /dev/null +++ b/examples/Authentication/cert.h @@ -0,0 +1,4 @@ +#ifndef CERT_H_ +#define CERT_H_ + #error You have to run the srcipt extras/create_cert.sh to recreate these files +#endif diff --git a/examples/Authentication/private_key.h b/examples/Authentication/private_key.h new file mode 100644 index 0000000..ec2498e --- /dev/null +++ b/examples/Authentication/private_key.h @@ -0,0 +1,4 @@ +#ifndef PRIVATE_KEY_H_ +#define PRIVATE_KEY_H_ + #error You have to run the srcipt extras/create_cert.sh to recreate these files +#endif diff --git a/examples/HTML-Forms/HTML-Forms.ino b/examples/HTML-Forms/HTML-Forms.ino index b29dfba..d4ca00f 100644 --- a/examples/HTML-Forms/HTML-Forms.ino +++ b/examples/HTML-Forms/HTML-Forms.ino @@ -1,24 +1,24 @@ /** - * Example for the ESP32 HTTP(S) Webserver - * - * IMPORTANT NOTE: - * To run this script, you need to - * 1) Enter your WiFi SSID and PSK below this comment - * 2) Make sure to have certificate data available. You will find a - * shell script and instructions to do so in the library folder - * under extras/ - * - * This script will install an HTTPS Server on your ESP32 with the following - * functionalities: - * - Show simple page on web server root that includes some HTML Forms - * - Define a POST handler that handles the forms using the HTTPBodyParser API - * provided by the library. - * - 404 for everything else - */ + Example for the ESP32 HTTP(S) Webserver + + IMPORTANT NOTE: + To run this script, you need to + 1) Enter your WiFi SSID and PSK below this comment + 2) Make sure to have certificate data available. You will find a + shell script and instructions to do so in the library folder + under extras/ + + This script will install an HTTPS Server on your ESP32 with the following + functionalities: + - Show simple page on web server root that includes some HTML Forms + - Define a POST handler that handles the forms using the HTTPBodyParser API + provided by the library. + - 404 for everything else +*/ // TODO: Configure your WiFi here -#define WIFI_SSID "" -#define WIFI_PSK "" +#define WIFI_SSID "your_ssid" +#define WIFI_PSK "12345678" // Include certificate data (see note above) #include "cert.h" @@ -42,11 +42,11 @@ // We need to specify some content-type mapping, so the resources get delivered with the // right content type and are displayed correctly in the browser -char contentTypes[][2][32] = { - {".txt", "text/plain"}, - {".html", "text/html"}, - {".png", "image/png"}, - {".jpg", "image/jpg"}, +char contentTypes[][2][32] = +{ + {".txt", "text/plain"}, + {".png", "image/png"}, + {".jpg", "image/jpg"}, {"", ""} }; @@ -55,33 +55,31 @@ using namespace httpsserver; // Create an SSL certificate object from the files included above SSLCert cert = SSLCert( - example_crt_DER, example_crt_DER_len, - example_key_DER, example_key_DER_len -); + example_crt_DER, example_crt_DER_len, + example_key_DER, example_key_DER_len + ); // Create an SSL-enabled server that uses the certificate // The contstructor takes some more parameters, but we go for default values here. HTTPSServer secureServer = HTTPSServer(&cert); -// Declare some handler functions for the various URLs on the server -// See the static-page example for how handler functions work. -// The comments in setup() describe what each handler function does in this example. -void handleRoot(HTTPRequest * req, HTTPResponse * res); -void handleFormUpload(HTTPRequest * req, HTTPResponse * res); -void handleFormEdit(HTTPRequest * req, HTTPResponse * res); -void handleFile(HTTPRequest * req, HTTPResponse * res); -void handleDirectory(HTTPRequest * req, HTTPResponse * res); -void handle404(HTTPRequest * req, HTTPResponse * res); - -// As we have a file editor where the content of a file is pasted into a
"); res->println(""); res->println(""); } + res->println(""); - - } else { // method != GET + } + else + { + // method != GET // Assume POST request. Contains submitted data. res->println("File Edited

File Edited

"); - - // The form is submitted with the x-www-form-urlencoded content type, so we need the - // HTTPURLEncodedBodyParser to read the fields. - // Note that the content of the file's content comes from a