(require 'cl-lib)
(require 'rx)
(require 'subr-x)
(define-error 'plz-error "plz error")
(define-error 'plz-curl-error "plz: Curl error" 'plz-error)
(define-error 'plz-http-error "plz: HTTP error" 'plz-error)
(cl-defstruct plz-response
version status headers body)
(cl-defstruct plz-error
curl-error response message)
(defconst plz-http-response-status-line-regexp
(rx "HTTP/" (group (or "1.0" "1.1" "2")) " "
(group (1+ digit)) " "
(optional (group (1+ (not (any "\r\n")))))
(or
"\r\n"
"\n"))
"Regular expression matching HTTP response status line.")
(defconst plz-http-end-of-headers-regexp
(rx (or "\r\n\r\n" "\n\n"))
"Regular expression matching the end of HTTP headers.
This must work with both HTTP/1 (using CRLF) and HTTP/2 (using
only LF).")
(defconst plz-curl-errors
'((1 . "Unsupported protocol.")
(2 . "Failed to initialize.")
(3 . "URL malformed. The syntax was not correct.")
(4 . "A feature or option that was needed to perform the desired request was not enabled or was explicitly disabled at build-time.")
(5 . "Couldn't resolve proxy. The given proxy host could not be resolved.")
(6 . "Couldn't resolve host. The given remote host was not resolved.")
(7 . "Failed to connect to host.")
(8 . "FTP weird server reply. The server sent data curl couldn't parse.")
(9 . "FTP access denied.")
(11 . "FTP weird PASS reply.")
(13 . "FTP weird PASV reply.")
(14 . "FTP weird 227 format.")
(15 . "FTP can't get host.")
(17 . "FTP couldn't set binary.")
(18 . "Partial file. Only a part of the file was transferred.")
(19 . "FTP couldn't download/access the given file, the RETR (or similar) command failed.")
(21 . "FTP quote error. A quote command returned error from the server.")
(22 . "HTTP page not retrieved.")
(23 . "Write error.")
(25 . "FTP couldn't STOR file.")
(26 . "Read error. Various reading problems.")
(27 . "Out of memory. A memory allocation request failed.")
(28 . "Operation timeout.")
(30 . "FTP PORT failed.")
(31 . "FTP couldn't use REST.")
(33 . "HTTP range error. The range \"command\" didn't work.")
(34 . "HTTP post error. Internal post-request generation error.")
(35 . "SSL connect error. The SSL handshaking failed.")
(36 . "FTP bad download resume.")
(37 . "FILE couldn't read file.")
(38 . "LDAP bind operation failed.")
(39 . "LDAP search failed.")
(41 . "Function not found. A required LDAP function was not found.")
(42 . "Aborted by callback.")
(43 . "Internal error. A function was called with a bad parameter.")
(45 . "Interface error. A specified outgoing interface could not be used.")
(47 . "Too many redirects.")
(48 . "Unknown option specified to libcurl.")
(49 . "Malformed telnet option.")
(51 . "The peer's SSL certificate or SSH MD5 fingerprint was not OK.")
(52 . "The server didn't reply anything, which here is considered an error.")
(53 . "SSL crypto engine not found.")
(54 . "Cannot set SSL crypto engine as default.")
(55 . "Failed sending network data.")
(56 . "Failure in receiving network data.")
(58 . "Problem with the local certificate.")
(59 . "Couldn't use specified SSL cipher.")
(60 . "Peer certificate cannot be authenticated with known CA certificates.")
(61 . "Unrecognized transfer encoding.")
(62 . "Invalid LDAP URL.")
(63 . "Maximum file size exceeded.")
(64 . "Requested FTP SSL level failed.")
(65 . "Sending the data requires a rewind that failed.")
(66 . "Failed to initialise SSL Engine.")
(67 . "The user name, password, or similar was not accepted and curl failed to log in.")
(68 . "File not found on TFTP server.")
(69 . "Permission problem on TFTP server.")
(70 . "Out of disk space on TFTP server.")
(71 . "Illegal TFTP operation.")
(72 . "Unknown TFTP transfer ID.")
(73 . "File already exists (TFTP).")
(74 . "No such user (TFTP).")
(75 . "Character conversion failed.")
(76 . "Character conversion functions required.")
(77 . "Problem with reading the SSL CA cert (path? access rights?).")
(78 . "The resource referenced in the URL does not exist.")
(79 . "An unspecified error occurred during the SSH session.")
(80 . "Failed to shut down the SSL connection.")
(82 . "Could not load CRL file, missing or wrong format (added in 7.19.0).")
(83 . "Issuer check failed (added in 7.19.0).")
(84 . "The FTP PRET command failed")
(85 . "RTSP: mismatch of CSeq numbers")
(86 . "RTSP: mismatch of Session Identifiers")
(87 . "unable to parse FTP file list")
(88 . "FTP chunk callback reported error")
(89 . "No connection available, the session will be queued")
(90 . "SSL public key does not matched pinned public key"))
"Alist mapping curl error code integers to helpful error messages.")
(defgroup plz nil
"Options for `plz'."
:group 'network
:link '(url-link "https://github.com/alphapapa/plz.el"))
(defcustom plz-curl-program "curl"
"Name of curl program to call."
:type 'string)
(defcustom plz-curl-default-args
'("--silent"
"--compressed"
"--location")
"Default arguments to curl.
Note that these arguments are passed on the command line, which
may be visible to other users on the local system."
:type '(repeat string))
(defcustom plz-connect-timeout 5
"Default connection timeout in seconds.
This limits how long the connection phase may last (the
\"--connect-timeout\" argument to curl)."
:type 'number)
(defcustom plz-timeout 60
"Default request timeout in seconds.
This limits how long an entire request may take, including the
connection phase and waiting to receive the response (the
\"--max-time\" argument to curl)."
:type 'number)
(cl-defun plz (method url &rest rest &key headers body else finally noquery
(as 'string) (then 'sync)
(body-type 'text) (decode t decode-s)
(connect-timeout plz-connect-timeout) (timeout plz-timeout))
"Request METHOD from URL with curl.
Return the curl process object or, for a synchronous request, the
selected result.
HEADERS may be an alist of extra headers to send with the
request.
BODY may be a string, a buffer, or a list like `(file FILENAME)'
to upload a file from disk.
BODY-TYPE may be `text' to send BODY as text, or `binary' to send
it as binary.
AS selects the kind of result to pass to the callback function
THEN, or the kind of result to return for synchronous requests.
It may be:
- `buffer' to pass the response buffer, which will be narrowed to
the response body and decoded according to DECODE.
- `binary' to pass the response body as an un-decoded string.
- `string' to pass the response body as a decoded string.
- `response' to pass a `plz-response' structure.
- `file' to pass a temporary filename to which the response body
has been saved without decoding.
- `(file FILENAME)' to pass FILENAME after having saved the
response body to it without decoding. FILENAME must be a
non-existent file; if it exists, it will not be overwritten,
and an error will be signaled.
- A function, which is called in the response buffer with it
narrowed to the response body (suitable for, e.g. `json-read').
If DECODE is non-nil, the response body is decoded automatically.
For binary content, it should be nil. When AS is `binary',
DECODE is automatically set to nil.
THEN is a callback function, whose sole argument is selected
above with AS; if the request fails and no ELSE function is
given (see below), the argument will be a `plz-error' structure
describing the error. Or THEN may be `sync' to make a
synchronous request, in which case the result is returned
directly from this function.
ELSE is an optional callback function called when the request
fails (i.e. if curl fails, or if the HTTP response has a non-2xx
status code). It is called with one argument, a `plz-error'
structure. If ELSE is nil, a `plz-curl-error' or
`plz-http-error' is signaled when the request fails, with a
`plz-error' structure as the error data. For synchronous
requests, this argument is ignored.
NOTE: In v0.8 of `plz', only one error will be signaled:
`plz-error'. The existing errors, `plz-curl-error' and
`plz-http-error', inherit from `plz-error' to allow applications
to update their code while using v0.7 (i.e. any `condition-case'
forms should now handle only `plz-error', not the other two).
FINALLY is an optional function called without argument after
THEN or ELSE, as appropriate. For synchronous requests, this
argument is ignored.
CONNECT-TIMEOUT and TIMEOUT are a number of seconds that limit
how long it takes to connect to a host and to receive a response
from a host, respectively.
NOQUERY is passed to `make-process', which see.
\(To silence checkdoc, we mention the internal argument REST.)"
(declare (indent defun))
(setf decode (if (and decode-s (not decode))
nil decode))
(push (cons "Expect" "") headers)
(let* ((data-arg (pcase-exhaustive body-type
('binary "--data-binary")
('text "--data")))
(curl-command-line-args (append plz-curl-default-args
(list "--config" "-")))
(curl-config-header-args (cl-loop for (key . value) in headers
collect (cons "--header" (format "%s: %s" key value))))
(curl-config-args (append curl-config-header-args
(list (cons "--url" url))
(when connect-timeout
(list (cons "--connect-timeout"
(number-to-string connect-timeout))))
(when timeout
(list (cons "--max-time" (number-to-string timeout))))
(pcase method
('get
(list (cons "--dump-header" "-")))
((or 'put 'post)
(list (cons "--dump-header" "-")
(cons "--request" (upcase (symbol-name method)))
(pcase body
(`(file ,filename)
(cons "--upload-file" (expand-file-name filename)))
(_ (cons data-arg "@-")))))
('delete
(list (cons "--dump-header" "-")
(cons "--request" (upcase (symbol-name method)))))
('head
(list (cons "--head" "")
(cons "--request" "HEAD"))))))
(curl-config (cl-loop for (key . value) in curl-config-args
concat (format "%s \"%s\"\n" key value)))
(decode (pcase as
('binary nil)
(_ decode)))
(default-directory
temporary-file-directory)
(process-buffer (generate-new-buffer " *plz-request-curl*"))
(stderr-process (make-pipe-process :name "plz-request-curl-stderr"
:buffer (generate-new-buffer " *plz-request-curl-stderr*")
:noquery t
:sentinel #'plz--stderr-sentinel))
(process (make-process :name "plz-request-curl"
:buffer process-buffer
:coding 'binary
:command (append (list plz-curl-program) curl-command-line-args)
:connection-type 'pipe
:sentinel #'plz--sentinel
:stderr stderr-process
:noquery noquery))
sync-p)
(when (eq 'sync then)
(setf sync-p t
then (lambda (result)
(process-put process :plz-result result))
else nil))
(setf
(process-get process :plz-then)
(pcase-exhaustive as
((or 'binary 'string)
(lambda ()
(let ((coding-system (or (plz--coding-system) 'utf-8)))
(pcase as
('binary (set-buffer-multibyte nil)))
(plz--narrow-to-body)
(when decode
(decode-coding-region (point) (point-max) coding-system))
(funcall then (or (buffer-string)
(make-plz-error :message (format "buffer-string is nil in buffer:%S" process-buffer)))))))
('buffer (progn
(setf (process-get process :plz-as) 'buffer)
(lambda ()
(let ((coding-system (or (plz--coding-system) 'utf-8)))
(pcase as
('binary (set-buffer-multibyte nil)))
(plz--narrow-to-body)
(when decode
(decode-coding-region (point) (point-max) coding-system)))
(funcall then (current-buffer)))))
('response (lambda ()
(funcall then (or (plz--response :decode-p decode)
(make-plz-error :message (format "response is nil for buffer:%S buffer-string:%S"
process-buffer (buffer-string)))))))
('file (lambda ()
(set-buffer-multibyte nil)
(plz--narrow-to-body)
(let ((filename (make-temp-file "plz-")))
(condition-case err
(progn
(write-region (point-min) (point-max) filename)
(funcall then filename))
(error (ignore-errors
(delete-file filename))
(funcall then (make-plz-error :message (format "error while writing to file %S: %S" filename err))))))))
(`(file ,(and (pred stringp) filename))
(lambda ()
(set-buffer-multibyte nil)
(plz--narrow-to-body)
(condition-case err
(progn
(write-region (point-min) (point-max) filename nil nil nil 'excl)
(funcall then filename))
(error (ignore-errors
(when (file-exists-p filename)
(delete-file filename)))
(funcall then (make-plz-error :message (format "error while writing to file %S: %S" filename err)))))))
((pred functionp) (lambda ()
(let ((coding-system (or (plz--coding-system) 'utf-8)))
(plz--narrow-to-body)
(when decode
(decode-coding-region (point) (point-max) coding-system))
(funcall then (funcall as))))))
(process-get process :plz-else) else
(process-get process :plz-finally) finally
(process-get process :plz-sync) sync-p
(process-get process :plz-args) (apply #'list method url rest)
(process-get process :plz-result) :plz-result)
(process-send-string process curl-config)
(when body
(cl-typecase body
(string (process-send-string process body))
(buffer (with-current-buffer body
(process-send-region process (point-min) (point-max))))))
(process-send-eof process)
(if sync-p
(unwind-protect
(with-local-quit
(unless (and process stderr-process)
(error "Process unexpectedly nil"))
(while (accept-process-output process))
(while (accept-process-output stderr-process))
(when (eq :plz-result (process-get process :plz-result))
(plz--sentinel process "finished\n")
(when (eq :plz-result (process-get process :plz-result))
(error "Plz: NO RESULT FROM PROCESS:%S ARGS:%S"
process rest)))
(pcase (process-get process :plz-result)
((and (pred plz-error-p) data)
(if (plz-error-response data)
(signal 'plz-http-error (list "HTTP error" data))
(signal 'plz-curl-error (list "Curl error" data))))
(else
else)))
(unless (eq as 'buffer)
(kill-buffer process-buffer))
(kill-buffer (process-buffer stderr-process)))
process)))
(cl-defstruct plz-queued-request
"Structure representing a queued `plz' HTTP request.
For more details on these slots, see arguments to the function
`plz'."
method url headers body else finally noquery
as then body-type decode
connect-timeout timeout
next previous process)
(cl-defstruct plz-queue
"Structure forming a queue for `plz' requests.
The queue may be appended to (the default) and pre-pended to, and
items may be removed from the front of the queue (i.e. by
default, it's FIFO). Use functions `plz-queue', `plz-run', and
`plz-clear' to queue, run, and clear requests, respectively."
(limit 1
:documentation "Number of simultaneous requests.")
(active nil
:documentation "Active requests.")
(requests nil
:documentation "Queued requests.")
(canceled-p nil
:documentation "Non-nil when queue has been canceled.")
first-active last-active
first-request last-request
(finally nil
:documentation "Function called with no arguments after queue has been emptied or canceled."))
(defun plz-queue (queue &rest args)
"Queue request for ARGS on QUEUE and return QUEUE.
To pre-pend to QUEUE rather than append, it may be a list of the
form (`prepend' QUEUE). QUEUE is a `plz-request' queue. ARGS
are those passed to `plz', which see. Use `plz-run' to start
making QUEUE's requests."
(declare (indent defun))
(cl-assert (not (equal 'sync (plist-get (cddr args) :then))) nil
"Only async requests may be queued")
(pcase-let* ((`(,method ,url . ,rest) args)
(args `(:method ,method :url ,url ,@rest))
(request (apply #'make-plz-queued-request args)))
(pcase queue
(`(prepend ,queue) (plz--queue-prepend request queue))
(_ (plz--queue-append request queue))))
queue)
(defun plz--queue-append (request queue)
"Add REQUEST to end of QUEUE and return QUEUE."
(cl-check-type request plz-queued-request
"REQUEST must be a `plz-queued-request' structure.")
(cl-check-type queue plz-queue
"QUEUE must be a `plz-queue' structure.")
(when (plz-queue-last-request queue)
(setf (plz-queued-request-next (plz-queue-last-request queue)) request))
(setf (plz-queued-request-previous request) (plz-queue-last-request queue)
(plz-queue-last-request queue) request)
(unless (plz-queue-first-request queue)
(setf (plz-queue-first-request queue) request))
(unless (plz-queue-last-request queue)
(setf (plz-queue-last-request queue) request))
(push request (plz-queue-requests queue))
queue)
(defun plz--queue-prepend (request queue)
"Add REQUEST to front of QUEUE and return QUEUE."
(cl-check-type request plz-queued-request
"REQUEST must be a `plz-queued-request' structure.")
(cl-check-type queue plz-queue
"QUEUE must be a `plz-queue' structure.")
(when (plz-queue-requests queue)
(setf (plz-queued-request-next request) (car (plz-queue-requests queue))
(plz-queued-request-previous (plz-queued-request-next request)) request))
(setf (plz-queue-first-request queue) request)
(unless (plz-queue-first-request queue)
(setf (plz-queue-first-request queue) request))
(unless (plz-queue-last-request queue)
(setf (plz-queue-last-request queue) request))
(push request (plz-queue-requests queue))
queue)
(defun plz--queue-pop (queue)
"Return the first queued request on QUEUE and remove it from QUEUE."
(let* ((request (plz-queue-first-request queue))
(next (plz-queued-request-next request)))
(when next
(setf (plz-queued-request-previous next) nil))
(setf (plz-queue-first-request queue) next
(plz-queue-requests queue) (delq request (plz-queue-requests queue)))
(when (eq request (plz-queue-last-request queue))
(setf (plz-queue-last-request queue) nil))
request))
(defun plz-run (queue)
"Process requests in QUEUE and return QUEUE.
Return when QUEUE is at limit or has no more queued requests.
QUEUE should be a `plz-queue' structure."
(cl-labels ((readyp
(queue) (and (not (plz-queue-canceled-p queue))
(plz-queue-requests queue)
(< (length (plz-queue-active queue)) (plz-queue-limit queue)))))
(while (readyp queue)
(pcase-let* ((request (plz--queue-pop queue))
((cl-struct plz-queued-request method url
headers body finally noquery as body-type decode connect-timeout timeout
(else orig-else) (then orig-then))
request)
(then (lambda (response)
(unwind-protect
(funcall orig-then response)
(setf (plz-queue-active queue) (delq request (plz-queue-active queue)))
(plz-run queue))))
(else (lambda (arg)
(unwind-protect
(when orig-else
(funcall orig-else arg))
(setf (plz-queue-active queue) (delq request (plz-queue-active queue)))
(plz-run queue))))
(args (list method url
:headers headers :body body :finally finally :noquery noquery
:connect-timeout connect-timeout :timeout timeout)))
(when as
(setf args (plist-put args :as as)))
(when else
(setf args (plist-put args :else else)))
(when then
(setf args (plist-put args :then then)))
(when decode
(setf args (plist-put args :decode decode)))
(when body-type
(setf args (plist-put args :body-type body-type)))
(when connect-timeout
(setf args (plist-put args :connect-timeout connect-timeout)))
(when timeout
(setf args (plist-put args :timeout timeout)))
(setf (plz-queued-request-process request) (apply #'plz args))
(push request (plz-queue-active queue))))
(when (and (plz-queue-finally queue)
(zerop (length (plz-queue-active queue)))
(zerop (length (plz-queue-requests queue))))
(funcall (plz-queue-finally queue)))
queue))
(defun plz-clear (queue)
"Clear QUEUE and return it.
Cancels any active or pending requests and calls the queue's
FINALLY function. For pending requests, their ELSE functions
will be called with a `plz-error' structure with the message,
\"`plz' queue cleared; request canceled.\"; active requests will
have their curl processes killed and their ELSE functions called
with the corresponding data."
(setf (plz-queue-canceled-p queue) t)
(dolist (request (plz-queue-active queue))
(when (process-live-p (plz-queued-request-process request))
(kill-process (plz-queued-request-process request)))
(setf (plz-queue-active queue) (delq request (plz-queue-active queue))))
(dolist (request (plz-queue-requests queue))
(funcall (plz-queued-request-else request)
(make-plz-error :message "`plz' queue cleared; request canceled."))
(setf (plz-queue-requests queue) (delq request (plz-queue-requests queue))))
(when (plz-queue-finally queue)
(funcall (plz-queue-finally queue)))
(setf (plz-queue-first-active queue) nil
(plz-queue-last-active queue) nil
(plz-queue-first-request queue) nil
(plz-queue-last-request queue) nil
(plz-queue-canceled-p queue) nil)
queue)
(defun plz-length (queue)
"Return number of of QUEUE's outstanding requests.
Includes active and queued requests."
(+ (length (plz-queue-active queue))
(length (plz-queue-requests queue))))
(defun plz--sentinel (process status)
"Sentinel for curl PROCESS.
STATUS should be the process's event string (see info
node `(elisp) Sentinels'). Calls `plz--respond' to process the
HTTP response (directly for synchronous requests, or from a timer
for asynchronous ones)."
(pcase status
((or "finished\n" "killed\n" "interrupt\n"
(pred numberp)
(rx "exited abnormally with code " (group (1+ digit))))
(let ((buffer (process-buffer process)))
(if (process-get process :plz-sync)
(plz--respond process buffer status)
(run-at-time 0 nil #'plz--respond process buffer status))))))
(defun plz--respond (process buffer status)
"Respond to HTTP response from PROCESS in BUFFER.
Parses the response and calls the THEN/ELSE callbacks
accordingly. To be called from `plz--sentinel'. STATUS is the
argument passed to `plz--sentinel', which see."
(unwind-protect
(with-current-buffer buffer
(pcase-exhaustive status
((or 0 "finished\n")
(goto-char (point-min))
(plz--skip-proxy-headers)
(while (plz--skip-redirect-headers))
(pcase (plz--http-status)
((and status (guard (<= 200 status 299)))
(ignore status) (funcall (process-get process :plz-then)))
(_
(let ((err (make-plz-error :response (plz--response))))
(pcase-exhaustive (process-get process :plz-else)
(`nil (process-put process :plz-result err))
((and (pred functionp) fn) (funcall fn err)))))))
((or (and (pred numberp) code)
(rx "exited abnormally with code " (let code (group (1+ digit)))))
(let* ((curl-exit-code (cl-typecase code
(string (string-to-number code))
(number code)))
(curl-error-message (alist-get curl-exit-code plz-curl-errors))
(err (make-plz-error :curl-error (cons curl-exit-code curl-error-message))))
(pcase-exhaustive (process-get process :plz-else)
(`nil (process-put process :plz-result err))
((and (pred functionp) fn) (funcall fn err)))))
((and (or "killed\n" "interrupt\n") status)
(let* ((message (pcase status
("killed\n" "curl process killed")
("interrupt\n" "curl process interrupted")))
(err (make-plz-error :message message)))
(pcase-exhaustive (process-get process :plz-else)
(`nil (process-put process :plz-result err))
((and (pred functionp) fn) (funcall fn err)))))))
(when-let ((finally (process-get process :plz-finally)))
(funcall finally))
(unless (or (process-get process :plz-sync)
(eq 'buffer (process-get process :plz-as)))
(kill-buffer buffer))))
(defun plz--stderr-sentinel (process status)
"Sentinel for STDERR buffer.
Arguments are PROCESS and STATUS (ok, checkdoc?)."
(pcase status
((or "finished\n" "killed\n" "interrupt\n"
(pred numberp)
(rx "exited abnormally with code " (1+ digit)))
(kill-buffer (process-buffer process)))))
(defun plz--skip-proxy-headers ()
"Skip proxy headers in current buffer."
(when (looking-at plz-http-response-status-line-regexp)
(let* ((status-code (string-to-number (match-string 2)))
(reason-phrase (match-string 3)))
(when (and (equal 200 status-code)
(equal "Connection established" reason-phrase))
(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)))
(or (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
according to the apparent coding system.
Assumes that point is at beginning of HTTP response."
(save-excursion
(unless (looking-at plz-http-response-status-line-regexp)
(signal 'plz-http-error
(list "plz--response: Unable to parse HTTP response status line"
(buffer-substring (point) (line-end-position)))))
(let* ((http-version (string-to-number (match-string 1)))
(status-code (string-to-number (match-string 2)))
(headers (plz--headers))
(coding-system (or (plz--coding-system headers) 'utf-8)))
(plz--narrow-to-body)
(when decode-p
(decode-coding-region (point) (point-max) coding-system))
(make-plz-response
:version http-version
:status status-code
:headers headers
:body (buffer-string)))))
(defun plz--coding-system (&optional headers)
"Return coding system for HTTP response in current buffer.
HEADERS may optionally be an alist of parsed HTTP headers to
refer to rather than the current buffer's un-parsed headers."
(let* ((headers (or headers (plz--headers)))
(content-type (alist-get 'content-type headers)))
(when content-type
(coding-system-from-name content-type))))
(defun plz--http-status ()
"Return HTTP status code for HTTP response in current buffer.
Assumes point is at start of HTTP response."
(when (looking-at plz-http-response-status-line-regexp)
(string-to-number (match-string 2))))
(defun plz--headers ()
"Return headers alist for HTTP response in current buffer.
Assumes point is at start of HTTP response."
(save-excursion
(forward-line 1)
(let ((limit (save-excursion
(re-search-forward plz-http-end-of-headers-regexp nil)
(point))))
(cl-loop while (re-search-forward (rx bol (group (1+ (not (in ":")))) ":" (1+ blank)
(group (1+ (not (in "\r\n")))))
limit t)
collect (cons (intern (downcase (match-string 1))) (match-string 2))))))
(defun plz--narrow-to-body ()
"Narrow to body of HTTP response in current buffer.
Assumes point is at start of HTTP response."
(unless (re-search-forward plz-http-end-of-headers-regexp nil t)
(signal 'plz-http-error '("plz--narrow-to-body: Unable to find end of headers")))
(narrow-to-region (point) (point-max)))
(provide 'plz)