Fri, 05 Jul 2019 16:11:55 +0200
Added more plain logfile entries. Change some message details. Added anohe function to eliminate duplicated code. Moved the logfile to the web so it can be downloaded or viewed.
0 | 1 | /** |
2 | * @file task_http.c | |
3 | * @brief HTTP and Websocket server functions. | |
4 | * This uses some ESP32 Websocket code written by Blake Felt - blake.w.felt@gmail.com | |
5 | */ | |
6 | #include "config.h" | |
7 | #include "mbedtls/base64.h" | |
8 | #include "mbedtls/sha1.h" | |
41
7639cfa6aec0
Websocket interface is working for the main screen and manual mode.
Michiel Broek <mbroek@mbse.eu>
parents:
38
diff
changeset
|
9 | #include "cJSON.h" |
0 | 10 | |
11 | ||
1
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
12 | static const char *TAG = "task_http"; |
0 | 13 | static QueueHandle_t client_queue; |
14 | const static int client_queue_size = 10; | |
15 | static TaskHandle_t xTaskHTTP = NULL; | |
16 | static TaskHandle_t xTaskQueue = NULL; | |
17 | ||
41
7639cfa6aec0
Websocket interface is working for the main screen and manual mode.
Michiel Broek <mbroek@mbse.eu>
parents:
38
diff
changeset
|
18 | cJSON *root = NULL; |
7639cfa6aec0
Websocket interface is working for the main screen and manual mode.
Michiel Broek <mbroek@mbse.eu>
parents:
38
diff
changeset
|
19 | cJSON *touch = NULL; |
0 | 20 | |
21 | ||
1
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
22 | /** |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
23 | * @brief Debug dump buffer |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
24 | * @param buf The buffer |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
25 | * @param buflen Length of the buffer |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
26 | */ |
41
7639cfa6aec0
Websocket interface is working for the main screen and manual mode.
Michiel Broek <mbroek@mbse.eu>
parents:
38
diff
changeset
|
27 | #if 0 |
0 | 28 | void dump_buf(char *buf, uint16_t buflen); |
41
7639cfa6aec0
Websocket interface is working for the main screen and manual mode.
Michiel Broek <mbroek@mbse.eu>
parents:
38
diff
changeset
|
29 | #endif |
0 | 30 | |
1
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
31 | |
0 | 32 | /** |
33 | * @brief Send HTTP error and message | |
34 | * @param conn The socket to send to. | |
35 | * @param error The HTTP error code. | |
36 | * @param message Yhe human readable explanation. | |
37 | * @paeam body The human readable explanation. | |
38 | */ | |
39 | static void http_error(struct netconn *conn, int error, char *message, char *body) | |
40 | { | |
41 | char *tmp = malloc(200); | |
42 | ||
43 | ESP_LOGI(TAG, "Http response %d - %s", error, message); | |
44 | if (strlen(body)) { | |
45 | snprintf(tmp, 199, "HTTP/1.1 %d %s\r\nContent-type: text/plain\r\n\r\n%s\n", error, message, body); | |
46 | } else { | |
47 | snprintf(tmp, 199, "HTTP/1.1 %d %s\r\n\r\n", error, message); | |
48 | } | |
49 | netconn_write(conn, tmp, strlen(tmp), NETCONN_NOCOPY); | |
50 | free(tmp); | |
51 | } | |
52 | ||
53 | ||
54 | ||
55 | /** | |
56 | * @brief Send HTTP file from the filesystem. | |
57 | * @param conn The network connection. | |
58 | * @param url The requested url. | |
59 | * @param mod_since The date/time of the file, or NULL. | |
60 | * @param ipstr The IP address of the remote. | |
61 | */ | |
62 | static void http_sendfile(struct netconn *conn, char *url, char *mod_since, char *ipstr) | |
63 | { | |
54
7b134c27fadb
Upgraded esp-ide and compilers. Adjusted the sources for the new compiler warnings.
Michiel Broek <mbroek@mbse.eu>
parents:
47
diff
changeset
|
64 | char temp_url[128], temp_url_gz[132], header[256], c_type[32]; |
0 | 65 | struct stat st; |
66 | off_t filesize; | |
67 | size_t sentsize; | |
68 | char strftime_buf[64]; | |
69 | err_t err; | |
70 | bool send_gz; | |
71 | FILE *f; | |
72 | ||
73 | if (url[strlen(url) - 1] == '/') { | |
74 | // If no filename given, use index.html | |
75 | sprintf(temp_url, "/spiffs/w%sindex.html", url); | |
76 | } else if (strncmp(url, "/log/", 5) == 0) { | |
77 | // Logfiles are on the SD card. | |
78 | sprintf(temp_url, "/sdcard/w%s", url); | |
79 | } else { | |
80 | sprintf(temp_url, "/spiffs/w%s", url); | |
81 | for (int i = 0; i < 127; i++) { | |
82 | if (temp_url[i] == '?') | |
83 | temp_url[i] = '\0'; | |
84 | if (temp_url[i] == '\0') | |
85 | break; | |
86 | } | |
87 | } | |
54
7b134c27fadb
Upgraded esp-ide and compilers. Adjusted the sources for the new compiler warnings.
Michiel Broek <mbroek@mbse.eu>
parents:
47
diff
changeset
|
88 | snprintf(temp_url_gz, 131, "%s.gz", temp_url); |
0 | 89 | |
90 | /* | |
91 | * Get filesize and date for the response headers. | |
92 | * Note that the /spiffs filesystem doesn't support file timestamps | |
93 | * and returns the epoch for each file. Therefore we cannot use | |
94 | * the cache based on timestamps. | |
95 | */ | |
96 | filesize = 0; | |
97 | strftime_buf[0] = '\0'; | |
98 | send_gz = false; | |
99 | if (stat(temp_url_gz, &st) == 0) { | |
100 | filesize = st.st_size; | |
101 | strftime(strftime_buf, sizeof(strftime_buf), "%a, %d %b %Y %T %z", localtime(&(st.st_mtime))); | |
102 | send_gz = true; | |
103 | } else if (stat(temp_url, &st) == 0) { | |
104 | filesize = st.st_size; | |
105 | strftime(strftime_buf, sizeof(strftime_buf), "%a, %d %b %Y %T %z", localtime(&(st.st_mtime))); | |
106 | } | |
107 | ||
108 | /* | |
109 | * If we have a If-Modified-Since parameter, compare that with the current | |
110 | * filedate on disk. If It's the same send a 304 response. | |
111 | * Cannot work on /spiffs. | |
112 | */ | |
113 | #if 0 | |
114 | time_t Now; | |
115 | struct tm timeInfo; | |
116 | if (mod_since && strlen(strftime_buf)) { | |
117 | time(&Now); | |
118 | localtime_r(&Now, &timeInfo); | |
119 | strftime(strftime_buf, sizeof(strftime_buf), "%a, %d %b %Y %T %z", &timeInfo); | |
120 | sprintf(header, "HTTP/1.1 304 Not Modified\r\nDate: %s\r\n\r\n", strftime_buf); | |
121 | netconn_write(conn, header, strlen(header), NETCONN_NOCOPY); | |
122 | ESP_LOGI(TAG, "http_sendfile %s Not Modified, ok", temp_url); | |
123 | return; | |
124 | } | |
125 | #endif | |
126 | ||
127 | if (send_gz) { | |
128 | f = fopen(temp_url_gz, "r"); | |
129 | } else { | |
130 | f = fopen(temp_url, "r"); | |
131 | } | |
132 | if (f == NULL) { | |
133 | ESP_LOGI(TAG, "%s url \'%s\' file \'%s\' not found", ipstr, url, temp_url); | |
54
7b134c27fadb
Upgraded esp-ide and compilers. Adjusted the sources for the new compiler warnings.
Michiel Broek <mbroek@mbse.eu>
parents:
47
diff
changeset
|
134 | http_error(conn, 404, (char *)"Not found", (char *)"Not found"); |
0 | 135 | return; |
136 | } | |
137 | ||
138 | if (strcmp(".html", &temp_url[strlen(temp_url) - 5]) == 0) { | |
139 | sprintf(c_type, "text/html"); | |
140 | } else if (strcmp(".css", &temp_url[strlen(temp_url) - 4]) == 0) { | |
141 | sprintf(c_type, "text/css"); | |
64
326c38d3681b
Added more plain logfile entries. Change some message details. Added anohe function to eliminate duplicated code. Moved the logfile to the web so it can be downloaded or viewed.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
142 | } else if (strcmp(".log", &temp_url[strlen(temp_url) - 4]) == 0) { |
326c38d3681b
Added more plain logfile entries. Change some message details. Added anohe function to eliminate duplicated code. Moved the logfile to the web so it can be downloaded or viewed.
Michiel Broek <mbroek@mbse.eu>
parents:
61
diff
changeset
|
143 | sprintf(c_type, "text/plain"); |
0 | 144 | } else if (strcmp(".js", &temp_url[strlen(temp_url) - 3]) == 0) { |
145 | sprintf(c_type, "text/javascript"); | |
146 | } else if (strcmp(".json", &temp_url[strlen(temp_url) - 5]) == 0) { | |
147 | sprintf(c_type, "text/json"); | |
148 | } else if (strcmp(".gz", &temp_url[strlen(temp_url) - 3]) == 0) { | |
149 | sprintf(c_type, "application/x-gzip"); | |
150 | } else if (strcmp(".png", &temp_url[strlen(temp_url) - 4]) == 0) { | |
151 | sprintf(c_type, "image/png"); | |
152 | } else if (strcmp(".svg", &temp_url[strlen(temp_url) - 4]) == 0) { | |
153 | sprintf(c_type, "image/svg+xml"); | |
154 | } else if (strcmp(".oga", &temp_url[strlen(temp_url) - 4]) == 0) { | |
155 | sprintf(c_type, "audio/ogg"); | |
156 | } else if (strcmp(".ico", &temp_url[strlen(temp_url) - 4]) == 0) { | |
157 | sprintf(c_type, "image/x-icon"); | |
158 | } else if (strcmp(".xml", &temp_url[strlen(temp_url) - 4]) == 0) { | |
159 | sprintf(c_type, "text/xml"); | |
160 | } else { | |
161 | sprintf(c_type, "application/octet-stream"); | |
162 | printf("Unknown content type for %s\n", temp_url); | |
163 | } | |
164 | ||
165 | vTaskDelay(2 / portTICK_PERIOD_MS); | |
166 | // httpdHeader(connData, "Cache-Control", "max-age=3600, must-revalidate"); | |
167 | if (filesize) { | |
168 | sprintf(header, "HTTP/1.1 200 OK\r\nLast-Modified: %s\r\nContent-Length: %ld\r\nContent-type: %s\r\n", strftime_buf, filesize, c_type); | |
169 | } else { | |
170 | sprintf(header, "HTTP/1.1 200 OK\r\nContent-type: %s\r\n", c_type); | |
171 | } | |
172 | if (send_gz) { | |
54
7b134c27fadb
Upgraded esp-ide and compilers. Adjusted the sources for the new compiler warnings.
Michiel Broek <mbroek@mbse.eu>
parents:
47
diff
changeset
|
173 | strncat(header, "Content-Encoding: gzip\r\n", 255 - strlen(header)); |
0 | 174 | } |
54
7b134c27fadb
Upgraded esp-ide and compilers. Adjusted the sources for the new compiler warnings.
Michiel Broek <mbroek@mbse.eu>
parents:
47
diff
changeset
|
175 | strncat(header, "\r\n", 255 - strlen(header)); // Add last empty line. |
0 | 176 | err = netconn_write(conn, header, strlen(header), NETCONN_NOCOPY); |
177 | if (err != ERR_OK) { | |
61
c7b8a9931b59
Updated esp-ide. Changed some logging levels. Disabled support for termios.h
Michiel Broek <mbroek@mbse.eu>
parents:
54
diff
changeset
|
178 | ESP_LOGW(TAG, "%s sendfile %s%s err=%d on header write", ipstr, temp_url, (send_gz) ? ".gz":"", err); |
0 | 179 | fclose(f); |
180 | return; | |
181 | } | |
182 | // if (strstr(acceptEncodingBuffer, "gzip") == NULL) | |
183 | // http_error(conn, 501, "Not implemented", "Your browser does not accept gzip-compressed data."); | |
184 | ||
185 | sentsize = 0; | |
186 | uint8_t *buff = malloc(1024); | |
187 | size_t bytes; | |
188 | int pause = 0; | |
189 | ||
190 | for (;;) { | |
191 | bytes = fread(buff, 1, 1024, f); | |
192 | if (bytes == 0) | |
193 | break; | |
194 | ||
195 | err = netconn_write(conn, buff, bytes, NETCONN_NOCOPY); | |
196 | if (err != ERR_OK) { | |
61
c7b8a9931b59
Updated esp-ide. Changed some logging levels. Disabled support for termios.h
Michiel Broek <mbroek@mbse.eu>
parents:
54
diff
changeset
|
197 | ESP_LOGW(TAG, "%s sendfile %s%s err=%d send %u bytes of %ld bytes", ipstr, temp_url, (send_gz) ? ".gz":"", err, sentsize, filesize); |
0 | 198 | break; |
199 | } | |
200 | vTaskDelay(2 / portTICK_PERIOD_MS); | |
201 | sentsize += bytes; | |
202 | pause++; | |
203 | if (pause > 50) { // 50 K | |
204 | pause = 0; | |
205 | vTaskDelay(50 / portTICK_PERIOD_MS); | |
206 | } | |
207 | } | |
208 | fclose(f); | |
209 | free(buff); | |
210 | ||
211 | if (sentsize == filesize) { | |
212 | ESP_LOGI(TAG, "%s sendfile %s%s sent %u bytes, ok (%s)", ipstr, temp_url, (send_gz) ? ".gz":"", sentsize, url); | |
213 | } | |
214 | } | |
215 | ||
216 | ||
217 | ||
218 | /** | |
219 | * @brief Handle web ui websocket events. | |
220 | */ | |
221 | void websock_callback(uint8_t num, WEBSOCKET_TYPE_t type, char* msg, uint64_t len) | |
222 | { | |
41
7639cfa6aec0
Websocket interface is working for the main screen and manual mode.
Michiel Broek <mbroek@mbse.eu>
parents:
38
diff
changeset
|
223 | char jbuf[128]; |
7639cfa6aec0
Websocket interface is working for the main screen and manual mode.
Michiel Broek <mbroek@mbse.eu>
parents:
38
diff
changeset
|
224 | |
0 | 225 | switch(type) { |
226 | case WEBSOCKET_CONNECT: | |
227 | ESP_LOGI(TAG,"Websocket client %i connected!",num); | |
228 | break; | |
229 | ||
230 | case WEBSOCKET_DISCONNECT_EXTERNAL: | |
231 | ESP_LOGI(TAG,"Websocket client %i sent a disconnect message",num); | |
232 | break; | |
233 | ||
234 | case WEBSOCKET_DISCONNECT_INTERNAL: | |
235 | ESP_LOGI(TAG,"Websocket client %i was disconnected",num); | |
236 | break; | |
237 | ||
238 | case WEBSOCKET_DISCONNECT_ERROR: | |
239 | ESP_LOGI(TAG,"Websocket client %i was disconnected due to an error",num); | |
240 | break; | |
241 | ||
242 | case WEBSOCKET_TEXT: | |
41
7639cfa6aec0
Websocket interface is working for the main screen and manual mode.
Michiel Broek <mbroek@mbse.eu>
parents:
38
diff
changeset
|
243 | /* |
7639cfa6aec0
Websocket interface is working for the main screen and manual mode.
Michiel Broek <mbroek@mbse.eu>
parents:
38
diff
changeset
|
244 | * Handle json actions from the web clients, like button presses. |
7639cfa6aec0
Websocket interface is working for the main screen and manual mode.
Michiel Broek <mbroek@mbse.eu>
parents:
38
diff
changeset
|
245 | */ |
7639cfa6aec0
Websocket interface is working for the main screen and manual mode.
Michiel Broek <mbroek@mbse.eu>
parents:
38
diff
changeset
|
246 | if (len < 128) { // Safety, messages are small. |
7639cfa6aec0
Websocket interface is working for the main screen and manual mode.
Michiel Broek <mbroek@mbse.eu>
parents:
38
diff
changeset
|
247 | memcpy(jbuf, msg, len); |
7639cfa6aec0
Websocket interface is working for the main screen and manual mode.
Michiel Broek <mbroek@mbse.eu>
parents:
38
diff
changeset
|
248 | jbuf[len] = '\0'; |
7639cfa6aec0
Websocket interface is working for the main screen and manual mode.
Michiel Broek <mbroek@mbse.eu>
parents:
38
diff
changeset
|
249 | if ((root = cJSON_Parse(jbuf))) { |
7639cfa6aec0
Websocket interface is working for the main screen and manual mode.
Michiel Broek <mbroek@mbse.eu>
parents:
38
diff
changeset
|
250 | if ((touch = cJSON_GetObjectItem(root,"touch"))) { |
7639cfa6aec0
Websocket interface is working for the main screen and manual mode.
Michiel Broek <mbroek@mbse.eu>
parents:
38
diff
changeset
|
251 | int x = cJSON_GetObjectItem(touch, "x")->valueint; |
7639cfa6aec0
Websocket interface is working for the main screen and manual mode.
Michiel Broek <mbroek@mbse.eu>
parents:
38
diff
changeset
|
252 | int y = cJSON_GetObjectItem(touch, "y")->valueint; |
7639cfa6aec0
Websocket interface is working for the main screen and manual mode.
Michiel Broek <mbroek@mbse.eu>
parents:
38
diff
changeset
|
253 | WS_touched(x, y); |
7639cfa6aec0
Websocket interface is working for the main screen and manual mode.
Michiel Broek <mbroek@mbse.eu>
parents:
38
diff
changeset
|
254 | break; |
7639cfa6aec0
Websocket interface is working for the main screen and manual mode.
Michiel Broek <mbroek@mbse.eu>
parents:
38
diff
changeset
|
255 | } else { |
7639cfa6aec0
Websocket interface is working for the main screen and manual mode.
Michiel Broek <mbroek@mbse.eu>
parents:
38
diff
changeset
|
256 | ESP_LOGI(TAG,"not json touch"); |
7639cfa6aec0
Websocket interface is working for the main screen and manual mode.
Michiel Broek <mbroek@mbse.eu>
parents:
38
diff
changeset
|
257 | } |
7639cfa6aec0
Websocket interface is working for the main screen and manual mode.
Michiel Broek <mbroek@mbse.eu>
parents:
38
diff
changeset
|
258 | cJSON_Delete(root); |
7639cfa6aec0
Websocket interface is working for the main screen and manual mode.
Michiel Broek <mbroek@mbse.eu>
parents:
38
diff
changeset
|
259 | } else { |
7639cfa6aec0
Websocket interface is working for the main screen and manual mode.
Michiel Broek <mbroek@mbse.eu>
parents:
38
diff
changeset
|
260 | ESP_LOGI(TAG,"not json"); |
7639cfa6aec0
Websocket interface is working for the main screen and manual mode.
Michiel Broek <mbroek@mbse.eu>
parents:
38
diff
changeset
|
261 | } |
7639cfa6aec0
Websocket interface is working for the main screen and manual mode.
Michiel Broek <mbroek@mbse.eu>
parents:
38
diff
changeset
|
262 | } |
7639cfa6aec0
Websocket interface is working for the main screen and manual mode.
Michiel Broek <mbroek@mbse.eu>
parents:
38
diff
changeset
|
263 | // Log if the message in not processed. |
0 | 264 | ESP_LOGI(TAG,"Websocket client %i sent text message of size %i:\n%s",num,(uint32_t)len,msg); |
265 | break; | |
266 | ||
267 | case WEBSOCKET_BIN: | |
268 | ESP_LOGI(TAG,"Websocket client %i sent bin message of size %i:\n",num,(uint32_t)len); | |
269 | break; | |
270 | ||
271 | case WEBSOCKET_PING: | |
272 | ESP_LOGI(TAG,"client %i pinged us with message of size %i:\n%s",num,(uint32_t)len,msg); | |
273 | break; | |
274 | ||
275 | case WEBSOCKET_PONG: | |
276 | ESP_LOGI(TAG,"client %i responded to the ping",num); | |
277 | break; | |
278 | } | |
279 | } | |
280 | ||
281 | ||
282 | ||
41
7639cfa6aec0
Websocket interface is working for the main screen and manual mode.
Michiel Broek <mbroek@mbse.eu>
parents:
38
diff
changeset
|
283 | #if 0 |
0 | 284 | void dump_buf(char *buf, uint16_t buflen) |
285 | { | |
286 | int i = 0, l = 1; | |
287 | ||
288 | printf("request length %d\n00: ", buflen); | |
289 | for (;;) { | |
290 | if (i >= buflen) | |
291 | break; | |
292 | if ((buf[i] < ' ') || (buf[i] > 126)) { | |
293 | if (buf[i] == '\n') { | |
294 | printf("\\n\n%02d: ", l); | |
295 | l++; | |
296 | } else if (buf[i] == '\r') { | |
297 | printf("\\r"); | |
298 | } else { | |
299 | printf("\\%02x", buf[i]); | |
300 | } | |
301 | } else { | |
302 | printf("%c", buf[i]); | |
303 | } | |
304 | i++; | |
305 | } | |
306 | printf("\n"); | |
307 | } | |
41
7639cfa6aec0
Websocket interface is working for the main screen and manual mode.
Michiel Broek <mbroek@mbse.eu>
parents:
38
diff
changeset
|
308 | #endif |
0 | 309 | |
310 | ||
311 | ||
312 | /** | |
313 | * @brief Serve any client. | |
314 | * @param http_client_sock The socket on which the client is connected. | |
315 | */ | |
316 | static void http_serve(struct netconn *conn) | |
317 | { | |
318 | char ipstr[IPADDR_STRLEN_MAX]; | |
319 | struct netbuf *inbuf; | |
320 | static char *buf; | |
321 | static uint16_t buflen; | |
322 | static err_t err; | |
323 | char url[128], *p, *mod_since = NULL; | |
324 | ip_addr_t remote_addr; | |
325 | uint16_t remote_port; | |
326 | ||
327 | if (netconn_getaddr(conn, &remote_addr, &remote_port, 0) == ERR_OK) { | |
328 | strcpy(ipstr, ip4addr_ntoa(ip_2_ip4(&remote_addr))); | |
329 | } else { | |
330 | ipstr[0] = '\0'; | |
331 | } | |
332 | ||
333 | netconn_set_recvtimeout(conn,1000); // allow a connection timeout of 1 second | |
334 | err = netconn_recv(conn, &inbuf); | |
335 | ||
336 | if (err != ERR_OK) { | |
337 | if (err != ERR_TIMEOUT) { // Ignore timeout | |
61
c7b8a9931b59
Updated esp-ide. Changed some logging levels. Disabled support for termios.h
Michiel Broek <mbroek@mbse.eu>
parents:
54
diff
changeset
|
338 | ESP_LOGW(TAG,"%s error %d on read", ipstr, err); |
0 | 339 | } |
340 | netconn_close(conn); | |
341 | netconn_delete(conn); | |
342 | netbuf_delete(inbuf); | |
343 | return; | |
344 | } | |
345 | ||
346 | netbuf_data(inbuf, (void**)&buf, &buflen); | |
347 | ||
348 | if (buf) { | |
349 | /* | |
350 | * Build url string from the data buffer. | |
351 | * It looks like: GET /app/localization.js HTTP/1.1 | |
352 | */ | |
353 | for (int i = 0; i < 10; i++) { | |
354 | if (buf[i] == ' ') { | |
355 | i++; | |
356 | for (int j = i; j < 128; j++) { | |
357 | url[j-i] = buf[j]; | |
358 | if (url[j-i] == ' ') { | |
359 | url[j-i] = '\0'; | |
360 | break; | |
361 | } | |
362 | } | |
363 | break; | |
364 | } | |
365 | } | |
366 | ||
367 | if (strstr(buf, "GET /logfiles.json")) { | |
368 | char temp[64]; | |
369 | FILE *dest = fopen("/spiffs/w/logfiles.json", "w"); | |
370 | if (dest) { | |
371 | fprintf(dest, "{\"Dir\":[{\"Folder\":\"/log\",\"Files\":["); | |
372 | DIR *dir = opendir("/sdcard/w/log"); | |
373 | if (dir) { | |
374 | struct dirent* de = readdir(dir); | |
375 | struct stat st; | |
376 | bool comma = false; | |
377 | while (de) { | |
54
7b134c27fadb
Upgraded esp-ide and compilers. Adjusted the sources for the new compiler warnings.
Michiel Broek <mbroek@mbse.eu>
parents:
47
diff
changeset
|
378 | snprintf(temp, 63, "/sdcard/w/log/"); |
7b134c27fadb
Upgraded esp-ide and compilers. Adjusted the sources for the new compiler warnings.
Michiel Broek <mbroek@mbse.eu>
parents:
47
diff
changeset
|
379 | strncat(temp, de->d_name, 63 - strlen(temp)); |
0 | 380 | if (stat(temp, &st) == ESP_OK) { |
381 | fprintf(dest, "%s{\"File\":\"%s\",\"Size\":%ld,\"Date\":%ld}", (comma)?",":"", de->d_name, st.st_size, st.st_mtime); | |
382 | comma = true; | |
383 | } | |
384 | de = readdir(dir); | |
385 | vTaskDelay(5 / portTICK_PERIOD_MS); | |
386 | } | |
387 | closedir(dir); | |
47
2aab3b5af4b5
Fixed css path in chart.html. Show errors in console when writing logfiles.json. Upgraded esp-idf.
Michiel Broek <mbroek@mbse.eu>
parents:
41
diff
changeset
|
388 | } else { |
2aab3b5af4b5
Fixed css path in chart.html. Show errors in console when writing logfiles.json. Upgraded esp-idf.
Michiel Broek <mbroek@mbse.eu>
parents:
41
diff
changeset
|
389 | ESP_LOGE(TAG, "Error %d open directory /sdcard/w/log", errno); |
2aab3b5af4b5
Fixed css path in chart.html. Show errors in console when writing logfiles.json. Upgraded esp-idf.
Michiel Broek <mbroek@mbse.eu>
parents:
41
diff
changeset
|
390 | } |
0 | 391 | fprintf(dest, "]}]}"); |
392 | fclose(dest); | |
47
2aab3b5af4b5
Fixed css path in chart.html. Show errors in console when writing logfiles.json. Upgraded esp-idf.
Michiel Broek <mbroek@mbse.eu>
parents:
41
diff
changeset
|
393 | } else { |
2aab3b5af4b5
Fixed css path in chart.html. Show errors in console when writing logfiles.json. Upgraded esp-idf.
Michiel Broek <mbroek@mbse.eu>
parents:
41
diff
changeset
|
394 | ESP_LOGE(TAG, "Error %d write /spiffs/w/logfiles.json", errno); |
0 | 395 | } |
54
7b134c27fadb
Upgraded esp-ide and compilers. Adjusted the sources for the new compiler warnings.
Michiel Broek <mbroek@mbse.eu>
parents:
47
diff
changeset
|
396 | http_sendfile(conn, (char *)"/logfiles.json", NULL, ipstr); |
0 | 397 | netconn_close(conn); |
398 | netconn_delete(conn); | |
399 | netbuf_delete(inbuf); | |
400 | unlink("/spiffs/w/logfiles.json"); | |
401 | return; | |
402 | } | |
403 | ||
404 | // http requests | |
405 | if (! strstr(buf,"Upgrade: websocket")) { // Not websocket | |
406 | p = strstr(buf, "If-Modified-Since:"); // May be cached | |
407 | if (p) { | |
408 | size_t mod_len = strcspn(p, " "); | |
409 | p += (int)(mod_len + 1); | |
410 | mod_len = strcspn(p, "\r\n"); | |
411 | mod_since = malloc(mod_len + 2); | |
412 | memcpy(mod_since, p, mod_len); | |
413 | mod_since[mod_len] = '\0'; | |
414 | } | |
415 | http_sendfile(conn, url, mod_since, ipstr); | |
416 | if (mod_since) | |
417 | free(mod_since); | |
418 | mod_since = NULL; | |
419 | netconn_close(conn); | |
420 | netconn_delete(conn); | |
421 | netbuf_delete(inbuf); | |
422 | return; | |
423 | } | |
424 | ||
425 | // websocket for web UI. | |
426 | if ((strstr(buf,"GET /ws ") && strstr(buf,"Upgrade: websocket"))) { | |
54
7b134c27fadb
Upgraded esp-ide and compilers. Adjusted the sources for the new compiler warnings.
Michiel Broek <mbroek@mbse.eu>
parents:
47
diff
changeset
|
427 | int nr = ws_server_add_client_protocol(conn, buf, buflen, (char *)"/ws", (char *)"binary", websock_callback); |
0 | 428 | ESP_LOGI(TAG, "%s new websocket on /ws client: %d", ipstr, nr); |
429 | netbuf_delete(inbuf); | |
430 | TFTstartWS(nr); | |
431 | // Startup something? Init webscreen? | |
432 | return; | |
433 | } | |
434 | ||
41
7639cfa6aec0
Websocket interface is working for the main screen and manual mode.
Michiel Broek <mbroek@mbse.eu>
parents:
38
diff
changeset
|
435 | #if 0 |
0 | 436 | dump_buf(buf, buflen); |
41
7639cfa6aec0
Websocket interface is working for the main screen and manual mode.
Michiel Broek <mbroek@mbse.eu>
parents:
38
diff
changeset
|
437 | #endif |
0 | 438 | |
439 | if (strstr(buf, "GET /")) { | |
440 | ESP_LOGI(TAG, "%s request: %s", ipstr, buf); | |
54
7b134c27fadb
Upgraded esp-ide and compilers. Adjusted the sources for the new compiler warnings.
Michiel Broek <mbroek@mbse.eu>
parents:
47
diff
changeset
|
441 | http_error(conn, 404, (char *)"Not found", (char *)"Not found"); |
0 | 442 | } else { |
54
7b134c27fadb
Upgraded esp-ide and compilers. Adjusted the sources for the new compiler warnings.
Michiel Broek <mbroek@mbse.eu>
parents:
47
diff
changeset
|
443 | http_error(conn, 405, (char *)"Invalid method", (char *)"Invalid method"); |
0 | 444 | } |
445 | } | |
446 | ||
447 | netconn_close(conn); | |
448 | netconn_delete(conn); | |
449 | netbuf_delete(inbuf); | |
450 | } | |
451 | ||
452 | ||
453 | ||
454 | /** | |
455 | * @brief Handles clients when they first connect. passes to a queue | |
456 | */ | |
457 | static void task_HTTPserver(void* pvParameters) | |
458 | { | |
459 | struct netconn *conn, *newconn; | |
460 | static err_t err; | |
461 | ||
462 | ESP_LOGI(TAG, "Starting http server_task"); | |
463 | client_queue = xQueueCreate(client_queue_size,sizeof(struct netconn*)); | |
464 | ||
465 | conn = netconn_new(NETCONN_TCP); | |
466 | netconn_bind(conn,NULL,80); | |
467 | netconn_listen(conn); | |
468 | ||
469 | do { | |
470 | err = netconn_accept(conn, &newconn); | |
471 | if (err == ERR_OK) { | |
472 | if (xQueueSendToBack(client_queue,&newconn,portMAX_DELAY) != pdTRUE) { | |
61
c7b8a9931b59
Updated esp-ide. Changed some logging levels. Disabled support for termios.h
Michiel Broek <mbroek@mbse.eu>
parents:
54
diff
changeset
|
473 | ESP_LOGW(TAG, "xQueueSendToBack() queue full"); |
0 | 474 | }; |
475 | } | |
476 | vTaskDelay(5 / portTICK_PERIOD_MS); | |
477 | } while (err == ERR_OK); | |
478 | ||
479 | ESP_LOGE(TAG, "Stopping http server_task"); | |
480 | netconn_close(conn); | |
481 | netconn_delete(conn); | |
482 | vTaskDelete(NULL); | |
483 | } | |
484 | ||
485 | ||
486 | ||
487 | /** | |
488 | * @brief Receives clients from queue and handle them. | |
489 | */ | |
490 | static void task_Queue(void* pvParameters) | |
491 | { | |
492 | struct netconn* conn; | |
493 | ||
494 | ESP_LOGI(TAG, "Starting Queue task"); | |
495 | for(;;) { | |
496 | xQueueReceive(client_queue, &conn, portMAX_DELAY); | |
497 | if (!conn) | |
498 | continue; | |
499 | http_serve(conn); | |
500 | vTaskDelay(2 / portTICK_PERIOD_MS); | |
501 | } | |
502 | ESP_LOGE(TAG, "Stopping Queue task"); | |
503 | vTaskDelete(NULL); | |
504 | } | |
505 | ||
506 | ||
507 | ||
508 | void start_http_websocket(void) | |
509 | { | |
510 | ESP_LOGI(TAG, "Starting HTTP/Websocket server"); | |
511 | ||
512 | ws_server_start(); | |
513 | xTaskCreate(&task_HTTPserver, "HTTPserver", 3000, NULL, 9, &xTaskHTTP); | |
514 | xTaskCreate(&task_Queue, "Queue", 4000, NULL, 6, &xTaskQueue); | |
515 | } | |
516 |