-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileHandler.cpp
207 lines (198 loc) · 6.31 KB
/
FileHandler.cpp
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/**
* @file FileHandler.cpp
* @author Phil Hilger ([email protected])
* @brief
* @version 0.1
* @date 2023-03-02
*
* CAN-talk. A library for microcontrollers that allows decent comms
* over a CAN bus.
*
* Copyright (C) 2023, PeerGum
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https: //www.gnu.org/licenses/>.
*
*/
#include "FileHandler.h"
#include "esp_log.h"
#include <stdio.h>
#include <string.h>
static const char* TAG = "FileHdlr";
static char buffer[2048];
static char uri[255];
FileHandler::FileHandler(int numAliases, Alias* aliases, bool useFAT)
: numAliases(numAliases), aliases(aliases), _useFAT(useFAT) {}
FileHandler& FileHandler::name(const char* filename) {
_name = filename;
return *this;
}
FileHandler& FileHandler::type(const char* mimeType) {
_type = mimeType;
return *this;
}
esp_err_t FileHandler::handler(httpd_req_t* req) {
// size_t len = 0;
ESP_LOGD(TAG, "uri = %s", req->uri);
strcpy(uri, req->uri);
char *parts[5];
int part = 0;
for (char* pos = strtok(uri, "/"); pos; pos = strtok(NULL, "/")) {
ESP_LOGD(TAG, "[%s]", pos);
parts[part] = pos;
part++;
}
if (req->method == HTTP_POST) {
ESP_LOGD(TAG, "Analyzing POST");
// while (req->content_len>sizeof(buffer)) {
int ret = httpd_req_recv(req, buffer, req->content_len);
if (ret > 0) {
ESP_LOGD(TAG, "buffer = %.*s", ret, buffer);
}
else if (ret < 0) {
return ret;
} else {
ESP_LOGD(TAG, "empty body");
}
// }
}
// if (part == 0) {
// for (int i=0; i<numAliases; i++) {
// }
// } else {
// if (!strcmp())
// }
char filename[255];
strcpy(filename,FAT_MOUNT_POINT);
strcat(filename, "/web");
int i;
for (i=0; i<numAliases; i++) {
if (!strcmp(req->uri, aliases[i].uri)) {
strcat(filename, aliases[i].page);
break;
}
}
if (i == numAliases) {
strcat(filename,req->uri);
}
char contentType[30];
if (part > 0) {
if (strstr(parts[part - 1], ".jpg")) {
strcpy(contentType, "image/jpg");
} else if (strstr(parts[part - 1], ".png")) {
strcpy(contentType, "image/png");
} else if (strstr(parts[part - 1], ".svg")) {
strcpy(contentType, "image/svg+xml");
}
// else if (strstr(parts[part - 1], ".json")) {
// strcpy(contentType,"application/json");
// for (int j = 0; j < numApis; j++) {
// if (!strcmp(parts[part - 1], apis[j].name)) {
// return apis[j].handler(req);
// }
// }
// // no API found.
// return httpd_resp_send_404(req);
// }
else if (strstr(parts[part - 1], ".js")) {
strcpy(contentType, "text/javascript");
} else if (strstr(parts[part - 1], ".css")) {
strcpy(contentType, "text/css");
} else {
strcpy(contentType, "text/html");
}
} else {
strcpy(contentType, "text/html");
}
ESP_LOGD(TAG, "file: %s", filename);
int packet = 0;
int read = 0;
size_t readSize = sizeof(buffer);
esp_err_t err = ESP_OK;
if (_useFAT) {
FILE* file = fopen(filename, "r");
while (file && (read = fread(buffer, 1, readSize, file)) >= 0) {
httpd_resp_set_status(req, "200 OK");
httpd_resp_set_type(req, contentType);
httpd_resp_set_hdr(req, "Cache-Control", "max-age=120");
// ESP_LOGD(TAG,"read=%d",read);
if (read < readSize && packet == 0) {
httpd_resp_send(req, buffer, read);
break;
}
err = httpd_resp_send_chunk(req, buffer, read);
if (err != ESP_OK || read == 0) {
break;
}
packet++;
}
if (file) {
if (packet>0) {
httpd_resp_set_status(req, "200 OK");
httpd_resp_set_type(req, contentType);
httpd_resp_set_hdr(req, "Cache-Control", "max-age=120");
err = httpd_resp_send_chunk(req, buffer, 0);
}
fclose(file);
} else {
return httpd_resp_send_404(req);
}
} else {
char* pageName = strrchr(filename, '/')+1;
uint8_t *start = NULL, *end;
int i = 0;
do {
if (!strcmp(pageName,memFiles[i].name)) {
start = (uint8_t *)memFiles
[i].start;
end = (uint8_t*)memFiles[i].end;
ESP_LOGD(TAG, "Mem Page found: %s", pageName);
}
i++;
} while (!start && memFiles[i].name);
if (!start) {
return httpd_resp_send_404(req);
}
size_t length = end - start - 1;
char contentLength[10];
sprintf(contentLength, "%d", length);
httpd_resp_set_hdr(req, "Content-Length", contentLength);
for (int i = 0; i < length; i += readSize) {
httpd_resp_set_status(req, "200 OK");
httpd_resp_set_type(req, contentType);
httpd_resp_set_hdr(req, "Cache-Control", "max-age=120");
read = (readSize < length - i) ? readSize : (length - i);
memcpy((void*)buffer, (void*)(start + i), read);
if ((read < readSize && packet == 0) || length == read) {
err = httpd_resp_send(req, buffer, (ssize_t)read);
break;
}
err = httpd_resp_send_chunk(req, buffer, (ssize_t)read);
if (err != ESP_OK) {
break;
}
packet++;
}
if (packet > 0) {
httpd_resp_set_status(req, "200 OK");
httpd_resp_set_type(req, contentType);
httpd_resp_set_hdr(req, "Cache-Control", "max-age=120");
err = httpd_resp_send_chunk(req, buffer, 0);
}
}
return err;
}
// void FileHandler::setAliases(Alias *aliases, int numAliases) {
// _aliases = aliases;
// _numAliases = numAliases;
// }