url-parse.el 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. ;;; url-parse.el --- Uniform Resource Locator parser
  2. ;; Copyright (C) 1996-1999, 2004-2012 Free Software Foundation, Inc.
  3. ;; Keywords: comm, data, processes
  4. ;; This file is part of GNU Emacs.
  5. ;;
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;; Code:
  18. (require 'url-vars)
  19. (require 'auth-source)
  20. (eval-when-compile (require 'cl))
  21. (autoload 'url-scheme-get-property "url-methods")
  22. (defstruct (url
  23. (:constructor nil)
  24. (:constructor url-parse-make-urlobj
  25. (&optional type user password host portspec filename
  26. target attributes fullness))
  27. (:copier nil))
  28. type user password host portspec filename target attributes fullness
  29. silent (use-cookies t))
  30. (defsubst url-port (urlobj)
  31. (or (url-portspec urlobj)
  32. (if (url-fullness urlobj)
  33. (url-scheme-get-property (url-type urlobj) 'default-port))))
  34. (defsetf url-port (urlobj) (port) `(setf (url-portspec ,urlobj) ,port))
  35. ;;;###autoload
  36. (defun url-recreate-url (urlobj)
  37. "Recreate a URL string from the parsed URLOBJ."
  38. (concat (url-type urlobj) ":" (if (url-host urlobj) "//" "")
  39. (if (url-user urlobj)
  40. (concat (url-user urlobj)
  41. (if (url-password urlobj)
  42. (concat ":" (url-password urlobj)))
  43. "@"))
  44. (url-host urlobj)
  45. (if (and (url-port urlobj)
  46. (not (equal (url-port urlobj)
  47. (url-scheme-get-property (url-type urlobj) 'default-port))))
  48. (format ":%d" (url-port urlobj)))
  49. (or (url-filename urlobj) "/")
  50. (url-recreate-url-attributes urlobj)
  51. (if (url-target urlobj)
  52. (concat "#" (url-target urlobj)))))
  53. (defun url-recreate-url-attributes (urlobj)
  54. "Recreate the attributes of an URL string from the parsed URLOBJ."
  55. (when (url-attributes urlobj)
  56. (concat ";"
  57. (mapconcat (lambda (x)
  58. (if (cdr x)
  59. (concat (car x) "=" (cdr x))
  60. (car x)))
  61. (url-attributes urlobj) ";"))))
  62. ;;;###autoload
  63. (defun url-generic-parse-url (url)
  64. "Return an URL-struct of the parts of URL.
  65. The CL-style struct contains the following fields:
  66. TYPE USER PASSWORD HOST PORTSPEC FILENAME TARGET ATTRIBUTES FULLNESS."
  67. ;; See RFC 3986.
  68. (cond
  69. ((null url)
  70. (url-parse-make-urlobj))
  71. ((or (not (string-match url-nonrelative-link url))
  72. (= ?/ (string-to-char url)))
  73. ;; This isn't correct, as a relative URL can be a fragment link
  74. ;; (e.g. "#foo") and many other things (see section 4.2).
  75. ;; However, let's not fix something that isn't broken, especially
  76. ;; when close to a release.
  77. (url-parse-make-urlobj nil nil nil nil nil url))
  78. (t
  79. (with-temp-buffer
  80. ;; Don't let those temp-buffer modifications accidentally
  81. ;; deactivate the mark of the current-buffer.
  82. (let ((deactivate-mark nil))
  83. (set-syntax-table url-parse-syntax-table)
  84. (let ((save-pos nil)
  85. (prot nil)
  86. (user nil)
  87. (pass nil)
  88. (host nil)
  89. (port nil)
  90. (file nil)
  91. (refs nil)
  92. (attr nil)
  93. (full nil)
  94. (inhibit-read-only t))
  95. (erase-buffer)
  96. (insert url)
  97. (goto-char (point-min))
  98. (setq save-pos (point))
  99. ;; 3.1. Scheme
  100. (unless (looking-at "//")
  101. (skip-chars-forward "a-zA-Z+.\\-")
  102. (downcase-region save-pos (point))
  103. (setq prot (buffer-substring save-pos (point)))
  104. (skip-chars-forward ":")
  105. (setq save-pos (point)))
  106. ;; 3.2. Authority
  107. (when (looking-at "//")
  108. (setq full t)
  109. (forward-char 2)
  110. (setq save-pos (point))
  111. (skip-chars-forward "^/")
  112. (setq host (buffer-substring save-pos (point)))
  113. (if (string-match "^\\([^@]+\\)@" host)
  114. (setq user (match-string 1 host)
  115. host (substring host (match-end 0) nil)))
  116. (if (and user (string-match "\\([^:]+\\):\\(.*\\)" user))
  117. (setq pass (match-string 2 user)
  118. user (match-string 1 user)))
  119. ;; This gives wrong results for IPv6 literal addresses.
  120. (if (string-match ":\\([0-9+]+\\)" host)
  121. (setq port (string-to-number (match-string 1 host))
  122. host (substring host 0 (match-beginning 0))))
  123. (if (string-match ":$" host)
  124. (setq host (substring host 0 (match-beginning 0))))
  125. (setq host (downcase host)
  126. save-pos (point)))
  127. (if (not port)
  128. (setq port (url-scheme-get-property prot 'default-port)))
  129. ;; 3.3. Path
  130. ;; Gross hack to preserve ';' in data URLs
  131. (setq save-pos (point))
  132. ;; 3.4. Query
  133. (if (string= "data" prot)
  134. (goto-char (point-max))
  135. ;; Now check for references
  136. (skip-chars-forward "^#")
  137. (if (eobp)
  138. nil
  139. (delete-region
  140. (point)
  141. (progn
  142. (skip-chars-forward "#")
  143. (setq refs (buffer-substring (point) (point-max)))
  144. (point-max))))
  145. (goto-char save-pos)
  146. (skip-chars-forward "^;")
  147. (unless (eobp)
  148. (setq attr (url-parse-args (buffer-substring (point) (point-max))
  149. t)
  150. attr (nreverse attr))))
  151. (setq file (buffer-substring save-pos (point)))
  152. (if (and host (string-match "%[0-9][0-9]" host))
  153. (setq host (url-unhex-string host)))
  154. (url-parse-make-urlobj
  155. prot user pass host port file refs attr full)))))))
  156. (defmacro url-bit-for-url (method lookfor url)
  157. `(let* ((urlobj (url-generic-parse-url url))
  158. (bit (funcall ,method urlobj))
  159. (methods (list 'url-recreate-url
  160. 'url-host))
  161. auth-info)
  162. (while (and (not bit) (> (length methods) 0))
  163. (setq auth-info (auth-source-search
  164. :max 1
  165. :host (funcall (pop methods) urlobj)
  166. :port (url-type urlobj)))
  167. (setq bit (plist-get (nth 0 auth-info) ,lookfor))
  168. (when (functionp bit)
  169. (setq bit (funcall bit))))
  170. bit))
  171. (defun url-user-for-url (url)
  172. "Attempt to use .authinfo to find a user for this URL."
  173. (url-bit-for-url 'url-user :user url))
  174. (defun url-password-for-url (url)
  175. "Attempt to use .authinfo to find a password for this URL."
  176. (url-bit-for-url 'url-password :secret url))
  177. (provide 'url-parse)
  178. ;;; url-parse.el ends here