| Revision 11818, 8.4 KB checked in by ara_t, 8 years ago (diff) |
|---|
From 172363d31b3ad5f45da44aa09652d0e0779ef5f2 Mon Sep 17 00:00:00 2001 From: Tao Fang <fangtao0901@gmail.com> Date: Tue, 22 Mar 2016 22:39:51 +0800 Subject: [PATCH] Fix url https over proxy implement. (Bug#11788) * lisp/url/url-http.el: Fix url https over proxy implement. (Bug#11788) * etc/NEWS: Mention this. --- etc/NEWS | 3 ++ lisp/url/url-http.el | 105 ++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 94 insertions(+), 14 deletions(-) diff --git a/etc/NEWS b/etc/NEWS index 4414625..7d2cc92 100644
| a | b | plist will contain a :peer element that has the output of |
|---|---|---|
| 1193 | 1193 | programmatically delete all cookies, or cookies from a specific |
| 1194 | 1194 | domain. |
| 1195 | 1195 | |
| 1196 | +++ | |
| 1197 | *** The URL package now support https over proxy. | |
| 1198 | ||
| 1196 | 1199 | ** Tramp |
| 1197 | 1200 | |
| 1198 | 1201 | +++ |
diff --git a/lisp/url/url-http.el b/lisp/url/url-http.el index 33f6d11..4f180ed 100644
| a | b | request.") |
|---|---|---|
| 197 | 197 | ;; `url-open-stream' needs a buffer in which to do things |
| 198 | 198 | ;; like authentication. But we use another buffer afterwards. |
| 199 | 199 | (unwind-protect |
| 200 | (let ((proc (url-open-stream host buf host port gateway-method))) | |
| 200 | (let ((proc (url-open-stream host buf | |
| 201 | (if url-using-proxy | |
| 202 | (url-host url-using-proxy) | |
| 203 | host) | |
| 204 | (if url-using-proxy | |
| 205 | (url-port url-using-proxy) | |
| 206 | port) | |
| 207 | gateway-method))) | |
| 201 | 208 | ;; url-open-stream might return nil. |
| 202 | 209 | (when (processp proc) |
| 203 | 210 | ;; Drop the temp buffer link before killing the buffer. |
| … | … | should be shown to the user." |
| 925 | 932 | (erase-buffer) |
| 926 | 933 | (let ((url-request-method url-http-method) |
| 927 | 934 | (url-request-extra-headers url-http-extra-headers) |
| 928 | (url-request-data url-http-data)) | |
| 935 | (url-request-data url-http-data) | |
| 936 | (url-using-proxy (url-find-proxy-for-url | |
| 937 | url-current-object | |
| 938 | (url-host url-current-object)))) | |
| 939 | (when url-using-proxy | |
| 940 | (setq url-using-proxy | |
| 941 | (url-generic-parse-url url-using-proxy))) | |
| 929 | 942 | (url-http url-current-object url-callback-function |
| 930 | 943 | url-callback-arguments (current-buffer))))) |
| 931 | 944 | ((url-http-parse-headers) |
| … | … | The return value of this function is the retrieval buffer." |
| 1209 | 1222 | (nsm-noninteractive (or url-request-noninteractive |
| 1210 | 1223 | (and (boundp 'url-http-noninteractive) |
| 1211 | 1224 | url-http-noninteractive))) |
| 1212 | (connection (url-http-find-free-connection host port gateway-method)) | |
| 1225 | (connection (url-http-find-free-connection (url-host url) | |
| 1226 | (url-port url) | |
| 1227 | gateway-method)) | |
| 1213 | 1228 | (mime-accept-string url-mime-accept-string) |
| 1214 | 1229 | (buffer (or retry-buffer |
| 1215 | 1230 | (generate-new-buffer |
| 1216 | (format " *http %s:%d*" |
|
| 1231 | (format " *http %s:%d*" (url-host url) (url-port url)))))) | |
| 1217 | 1232 | (if (not connection) |
| 1218 | 1233 | ;; Failed to open the connection for some reason |
| 1219 | 1234 | (progn |
| 1220 | 1235 | (kill-buffer buffer) |
| 1221 | 1236 | (setq buffer nil) |
| 1222 | (error "Could not create connection to %s:%d" host port)) | |
| 1237 | (error "Could not create connection to %s:%d" (url-host url) | |
| 1238 | (url-port url))) | |
| 1223 | 1239 | (with-current-buffer buffer |
| 1224 | 1240 | (mm-disable-multibyte) |
| 1225 | 1241 | (setq url-current-object url |
| … | … | The return value of this function is the retrieval buffer." |
| 1275 | 1291 | (set-process-sentinel connection 'url-http-async-sentinel)) |
| 1276 | 1292 | (`failed |
| 1277 | 1293 | ;; Asynchronous connection failed |
| 1278 | (error "Could not create connection to %s:%d" host port)) | |
| 1294 | (error "Could not create connection to %s:%d" (url-host url) | |
| 1295 | (url-port url))) | |
| 1279 | 1296 | (_ |
| 1280 | (set-process-sentinel connection | |
| 1281 | 'url-http-end-of-document-sentinel) | |
| 1282 | (process-send-string connection (url-http-create-request)))))) | |
| 1297 | (if (and url-http-proxy (string= "https" | |
| 1298 | (url-type url-current-object))) | |
| 1299 | (url-https-proxy-connect connection) | |
| 1300 | (set-process-sentinel connection | |
| 1301 | 'url-http-end-of-document-sentinel) | |
| 1302 | (process-send-string connection (url-http-create-request))))))) | |
| 1283 | 1303 | buffer)) |
| 1284 | 1304 | |
| 1305 | (defun url-https-proxy-connect (connection) | |
| 1306 | (setq url-http-after-change-function 'url-https-proxy-after-change-function) | |
| 1307 | (process-send-string connection (format (concat "CONNECT %s:%d HTTP/1.1\r\n" | |
| 1308 | "Host: %s\r\n" | |
| 1309 | "\r\n") | |
| 1310 | (url-host url-current-object) | |
| 1311 | (or (url-port url-current-object) | |
| 1312 | url-https-default-port) | |
| 1313 | (url-host url-current-object)))) | |
| 1314 | ||
| 1315 | (defun url-https-proxy-after-change-function (st nd length) | |
| 1316 | (let* ((process-buffer (current-buffer)) | |
| 1317 | (proc (get-buffer-process process-buffer))) | |
| 1318 | (goto-char (point-min)) | |
| 1319 | (when (re-search-forward "^\r?\n" nil t) | |
| 1320 | (backward-char 1) | |
| 1321 | ;; Saw the end of the headers | |
| 1322 | (setq url-http-end-of-headers (set-marker (make-marker) (point))) | |
| 1323 | (url-http-parse-response) | |
| 1324 | (cond | |
| 1325 | ((null url-http-response-status) | |
| 1326 | ;; We got back a headerless malformed response from the | |
| 1327 | ;; server. | |
| 1328 | (url-http-activate-callback) | |
| 1329 | (error "Malformed response from proxy, fail!")) | |
| 1330 | ((= url-http-response-status 200) | |
| 1331 | (if (gnutls-available-p) | |
| 1332 | (condition-case e | |
| 1333 | (let ((tls-connection (gnutls-negotiate | |
| 1334 | :process proc | |
| 1335 | :hostname (url-host url-current-object) | |
| 1336 | :verify-error nil))) | |
| 1337 | ;; check certificate validity | |
| 1338 | (setq tls-connection | |
| 1339 | (nsm-verify-connection tls-connection | |
| 1340 | (url-host url-current-object) | |
| 1341 | (url-port url-current-object))) | |
| 1342 | (with-current-buffer process-buffer (erase-buffer)) | |
| 1343 | (set-process-buffer tls-connection process-buffer) | |
| 1344 | (setq url-http-after-change-function | |
| 1345 | 'url-http-wait-for-headers-change-function) | |
| 1346 | (set-process-filter tls-connection 'url-http-generic-filter) | |
| 1347 | (process-send-string tls-connection | |
| 1348 | (url-http-create-request))) | |
| 1349 | (gnutls-error | |
| 1350 | (url-http-activate-callback) | |
| 1351 | (error "gnutls-error: %s" e)) | |
| 1352 | (error | |
| 1353 | (url-http-activate-callback) | |
| 1354 | (error "error: %s" e))) | |
| 1355 | (error "error: gnutls support needed!"))) | |
| 1356 | (t | |
| 1357 | (url-http-activate-callback) | |
| 1358 | (message "error response: %d" url-http-response-status)))))) | |
| 1359 | ||
| 1285 | 1360 | (defun url-http-async-sentinel (proc why) |
| 1286 | 1361 | ;; We are performing an asynchronous connection, and a status change |
| 1287 | 1362 | ;; has occurred. |
| … | … | The return value of this function is the retrieval buffer." |
| 1293 | 1368 | (url-http-end-of-document-sentinel proc why)) |
| 1294 | 1369 | ((string= (substring why 0 4) "open") |
| 1295 | 1370 | (setq url-http-connection-opened t) |
| 1296 | (condition-case error | |
| 1297 | (process-send-string proc (url-http-create-request)) | |
| 1298 | (file-error | |
| 1299 | (setq url-http-connection-opened nil) | |
| 1300 | (message "HTTP error: %s" error)))) | |
| 1371 | (if (and url-http-proxy (string= "https" (url-type url-current-object))) | |
| 1372 | (url-https-proxy-connect proc) | |
| 1373 | (condition-case error | |
| 1374 | (process-send-string proc (url-http-create-request)) | |
| 1375 | (file-error | |
| 1376 | (setq url-http-connection-opened nil) | |
| 1377 | (message "HTTP error: %s" error))))) | |
| 1301 | 1378 | (t |
| 1302 | 1379 | (setf (car url-callback-arguments) |
| 1303 | 1380 | (nconc (list :error (list 'error 'connection-failed why |