Subject: Re: bug#16733: messed up unicode chars in package description

From: Juanma Barranquero <[email-masked]>
Date: Sat, 22 Mar 2014 02:33:21 +0100

This is the same patch, but adding additional parameters to
package-handle-response to pass the buffer to check.

=== modified file 'lisp/emacs-lisp/package.el'
--- lisp/emacs-lisp/package.el  2014-03-21 06:06:52 +0000
+++ lisp/emacs-lisp/package.el  2014-03-22 01:24:09 +0000
@@ -770,38 +770,35 @@
 and evaluates BODY while that buffer is current.  This work
 buffer is killed afterwards.  Return the last value in BODY."
   (declare (indent 2) (debug t))
-  `(let* ((http (string-match "\\`https?:" ,location))
-         (buffer
-          (if http
-              (url-retrieve-synchronously (concat ,location ,file))
-            (generate-new-buffer "*package work buffer*"))))
-     (prog1
-        (with-current-buffer buffer
-          (if http
-              (progn (package-handle-response)
-                     (re-search-forward "^$" nil 'move)
-                     (forward-char)
-                     (delete-region (point-min) (point)))
-            (unless (file-name-absolute-p ,location)
-              (error "Archive location %s is not an absolute file name"
-                     ,location))
-            (insert-file-contents (expand-file-name ,file ,location)))
-          ,@body)
-       (kill-buffer buffer))))
+  `(with-temp-buffer
+     (if (string-match-p "\\`https?:" ,location)
+        (progn
+          (require 'url-handlers)
+          (url-insert-file-contents-internal #'package-handle-response
+                                             (concat ,location ,file))
+          (goto-char (point-min)))
+       (unless (file-name-absolute-p ,location)
+        (error "Archive location %s is not an absolute file name"
+               ,location))
+       (insert-file-contents (expand-file-name ,file ,location)))
+     ,@body))

-(defun package-handle-response ()
+(defun package-handle-response (&optional buffer &rest _ignore)
   "Handle the response from a `url-retrieve-synchronously' call.
 Parse the HTTP response and throw if an error occurred.
+Parsing happens in BUFFER, or the current buffer if nil.
 The url package seems to require extra processing for this.
 This should be called in a `save-excursion', in the download buffer.
 It will move point to somewhere in the headers."
   ;; We assume HTTP here.
   (require 'url-http)
-  (let ((response (url-http-parse-response)))
-    (when (or (< response 200) (>= response 300))
-      (error "Error downloading %s:%s"
-            (url-recreate-url url-http-target-url)
-            (buffer-substring-no-properties (point) (line-end-position))))))
+  (with-current-buffer (or buffer (current-buffer))
+    (let ((response (url-http-parse-response)))
+      (when (or (< response 200) (>= response 300))
+       (error "Error downloading %s:%s"
+              (url-recreate-url url-http-target-url)
+              (buffer-substring-no-properties (point)
+                                              (line-end-position)))))))

 (defun package--archive-file-exists-p (location file)
   (let ((http (string-match "\\`https?:" location)))
@@ -1272,7 +1269,7 @@
                     (car archive)))))
       ;; Read the retrieved buffer to make sure it is valid (e.g. it
       ;; may fetch a URL redirect page).
-      (when (listp (read buffer))
+      (when (listp (read (current-buffer)))
        (make-directory dir t)
        (setq buffer-file-name (expand-file-name file dir))
        (let ((version-control 'never)
@@ -1531,8 +1528,7 @@
                        (setq readme-string (buffer-string))
                        t))
                 (error nil))
-              (let ((coding (detect-coding-string readme-string t)))
-                (insert (decode-coding-string readme-string coding t))))
+              (insert readme-string))
              ((file-readable-p readme)
               (insert-file-contents readme)
               (goto-char (point-max))))))))

=== modified file 'lisp/url/url-handlers.el'
--- lisp/url/url-handlers.el    2014-01-01 07:43:34 +0000
+++ lisp/url/url-handlers.el    2014-03-22 01:16:06 +0000
@@ -290,11 +290,14 @@
       (insert data))
     (list (length data) charset)))

-;;;###autoload
-(defun url-insert-file-contents (url &optional visit beg end replace)
+(defun url-insert-file-contents-internal (check url &optional visit
beg end replace)
   (let ((buffer (url-retrieve-synchronously url)))
-    (if (not buffer)
-       (error "Opening input file: No such file or directory, %s" url))
+    (when check
+      (condition-case err
+         (funcall check buffer url visit beg end replace)
+       (error
+        (when buffer (kill-buffer))
+        (signal (car err) (cdr err)))))
     (if visit (setq buffer-file-name url))
     (save-excursion
       (let* ((start (point))
@@ -308,6 +311,14 @@
           ;; usual heuristic/rules that we apply to files.
           (decode-coding-inserted-region start (point) url visit beg
end replace))
         (list url (car size-and-charset))))))
+
+;;;###autoload
+(defun url-insert-file-contents (url &optional visit beg end replace)
+  (url-insert-file-contents-internal
+   (lambda (buffer url &rest _ignore)
+     (unless buffer
+       (error "Opening input file: No such file or directory, %s" url)))
+   url visit beg end replace))
 (put 'insert-file-contents 'url-file-handlers 'url-insert-file-contents)

 (defun url-file-name-completion (url directory &optional predicate)