aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLars Ingebrigtsen2015-12-24 22:21:24 +0100
committerLars Ingebrigtsen2015-12-25 17:02:31 +0100
commit90f82ffa5dee8314edd8c73d72ef2f82ee617a11 (patch)
tree6f880a92170cfb2e2343345c7229befc5c7ca48e
parent5bd3a0c9e00cde01cf325389458f86fd9f05db3f (diff)
downloademacs-90f82ffa5dee8314edd8c73d72ef2f82ee617a11.tar.gz
emacs-90f82ffa5dee8314edd8c73d72ef2f82ee617a11.zip
Decode hex-encoded URLs before using them as file names
* eww.el (eww-decode-url-file-name): New function. (eww-download-callback): Use it to decode file names before saving them. Backport: (cherry picked from commit af22a010d87516c2a646572fb27512c03057784f)
-rw-r--r--lisp/net/eww.el29
1 files changed, 27 insertions, 2 deletions
diff --git a/lisp/net/eww.el b/lisp/net/eww.el
index 179010cf4cd..90ddd05b845 100644
--- a/lisp/net/eww.el
+++ b/lisp/net/eww.el
@@ -1400,13 +1400,38 @@ Differences in #targets are ignored."
1400 (unless (plist-get status :error) 1400 (unless (plist-get status :error)
1401 (let* ((obj (url-generic-parse-url url)) 1401 (let* ((obj (url-generic-parse-url url))
1402 (path (car (url-path-and-query obj))) 1402 (path (car (url-path-and-query obj)))
1403 (file (eww-make-unique-file-name (file-name-nondirectory path) 1403 (file (eww-make-unique-file-name
1404 eww-download-directory))) 1404 (eww-decode-url-file-name (file-name-nondirectory path))
1405 eww-download-directory)))
1405 (goto-char (point-min)) 1406 (goto-char (point-min))
1406 (re-search-forward "\r?\n\r?\n") 1407 (re-search-forward "\r?\n\r?\n")
1407 (write-region (point) (point-max) file) 1408 (write-region (point) (point-max) file)
1408 (message "Saved %s" file)))) 1409 (message "Saved %s" file))))
1409 1410
1411(defun eww-decode-url-file-name (string)
1412 (let* ((binary (url-unhex-string string))
1413 (decoded
1414 (decode-coding-string
1415 binary
1416 ;; Possibly set by `universal-coding-system-argument'.
1417 (or coding-system-for-read
1418 ;; RFC 3986 says that %AB stuff is utf-8.
1419 (if (equal (decode-coding-string binary 'utf-8)
1420 '(unicode))
1421 'utf-8
1422 ;; But perhaps not.
1423 (car (detect-coding-string binary))))))
1424 (encodes (find-coding-systems-string decoded)))
1425 (if (or (equal encodes '(undecided))
1426 (memq (or file-name-coding-system
1427 default-file-name-coding-system)
1428 encodes))
1429 decoded
1430 ;; If we can't encode the decoded file name (due to language
1431 ;; environment settings), then we return the original, hexified
1432 ;; string.
1433 string)))
1434
1410(defun eww-make-unique-file-name (file directory) 1435(defun eww-make-unique-file-name (file directory)
1411 (cond 1436 (cond
1412 ((zerop (length file)) 1437 ((zerop (length file))