1
Fork 0
mirror of git://git.sv.gnu.org/emacs.git synced 2026-01-06 03:40:56 -08:00

Backport: * lisp/emacs-lisp/package.el: Fix a decoding issue.

* lisp/url/url-handlers.el (url-insert-file-contents): Move some code to
`url-insert-buffer-contents'.
(url-insert-buffer-contents): New function

(package--with-response-buffer): Use `url-insert-buffer-contents'.
The previous code had some issues with decoding. Refactoring that
function allows us to use the decoding from url-handlers while still
treating both sync and async requests the same.
This commit is contained in:
Artur Malabarba 2015-11-15 21:28:37 +00:00
parent a6843cce90
commit d99ccd6dd1
2 changed files with 32 additions and 26 deletions

View file

@ -1165,16 +1165,16 @@ BODY (does not apply to errors signaled by ERROR-FORM).
(when-let ((er (plist-get status :error))) (when-let ((er (plist-get status :error)))
(error "Error retrieving: %s %S" url er)) (error "Error retrieving: %s %S" url er))
(unless (search-forward-regexp "^\r?\n\r?" nil 'noerror) (unless (search-forward-regexp "^\r?\n\r?" nil 'noerror)
(rest-error 'rest-unintelligible-result)) (error "Error retrieving: %s %S" url "incomprehensible buffer"))
(delete-region (point-min) (point)) (with-temp-buffer
,@body) (url-insert-buffer-contents b url)
(when (buffer-live-p b) (kill-buffer b)
(kill-buffer b))))))) (goto-char (point-min))
,@body)))))))
(if ,async (if ,async
(wrap-errors (url-retrieve url callback nil 'silent)) (wrap-errors (url-retrieve url callback nil 'silent))
(let ((buffer (wrap-errors (url-retrieve-synchronously url 'silent)))) (with-current-buffer (wrap-errors (url-retrieve-synchronously url 'silent))
(with-current-buffer buffer (funcall callback nil))))
(funcall callback nil)))))
(wrap-errors (with-temp-buffer (wrap-errors (with-temp-buffer
(let ((url (expand-file-name ,file ,url-1))) (let ((url (expand-file-name ,file ,url-1)))
(unless (file-name-absolute-p url) (unless (file-name-absolute-p url)

View file

@ -309,6 +309,29 @@ They count bytes from the beginning of the body."
(defvar url-http-codes) (defvar url-http-codes)
(defun url-insert-buffer-contents (buffer url &optional visit beg end replace)
"Insert the contents of BUFFER into current buffer.
This is like `url-insert', but also decodes the current buffer as
if it had been inserted from a file named URL."
(if visit (setq buffer-file-name url))
(save-excursion
(let* ((start (point))
(size-and-charset (url-insert buffer beg end)))
(kill-buffer buffer)
(when replace
(delete-region (point-min) start)
(delete-region (point) (point-max)))
(unless (cadr size-and-charset)
;; If the headers don't specify any particular charset, use the
;; usual heuristic/rules that we apply to files.
(decode-coding-inserted-region (point-min) (point) url
visit beg end replace))
(let ((inserted (car size-and-charset)))
(when (fboundp 'after-insert-file-set-coding)
(let ((insval (after-insert-file-set-coding inserted visit)))
(if insval (setq inserted insval))))
(list url inserted)))))
;;;###autoload ;;;###autoload
(defun url-insert-file-contents (url &optional visit beg end replace) (defun url-insert-file-contents (url &optional visit beg end replace)
(let ((buffer (url-retrieve-synchronously url))) (let ((buffer (url-retrieve-synchronously url)))
@ -323,24 +346,7 @@ They count bytes from the beginning of the body."
(kill-buffer buffer) (kill-buffer buffer)
;; Signal file-error per http://debbugs.gnu.org/16733. ;; Signal file-error per http://debbugs.gnu.org/16733.
(signal 'file-error (list url desc)))))) (signal 'file-error (list url desc))))))
(if visit (setq buffer-file-name url)) (url-insert-buffer-contents buffer url visit beg end replace)))
(save-excursion
(let* ((start (point))
(size-and-charset (url-insert buffer beg end)))
(kill-buffer buffer)
(when replace
(delete-region (point-min) start)
(delete-region (point) (point-max)))
(unless (cadr size-and-charset)
;; If the headers don't specify any particular charset, use the
;; usual heuristic/rules that we apply to files.
(decode-coding-inserted-region start (point) url
visit beg end replace))
(let ((inserted (car size-and-charset)))
(when (fboundp 'after-insert-file-set-coding)
(let ((insval (after-insert-file-set-coding inserted visit)))
(if insval (setq inserted insval))))
(list url inserted))))))
(put 'insert-file-contents 'url-file-handlers 'url-insert-file-contents) (put 'insert-file-contents 'url-file-handlers 'url-insert-file-contents)