branch: externals/plz
commit a0a6d623352aa1caee722c16649190611a253cbc
Author: Adam Porter <a...@alphapapa.net>
Commit: Adam Porter <a...@alphapapa.net>

    Fix: (plz--sentinel) Skip HTTP redirect headers
    
    When using ":as 'response", we call curl with "--dump-header -" so we
    can parse the headers of the response, but this causes curl to dump
    the headers of any intermediate HTTP redirect responses as well, so
    now we skip those headers.
---
 README.org        |  1 +
 plz.el            | 10 ++++++++++
 plz.info          | 29 +++++++++++++++--------------
 tests/test-plz.el | 22 ++++++++++++++++++++++
 4 files changed, 48 insertions(+), 14 deletions(-)

diff --git a/README.org b/README.org
index 2a486f0806..01f2faa1e6 100644
--- a/README.org
+++ b/README.org
@@ -184,6 +184,7 @@ You may also clear a queue with ~plz-clear~, which cancels 
any active or queued
 
 *Fixes*
 + Function ~plz~'s docstring now mentions that the ~:body~ argument may also 
be a buffer (an intentional feature that was accidentally undocumented).
++ Handle HTTP 301 redirects when using ~:as 'response~.
 
 ** 0.5.4
 
diff --git a/plz.el b/plz.el
index c66f486e89..f746d6a45e 100644
--- a/plz.el
+++ b/plz.el
@@ -686,6 +686,7 @@ node `(elisp) Sentinels').  Kills the buffer before 
returning."
              ;; Curl exited normally: check HTTP status code.
              (goto-char (point-min))
              (plz--skip-proxy-headers)
+             (while (plz--skip-redirect-headers))
              (pcase (plz--http-status)
                ((and status (guard (<= 200 status 299)))
                 ;; Any 2xx response is considered successful.
@@ -741,6 +742,15 @@ node `(elisp) Sentinels').  Kills the buffer before 
returning."
         (unless (re-search-forward "\r\n\r\n" nil t)
           (signal 'plz-http-error '("plz--response: End of proxy headers not 
found")))))))
 
+(defun plz--skip-redirect-headers ()
+  "Skip HTTP redirect headers in current buffer."
+  (when (and (looking-at plz-http-response-status-line-regexp)
+             (member (string-to-number (match-string 2)) '(301 302 307 308)))
+    ;; Skip redirect headers ("--dump-header" forces redirect headers to be 
included
+    ;; even when used with "--location").
+    (unless (re-search-forward "\r\n\r\n" nil t)
+      (signal 'plz-http-error '("plz--response: End of redirect headers not 
found")))))
+
 (cl-defun plz--response (&key (decode-p t))
   "Return response structure for HTTP response in current buffer.
 When DECODE-P is non-nil, decode the response body automatically
diff --git a/plz.info b/plz.info
index 357e1c98a9..188f54f350 100644
--- a/plz.info
+++ b/plz.info
@@ -307,6 +307,7 @@ File: README.info,  Node: 06-pre,  Next: 054,  Up: Changelog
    • Function ‘plz’’s docstring now mentions that the ‘:body’ argument
      may also be a buffer (an intentional feature that was accidentally
      undocumented).
+   • Handle HTTP 301 redirects when using ‘:as 'response’.
 
 
 File: README.info,  Node: 054,  Next: 053,  Prev: 06-pre,  Up: Changelog
@@ -493,20 +494,20 @@ Node: Queueing6848
 Node: Tips8106
 Node: Changelog8407
 Node: 06-pre8665
-Node: 0549212
-Node: 0539459
-Node: 0529775
-Node: 0519982
-Node: 0510234
-Node: 0410440
-Node: 0311346
-Node: 02111794
-Node: 0211943
-Node: 0112074
-Node: Credits12170
-Node: Development12536
-Node: Copyright assignment13050
-Node: License13638
+Node: 0549277
+Node: 0539524
+Node: 0529840
+Node: 05110047
+Node: 0510299
+Node: 0410505
+Node: 0311411
+Node: 02111859
+Node: 0212008
+Node: 0112139
+Node: Credits12235
+Node: Development12601
+Node: Copyright assignment13115
+Node: License13703
 
 End Tag Table
 
diff --git a/tests/test-plz.el b/tests/test-plz.el
index f78f6e4417..5c3facc9ed 100644
--- a/tests/test-plz.el
+++ b/tests/test-plz.el
@@ -352,6 +352,28 @@
 (plz-deftest plz-500-errors ()
   (should-error (plz 'get "https://httpbin.org/status/500";)))
 
+;;;;; Redirects
+
+(plz-deftest plz-301-redirects ()
+  (plz-test-get-response
+   (plz 'get 
"https://httpbin.org/redirect-to?url=https%3A%2F%2Fhttpbin.org/get&status_code=301";
+     :as 'response :then 'sync)))
+
+(plz-deftest plz-302-redirects ()
+  (plz-test-get-response
+   (plz 'get 
"https://httpbin.org/redirect-to?url=https%3A%2F%2Fhttpbin.org/get&status_code=302";
+     :as 'response :then 'sync)))
+
+(plz-deftest plz-307-redirects ()
+  (plz-test-get-response
+   (plz 'get 
"https://httpbin.org/redirect-to?url=https%3A%2F%2Fhttpbin.org/get&status_code=307";
+     :as 'response :then 'sync)))
+
+(plz-deftest plz-308-redirects ()
+  (plz-test-get-response
+   (plz 'get 
"https://httpbin.org/redirect-to?url=https%3A%2F%2Fhttpbin.org/get&status_code=308";
+     :as 'response :then 'sync)))
+
 ;;;;; Errors
 
 (plz-deftest plz-get-curl-error nil

Reply via email to