Function: url-http-find-free-connection

url-http-find-free-connection is a byte-compiled function defined in url-http.el.gz.

Signature

(url-http-find-free-connection HOST PORT &optional GATEWAY-METHOD)

Source Code

;; Defined in /usr/src/emacs/lisp/url/url-http.el.gz
(defun url-http-find-free-connection (host port &optional gateway-method)
  (when main-thread
    (maphash
     (lambda (key _val)
       (unless (thread-live-p (caddr key))
         (remhash key url-http-open-connections)))
     url-http-open-connections))
  (let ((conns (gethash
                (if main-thread
                    (list host port (current-thread))
                  (cons host port))
                url-http-open-connections))
	(connection nil))
    (while (and conns (not connection))
      (if (not (memq (process-status (car conns)) '(run open connect)))
	  (progn
	    (url-http-debug "Cleaning up dead process: %s:%d %S"
			    host port (car conns))
	    (url-http-idle-sentinel (car conns) nil))
	(setq connection (car conns))
	(url-http-debug
         "Found existing connection: %s:%d %S" host port connection))
      (pop conns))
    (if connection
	(url-http-debug "Reusing existing connection: %s:%d" host port)
      (url-http-debug "Contacting host: %s:%d" host port))
    (url-lazy-message "Contacting host: %s:%d" host port)

    (unless connection
      (let ((buf (generate-new-buffer " *url-http-temp*")))
	;; `url-open-stream' needs a buffer in which to do things
	;; like authentication.  But we use another buffer afterwards.
	(unwind-protect
            (let ((proc (url-open-stream host buf
                                         (if url-using-proxy
                                             (url-host url-using-proxy)
                                           host)
                                         (if url-using-proxy
                                             (url-port url-using-proxy)
                                           port)
                                         gateway-method)))
	      ;; url-open-stream might return nil.
	      (when (processp proc)
		;; Drop the temp buffer link before killing the buffer.
		(set-process-buffer proc nil)
		(setq connection proc)))
	  ;; If there was an error on connect, make sure we don't
	  ;; get queried.
	  (when (get-buffer-process buf)
	    (set-process-query-on-exit-flag (get-buffer-process buf) nil))
	  (kill-buffer buf))))

    (if connection
	(url-http-mark-connection-as-busy host port connection))))