Function: url-http
url-http is an autoloaded and byte-compiled function defined in
url-http.el.gz.
Signature
(url-http URL CALLBACK CBARGS &optional RETRY-BUFFER GATEWAY-METHOD)
Documentation
Retrieve URL via HTTP asynchronously.
URL must be a parsed URL. See url-generic-parse-url for details.
When retrieval is completed, execute the function CALLBACK,
passing it an updated value of CBARGS as arguments. The first
element in CBARGS should be a plist describing what has happened
so far during the request, as described in the docstring of
url-retrieve (if in doubt, specify nil). The current buffer
when CALLBACK is executed is the retrieval buffer.
Optional arg RETRY-BUFFER, if non-nil, specifies the buffer of a
previous url-http call, which is being re-attempted.
Optional arg GATEWAY-METHOD specifies the gateway to be used,
overriding the value of url-gateway-method.
The return value of this function is the retrieval buffer.
Source Code
;; Defined in /usr/src/emacs/lisp/url/url-http.el.gz
(defun url-http (url callback cbargs &optional retry-buffer gateway-method)
"Retrieve URL via HTTP asynchronously.
URL must be a parsed URL. See `url-generic-parse-url' for details.
When retrieval is completed, execute the function CALLBACK,
passing it an updated value of CBARGS as arguments. The first
element in CBARGS should be a plist describing what has happened
so far during the request, as described in the docstring of
`url-retrieve' (if in doubt, specify nil). The current buffer
when CALLBACK is executed is the retrieval buffer.
Optional arg RETRY-BUFFER, if non-nil, specifies the buffer of a
previous `url-http' call, which is being re-attempted.
Optional arg GATEWAY-METHOD specifies the gateway to be used,
overriding the value of `url-gateway-method'.
The return value of this function is the retrieval buffer."
(cl-check-type url url "Need a pre-parsed URL.")
;; The request is handled by asynchronous processes, which are outside
;; the dynamic scope of the caller of url-http (sometimes, sometimes
;; not). The caller may still desire to bind variables controlling
;; aspects of the request for the duration of this one http request.
;; The async processes operate on a buffer created in this function,
;; so the way to accomplish this goal is to set buffer local copies of
;; the relevant variables to the dynamic values in scope as we create
;; the buffer. When new variables are added that influence behavior
;; of requests, they should be added to the handling in this function
;; to make them work reliably without changing their global values.
(let* (;; (host (url-host (or url-using-proxy url)))
;; (port (url-port (or url-using-proxy url)))
(noninteractive-p (not (url-interactive-p)))
;; The following binding is needed in url-open-stream, which
;; is called from url-http-find-free-connection.
(url-current-object url)
(connection (url-http-find-free-connection (url-host url)
(url-port url)
gateway-method))
(mime-accept-string url-mime-accept-string)
(mime-encoding-string url-mime-encoding-string)
(mime-charset-string url-mime-charset-string)
(mime-language-string url-mime-language-string)
(buffer (or retry-buffer
(generate-new-buffer
(format " *http %s:%d*" (url-host url) (url-port url)))))
(referer (url-http--encode-string (url-http--get-referer url)))
(httpver url-http-version)
(httpkeepalive url-http-attempt-keepalives)
(user-agent url-user-agent)
(privacy-level url-privacy-level)
(max-redirections url-max-redirections))
(if (not connection)
;; Failed to open the connection for some reason
(progn
(kill-buffer buffer)
(setq buffer nil)
(error "Could not create connection to %s:%d" (url-host url)
(url-port url)))
(with-current-buffer buffer
(mm-disable-multibyte)
(setq url-current-object url
mode-line-format "%b [%s]")
(dolist (var '(url-http-end-of-headers
url-http-content-type
url-http-content-length
url-http-transfer-encoding
url-http-after-change-function
url-http-response-version
url-http-response-status
url-http-chunked-last-crlf-missing
url-http-chunked-length
url-http-chunked-counter
url-http-chunked-start
url-callback-function
url-callback-arguments
url-show-status
url-http-process
url-http-method
url-http-extra-headers
url-http-noninteractive
url-http-data
url-http-target-url
url-http-no-retry
url-http-connection-opened
url-mime-accept-string
url-mime-encoding-string
url-mime-charset-string
url-mime-language-string
url-http-proxy
url-http-referer
url-http-version
url-http-attempt-keepalives
url-http-extensions-header
url-user-agent
url-privacy-level
url-max-redirections
nsm-noninteractive))
(set (make-local-variable var) nil))
(setq url-http-method (or url-request-method "GET")
url-http-extra-headers url-request-extra-headers
url-http-noninteractive url-request-noninteractive
url-http-data url-request-data
url-http-process connection
url-http-chunked-last-crlf-missing nil
url-http-chunked-length nil
url-http-chunked-start nil
url-http-chunked-counter 0
url-callback-function callback
url-callback-arguments cbargs
url-http-after-change-function 'url-http-wait-for-headers-change-function
url-http-target-url url-current-object
url-http-no-retry retry-buffer
url-http-connection-opened nil
url-mime-accept-string mime-accept-string
url-mime-encoding-string mime-encoding-string
url-mime-charset-string mime-charset-string
url-mime-language-string mime-language-string
url-http-proxy url-using-proxy
url-http-referer referer
url-http-version httpver
url-http-attempt-keepalives httpkeepalive
url-http-extensions-header url-extensions-header
url-user-agent user-agent
url-privacy-level privacy-level
url-max-redirections max-redirections
nsm-noninteractive noninteractive-p)
(set-process-buffer connection buffer)
(set-process-filter connection #'url-http-generic-filter)
(pcase (process-status connection)
('connect
;; Asynchronous connection
(set-process-sentinel connection 'url-http-async-sentinel))
('failed
;; Asynchronous connection failed
(error "Could not create connection to %s:%d" (url-host url)
(url-port url)))
(_
(if (and url-http-proxy (string= "https"
(url-type url-current-object)))
(url-https-proxy-connect connection)
(set-process-sentinel connection
#'url-http-end-of-document-sentinel)
(process-send-string connection (url-http-create-request)))))))
buffer))