Skip to content

Commit

Permalink
Tidy: (plz) Consolidate duplicate (pcase as ...) forms
Browse files Browse the repository at this point in the history
This commit avoids duplicate headers by passing --dump-header with "-"
as its value in all requests and then passing it again with the
overriding "/dev/null" value forn HEAD requests which output to the
terminal.  For HEAD requests which output to a file with --output,
--dump-header "-" is necessary since plz--http-status expects the
process buffer to always contain the headers.
  • Loading branch information
josephmturner committed Aug 16, 2024
1 parent a91994a commit 570eae7
Showing 1 changed file with 26 additions and 56 deletions.
82 changes: 26 additions & 56 deletions plz.el
Original file line number Diff line number Diff line change
Expand Up @@ -433,69 +433,39 @@ into the process buffer.
(curl-config-args (append curl-config-header-args
(list (cons "--url" url)
(cons "--create-dirs" "")
(cons "--request" (upcase (symbol-name method))))
(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" "-"))
(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" "-"))
(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" ""))))))
(curl-config (cl-loop for (key . value) in curl-config-args
Expand Down

0 comments on commit 570eae7

Please sign in to comment.