Skip to content

Commit 08e3133

Browse files
authored
Merge pull request #6731 from tannewt/ww_unicode
Decode percent encoded file paths and set charset
2 parents ff04c25 + e0fb308 commit 08e3133

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

supervisor/shared/web_workflow/static/directory.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,10 @@ async function upload(e) {
149149
)
150150
if (response.ok) {
151151
refresh_list();
152-
files.value = "";
153-
upload_button.disabled = true;
154152
}
155153
}
154+
files.value = "";
155+
upload_button.disabled = true;
156156
}
157157

158158
async function del(e) {

supervisor/shared/web_workflow/web_workflow.c

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -659,13 +659,13 @@ static void _reply_with_file(socketpool_socket_obj_t *socket, _request *request,
659659
mp_printf(&_socket_print, "Content-Length: %d\r\n", total_length);
660660
// TODO: Make this a table to save space.
661661
if (_endswith(filename, ".txt") || _endswith(filename, ".py")) {
662-
_send_str(socket, "Content-Type: text/plain\r\n");
662+
_send_strs(socket, "Content-Type: text/plain", ";charset=UTF-8\r\n", NULL);
663663
} else if (_endswith(filename, ".js")) {
664-
_send_str(socket, "Content-Type: text/javascript\r\n");
664+
_send_strs(socket, "Content-Type: text/javascript", ";charset=UTF-8\r\n", NULL);
665665
} else if (_endswith(filename, ".html")) {
666-
_send_str(socket, "Content-Type: text/html\r\n");
666+
_send_strs(socket, "Content-Type: text/html", ";charset=UTF-8\r\n", NULL);
667667
} else if (_endswith(filename, ".json")) {
668-
_send_str(socket, "Content-Type: application/json\r\n");
668+
_send_strs(socket, "Content-Type: application/json", ";charset=UTF-8\r\n", NULL);
669669
} else {
670670
_send_str(socket, "Content-Type: application/octet-stream\r\n");
671671
}
@@ -966,6 +966,16 @@ static void _reply_websocket_upgrade(socketpool_socket_obj_t *socket, _request *
966966
// socket is now closed and "disconnected".
967967
}
968968

969+
static uint8_t _hex2nibble(char h) {
970+
if ('0' <= h && h <= '9') {
971+
return h - '0';
972+
} else if ('A' <= h && h <= 'F') {
973+
return h - 'A' + 0xa;
974+
}
975+
// Shouldn't usually use lower case.
976+
return h - 'a' + 0xa;
977+
}
978+
969979
static bool _reply(socketpool_socket_obj_t *socket, _request *request) {
970980
if (request->redirect) {
971981
_reply_redirect(socket, request, request->path);
@@ -983,6 +993,26 @@ static bool _reply(socketpool_socket_obj_t *socket, _request *request) {
983993
_reply_forbidden(socket, request);
984994
}
985995
} else {
996+
// Decode any percent encoded bytes so that we're left with UTF-8.
997+
// We only do this on /fs/ paths and after redirect so that any
998+
// path echoing we do stays encoded.
999+
size_t o = 0;
1000+
size_t i = 0;
1001+
while (i < strlen(request->path)) {
1002+
if (request->path[i] == '%') {
1003+
request->path[o] = _hex2nibble(request->path[i + 1]) << 4 | _hex2nibble(request->path[i + 2]);
1004+
i += 3;
1005+
} else {
1006+
if (i != o) {
1007+
request->path[o] = request->path[i];
1008+
}
1009+
i += 1;
1010+
}
1011+
o += 1;
1012+
}
1013+
if (o < i) {
1014+
request->path[o] = '\0';
1015+
}
9861016
char *path = request->path + 3;
9871017
size_t pathlen = strlen(path);
9881018
FATFS *fs = filesystem_circuitpy();

0 commit comments

Comments
 (0)