url-util.el 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. ;;; url-util.el --- Miscellaneous helper routines for URL library
  2. ;; Copyright (C) 1996-1999, 2001, 2004-2012 Free Software Foundation, Inc.
  3. ;; Author: Bill Perry <wmperry@gnu.org>
  4. ;; Keywords: comm, data, processes
  5. ;; This file is part of GNU Emacs.
  6. ;;
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;;; Code:
  19. (require 'url-parse)
  20. (require 'url-vars)
  21. (eval-when-compile (require 'cl))
  22. (autoload 'timezone-parse-date "timezone")
  23. (autoload 'timezone-make-date-arpa-standard "timezone")
  24. (autoload 'mail-header-extract "mailheader")
  25. (defvar url-parse-args-syntax-table
  26. (copy-syntax-table emacs-lisp-mode-syntax-table)
  27. "A syntax table for parsing sgml attributes.")
  28. (modify-syntax-entry ?' "\"" url-parse-args-syntax-table)
  29. (modify-syntax-entry ?` "\"" url-parse-args-syntax-table)
  30. (modify-syntax-entry ?{ "(" url-parse-args-syntax-table)
  31. (modify-syntax-entry ?} ")" url-parse-args-syntax-table)
  32. ;;;###autoload
  33. (defcustom url-debug nil
  34. "What types of debug messages from the URL library to show.
  35. Debug messages are logged to the *URL-DEBUG* buffer.
  36. If t, all messages will be logged.
  37. If a number, all messages will be logged, as well shown via `message'.
  38. If a list, it is a list of the types of messages to be logged."
  39. :type '(choice (const :tag "none" nil)
  40. (const :tag "all" t)
  41. (checklist :tag "custom"
  42. (const :tag "HTTP" :value http)
  43. (const :tag "DAV" :value dav)
  44. (const :tag "General" :value retrieval)
  45. (const :tag "Filename handlers" :value handlers)
  46. (symbol :tag "Other")))
  47. :group 'url-hairy)
  48. ;;;###autoload
  49. (defun url-debug (tag &rest args)
  50. (if quit-flag
  51. (error "Interrupted!"))
  52. (if (or (eq url-debug t)
  53. (numberp url-debug)
  54. (and (listp url-debug) (memq tag url-debug)))
  55. (with-current-buffer (get-buffer-create "*URL-DEBUG*")
  56. (goto-char (point-max))
  57. (insert (symbol-name tag) " -> " (apply 'format args) "\n")
  58. (if (numberp url-debug)
  59. (apply 'message args)))))
  60. ;;;###autoload
  61. (defun url-parse-args (str &optional nodowncase)
  62. ;; Return an assoc list of attribute/value pairs from an RFC822-type string
  63. (let (
  64. name ; From name=
  65. value ; its value
  66. results ; Assoc list of results
  67. name-pos ; Start of XXXX= position
  68. val-pos ; Start of value position
  69. st
  70. nd
  71. )
  72. (save-excursion
  73. (save-restriction
  74. (set-buffer (get-buffer-create " *urlparse-temp*"))
  75. (set-syntax-table url-parse-args-syntax-table)
  76. (erase-buffer)
  77. (insert str)
  78. (setq st (point-min)
  79. nd (point-max))
  80. (set-syntax-table url-parse-args-syntax-table)
  81. (narrow-to-region st nd)
  82. (goto-char (point-min))
  83. (while (not (eobp))
  84. (skip-chars-forward "; \n\t")
  85. (setq name-pos (point))
  86. (skip-chars-forward "^ \n\t=;")
  87. (if (not nodowncase)
  88. (downcase-region name-pos (point)))
  89. (setq name (buffer-substring name-pos (point)))
  90. (skip-chars-forward " \t\n")
  91. (if (/= (or (char-after (point)) 0) ?=) ; There is no value
  92. (setq value nil)
  93. (skip-chars-forward " \t\n=")
  94. (setq val-pos (point)
  95. value
  96. (cond
  97. ((or (= (or (char-after val-pos) 0) ?\")
  98. (= (or (char-after val-pos) 0) ?'))
  99. (buffer-substring (1+ val-pos)
  100. (condition-case ()
  101. (prog2
  102. (forward-sexp 1)
  103. (1- (point))
  104. (skip-chars-forward "\""))
  105. (error
  106. (skip-chars-forward "^ \t\n")
  107. (point)))))
  108. (t
  109. (buffer-substring val-pos
  110. (progn
  111. (skip-chars-forward "^;")
  112. (skip-chars-backward " \t")
  113. (point)))))))
  114. (setq results (cons (cons name value) results))
  115. (skip-chars-forward "; \n\t"))
  116. results))))
  117. ;;;###autoload
  118. (defun url-insert-entities-in-string (string)
  119. "Convert HTML markup-start characters to entity references in STRING.
  120. Also replaces the \" character, so that the result may be safely used as
  121. an attribute value in a tag. Returns a new string with the result of the
  122. conversion. Replaces these characters as follows:
  123. & ==> &amp;
  124. < ==> &lt;
  125. > ==> &gt;
  126. \" ==> &quot;"
  127. (if (string-match "[&<>\"]" string)
  128. (with-current-buffer (get-buffer-create " *entity*")
  129. (erase-buffer)
  130. (buffer-disable-undo (current-buffer))
  131. (insert string)
  132. (goto-char (point-min))
  133. (while (progn
  134. (skip-chars-forward "^&<>\"")
  135. (not (eobp)))
  136. (insert (cdr (assq (char-after (point))
  137. '((?\" . "&quot;")
  138. (?& . "&amp;")
  139. (?< . "&lt;")
  140. (?> . "&gt;")))))
  141. (delete-char 1))
  142. (buffer-string))
  143. string))
  144. ;;;###autoload
  145. (defun url-normalize-url (url)
  146. "Return a 'normalized' version of URL.
  147. Strips out default port numbers, etc."
  148. (let (type data retval)
  149. (setq data (url-generic-parse-url url)
  150. type (url-type data))
  151. (if (member type '("www" "about" "mailto" "info"))
  152. (setq retval url)
  153. ;; FIXME all this does, and all this function seems to do in
  154. ;; most cases, is remove any trailing "#anchor" part of a url.
  155. (setf (url-target data) nil)
  156. (setq retval (url-recreate-url data)))
  157. retval))
  158. ;;;###autoload
  159. (defun url-lazy-message (&rest args)
  160. "Just like `message', but is a no-op if called more than once a second.
  161. Will not do anything if `url-show-status' is nil."
  162. (if (or (and url-current-object
  163. (url-silent url-current-object))
  164. (null url-show-status)
  165. (active-minibuffer-window)
  166. (= url-lazy-message-time
  167. (setq url-lazy-message-time (nth 1 (current-time)))))
  168. nil
  169. (apply 'message args)))
  170. ;;;###autoload
  171. (defun url-get-normalized-date (&optional specified-time)
  172. "Return a 'real' date string that most HTTP servers can understand."
  173. (let ((system-time-locale "C"))
  174. (format-time-string "%a, %d %b %Y %T GMT"
  175. (or specified-time (current-time)) t)))
  176. ;;;###autoload
  177. (defun url-eat-trailing-space (x)
  178. "Remove spaces/tabs at the end of a string."
  179. (let ((y (1- (length x)))
  180. (skip-chars (list ? ?\t ?\n)))
  181. (while (and (>= y 0) (memq (aref x y) skip-chars))
  182. (setq y (1- y)))
  183. (substring x 0 (1+ y))))
  184. ;;;###autoload
  185. (defun url-strip-leading-spaces (x)
  186. "Remove spaces at the front of a string."
  187. (let ((y (1- (length x)))
  188. (z 0)
  189. (skip-chars (list ? ?\t ?\n)))
  190. (while (and (<= z y) (memq (aref x z) skip-chars))
  191. (setq z (1+ z)))
  192. (substring x z nil)))
  193. ;;;###autoload
  194. (defun url-pretty-length (n)
  195. (cond
  196. ((< n 1024)
  197. (format "%d bytes" n))
  198. ((< n (* 1024 1024))
  199. (format "%dk" (/ n 1024.0)))
  200. (t
  201. (format "%2.2fM" (/ n (* 1024 1024.0))))))
  202. ;;;###autoload
  203. (defun url-display-percentage (fmt perc &rest args)
  204. (when (and url-show-status
  205. (or (null url-current-object)
  206. (not (url-silent url-current-object))))
  207. (if (null fmt)
  208. (if (fboundp 'clear-progress-display)
  209. (clear-progress-display))
  210. (if (and (fboundp 'progress-display) perc)
  211. (apply 'progress-display fmt perc args)
  212. (apply 'message fmt args)))))
  213. ;;;###autoload
  214. (defun url-percentage (x y)
  215. (if (fboundp 'float)
  216. (round (* 100 (/ x (float y))))
  217. (/ (* x 100) y)))
  218. ;;;###autoload
  219. (defalias 'url-basepath 'url-file-directory)
  220. ;;;###autoload
  221. (defun url-file-directory (file)
  222. "Return the directory part of FILE, for a URL."
  223. (cond
  224. ((null file) "")
  225. ((string-match "\\?" file)
  226. (file-name-directory (substring file 0 (match-beginning 0))))
  227. (t (file-name-directory file))))
  228. ;;;###autoload
  229. (defun url-file-nondirectory (file)
  230. "Return the nondirectory part of FILE, for a URL."
  231. (cond
  232. ((null file) "")
  233. ((string-match "\\?" file)
  234. (file-name-nondirectory (substring file 0 (match-beginning 0))))
  235. (t (file-name-nondirectory file))))
  236. ;;;###autoload
  237. (defun url-parse-query-string (query &optional downcase allow-newlines)
  238. (let (retval pairs cur key val)
  239. (setq pairs (split-string query "[;&]"))
  240. (while pairs
  241. (setq cur (car pairs)
  242. pairs (cdr pairs))
  243. (unless (string-match "=" cur)
  244. (setq cur (concat cur "=")))
  245. (when (string-match "=" cur)
  246. (setq key (url-unhex-string (substring cur 0 (match-beginning 0))
  247. allow-newlines))
  248. (setq val (url-unhex-string (substring cur (match-end 0) nil)
  249. allow-newlines))
  250. (if downcase
  251. (setq key (downcase key)))
  252. (setq cur (assoc key retval))
  253. (if cur
  254. (setcdr cur (cons val (cdr cur)))
  255. (setq retval (cons (list key val) retval)))))
  256. retval))
  257. ;;;###autoload
  258. (defun url-build-query-string (query &optional semicolons keep-empty)
  259. "Build a query-string.
  260. Given a QUERY in the form:
  261. '((key1 val1)
  262. (key2 val2)
  263. (key3 val1 val2)
  264. (key4)
  265. (key5 ""))
  266. \(This is the same format as produced by `url-parse-query-string')
  267. This will return a string
  268. \"key1=val1&key2=val2&key3=val1&key3=val2&key4&key5\". Keys may
  269. be strings or symbols; if they are symbols, the symbol name will
  270. be used.
  271. When SEMICOLONS is given, the separator will be \";\".
  272. When KEEP-EMPTY is given, empty values will show as \"key=\"
  273. instead of just \"key\" as in the example above."
  274. (mapconcat
  275. (lambda (key-vals)
  276. (let ((escaped
  277. (mapcar (lambda (sym)
  278. (url-hexify-string (format "%s" sym))) key-vals)))
  279. (mapconcat (lambda (val)
  280. (let ((vprint (format "%s" val))
  281. (eprint (format "%s" (car escaped))))
  282. (concat eprint
  283. (if (or keep-empty
  284. (and val (not (zerop (length vprint)))))
  285. "="
  286. "")
  287. vprint)))
  288. (or (cdr escaped) '("")) (if semicolons ";" "&"))))
  289. query (if semicolons ";" "&")))
  290. (defun url-unhex (x)
  291. (if (> x ?9)
  292. (if (>= x ?a)
  293. (+ 10 (- x ?a))
  294. (+ 10 (- x ?A)))
  295. (- x ?0)))
  296. ;; Fixme: Is this definition better, and does it ever matter?
  297. ;; (defun url-unhex-string (str &optional allow-newlines)
  298. ;; "Remove %XX, embedded spaces, etc in a url.
  299. ;; If optional second argument ALLOW-NEWLINES is non-nil, then allow the
  300. ;; decoding of carriage returns and line feeds in the string, which is normally
  301. ;; forbidden in URL encoding."
  302. ;; (setq str (or str ""))
  303. ;; (setq str (replace-regexp-in-string "%[[:xdigit:]]\\{2\\}"
  304. ;; (lambda (match)
  305. ;; (string (string-to-number
  306. ;; (substring match 1) 16)))
  307. ;; str t t))
  308. ;; (if allow-newlines
  309. ;; (replace-regexp-in-string "[\n\r]" (lambda (match)
  310. ;; (format "%%%.2X" (aref match 0)))
  311. ;; str t t)
  312. ;; str))
  313. ;;;###autoload
  314. (defun url-unhex-string (str &optional allow-newlines)
  315. "Remove %XX embedded spaces, etc in a URL.
  316. If optional second argument ALLOW-NEWLINES is non-nil, then allow the
  317. decoding of carriage returns and line feeds in the string, which is normally
  318. forbidden in URL encoding."
  319. (setq str (or str ""))
  320. (let ((tmp "")
  321. (case-fold-search t))
  322. (while (string-match "%[0-9a-f][0-9a-f]" str)
  323. (let* ((start (match-beginning 0))
  324. (ch1 (url-unhex (elt str (+ start 1))))
  325. (code (+ (* 16 ch1)
  326. (url-unhex (elt str (+ start 2))))))
  327. (setq tmp (concat
  328. tmp (substring str 0 start)
  329. (cond
  330. (allow-newlines
  331. (byte-to-string code))
  332. ((or (= code ?\n) (= code ?\r))
  333. " ")
  334. (t (byte-to-string code))))
  335. str (substring str (match-end 0)))))
  336. (concat tmp str)))
  337. (defconst url-unreserved-chars
  338. '(?a ?b ?c ?d ?e ?f ?g ?h ?i ?j ?k ?l ?m ?n ?o ?p ?q ?r ?s ?t ?u ?v ?w ?x ?y ?z
  339. ?A ?B ?C ?D ?E ?F ?G ?H ?I ?J ?K ?L ?M ?N ?O ?P ?Q ?R ?S ?T ?U ?V ?W ?X ?Y ?Z
  340. ?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9
  341. ?- ?_ ?. ?~)
  342. "List of characters that are unreserved in the URL spec.
  343. This is taken from RFC 3986 (section 2.3).")
  344. (defconst url-encoding-table
  345. (let ((vec (make-vector 256 nil)))
  346. (dotimes (byte 256)
  347. ;; RFC 3986 (Section 2.1): For consistency, URI producers and
  348. ;; normalizers should use uppercase hexadecimal digits for all
  349. ;; percent-encodings.
  350. (aset vec byte (format "%%%02X" byte)))
  351. vec)
  352. "Vector translating bytes to URI-encoded %-sequences.")
  353. (defun url--allowed-chars (char-list)
  354. "Return an \"allowed character\" mask (a 256-slot vector).
  355. The Nth element is non-nil if character N is in CHAR-LIST. The
  356. result can be passed as the second arg to `url-hexify-string'."
  357. (let ((vec (make-vector 256 nil)))
  358. (dolist (byte char-list)
  359. (ignore-errors (aset vec byte t)))
  360. vec))
  361. ;;;###autoload
  362. (defun url-hexify-string (string &optional allowed-chars)
  363. "URI-encode STRING and return the result.
  364. If STRING is multibyte, it is first converted to a utf-8 byte
  365. string. Each byte corresponding to an allowed character is left
  366. as-is, while all other bytes are converted to a three-character
  367. string: \"%\" followed by two upper-case hex digits.
  368. The allowed characters are specified by ALLOWED-CHARS. If this
  369. argument is nil, the list `url-unreserved-chars' determines the
  370. allowed characters. Otherwise, ALLOWED-CHARS should be a vector
  371. whose Nth element is non-nil if character N is allowed."
  372. (unless allowed-chars
  373. (setq allowed-chars (url--allowed-chars url-unreserved-chars)))
  374. (mapconcat (lambda (byte)
  375. (if (aref allowed-chars byte)
  376. (char-to-string byte)
  377. (aref url-encoding-table byte)))
  378. (if (multibyte-string-p string)
  379. (encode-coding-string string 'utf-8)
  380. string)
  381. ""))
  382. (defconst url-host-allowed-chars
  383. ;; Allow % to avoid re-encoding %-encoded sequences.
  384. (url--allowed-chars (append '(?% ?! ?$ ?& ?' ?\( ?\) ?* ?+ ?, ?\; ?=)
  385. url-unreserved-chars))
  386. "Allowed-character byte mask for the host segment of a URI.
  387. These characters are specified in RFC 3986, Appendix A.")
  388. (defconst url-path-allowed-chars
  389. (let ((vec (copy-sequence url-host-allowed-chars)))
  390. (aset vec ?/ t)
  391. (aset vec ?: t)
  392. (aset vec ?@ t)
  393. vec)
  394. "Allowed-character byte mask for the path segment of a URI.
  395. These characters are specified in RFC 3986, Appendix A.")
  396. (defconst url-query-allowed-chars
  397. (let ((vec (copy-sequence url-path-allowed-chars)))
  398. (aset vec ?? t)
  399. vec)
  400. "Allowed-character byte mask for the query segment of a URI.
  401. These characters are specified in RFC 3986, Appendix A.")
  402. ;;;###autoload
  403. (defun url-encode-url (url)
  404. "Return a properly URI-encoded version of URL.
  405. This function also performs URI normalization, e.g. converting
  406. the scheme to lowercase if it is uppercase. Apart from
  407. normalization, if URL is already URI-encoded, this function
  408. should return it unchanged."
  409. (if (multibyte-string-p url)
  410. (setq url (encode-coding-string url 'utf-8)))
  411. (let* ((obj (url-generic-parse-url url))
  412. (user (url-user obj))
  413. (pass (url-password obj))
  414. (host (url-host obj))
  415. (path-and-query (url-path-and-query obj))
  416. (path (car path-and-query))
  417. (query (cdr path-and-query))
  418. (frag (url-target obj)))
  419. (if user
  420. (setf (url-user obj) (url-hexify-string user)))
  421. (if pass
  422. (setf (url-password obj) (url-hexify-string pass)))
  423. ;; No special encoding for IPv6 literals.
  424. (and host
  425. (not (string-match "\\`\\[.*\\]\\'" host))
  426. (setf (url-host obj)
  427. (url-hexify-string host url-host-allowed-chars)))
  428. (if path
  429. (setq path (url-hexify-string path url-path-allowed-chars)))
  430. (if query
  431. (setq query (url-hexify-string query url-query-allowed-chars)))
  432. (setf (url-filename obj) (if query (concat path "?" query) path))
  433. (if frag
  434. (setf (url-target obj)
  435. (url-hexify-string frag url-query-allowed-chars)))
  436. (url-recreate-url obj)))
  437. ;;;###autoload
  438. (defun url-file-extension (fname &optional x)
  439. "Return the filename extension of FNAME.
  440. If optional argument X is t, then return the basename
  441. of the file with the extension stripped off."
  442. (if (and fname
  443. (setq fname (url-file-nondirectory fname))
  444. (string-match "\\.[^./]+$" fname))
  445. (if x (substring fname 0 (match-beginning 0))
  446. (substring fname (match-beginning 0) nil))
  447. ;;
  448. ;; If fname has no extension, and x then return fname itself instead of
  449. ;; nothing. When caching it allows the correct .hdr file to be produced
  450. ;; for filenames without extension.
  451. ;;
  452. (if x
  453. fname
  454. "")))
  455. ;;;###autoload
  456. (defun url-truncate-url-for-viewing (url &optional width)
  457. "Return a shortened version of URL that is WIDTH characters wide or less.
  458. WIDTH defaults to the current frame width."
  459. (let* ((fr-width (or width (frame-width)))
  460. (str-width (length url))
  461. (fname nil)
  462. (modified 0)
  463. (urlobj nil))
  464. ;; The first thing that can go are the search strings
  465. (if (and (>= str-width fr-width)
  466. (string-match "?" url))
  467. (setq url (concat (substring url 0 (match-beginning 0)) "?...")
  468. str-width (length url)))
  469. (if (< str-width fr-width)
  470. nil ; Hey, we are done!
  471. (setq urlobj (url-generic-parse-url url)
  472. fname (url-filename urlobj)
  473. fr-width (- fr-width 4))
  474. (while (and (>= str-width fr-width)
  475. (string-match "/" fname))
  476. (setq fname (substring fname (match-end 0) nil)
  477. modified (1+ modified))
  478. (setf (url-filename urlobj) fname)
  479. (setq url (url-recreate-url urlobj)
  480. str-width (length url)))
  481. (if (> modified 1)
  482. (setq fname (concat "/.../" fname))
  483. (setq fname (concat "/" fname)))
  484. (setf (url-filename urlobj) fname)
  485. (setq url (url-recreate-url urlobj)))
  486. url))
  487. ;;;###autoload
  488. (defun url-view-url (&optional no-show)
  489. "View the current document's URL.
  490. Optional argument NO-SHOW means just return the URL, don't show it in
  491. the minibuffer.
  492. This uses `url-current-object', set locally to the buffer."
  493. (interactive)
  494. (if (not url-current-object)
  495. nil
  496. (if no-show
  497. (url-recreate-url url-current-object)
  498. (message "%s" (url-recreate-url url-current-object)))))
  499. (defvar url-get-url-filename-chars "-%.?@a-zA-Z0-9()_/:~=&"
  500. "Valid characters in a URL.")
  501. (defun url-get-url-at-point (&optional pt)
  502. "Get the URL closest to point, but don't change position.
  503. Has a preference for looking backward when not directly on a symbol."
  504. ;; Not at all perfect - point must be right in the name.
  505. (save-excursion
  506. (if pt (goto-char pt))
  507. (let (start url)
  508. (save-excursion
  509. ;; first see if you're just past a filename
  510. (if (not (eobp))
  511. (if (looking-at "[] \t\n[{}()]") ; whitespace or some parens
  512. (progn
  513. (skip-chars-backward " \n\t\r({[]})")
  514. (if (not (bobp))
  515. (backward-char 1)))))
  516. (if (and (char-after (point))
  517. (string-match (concat "[" url-get-url-filename-chars "]")
  518. (char-to-string (char-after (point)))))
  519. (progn
  520. (skip-chars-backward url-get-url-filename-chars)
  521. (setq start (point))
  522. (skip-chars-forward url-get-url-filename-chars))
  523. (setq start (point)))
  524. (setq url (buffer-substring-no-properties start (point))))
  525. (if (and url (string-match "^(.*)\\.?$" url))
  526. (setq url (match-string 1 url)))
  527. (if (and url (string-match "^URL:" url))
  528. (setq url (substring url 4 nil)))
  529. (if (and url (string-match "\\.$" url))
  530. (setq url (substring url 0 -1)))
  531. (if (and url (string-match "^www\\." url))
  532. (setq url (concat "http://" url)))
  533. (if (and url (not (string-match url-nonrelative-link url)))
  534. (setq url nil))
  535. url)))
  536. (defun url-generate-unique-filename (&optional fmt)
  537. "Generate a unique filename in `url-temporary-directory'."
  538. ;; This variable is obsolete, but so is this function.
  539. (let ((tempdir (with-no-warnings url-temporary-directory)))
  540. (if (not fmt)
  541. (let ((base (format "url-tmp.%d" (user-real-uid)))
  542. (fname "")
  543. (x 0))
  544. (setq fname (format "%s%d" base x))
  545. (while (file-exists-p
  546. (expand-file-name fname tempdir))
  547. (setq x (1+ x)
  548. fname (concat base (int-to-string x))))
  549. (expand-file-name fname tempdir))
  550. (let ((base (concat "url" (int-to-string (user-real-uid))))
  551. (fname "")
  552. (x 0))
  553. (setq fname (format fmt (concat base (int-to-string x))))
  554. (while (file-exists-p
  555. (expand-file-name fname tempdir))
  556. (setq x (1+ x)
  557. fname (format fmt (concat base (int-to-string x)))))
  558. (expand-file-name fname tempdir)))))
  559. (make-obsolete 'url-generate-unique-filename 'make-temp-file "23.1")
  560. (defun url-extract-mime-headers ()
  561. "Set `url-current-mime-headers' in current buffer."
  562. (save-excursion
  563. (goto-char (point-min))
  564. (unless url-current-mime-headers
  565. (set (make-local-variable 'url-current-mime-headers)
  566. (mail-header-extract)))))
  567. (defun url-make-private-file (file)
  568. "Make FILE only readable and writable by the current user.
  569. Creates FILE and its parent directories if they do not exist."
  570. (let ((dir (file-name-directory file)))
  571. (when dir
  572. ;; For historical reasons.
  573. (make-directory dir t)))
  574. ;; Based on doc-view-make-safe-dir.
  575. (condition-case nil
  576. (let ((umask (default-file-modes)))
  577. (unwind-protect
  578. (progn
  579. (set-default-file-modes #o0600)
  580. (with-temp-buffer
  581. (write-region (point-min) (point-max)
  582. file nil 'silent nil 'excl)))
  583. (set-default-file-modes umask)))
  584. (file-already-exists
  585. (if (file-symlink-p file)
  586. (error "Danger: `%s' is a symbolic link" file))
  587. (set-file-modes file #o0600))))
  588. (provide 'url-util)
  589. ;;; url-util.el ends here