webmcp
annotate framework/env/encode/file_path_element.lua @ 566:3b71fdb3a00d
Updated year + version in documentation
| author | jbe | 
|---|---|
| date | Wed Apr 28 12:54:38 2021 +0200 (2021-04-28) | 
| parents | 9fdfb27f8e67 | 
| children | 
| rev | line source | 
|---|---|
| jbe/bsw@0 | 1 --[[-- | 
| jbe/bsw@0 | 2 encoded_path_element = -- string which can't contain evil stuff like "/" | 
| jbe/bsw@0 | 3 encode.file_path_element( | 
| jbe/bsw@0 | 4 path_element -- string to be encoded | 
| jbe/bsw@0 | 5 ) | 
| jbe/bsw@0 | 6 | 
| jbe/bsw@0 | 7 This function is encoding a string in a way that it can be used as a file or directory name, without security risks. See the source for details. | 
| jbe/bsw@0 | 8 | 
| jbe/bsw@0 | 9 --]]-- | 
| jbe/bsw@0 | 10 | 
| jbe/bsw@0 | 11 function encode.file_path_element(path_element) | 
| jbe/bsw@0 | 12 return ( | 
| jbe/bsw@0 | 13 string.gsub( | 
| jbe/bsw@0 | 14 string.gsub( | 
| jbe/bsw@0 | 15 path_element, "[^0-9A-Za-z_%.-]", | 
| jbe/bsw@0 | 16 function(char) | 
| jbe/bsw@0 | 17 return string.format("%%%02x", string.byte(char)) | 
| jbe/bsw@0 | 18 end | 
| jbe/bsw@0 | 19 ), "^%.", string.format("%%%%%02x", string.byte(".")) | 
| jbe/bsw@0 | 20 ) | 
| jbe/bsw@0 | 21 ) | 
| jbe/bsw@0 | 22 end |