-
Notifications
You must be signed in to change notification settings - Fork 0
/
webserver.h
103 lines (88 loc) · 2.43 KB
/
webserver.h
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
int readbigfatsd(String thepath,uint8_t *buffer, size_t index,int maxlen)
{
FsFile vfile;
//char message[256];
volatile int thesize = 0;
volatile bool seekok;
//sprintf(message,"%s: Index: %d MaxLen: %d\n",thepath,index,maxlen);
//Serial.print(index);
//Serial.print(" - ");
//Serial.println(thepath);
if (maxlen > 4096){
maxlen = 4096;
}
vfile.open((char *)thepath.c_str(),O_RDWR);
if (vfile){
seekok = vfile.seekSet(index);
if (seekok == false){
//Serial.print(thepath);
//Serial.println(F(" - EOF"));
vfile.close();
vfile_busy = 0;
return(0);
}
if (vfile.available() == false){
//Serial.print(thepath);
//Serial.println(F(" - EOF1"));
vfile.close();
vfile_busy = 0;
return(0);
}
thesize = vfile.read(buffer,maxlen);
if (thesize == 0){
vfile_busy = 0;
//Serial.print(thepath);
//Serial.println(F(" - EOF2"));
}
vfile.close();
} else {
thesize = 0;
}
return(thesize);
}
bool handleStaticFile(AsyncWebServerRequest *request) {
boolean gzipped;
String xpath = request->url();
//Serial.println(xpath);
if (xpath.equals("/")){
xpath = F("index.html");
}
String contentType = request->contentType();
if (sd.exists(xpath)){
gzipped = false;
//Serial.println("Path Exists");
} else {
gzipped = true;
xpath = xpath + F(".gz");
if (sd.exists(xpath) == false){
request->send(404);
return(false);
}
}
//Serial.println(xpath);
AsyncWebServerResponse *response = request->beginChunkedResponse(contentType, [xpath](uint8_t *buffer, size_t maxLen, size_t index) -> size_t {
return readbigfatsd(xpath,buffer,index,maxLen);
});
//response->addHeader("Cache-Control", "no-cache");
if (gzipped){
response->addHeader("Content-Encoding", "gzip");
}
response->addHeader("Cache-Control", "max-age=31536000, immutable, public");
response->addHeader("Access-Control-Allow-Origin", "*");
request->send(response);
return true;
}
void webtask(void * parm0)
{
bool thestatus;
struct webreq_entry *webr;
AsyncWebServerRequest *request;
server.onNotFound([](AsyncWebServerRequest *request) {
if (handleStaticFile(request)) return;
//request->send(404);
});
server.begin();
while(1){
delay(1000);
}
}