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