-
Notifications
You must be signed in to change notification settings - Fork 1
/
minimalUploadAuthESP32.ino
149 lines (122 loc) · 5.31 KB
/
minimalUploadAuthESP32.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
/****************************************************************************
POC to upload a file to esp32 with authorization
Browse to your esp32 to upload files
*****************************************************************************/
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include "upload_htm.h"
const char* WIFI_SSID = "----------";
const char* WIFI_PASSWORD = "----------";
const char* HTTP_USERNAME = "admin";
const char* HTTP_PASSWORD = "admin";
const size_t MAX_FILESIZE = 1024 * 1024 * 15;
/* format bytes as KB, MB or GB string */
String humanReadableSize(const size_t bytes) {
if (bytes < 1024) return String(bytes) + " B";
else if (bytes < (1024 * 1024)) return String(bytes / 1024.0) + " KB";
else if (bytes < (1024 * 1024 * 1024)) return String(bytes / 1024.0 / 1024.0) + " MB";
else return String(bytes / 1024.0 / 1024.0 / 1024.0) + " GB";
}
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.setSleep(false);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.printf("Connecting to WIFI_SSID %s with PSK %s...\n", WIFI_SSID, WIFI_PASSWORD);
if (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.printf("WiFi Failed!\n");
return;
}
static AsyncWebServer server(80);
static const char* MIMETYPE_HTML{"text/html"};
server.on("/", HTTP_GET, [](AsyncWebServerRequest * request)
{
AsyncWebServerResponse *response = request->beginResponse_P(200, MIMETYPE_HTML, upload_htm, upload_htm_len);
response->addHeader("Server", "ESP Async Web Server");
request->send(response);
});
// preflight cors check
server.on("/", HTTP_OPTIONS, [](AsyncWebServerRequest * request)
{
AsyncWebServerResponse* response = request->beginResponse(204);
response->addHeader("Access-Control-Allow-Methods", "PUT,POST,GET,OPTIONS");
response->addHeader("Access-Control-Allow-Headers", "Accept, Content-Type, Authorization, FileSize");
response->addHeader("Access-Control-Allow-Credentials", "true");
request->send(response);
});
server.on("/", HTTP_POST, [](AsyncWebServerRequest * request)
{
if (request->authenticate(HTTP_USERNAME, HTTP_PASSWORD))
request->send(200);
else {
request->send(401);
request->client()->close();
}
},
[](AsyncWebServerRequest * request, String filename, size_t index, uint8_t *data, size_t len, bool final)
{
if (!request->authenticate(HTTP_USERNAME, HTTP_PASSWORD)) {
request->send(401);
request->client()->close();
return;
}
// https://javascript.info/formdata
static unsigned long startTimer;
if (!index) {
startTimer = millis();
const char* FILESIZE_HEADER{"FileSize"};
Serial.printf("UPLOAD: Receiving: '%s'\n", filename.c_str());
if (!request->hasHeader(FILESIZE_HEADER)) {
request->send(400, MIMETYPE_HTML, "No filesize header present!");
request->client()->close();
Serial.printf("UPLOAD: Aborted upload because missing filesize header.\n");
return;
}
Serial.printf("UPLOAD: fileSize: %s\n", request->header(FILESIZE_HEADER));
if (request->header(FILESIZE_HEADER).toInt() >= MAX_FILESIZE) {
request->send(400, MIMETYPE_HTML,
"Too large. (" + humanReadableSize(request->header(FILESIZE_HEADER).toInt()) +
") Max size is " + humanReadableSize(MAX_FILESIZE) + ".");
request->client()->close();
Serial.printf("UPLOAD: Aborted upload because filesize limit.\n");
return;
}
}
//Store or do something with the data...
//Serial.printf("file: '%s' received %i bytes\ttotal: %i\n", filename.c_str(), len, index + len);
if (final)
Serial.printf("UPLOAD: Done. Received %i bytes in %.2fs which is %.2f kB/s.\n",
index + len,
(millis() - startTimer) / 1000.0,
1.0 * (index + len) / (millis() - startTimer));
});
server.onNotFound([](AsyncWebServerRequest * request)
{
Serial.printf("NOT_FOUND: ");
if (request->method() == HTTP_GET)
Serial.printf("GET");
else if (request->method() == HTTP_POST)
Serial.printf("POST");
else if (request->method() == HTTP_DELETE)
Serial.printf("DELETE");
else if (request->method() == HTTP_PUT)
Serial.printf("PUT");
else if (request->method() == HTTP_PATCH)
Serial.printf("PATCH");
else if (request->method() == HTTP_HEAD)
Serial.printf("HEAD");
else if (request->method() == HTTP_OPTIONS)
Serial.printf("OPTIONS");
else
Serial.printf("UNKNOWN");
Serial.printf(" http://%s%s\n", request->host().c_str(), request->url().c_str());
request->send(404, MIMETYPE_HTML, "404 - Not found.");
});
DefaultHeaders::Instance().addHeader("Access-Control-Allow-Origin", "*");
server.begin();
Serial.print("Upload files at ");
Serial.println(WiFi.localIP());
}
void loop() {
delay(1000);
}