1;;; url-util.el --- Miscellaneous helper routines for URL library
2
3;; Copyright (C) 1996, 1997, 1998, 1999, 2001, 2004,
4;;   2005, 2006, 2007 Free Software Foundation, Inc.
5
6;; Author: Bill Perry <wmperry@gnu.org>
7;; Keywords: comm, data, processes
8
9;; This file is part of GNU Emacs.
10;;
11;; GNU Emacs is free software; you can redistribute it and/or modify
12;; it under the terms of the GNU General Public License as published by
13;; the Free Software Foundation; either version 2, or (at your option)
14;; any later version.
15;;
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19;; GNU General Public License for more details.
20;;
21;; You should have received a copy of the GNU General Public License
22;; along with GNU Emacs; see the file COPYING.  If not, write to the
23;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24;; Boston, MA 02110-1301, USA.
25
26;;; Commentary:
27
28;;; Code:
29
30(require 'url-parse)
31(autoload 'timezone-parse-date "timezone")
32(autoload 'timezone-make-date-arpa-standard "timezone")
33(autoload 'mail-header-extract "mailheader")
34
35(defvar url-parse-args-syntax-table
36  (copy-syntax-table emacs-lisp-mode-syntax-table)
37  "A syntax table for parsing sgml attributes.")
38
39(modify-syntax-entry ?' "\"" url-parse-args-syntax-table)
40(modify-syntax-entry ?` "\"" url-parse-args-syntax-table)
41(modify-syntax-entry ?{ "(" url-parse-args-syntax-table)
42(modify-syntax-entry ?} ")" url-parse-args-syntax-table)
43
44;;;###autoload
45(defcustom url-debug nil
46  "*What types of debug messages from the URL library to show.
47Debug messages are logged to the *URL-DEBUG* buffer.
48
49If t, all messages will be logged.
50If a number, all messages will be logged, as well shown via `message'.
51If a list, it is a list of the types of messages to be logged."
52  :type '(choice (const :tag "none" nil)
53		 (const :tag "all" t)
54		 (checklist :tag "custom"
55			    (const :tag "HTTP" :value http)
56			    (const :tag "DAV" :value dav)
57			    (const :tag "General" :value retrieval)
58			    (const :tag "Filename handlers" :value handlers)
59			    (symbol :tag "Other")))
60  :group 'url-hairy)
61
62;;;###autoload
63(defun url-debug (tag &rest args)
64  (if quit-flag
65      (error "Interrupted!"))
66  (if (or (eq url-debug t)
67	  (numberp url-debug)
68	  (and (listp url-debug) (memq tag url-debug)))
69      (with-current-buffer (get-buffer-create "*URL-DEBUG*")
70	(goto-char (point-max))
71	(insert (symbol-name tag) " -> " (apply 'format args) "\n")
72	(if (numberp url-debug)
73	    (apply 'message args)))))
74
75;;;###autoload
76(defun url-parse-args (str &optional nodowncase)
77  ;; Return an assoc list of attribute/value pairs from an RFC822-type string
78  (let (
79	name				; From name=
80	value				; its value
81	results				; Assoc list of results
82	name-pos			; Start of XXXX= position
83	val-pos				; Start of value position
84	st
85	nd
86	)
87    (save-excursion
88      (save-restriction
89	(set-buffer (get-buffer-create " *urlparse-temp*"))
90	(set-syntax-table url-parse-args-syntax-table)
91	(erase-buffer)
92	(insert str)
93	(setq st (point-min)
94	      nd (point-max))
95	(set-syntax-table url-parse-args-syntax-table)
96	(narrow-to-region st nd)
97	(goto-char (point-min))
98	(while (not (eobp))
99	  (skip-chars-forward "; \n\t")
100	  (setq name-pos (point))
101	  (skip-chars-forward "^ \n\t=;")
102	  (if (not nodowncase)
103	      (downcase-region name-pos (point)))
104	  (setq name (buffer-substring name-pos (point)))
105	  (skip-chars-forward " \t\n")
106	  (if (/= (or (char-after (point)) 0)  ?=) ; There is no value
107	      (setq value nil)
108	    (skip-chars-forward " \t\n=")
109	    (setq val-pos (point)
110		  value
111		  (cond
112		   ((or (= (or (char-after val-pos) 0) ?\")
113			(= (or (char-after val-pos) 0) ?'))
114		    (buffer-substring (1+ val-pos)
115				      (condition-case ()
116					  (prog2
117					      (forward-sexp 1)
118					      (1- (point))
119					    (skip-chars-forward "\""))
120					(error
121					 (skip-chars-forward "^ \t\n")
122					 (point)))))
123		   (t
124		    (buffer-substring val-pos
125				      (progn
126					(skip-chars-forward "^;")
127					(skip-chars-backward " \t")
128					(point)))))))
129	  (setq results (cons (cons name value) results))
130	  (skip-chars-forward "; \n\t"))
131	results))))
132
133;;;###autoload
134(defun url-insert-entities-in-string (string)
135  "Convert HTML markup-start characters to entity references in STRING.
136Also replaces the \" character, so that the result may be safely used as
137  an attribute value in a tag.  Returns a new string with the result of the
138  conversion.  Replaces these characters as follows:
139    &  ==>  &amp;
140    <  ==>  &lt;
141    >  ==>  &gt;
142    \"  ==>  &quot;"
143  (if (string-match "[&<>\"]" string)
144      (save-excursion
145	(set-buffer (get-buffer-create " *entity*"))
146	(erase-buffer)
147	(buffer-disable-undo (current-buffer))
148	(insert string)
149	(goto-char (point-min))
150	(while (progn
151		 (skip-chars-forward "^&<>\"")
152		 (not (eobp)))
153	  (insert (cdr (assq (char-after (point))
154			     '((?\" . "";")
155			       (?& . "&amp;")
156			       (?< . "&lt;")
157			       (?> . "&gt;")))))
158	  (delete-char 1))
159	(buffer-string))
160    string))
161
162;;;###autoload
163(defun url-normalize-url (url)
164  "Return a 'normalized' version of URL.
165Strips out default port numbers, etc."
166  (let (type data retval)
167    (setq data (url-generic-parse-url url)
168	  type (url-type data))
169    (if (member type '("www" "about" "mailto" "info"))
170	(setq retval url)
171      (url-set-target data nil)
172      (setq retval (url-recreate-url data)))
173    retval))
174
175;;;###autoload
176(defun url-lazy-message (&rest args)
177  "Just like `message', but is a no-op if called more than once a second.
178Will not do anything if `url-show-status' is nil."
179  (if (or (null url-show-status)
180	  (active-minibuffer-window)
181	  (= url-lazy-message-time
182	     (setq url-lazy-message-time (nth 1 (current-time)))))
183      nil
184    (apply 'message args)))
185
186;;;###autoload
187(defun url-get-normalized-date (&optional specified-time)
188  "Return a 'real' date string that most HTTP servers can understand."
189  (require 'timezone)
190  (let* ((raw (if specified-time (current-time-string specified-time)
191		(current-time-string)))
192	 (gmt (timezone-make-date-arpa-standard raw
193						(nth 1 (current-time-zone))
194						"GMT"))
195	 (parsed (timezone-parse-date gmt))
196	 (day (cdr-safe (assoc (substring raw 0 3) url-weekday-alist)))
197	 (year nil)
198	 (month (car
199		 (rassoc
200		  (string-to-number (aref parsed 1)) url-monthabbrev-alist)))
201	 )
202    (setq day (or (car-safe (rassoc day url-weekday-alist))
203		  (substring raw 0 3))
204	  year (aref parsed 0))
205    ;; This is needed for plexus servers, or the server will hang trying to
206    ;; parse the if-modified-since header.  Hopefully, I can take this out
207    ;; soon.
208    (if (and year (> (length year) 2))
209	(setq year (substring year -2 nil)))
210
211    (concat day ", " (aref parsed 2) "-" month "-" year " "
212	    (aref parsed 3) " " (or (aref parsed 4)
213				    (concat "[" (nth 1 (current-time-zone))
214					    "]")))))
215
216;;;###autoload
217(defun url-eat-trailing-space (x)
218  "Remove spaces/tabs at the end of a string."
219  (let ((y (1- (length x)))
220	(skip-chars (list ?  ?\t ?\n)))
221    (while (and (>= y 0) (memq (aref x y) skip-chars))
222      (setq y (1- y)))
223    (substring x 0 (1+ y))))
224
225;;;###autoload
226(defun url-strip-leading-spaces (x)
227  "Remove spaces at the front of a string."
228  (let ((y (1- (length x)))
229	(z 0)
230	(skip-chars (list ?  ?\t ?\n)))
231    (while (and (<= z y) (memq (aref x z) skip-chars))
232      (setq z (1+ z)))
233    (substring x z nil)))
234
235;;;###autoload
236(defun url-pretty-length (n)
237  (cond
238   ((< n 1024)
239    (format "%d bytes" n))
240   ((< n (* 1024 1024))
241    (format "%dk" (/ n 1024.0)))
242   (t
243    (format "%2.2fM" (/ n (* 1024 1024.0))))))
244
245;;;###autoload
246(defun url-display-percentage (fmt perc &rest args)
247  (when url-show-status
248    (if (null fmt)
249	(if (fboundp 'clear-progress-display)
250	    (clear-progress-display))
251      (if (and (fboundp 'progress-display) perc)
252	  (apply 'progress-display fmt perc args)
253	(apply 'message fmt args)))))
254
255;;;###autoload
256(defun url-percentage (x y)
257  (if (fboundp 'float)
258      (round (* 100 (/ x (float y))))
259    (/ (* x 100) y)))
260
261;;;###autoload
262(defun url-basepath (file &optional x)
263  "Return the base pathname of FILE, or the actual filename if X is true."
264  (cond
265   ((null file) "")
266   ((string-match (eval-when-compile (regexp-quote "?")) file)
267    (if x
268	(file-name-nondirectory (substring file 0 (match-beginning 0)))
269      (file-name-directory (substring file 0 (match-beginning 0)))))
270   (x (file-name-nondirectory file))
271   (t (file-name-directory file))))
272
273;;;###autoload
274(defun url-parse-query-string (query &optional downcase allow-newlines)
275  (let (retval pairs cur key val)
276    (setq pairs (split-string query "&"))
277    (while pairs
278      (setq cur (car pairs)
279	    pairs (cdr pairs))
280      (if (not (string-match "=" cur))
281	  nil				; Grace
282	(setq key (url-unhex-string (substring cur 0 (match-beginning 0))
283				    allow-newlines))
284	(setq val (url-unhex-string (substring cur (match-end 0) nil)
285				    allow-newlines))
286	(if downcase
287	    (setq key (downcase key)))
288	(setq cur (assoc key retval))
289	(if cur
290	    (setcdr cur (cons val (cdr cur)))
291	  (setq retval (cons (list key val) retval)))))
292    retval))
293
294(defun url-unhex (x)
295  (if (> x ?9)
296      (if (>= x ?a)
297	  (+ 10 (- x ?a))
298	(+ 10 (- x ?A)))
299    (- x ?0)))
300
301;; Fixme: Is this definition better, and does it ever matter?
302
303;; (defun url-unhex-string (str &optional allow-newlines)
304;;   "Remove %XX, embedded spaces, etc in a url.
305;; If optional second argument ALLOW-NEWLINES is non-nil, then allow the
306;; decoding of carriage returns and line feeds in the string, which is normally
307;; forbidden in URL encoding."
308;;   (setq str (or str ""))
309;;   (setq str (replace-regexp-in-string "%[[:xdigit:]]\\{2\\}"
310;; 				      (lambda (match)
311;; 					(string (string-to-number
312;; 						 (substring match 1) 16)))
313;; 				      str t t))
314;;   (if allow-newlines
315;;       (replace-regexp-in-string "[\n\r]" (lambda (match)
316;; 					   (format "%%%.2X" (aref match 0)))
317;; 				str t t)
318;;     str))
319
320;;;###autoload
321(defun url-unhex-string (str &optional allow-newlines)
322  "Remove %XX embedded spaces, etc in a url.
323If optional second argument ALLOW-NEWLINES is non-nil, then allow the
324decoding of carriage returns and line feeds in the string, which is normally
325forbidden in URL encoding."
326  (setq str (or str ""))
327  (let ((tmp "")
328	(case-fold-search t))
329    (while (string-match "%[0-9a-f][0-9a-f]" str)
330      (let* ((start (match-beginning 0))
331	     (ch1 (url-unhex (elt str (+ start 1))))
332	     (code (+ (* 16 ch1)
333		      (url-unhex (elt str (+ start 2))))))
334	(setq tmp (concat
335		   tmp (substring str 0 start)
336		   (cond
337		    (allow-newlines
338		     (char-to-string code))
339		    ((or (= code ?\n) (= code ?\r))
340		     " ")
341		    (t (char-to-string code))))
342	      str (substring str (match-end 0)))))
343    (setq tmp (concat tmp str))
344    tmp))
345
346(defconst url-unreserved-chars
347  '(
348    ?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
349    ?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
350    ?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9
351    ?- ?_ ?. ?! ?~ ?* ?' ?\( ?\))
352  "A list of characters that are _NOT_ reserved in the URL spec.
353This is taken from RFC 2396.")
354
355;;;###autoload
356(defun url-hexify-string (string)
357  "Return a new string that is STRING URI-encoded.
358First, STRING is converted to utf-8, if necessary.  Then, for each
359character in the utf-8 string, those found in `url-unreserved-chars'
360are left as-is, all others are represented as a three-character
361string: \"%\" followed by two lowercase hex digits."
362  ;; To go faster and avoid a lot of consing, we could do:
363  ;;
364  ;; (defconst url-hexify-table
365  ;;   (let ((map (make-vector 256 nil)))
366  ;;     (dotimes (byte 256) (aset map byte
367  ;;                               (if (memq byte url-unreserved-chars)
368  ;;                                   (char-to-string byte)
369  ;;                                 (format "%%%02x" byte))))
370  ;;     map))
371  ;;
372  ;; (mapconcat (curry 'aref url-hexify-table) ...)
373  (mapconcat (lambda (byte)
374               (if (memq byte url-unreserved-chars)
375                   (char-to-string byte)
376                 (format "%%%02x" byte)))
377             (if (multibyte-string-p string)
378                 (encode-coding-string string 'utf-8)
379               string)
380             ""))
381
382;;;###autoload
383(defun url-file-extension (fname &optional x)
384  "Return the filename extension of FNAME.
385If optional variable X is t,
386then return the basename of the file with the extension stripped off."
387  (if (and fname
388	   (setq fname (url-basepath fname t))
389	   (string-match "\\.[^./]+$" fname))
390      (if x (substring fname 0 (match-beginning 0))
391	(substring fname (match-beginning 0) nil))
392    ;;
393    ;; If fname has no extension, and x then return fname itself instead of
394    ;; nothing. When caching it allows the correct .hdr file to be produced
395    ;; for filenames without extension.
396    ;;
397    (if x
398 	fname
399      "")))
400
401;;;###autoload
402(defun url-truncate-url-for-viewing (url &optional width)
403  "Return a shortened version of URL that is WIDTH characters or less wide.
404WIDTH defaults to the current frame width."
405  (let* ((fr-width (or width (frame-width)))
406	 (str-width (length url))
407	 (fname nil)
408	 (modified 0)
409	 (urlobj nil))
410    ;; The first thing that can go are the search strings
411    (if (and (>= str-width fr-width)
412	     (string-match "?" url))
413	(setq url (concat (substring url 0 (match-beginning 0)) "?...")
414	      str-width (length url)))
415    (if (< str-width fr-width)
416	nil				; Hey, we are done!
417      (setq urlobj (url-generic-parse-url url)
418	    fname (url-filename urlobj)
419	    fr-width (- fr-width 4))
420      (while (and (>= str-width fr-width)
421		  (string-match "/" fname))
422	(setq fname (substring fname (match-end 0) nil)
423	      modified (1+ modified))
424	(url-set-filename urlobj fname)
425	(setq url (url-recreate-url urlobj)
426	      str-width (length url)))
427      (if (> modified 1)
428	  (setq fname (concat "/.../" fname))
429	(setq fname (concat "/" fname)))
430      (url-set-filename urlobj fname)
431      (setq url (url-recreate-url urlobj)))
432    url))
433
434;;;###autoload
435(defun url-view-url (&optional no-show)
436  "View the current document's URL.
437Optional argument NO-SHOW means just return the URL, don't show it in
438the minibuffer.
439
440This uses `url-current-object', set locally to the buffer."
441  (interactive)
442  (if (not url-current-object)
443      nil
444    (if no-show
445	(url-recreate-url url-current-object)
446      (message "%s" (url-recreate-url url-current-object)))))
447
448(eval-and-compile
449  (defvar url-get-url-filename-chars "-%.?@a-zA-Z0-9()_/:~=&"
450    "Valid characters in a URL")
451  )
452
453(defun url-get-url-at-point (&optional pt)
454  "Get the URL closest to point, but don't change position.
455Has a preference for looking backward when not directly on a symbol."
456  ;; Not at all perfect - point must be right in the name.
457  (save-excursion
458    (if pt (goto-char pt))
459    (let (start url)
460      (save-excursion
461	;; first see if you're just past a filename
462	(if (not (eobp))
463	    (if (looking-at "[] \t\n[{}()]") ; whitespace or some parens
464		(progn
465		  (skip-chars-backward " \n\t\r({[]})")
466		  (if (not (bobp))
467		      (backward-char 1)))))
468	(if (and (char-after (point))
469		 (string-match (eval-when-compile
470				 (concat "[" url-get-url-filename-chars "]"))
471			       (char-to-string (char-after (point)))))
472	    (progn
473	      (skip-chars-backward url-get-url-filename-chars)
474	      (setq start (point))
475	      (skip-chars-forward url-get-url-filename-chars))
476	  (setq start (point)))
477	(setq url (buffer-substring-no-properties start (point))))
478      (if (and url (string-match "^(.*)\\.?$" url))
479	  (setq url (match-string 1 url)))
480      (if (and url (string-match "^URL:" url))
481	  (setq url (substring url 4 nil)))
482      (if (and url (string-match "\\.$" url))
483	  (setq url (substring url 0 -1)))
484      (if (and url (string-match "^www\\." url))
485	  (setq url (concat "http://" url)))
486      (if (and url (not (string-match url-nonrelative-link url)))
487	  (setq url nil))
488      url)))
489
490(defun url-generate-unique-filename (&optional fmt)
491  "Generate a unique filename in `url-temporary-directory'."
492  (if (not fmt)
493      (let ((base (format "url-tmp.%d" (user-real-uid)))
494	    (fname "")
495	    (x 0))
496	(setq fname (format "%s%d" base x))
497	(while (file-exists-p
498		(expand-file-name fname url-temporary-directory))
499	  (setq x (1+ x)
500		fname (concat base (int-to-string x))))
501	(expand-file-name fname url-temporary-directory))
502    (let ((base (concat "url" (int-to-string (user-real-uid))))
503	  (fname "")
504	  (x 0))
505      (setq fname (format fmt (concat base (int-to-string x))))
506      (while (file-exists-p
507	      (expand-file-name fname url-temporary-directory))
508	(setq x (1+ x)
509	      fname (format fmt (concat base (int-to-string x)))))
510      (expand-file-name fname url-temporary-directory))))
511
512(defun url-extract-mime-headers ()
513  "Set `url-current-mime-headers' in current buffer."
514  (save-excursion
515    (goto-char (point-min))
516    (unless url-current-mime-headers
517      (set (make-local-variable 'url-current-mime-headers)
518	   (mail-header-extract)))))
519
520(provide 'url-util)
521
522;; arch-tag: 24352abc-5a5a-412e-90cd-313b26bed5c9
523;;; url-util.el ends here
524