Function: url-http-wait-for-headers-change-function

url-http-wait-for-headers-change-function is a byte-compiled function defined in url-http.el.gz.

Signature

(url-http-wait-for-headers-change-function ST ND LENGTH)

Source Code

;; Defined in /usr/src/emacs/lisp/url/url-http.el.gz
(defun url-http-wait-for-headers-change-function (_st nd _length)
  ;; This will wait for the headers to arrive and then splice in the
  ;; next appropriate after-change-function, etc.
  (url-http-debug "url-http-wait-for-headers-change-function (%s)"
		  (buffer-name))
  (let ((end-of-headers nil)
	(old-http nil)
	(process-buffer (current-buffer))
	;; (content-length nil)
        )
    (when (not (bobp))
      (goto-char (point-min))
      (if (and (looking-at ".*\n")	; have one line at least
	       (not (looking-at "^HTTP/[1-9]\\.[0-9]")))
	  ;; Not HTTP/x.y data, must be 0.9
	  ;; God, I wish this could die.
	  (setq end-of-headers t
		url-http-end-of-headers 0
		old-http t)
	;; Blank line at end of headers.
	(when (re-search-forward "^\r?\n" nil t)
	  (backward-char 1)
	  ;; Saw the end of the headers
	  (url-http-debug "Saw end of headers... (%s)" (buffer-name))
	  (setq url-http-end-of-headers (set-marker (make-marker)
						    (point))
		end-of-headers t)
	  (setq nd (- nd (url-http-clean-headers)))))

      (if (not end-of-headers)
	  ;; Haven't seen the end of the headers yet, need to wait
	  ;; for more data to arrive.
	  nil
	(unless old-http
	  (url-http-parse-response)
	  (mail-narrow-to-head)
	  (setq url-http-transfer-encoding (mail-fetch-field
					    "transfer-encoding")
		url-http-content-type (mail-fetch-field "content-type"))
	  (if (mail-fetch-field "content-length")
	      (setq url-http-content-length
		    (string-to-number (mail-fetch-field "content-length"))))
	  (widen))
	(when url-http-transfer-encoding
	  (setq url-http-transfer-encoding
		(downcase url-http-transfer-encoding)))

	(cond
	 ((null url-http-response-status)
	  ;; We got back a headerless malformed response from the
	  ;; server.
	  (url-http-activate-callback))
	 ((memq url-http-response-status '(204 205))
	  (url-http-debug "%d response must have headers only (%s)."
			  url-http-response-status (buffer-name))
	  (when (url-http-parse-headers)
	    (url-http-activate-callback)))
	 ((string= "HEAD" url-http-method)
	  ;; A HEAD request is _ALWAYS_ terminated by the header
	  ;; information, regardless of any entity headers,
	  ;; according to section 4.4 of the HTTP/1.1 draft.
	  (url-http-debug "HEAD request must have headers only (%s)."
			  (buffer-name))
	  (when (url-http-parse-headers)
	    (url-http-activate-callback)))
	 ((string= "CONNECT" url-http-method)
	  ;; A CONNECT request is finished, but we cannot stick this
	  ;; back on the free connection list
	  (url-http-debug "CONNECT request must have headers only.")
	  (when (url-http-parse-headers)
	    (url-http-activate-callback)))
	 ((equal url-http-response-status 304)
	  ;; Only allowed to have a header section.  We have to handle
	  ;; this here instead of in url-http-parse-headers because if
	  ;; you have a cached copy of something without a known
	  ;; content-length, and try to retrieve it from the cache, we'd
	  ;; fall into the 'being dumb' section and wait for the
	  ;; connection to terminate, which means we'd wait for 10
	  ;; seconds for the keep-alives to time out on some servers.
	  (when (url-http-parse-headers)
	    (url-http-activate-callback)))
	 (old-http
	  ;; HTTP/0.9 always signaled end-of-connection by closing the
	  ;; connection.
	  (url-http-debug
	   "Saw HTTP/0.9 response, connection closed means end of document.")
	  (setq url-http-after-change-function
		#'url-http-simple-after-change-function))
	 ((equal url-http-transfer-encoding "chunked")
	  (url-http-debug "Saw chunked encoding.")
	  (setq url-http-after-change-function
		#'url-http-chunked-encoding-after-change-function)
	  (when (> nd url-http-end-of-headers)
	    (url-http-debug
	     "Calling initial chunked-encoding for extra data at end of headers")
	    (url-http-chunked-encoding-after-change-function
	     (marker-position url-http-end-of-headers) nd
	     (- nd url-http-end-of-headers))))
	 ((integerp url-http-content-length)
	  (url-http-debug
	   "Got a content-length, being smart about document end.")
	  (setq url-http-after-change-function
		#'url-http-content-length-after-change-function)
	  (cond
	   ((= 0 url-http-content-length)
	    ;; We got a NULL body!  Activate the callback
	    ;; immediately!
	    (url-http-debug
	     "Got 0-length content-length, activating callback immediately.")
	    (when (url-http-parse-headers)
	      (url-http-activate-callback)))
	   ((> nd url-http-end-of-headers)
	    ;; Have some leftover data
	    (url-http-debug "Calling initial content-length for extra data at end of headers")
	    (url-http-content-length-after-change-function
	     (marker-position url-http-end-of-headers)
	     nd
	     (- nd url-http-end-of-headers)))
	   (t
	    nil)))
	 (t
	  (url-http-debug "No content-length, being dumb.")
	  (setq url-http-after-change-function
		#'url-http-simple-after-change-function)))))
    ;; We are still at the beginning of the buffer... must just be
    ;; waiting for a response.
    (url-http-debug "Spinning waiting for headers...")
    (when (eq process-buffer (current-buffer))
      (goto-char (point-max)))))