branch: externals/plz
commit 9fb22d84d9e39692392276d7b7b9979ca6ca1c01
Merge: 8ddabbb5a9 60c8e3fe16
Author: Adam Porter <a...@alphapapa.net>
Commit: Adam Porter <a...@alphapapa.net>

    Merge: Consolidate curl argument logic
    
    Also adds Joseph's test for "'head :as 'file".
---
 plz.el            | 88 ++++++++++++++++++-------------------------------------
 tests/test-plz.el | 10 +++++++
 2 files changed, 38 insertions(+), 60 deletions(-)

diff --git a/plz.el b/plz.el
index aff43bdd64..903d71e6b9 100644
--- a/plz.el
+++ b/plz.el
@@ -432,74 +432,42 @@ into the process buffer.
                                            collect (cons "--header" (format 
"%s: %s" key value))))
          (curl-config-args (append curl-config-header-args
                                    (list (cons "--url" url)
-                                         (cons "--create-dirs" ""))
+                                         (cons "--create-dirs" "")
+                                         (cons "--request" (upcase 
(symbol-name method)))
+                                         (cons "--dump-header" "-"))
                                    (when connect-timeout
                                      (list (cons "--connect-timeout"
                                                  (number-to-string 
connect-timeout))))
                                    (when timeout
                                      (list (cons "--max-time" 
(number-to-string timeout))))
-                                   ;; NOTE: To make a HEAD request
-                                   ;; requires using the "--head"
-                                   ;; option rather than "--request
-                                   ;; HEAD", and doing so with
-                                   ;; "--dump-header" duplicates the
-                                   ;; headers, so we must instead
-                                   ;; specify that for each other
-                                   ;; method.
+                                   (pcase as
+                                     ('file
+                                      (setf filename (make-temp-file "plz-"))
+                                      (list (cons "--output" filename)))
+                                     (`(file ,(and (pred stringp) as-filename))
+                                      (when (file-exists-p as-filename)
+                                        (error "File exists, will not 
overwrite: %S" as-filename))
+                                      ;; Use `expand-file-name' because curl 
doesn't
+                                      ;; expand, e.g. "~" into "/home/...".
+                                      (setf filename (expand-file-name 
as-filename))
+                                      (list (cons "--output" filename)))
+                                     ((guard (eq 'head method))
+                                      ;; Don't duplicate headers for HEAD
+                                      ;; requests which output to the terminal.
+                                      (list (cons "--dump-header" 
null-device))))
                                    (pcase method
-                                     ('get
-                                      (append (list (cons "--dump-header" "-"))
-                                              (pcase as
-                                                ('file
-                                                 (setf filename 
(make-temp-file "plz-"))
-                                                 (list (cons "--output" 
filename)))
-                                                (`(file ,(and (pred stringp) 
as-filename))
-                                                 (when (file-exists-p 
as-filename)
-                                                   (error "File exists, will 
not overwrite: %S" as-filename))
-                                                 ;; Use `expand-file-name' 
because curl doesn't
-                                                 ;; expand, e.g. "~" into 
"/home/...".
-                                                 (setf filename 
(expand-file-name as-filename))
-                                                 (list (cons "--output" 
filename))))))
                                      ((or 'put 'post)
-                                      (append (list (cons "--dump-header" "-")
-                                                    (cons "--request" (upcase 
(symbol-name method))))
-                                              (pcase as
-                                                ('file
-                                                 (setf filename 
(make-temp-file "plz-"))
-                                                 (list (cons "--output" 
filename)))
-                                                (`(file ,(and (pred stringp) 
as-filename))
-                                                 (when (file-exists-p 
as-filename)
-                                                   (error "File exists, will 
not overwrite: %S" as-filename))
-                                                 ;; Use `expand-file-name' 
because curl doesn't
-                                                 ;; expand, e.g. "~" into 
"/home/...".
-                                                 (setf filename 
(expand-file-name as-filename))
-                                                 (list (cons "--output" 
filename))))
-                                              (list
-                                               ;; It appears that this must be 
the last argument
-                                               ;; in order to pass data on the 
rest of STDIN.
-                                               (pcase body
-                                                 (`(file ,filename)
-                                                  ;; Use `expand-file-name' 
because curl doesn't
-                                                  ;; expand, e.g. "~" into 
"/home/...".
-                                                  (cons "--upload-file" 
(expand-file-name filename)))
-                                                 (_ (cons data-arg "@-"))))))
-                                     ('delete
-                                      (append (list (cons "--dump-header" "-")
-                                                    (cons "--request" (upcase 
(symbol-name method))))
-                                              (pcase as
-                                                ('file
-                                                 (setf filename 
(make-temp-file "plz-"))
-                                                 (list (cons "--output" 
filename)))
-                                                (`(file ,(and (pred stringp) 
as-filename))
-                                                 (when (file-exists-p 
as-filename)
-                                                   (error "File exists, will 
not overwrite: %S" as-filename))
-                                                 ;; Use `expand-file-name' 
because curl doesn't
-                                                 ;; expand, e.g. "~" into 
"/home/...".
-                                                 (setf filename 
(expand-file-name as-filename))
-                                                 (list (cons "--output" 
filename))))))
+                                      (list
+                                       ;; It appears that this must be the 
last argument
+                                       ;; in order to pass data on the rest of 
STDIN.
+                                       (pcase body
+                                         (`(file ,filename)
+                                          ;; Use `expand-file-name' because 
curl doesn't
+                                          ;; expand, e.g. "~" into "/home/...".
+                                          (cons "--upload-file" 
(expand-file-name filename)))
+                                         (_ (cons data-arg "@-")))))
                                      ('head
-                                      (list (cons "--head" "")
-                                            (cons "--request" "HEAD"))))))
+                                      (list (cons "--head" ""))))))
          (curl-config (cl-loop for (key . value) in curl-config-args
                                concat (format "%s \"%s\"\n" key value)))
          (decode (pcase as
diff --git a/tests/test-plz.el b/tests/test-plz.el
index 8725b9e4dc..415edfd6f5 100644
--- a/tests/test-plz.el
+++ b/tests/test-plz.el
@@ -470,6 +470,16 @@ in URL-encoded form)."
       ;; It's a temp file, so it should always be deleted.
       (delete-file filename))))
 
+(plz-deftest plz-head-temp-file ()
+  (let ((filename (plz 'head (url "/image/jpeg")
+                    :as 'file :then 'sync)))
+    (unwind-protect
+        (with-temp-buffer
+          (insert-file-contents filename)
+          (should (re-search-forward "Content-Type: image/jpeg")))
+      ;; It's a temp file, so it should always be deleted.
+      (delete-file filename))))
+
 (plz-deftest plz-get-named-file ()
   (let ((filename (make-temp-file "plz-")))
     ;; HACK: Delete the temp file and reuse its name, because

Reply via email to