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