|
| 1 | +#include <WiFi.h> |
| 2 | +#include <WebServer.h> |
| 3 | +#include <Middlewares.h> |
| 4 | + |
| 5 | +// Your AP WiFi Credentials |
| 6 | +// ( This is the AP your ESP will broadcast ) |
| 7 | +const char *ap_ssid = "ESP32_Demo"; |
| 8 | +const char *ap_password = ""; |
| 9 | + |
| 10 | +WebServer server(80); |
| 11 | + |
| 12 | +LoggingMiddleware logger; |
| 13 | +CorsMiddleware cors; |
| 14 | +AuthenticationMiddleware auth; |
| 15 | + |
| 16 | +void setup(void) { |
| 17 | + Serial.begin(115200); |
| 18 | + WiFi.softAP(ap_ssid, ap_password); |
| 19 | + |
| 20 | + Serial.print("IP address: "); |
| 21 | + Serial.println(WiFi.AP.localIP()); |
| 22 | + |
| 23 | + // curl-like output example: |
| 24 | + // |
| 25 | + // > curl -v -X OPTIONS -H "origin: http://192.168.4.1" http://192.168.4.1/ |
| 26 | + // |
| 27 | + // Connection from 192.168.4.2:51683 |
| 28 | + // > OPTIONS / HTTP/1.1 |
| 29 | + // > Host: 192.168.4.1 |
| 30 | + // > User-Agent: curl/8.10.0 |
| 31 | + // > Accept: */* |
| 32 | + // > origin: http://192.168.4.1 |
| 33 | + // > |
| 34 | + // * Processed in 5 ms |
| 35 | + // < HTTP/1.HTTP/1.1 200 OK |
| 36 | + // < Content-Type: text/html |
| 37 | + // < Access-Control-Allow-Origin: http://192.168.4.1 |
| 38 | + // < Access-Control-Allow-Methods: POST,GET,OPTIONS,DELETE |
| 39 | + // < Access-Control-Allow-Headers: X-Custom-Header |
| 40 | + // < Access-Control-Allow-Credentials: false |
| 41 | + // < Access-Control-Max-Age: 600 |
| 42 | + // < Content-Length: 0 |
| 43 | + // < Connection: close |
| 44 | + // < |
| 45 | + logger.setOutput(Serial); |
| 46 | + |
| 47 | + cors.setOrigin("http://192.168.4.1"); |
| 48 | + cors.setMethods("POST,GET,OPTIONS,DELETE"); |
| 49 | + cors.setHeaders("X-Custom-Header"); |
| 50 | + cors.setAllowCredentials(false); |
| 51 | + cors.setMaxAge(600); |
| 52 | + |
| 53 | + auth.setUsername("admin"); |
| 54 | + auth.setPassword("admin"); |
| 55 | + auth.setRealm("My Super App"); |
| 56 | + auth.setAuthMethod(DIGEST_AUTH); |
| 57 | + auth.setAuthFailureMessage("Authentication Failed"); |
| 58 | + |
| 59 | + server.addMiddleware(&logger); |
| 60 | + server.addMiddleware(&cors); |
| 61 | + |
| 62 | + // Not authenticated |
| 63 | + // |
| 64 | + // Test CORS preflight request with: |
| 65 | + // > curl -v -X OPTIONS -H "origin: http://192.168.4.1" http://192.168.4.1/ |
| 66 | + // |
| 67 | + // Test cross-domain request with: |
| 68 | + // > curl -v -X GET -H "origin: http://192.168.4.1" http://192.168.4.1/ |
| 69 | + // |
| 70 | + server.on("/", []() { |
| 71 | + server.send(200, "text/plain", "Home"); |
| 72 | + }); |
| 73 | + |
| 74 | + // Authenticated |
| 75 | + // |
| 76 | + // > curl -v -X GET -H "origin: http://192.168.4.1" http://192.168.4.1/protected |
| 77 | + // |
| 78 | + // Outputs: |
| 79 | + // |
| 80 | + // * Connection from 192.168.4.2:51750 |
| 81 | + // > GET /protected HTTP/1.1 |
| 82 | + // > Host: 192.168.4.1 |
| 83 | + // > User-Agent: curl/8.10.0 |
| 84 | + // > Accept: */* |
| 85 | + // > origin: http://192.168.4.1 |
| 86 | + // > |
| 87 | + // * Processed in 7 ms |
| 88 | + // < HTTP/1.HTTP/1.1 401 Unauthorized |
| 89 | + // < Content-Type: text/html |
| 90 | + // < Access-Control-Allow-Origin: http://192.168.4.1 |
| 91 | + // < Access-Control-Allow-Methods: POST,GET,OPTIONS,DELETE |
| 92 | + // < Access-Control-Allow-Headers: X-Custom-Header |
| 93 | + // < Access-Control-Allow-Credentials: false |
| 94 | + // < Access-Control-Max-Age: 600 |
| 95 | + // < WWW-Authenticate: Digest realm="My Super App", qop="auth", nonce="ac388a64184e3e102aae6fff1c9e8d76", opaque="e7d158f2b54d25328142d118ff0f932d" |
| 96 | + // < Content-Length: 21 |
| 97 | + // < Connection: close |
| 98 | + // < |
| 99 | + // |
| 100 | + // > curl -v -X GET -H "origin: http://192.168.4.1" --digest -u admin:admin http://192.168.4.1/protected |
| 101 | + // |
| 102 | + // Outputs: |
| 103 | + // |
| 104 | + // * Connection from 192.168.4.2:53662 |
| 105 | + // > GET /protected HTTP/1.1 |
| 106 | + // > Authorization: Digest username="admin", realm="My Super App", nonce="db9e6824eb2a13bc7b2bf8f3c43db896", uri="/protected", cnonce="NTliZDZiNTcwODM2MzAyY2JjMDBmZGJmNzFiY2ZmNzk=", nc=00000001, qop=auth, response="6ebd145ba0d3496a4a73f5ae79ff5264", opaque="23d739c22810282ff820538cba98bda4" |
| 107 | + // > Host: 192.168.4.1 |
| 108 | + // > User-Agent: curl/8.10.0 |
| 109 | + // > Accept: */* |
| 110 | + // > origin: http://192.168.4.1 |
| 111 | + // > |
| 112 | + // Request handling... |
| 113 | + // * Processed in 7 ms |
| 114 | + // < HTTP/1.HTTP/1.1 200 OK |
| 115 | + // < Content-Type: text/plain |
| 116 | + // < Access-Control-Allow-Origin: http://192.168.4.1 |
| 117 | + // < Access-Control-Allow-Methods: POST,GET,OPTIONS,DELETE |
| 118 | + // < Access-Control-Allow-Headers: X-Custom-Header |
| 119 | + // < Access-Control-Allow-Credentials: false |
| 120 | + // < Access-Control-Max-Age: 600 |
| 121 | + // < Content-Length: 9 |
| 122 | + // < Connection: close |
| 123 | + // < |
| 124 | + server |
| 125 | + .on( |
| 126 | + "/protected", |
| 127 | + []() { |
| 128 | + Serial.println("Request handling..."); |
| 129 | + server.send(200, "text/plain", "Protected"); |
| 130 | + } |
| 131 | + ) |
| 132 | + .addMiddleware(&auth); |
| 133 | + |
| 134 | + // Not found is also handled by global middleware |
| 135 | + // |
| 136 | + // curl -v -X GET -H "origin: http://192.168.4.1" http://192.168.4.1/inexsting |
| 137 | + // |
| 138 | + // Outputs: |
| 139 | + // |
| 140 | + // * Connection from 192.168.4.2:53683 |
| 141 | + // > GET /inexsting HTTP/1.1 |
| 142 | + // > Host: 192.168.4.1 |
| 143 | + // > User-Agent: curl/8.10.0 |
| 144 | + // > Accept: */* |
| 145 | + // > origin: http://192.168.4.1 |
| 146 | + // > |
| 147 | + // * Processed in 16 ms |
| 148 | + // < HTTP/1.HTTP/1.1 404 Not Found |
| 149 | + // < Content-Type: text/plain |
| 150 | + // < Access-Control-Allow-Origin: http://192.168.4.1 |
| 151 | + // < Access-Control-Allow-Methods: POST,GET,OPTIONS,DELETE |
| 152 | + // < Access-Control-Allow-Headers: X-Custom-Header |
| 153 | + // < Access-Control-Allow-Credentials: false |
| 154 | + // < Access-Control-Max-Age: 600 |
| 155 | + // < Content-Length: 14 |
| 156 | + // < Connection: close |
| 157 | + // < |
| 158 | + server.onNotFound([]() { |
| 159 | + server.send(404, "text/plain", "Page not found"); |
| 160 | + }); |
| 161 | + |
| 162 | + server.collectAllHeaders(); |
| 163 | + server.begin(); |
| 164 | + Serial.println("HTTP server started"); |
| 165 | +} |
| 166 | + |
| 167 | +void loop(void) { |
| 168 | + server.handleClient(); |
| 169 | + delay(2); //allow the cpu to switch to other tasks |
| 170 | +} |
0 commit comments