url-proxy.el 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. ;;; url-proxy.el --- Proxy server support
  2. ;; Copyright (C) 1999, 2004-2012 Free Software Foundation, Inc.
  3. ;; Keywords: comm, data, processes, hypermedia
  4. ;; This file is part of GNU Emacs.
  5. ;; GNU Emacs is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; GNU Emacs is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Code:
  16. (require 'url-parse)
  17. (autoload 'url-warn "url")
  18. (defun url-default-find-proxy-for-url (urlobj host)
  19. (cond
  20. ((or (and (assoc "no_proxy" url-proxy-services)
  21. (string-match
  22. (cdr
  23. (assoc "no_proxy" url-proxy-services))
  24. host))
  25. (equal "www" (url-type urlobj)))
  26. "DIRECT")
  27. ((cdr (assoc (url-type urlobj) url-proxy-services))
  28. (concat "PROXY " (cdr (assoc (url-type urlobj) url-proxy-services))))
  29. ;;
  30. ;; Should check for socks
  31. ;;
  32. (t
  33. "DIRECT")))
  34. (defvar url-proxy-locator 'url-default-find-proxy-for-url)
  35. (defun url-find-proxy-for-url (url host)
  36. (let ((proxies (split-string (funcall url-proxy-locator url host) " *; *"))
  37. (proxy nil)
  38. (case-fold-search t))
  39. ;; Not sure how I should handle gracefully degrading from one proxy to
  40. ;; another, so for now just deal with the first one
  41. ;; (while proxies
  42. (if (listp proxies)
  43. (setq proxy (car proxies))
  44. (setq proxy proxies))
  45. (cond
  46. ((string-match "^direct" proxy) nil)
  47. ((string-match "^proxy +" proxy)
  48. (concat "http://" (substring proxy (match-end 0)) "/"))
  49. ((string-match "^socks +" proxy)
  50. (concat "socks://" (substring proxy (match-end 0))))
  51. (t
  52. (url-warn 'url (format "Unknown proxy directive: %s" proxy) 'critical)
  53. nil))))
  54. (defun url-proxy (url callback &optional cbargs)
  55. ;; Retrieve URL from a proxy.
  56. ;; Expects `url-using-proxy' to be bound to the specific proxy to use."
  57. (setq url-using-proxy (url-generic-parse-url url-using-proxy))
  58. (cond
  59. ((string= (url-type url-using-proxy) "http")
  60. (url-http url callback cbargs))
  61. (t
  62. (error "Don't know how to use proxy `%s'" url-using-proxy))))
  63. (provide 'url-proxy)
  64. ;;; url-proxy.el ends here