This repository has been archived by the owner on Jan 29, 2023. It is now read-only.
forked from fhessel/esp32_https_server
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Middleware.ino
188 lines (153 loc) · 6.48 KB
/
Middleware.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/**
Example for the WT32_ETH01 HTTP(S) Webserver
IMPORTANT NOTE:
To run this script, your need to
1) Make sure to have certificate data available. You will find a
shell script (create_cert.sh) and instructions to do so in the library folder
under extras/
This script will install an HTTPS Server on your WT32_ETH01 with the following
functionalities:
- Show simple page on web server root
- Run a middleware that logs every request
- 404 for everything else
*/
// Include certificate data (see note above)
#include "cert.h"
#include "private_key.h"
//////////////////////////////////////////////////
// For WT32_ETH01
#define DEBUG_ETHERNET_WEBSERVER_PORT Serial
// Debug Level from 0 to 4
#define _ETHERNET_WEBSERVER_LOGLEVEL_ 3
#include <WebServer_WT32_ETH01.h>
// Select the IP address according to your local network
IPAddress myIP(192, 168, 2, 232);
IPAddress myGW(192, 168, 2, 1);
IPAddress mySN(255, 255, 255, 0);
// Google DNS Server IP
IPAddress myDNS(8, 8, 8, 8);
//////////////////////////////////////////////////
// For the middleware
#include <functional>
// Includes for the server
#include <HTTPSServer.hpp>
#include <SSLCert.hpp>
#include <HTTPRequest.hpp>
#include <HTTPResponse.hpp>
// The HTTPS Server comes in a separate namespace. For easier use, include it here.
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
);
// 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 a middleware function.
// Parameters:
// req: Request data, can be used to access URL, HTTP Method, Headers, ...
// res: Response data, can be used to access HTTP Status, Headers, ...
// next: This function is used to pass control down the chain. If you have done your work
// with the request object, you may decide if you want to process the request.
// If you do so, you call the next() function, and the next middleware function (if
// there is any) or the actual requestHandler will be called.
// If you want to skip the request, you do not call next, and set for example status
// code 403 on the response to show that the user is not allowed to access a specific
// resource.
// The Authentication examples provides more details on this.
// We want to log the following information for every request:
// - Response Status
// - Request Method
// - Request String (URL + Parameters)
void middlewareLogging(HTTPRequest * req, HTTPResponse * res, std::function<void()> next)
{
// We want to print the response status, so we need to call next() first.
next();
// After the call, the status is (hopefully) set by the handler function, so we can
// access it for logging.
Serial.printf("middlewareLogging(): %3d\t%s\t\t%s\n",
// Status code (like: 200)
res->getStatusCode(),
// Method used for the request (like: GET)
req->getMethod().c_str(),
// Request string (like /index.html)
req->getRequestString().c_str());
}
// For details on the implementation of the hanlder functions, refer to the Static-Page example.
void handleRoot(HTTPRequest * req, HTTPResponse * res)
{
res->setHeader("Content-Type", "text/html");
res->println("<!DOCTYPE html>");
res->println("<html>");
res->println("<head><title>Hello World!</title></head>");
res->println("<body>");
res->println("<h1>Hello World!</h1>");
res->print("<p>Your server is running for ");
res->print((int)(millis() / 1000), DEC);
res->println(" seconds.</p>");
res->println("</body>");
res->println("</html>");
}
void handle404(HTTPRequest * req, HTTPResponse * res)
{
req->discardRequestBody();
res->setStatusCode(404);
res->setStatusText("Not Found");
res->setHeader("Content-Type", "text/html");
res->println("<!DOCTYPE html>");
res->println("<html>");
res->println("<head><title>Not Found</title></head>");
res->println("<body><h1>404 Not Found</h1><p>The requested resource was not found on this server.</p></body>");
res->println("</html>");
}
void setup()
{
// For logging
Serial.begin(115200);
while (!Serial && millis() < 5000);
///////////////////////////////////////////////
Serial.print("\nStarting Middleware on "); Serial.print(ARDUINO_BOARD);
Serial.println(" with " + String(SHIELD_TYPE));
Serial.println(WEBSERVER_WT32_ETH01_VERSION);
// To be called before ETH.begin()
WT32_ETH01_onEvent();
//bool begin(uint8_t phy_addr=ETH_PHY_ADDR, int power=ETH_PHY_POWER, int mdc=ETH_PHY_MDC, int mdio=ETH_PHY_MDIO,
// eth_phy_type_t type=ETH_PHY_TYPE, eth_clock_mode_t clk_mode=ETH_CLK_MODE);
//ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER, ETH_PHY_MDC, ETH_PHY_MDIO, ETH_PHY_TYPE, ETH_CLK_MODE);
ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER);
// Static IP, leave without this line to get IP via DHCP
//bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
ETH.config(myIP, myGW, mySN, myDNS);
WT32_ETH01_waitForConnect();
Serial.print(F("HTTP EthernetWebServer is @ IP : "));
Serial.println(ETH.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 * node404 = new ResourceNode("", "GET", &handle404);
// Add the root node to the server
secureServer.registerNode(nodeRoot);
// Add the 404 not found node to the server.
// The path is ignored for the default node.
secureServer.setDefaultNode(node404);
// Add the middleware. The function will be called globally for every request
// Note: The functions are called in the order they are added to the server.
// Also, if you want a middleware to handle only specific requests, you can check
// the URL within the middleware function.
secureServer.addMiddleware(&middlewareLogging);
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);
}