emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Emacs-diffs] emacs-25 90f82ff 03/12: Decode hex-encoded URLs before usi


From: Lars Ingebrigtsen
Subject: [Emacs-diffs] emacs-25 90f82ff 03/12: Decode hex-encoded URLs before using them as file names
Date: Fri, 25 Dec 2015 16:06:00 +0000

branch: emacs-25
commit 90f82ffa5dee8314edd8c73d72ef2f82ee617a11
Author: Lars Ingebrigtsen <address@hidden>
Commit: Lars Ingebrigtsen <address@hidden>

    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)
---
 lisp/net/eww.el |   29 +++++++++++++++++++++++++++--
 1 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/lisp/net/eww.el b/lisp/net/eww.el
index 179010c..90ddd05 100644
--- a/lisp/net/eww.el
+++ b/lisp/net/eww.el
@@ -1400,13 +1400,38 @@ Differences in #targets are ignored."
   (unless (plist-get status :error)
     (let* ((obj (url-generic-parse-url url))
            (path (car (url-path-and-query obj)))
-           (file (eww-make-unique-file-name (file-name-nondirectory path)
-                                           eww-download-directory)))
+           (file (eww-make-unique-file-name
+                  (eww-decode-url-file-name (file-name-nondirectory path))
+                  eww-download-directory)))
       (goto-char (point-min))
       (re-search-forward "\r?\n\r?\n")
       (write-region (point) (point-max) file)
       (message "Saved %s" file))))
 
+(defun eww-decode-url-file-name (string)
+  (let* ((binary (url-unhex-string string))
+         (decoded
+          (decode-coding-string
+           binary
+           ;; Possibly set by `universal-coding-system-argument'.
+           (or coding-system-for-read
+               ;; RFC 3986 says that %AB stuff is utf-8.
+               (if (equal (decode-coding-string binary 'utf-8)
+                          '(unicode))
+                   'utf-8
+                 ;; But perhaps not.
+                 (car (detect-coding-string binary))))))
+         (encodes (find-coding-systems-string decoded)))
+    (if (or (equal encodes '(undecided))
+            (memq (or file-name-coding-system
+                      default-file-name-coding-system)
+                  encodes))
+        decoded
+      ;; If we can't encode the decoded file name (due to language
+      ;; environment settings), then we return the original, hexified
+      ;; string.
+      string)))
+
 (defun eww-make-unique-file-name (file directory)
     (cond
      ((zerop (length file))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]