@@ -659,13 +659,13 @@ static void _reply_with_file(socketpool_socket_obj_t *socket, _request *request,
659
659
mp_printf (& _socket_print , "Content-Length: %d\r\n" , total_length );
660
660
// TODO: Make this a table to save space.
661
661
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 );
663
663
} 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 );
665
665
} 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 );
667
667
} 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 );
669
669
} else {
670
670
_send_str (socket , "Content-Type: application/octet-stream\r\n" );
671
671
}
@@ -966,6 +966,16 @@ static void _reply_websocket_upgrade(socketpool_socket_obj_t *socket, _request *
966
966
// socket is now closed and "disconnected".
967
967
}
968
968
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
+
969
979
static bool _reply (socketpool_socket_obj_t * socket , _request * request ) {
970
980
if (request -> redirect ) {
971
981
_reply_redirect (socket , request , request -> path );
@@ -983,6 +993,26 @@ static bool _reply(socketpool_socket_obj_t *socket, _request *request) {
983
993
_reply_forbidden (socket , request );
984
994
}
985
995
} 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
+ }
986
1016
char * path = request -> path + 3 ;
987
1017
size_t pathlen = strlen (path );
988
1018
FATFS * fs = filesystem_circuitpy ();
0 commit comments