0001-Fix-url-https-over-proxy-implement.-Bug-11788.patch 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. From 172363d31b3ad5f45da44aa09652d0e0779ef5f2 Mon Sep 17 00:00:00 2001
  2. From: Tao Fang <fangtao0901@gmail.com>
  3. Date: Tue, 22 Mar 2016 22:39:51 +0800
  4. Subject: [PATCH] Fix url https over proxy implement. (Bug#11788)
  5. * lisp/url/url-http.el: Fix url https over proxy implement. (Bug#11788)
  6. * etc/NEWS: Mention this.
  7. ---
  8. etc/NEWS | 3 ++
  9. lisp/url/url-http.el | 105 ++++++++++++++++++++++++++++++++++++++++++++-------
  10. 2 files changed, 94 insertions(+), 14 deletions(-)
  11. diff --git a/etc/NEWS b/etc/NEWS
  12. index 4414625..7d2cc92 100644
  13. --- a/etc/NEWS
  14. +++ b/etc/NEWS
  15. @@ -1193,6 +1193,9 @@ plist will contain a :peer element that has the output of
  16. programmatically delete all cookies, or cookies from a specific
  17. domain.
  18. ++++
  19. +*** The URL package now support https over proxy.
  20. +
  21. ** Tramp
  22. +++
  23. diff --git a/lisp/url/url-http.el b/lisp/url/url-http.el
  24. index 33f6d11..4f180ed 100644
  25. --- a/lisp/url/url-http.el
  26. +++ b/lisp/url/url-http.el
  27. @@ -197,7 +197,14 @@ request.")
  28. ;; `url-open-stream' needs a buffer in which to do things
  29. ;; like authentication. But we use another buffer afterwards.
  30. (unwind-protect
  31. - (let ((proc (url-open-stream host buf host port gateway-method)))
  32. + (let ((proc (url-open-stream host buf
  33. + (if url-using-proxy
  34. + (url-host url-using-proxy)
  35. + host)
  36. + (if url-using-proxy
  37. + (url-port url-using-proxy)
  38. + port)
  39. + gateway-method)))
  40. ;; url-open-stream might return nil.
  41. (when (processp proc)
  42. ;; Drop the temp buffer link before killing the buffer.
  43. @@ -925,7 +932,13 @@ should be shown to the user."
  44. (erase-buffer)
  45. (let ((url-request-method url-http-method)
  46. (url-request-extra-headers url-http-extra-headers)
  47. - (url-request-data url-http-data))
  48. + (url-request-data url-http-data)
  49. + (url-using-proxy (url-find-proxy-for-url
  50. + url-current-object
  51. + (url-host url-current-object))))
  52. + (when url-using-proxy
  53. + (setq url-using-proxy
  54. + (url-generic-parse-url url-using-proxy)))
  55. (url-http url-current-object url-callback-function
  56. url-callback-arguments (current-buffer)))))
  57. ((url-http-parse-headers)
  58. @@ -1209,17 +1222,20 @@ The return value of this function is the retrieval buffer."
  59. (nsm-noninteractive (or url-request-noninteractive
  60. (and (boundp 'url-http-noninteractive)
  61. url-http-noninteractive)))
  62. - (connection (url-http-find-free-connection host port gateway-method))
  63. + (connection (url-http-find-free-connection (url-host url)
  64. + (url-port url)
  65. + gateway-method))
  66. (mime-accept-string url-mime-accept-string)
  67. (buffer (or retry-buffer
  68. (generate-new-buffer
  69. - (format " *http %s:%d*" host port)))))
  70. + (format " *http %s:%d*" (url-host url) (url-port url))))))
  71. (if (not connection)
  72. ;; Failed to open the connection for some reason
  73. (progn
  74. (kill-buffer buffer)
  75. (setq buffer nil)
  76. - (error "Could not create connection to %s:%d" host port))
  77. + (error "Could not create connection to %s:%d" (url-host url)
  78. + (url-port url)))
  79. (with-current-buffer buffer
  80. (mm-disable-multibyte)
  81. (setq url-current-object url
  82. @@ -1275,13 +1291,72 @@ The return value of this function is the retrieval buffer."
  83. (set-process-sentinel connection 'url-http-async-sentinel))
  84. (`failed
  85. ;; Asynchronous connection failed
  86. - (error "Could not create connection to %s:%d" host port))
  87. + (error "Could not create connection to %s:%d" (url-host url)
  88. + (url-port url)))
  89. (_
  90. - (set-process-sentinel connection
  91. - 'url-http-end-of-document-sentinel)
  92. - (process-send-string connection (url-http-create-request))))))
  93. + (if (and url-http-proxy (string= "https"
  94. + (url-type url-current-object)))
  95. + (url-https-proxy-connect connection)
  96. + (set-process-sentinel connection
  97. + 'url-http-end-of-document-sentinel)
  98. + (process-send-string connection (url-http-create-request)))))))
  99. buffer))
  100. +(defun url-https-proxy-connect (connection)
  101. + (setq url-http-after-change-function 'url-https-proxy-after-change-function)
  102. + (process-send-string connection (format (concat "CONNECT %s:%d HTTP/1.1\r\n"
  103. + "Host: %s\r\n"
  104. + "\r\n")
  105. + (url-host url-current-object)
  106. + (or (url-port url-current-object)
  107. + url-https-default-port)
  108. + (url-host url-current-object))))
  109. +
  110. +(defun url-https-proxy-after-change-function (st nd length)
  111. + (let* ((process-buffer (current-buffer))
  112. + (proc (get-buffer-process process-buffer)))
  113. + (goto-char (point-min))
  114. + (when (re-search-forward "^\r?\n" nil t)
  115. + (backward-char 1)
  116. + ;; Saw the end of the headers
  117. + (setq url-http-end-of-headers (set-marker (make-marker) (point)))
  118. + (url-http-parse-response)
  119. + (cond
  120. + ((null url-http-response-status)
  121. + ;; We got back a headerless malformed response from the
  122. + ;; server.
  123. + (url-http-activate-callback)
  124. + (error "Malformed response from proxy, fail!"))
  125. + ((= url-http-response-status 200)
  126. + (if (gnutls-available-p)
  127. + (condition-case e
  128. + (let ((tls-connection (gnutls-negotiate
  129. + :process proc
  130. + :hostname (url-host url-current-object)
  131. + :verify-error nil)))
  132. + ;; check certificate validity
  133. + (setq tls-connection
  134. + (nsm-verify-connection tls-connection
  135. + (url-host url-current-object)
  136. + (url-port url-current-object)))
  137. + (with-current-buffer process-buffer (erase-buffer))
  138. + (set-process-buffer tls-connection process-buffer)
  139. + (setq url-http-after-change-function
  140. + 'url-http-wait-for-headers-change-function)
  141. + (set-process-filter tls-connection 'url-http-generic-filter)
  142. + (process-send-string tls-connection
  143. + (url-http-create-request)))
  144. + (gnutls-error
  145. + (url-http-activate-callback)
  146. + (error "gnutls-error: %s" e))
  147. + (error
  148. + (url-http-activate-callback)
  149. + (error "error: %s" e)))
  150. + (error "error: gnutls support needed!")))
  151. + (t
  152. + (url-http-activate-callback)
  153. + (message "error response: %d" url-http-response-status))))))
  154. +
  155. (defun url-http-async-sentinel (proc why)
  156. ;; We are performing an asynchronous connection, and a status change
  157. ;; has occurred.
  158. @@ -1293,11 +1368,13 @@ The return value of this function is the retrieval buffer."
  159. (url-http-end-of-document-sentinel proc why))
  160. ((string= (substring why 0 4) "open")
  161. (setq url-http-connection-opened t)
  162. - (condition-case error
  163. - (process-send-string proc (url-http-create-request))
  164. - (file-error
  165. - (setq url-http-connection-opened nil)
  166. - (message "HTTP error: %s" error))))
  167. + (if (and url-http-proxy (string= "https" (url-type url-current-object)))
  168. + (url-https-proxy-connect proc)
  169. + (condition-case error
  170. + (process-send-string proc (url-http-create-request))
  171. + (file-error
  172. + (setq url-http-connection-opened nil)
  173. + (message "HTTP error: %s" error)))))
  174. (t
  175. (setf (car url-callback-arguments)
  176. (nconc (list :error (list 'error 'connection-failed why
  177. --
  178. 2.7.4