1 ;;; mm-url.el --- a wrapper of url functions/commands for Gnus
3 ;; Copyright (C) 2001-2015 Free Software Foundation, Inc.
5 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;; Some code is stolen from w3 and url packages. Some are moved from
27 ;; TODO: Support POST, cookie.
31 (eval-when-compile (require 'cl
))
36 (defvar url-current-object
)
37 (defvar url-package-name
)
38 (defvar url-package-version
)
41 "A wrapper of url package and external url command for Gnus."
44 (defcustom mm-url-use-external
(not
48 "*If non-nil, use external grab program `mm-url-program'."
53 (defvar mm-url-predefined-programs
54 '((wget "wget" "--user-agent=mm-url" "-q" "-O" "-")
55 (w3m "w3m" "-dump_source")
56 (lynx "lynx" "-source")
57 (curl "curl" "--silent" "--user-agent" "mm-url" "--location")))
59 (defcustom mm-url-program
61 ((executable-find "wget") 'wget
)
62 ((executable-find "w3m") 'w3m
)
63 ((executable-find "lynx") 'lynx
)
64 ((executable-find "curl") 'curl
)
66 "The url grab program.
67 Likely values are `wget', `w3m', `lynx' and `curl'."
70 (symbol :tag
"wget" wget
)
71 (symbol :tag
"w3m" w3m
)
72 (symbol :tag
"lynx" lynx
)
73 (symbol :tag
"curl" curl
)
74 (string :tag
"other"))
77 (defcustom mm-url-arguments nil
78 "The arguments for `mm-url-program'."
80 :type
'(repeat string
)
84 ;;; Internal variables
87 (defvar mm-url-html-entities
95 (rsquo .
39) ; should be U+8217
112 (uarr .
94) ; should be U+8593
114 (lsquo .
96) ; should be U+8216
216 ;; Special handling of these
230 ;; The following 5 entities are not mentioned in the HTML 2.0
231 ;; standard, nor in any other HTML proposed standard of which I
232 ;; am aware. I am not even sure they are ISO entity names. ***
233 ;; Hence, some arrangement should be made to give a bad HTML
234 ;; message when they are seen.
246 ;; (shy . ????) ; soft hyphen
248 "*An assoc list of entity names and how to actually display them.")
250 (defconst mm-url-unreserved-chars
252 ?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
253 ?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
254 ?
0 ?
1 ?
2 ?
3 ?
4 ?
5 ?
6 ?
7 ?
8 ?
9
255 ?- ?_ ?. ?
! ?~ ?
* ?
' ?\
( ?\
))
256 "A list of characters that are _NOT_ reserved in the URL spec.
257 This is taken from RFC 2396.")
259 (defun mm-url-load-url ()
260 "Load `url-insert-file-contents'."
261 (unless (condition-case ()
263 (require 'url-handlers
)
270 (defun mm-url-insert-file-contents (url)
271 "Insert file contents of URL.
272 If `mm-url-use-external' is non-nil, use `mm-url-program'."
273 (if mm-url-use-external
275 (if (string-match "^file:/+" url
)
276 (insert-file-contents (substring url
(1- (match-end 0))))
277 (mm-url-insert-file-contents-external url
))
278 (goto-char (point-min))
279 (if (fboundp 'url-generic-parse-url
)
280 (setq url-current-object
281 (url-generic-parse-url url
)))
282 (list url
(buffer-size)))
284 (let ((name buffer-file-name
)
285 (url-request-extra-headers
286 ;; ISTM setting a Connection header was a workaround for
287 ;; older versions of url included with w3, but it does more
288 ;; harm than good with the one shipped with Emacs. --ansel
289 (if (not (and (boundp 'url-version
)
290 (equal url-version
"Emacs")))
291 (list (cons "Connection" "Close"))))
293 (setq result
(url-insert-file-contents url
))
295 (goto-char (point-min))
296 (while (re-search-forward "\r 1000\r ?" nil t
)
298 (setq buffer-file-name name
)
299 (if (and (fboundp 'url-generic-parse-url
)
301 (setq url-current-object
(url-generic-parse-url
306 (defun mm-url-insert-file-contents-external (url)
307 "Insert file contents of URL using `mm-url-program'."
309 (if (symbolp mm-url-program
)
310 (let ((item (cdr (assq mm-url-program mm-url-predefined-programs
))))
311 (setq program
(car item
)
312 args
(append (cdr item
) (list url
))))
313 (setq program mm-url-program
314 args
(append mm-url-arguments
(list url
))))
315 (unless (eq 0 (apply 'call-process program nil t nil args
))
316 (error "Couldn't fetch %s" url
))))
318 (defvar mm-url-timeout
30
319 "The number of seconds before timing out an URL fetch.")
321 (defvar mm-url-retries
10
322 "The number of retries after timing out when fetching an URL.")
324 (defun mm-url-insert (url &optional follow-refresh
)
325 "Insert the contents from an URL in the current buffer.
326 If FOLLOW-REFRESH is non-nil, redirect refresh url in META."
327 (let ((times mm-url-retries
)
331 (while (and (not (zerop (decf times
)))
333 (with-timeout (mm-url-timeout)
335 (message "Trying again (%s)..." (- mm-url-retries times
)))
339 (narrow-to-region (point) (point))
340 (mm-url-insert-file-contents url
)
341 (goto-char (point-min))
342 (when (re-search-forward
343 "<meta[ \t\r\n]*http-equiv=\"Refresh\"[^>]*URL=\\([^\"]+\\)\"" nil t
)
344 (let ((url (match-string 1)))
345 (delete-region (point-min) (point-max))
346 (setq result
(mm-url-insert url t
)))))
347 (setq result
(mm-url-insert-file-contents url
)))
351 (defun mm-url-decode-entities ()
352 "Decode all HTML entities."
353 (goto-char (point-min))
354 (while (re-search-forward "&\\(#[0-9]+\\|#x[0-9a-f]+\\|[a-z]+[0-9]*\\);"
356 (let* ((entity (match-string 1))
357 (elem (if (eq (aref entity
0) ?\
#)
359 ;; Hex number: ㈒
360 (if (eq (aref entity
1) ?x
)
361 (string-to-number (substring entity
2)
363 ;; Decimal number: 
364 (string-to-number (substring entity
1)))))
365 (setq c
(or (cdr (assq c mm-extra-numeric-entities
))
367 (if (mm-char-or-char-int-p c
) c ?
#))
368 (or (cdr (assq (intern entity
)
369 mm-url-html-entities
))
371 (unless (stringp elem
)
372 (setq elem
(char-to-string elem
)))
373 (replace-match elem t t
))))
375 (defun mm-url-decode-entities-nbsp ()
376 "Decode all HTML entities and to a space."
377 (let ((mm-url-html-entities (cons '(nbsp .
32) mm-url-html-entities
)))
378 (mm-url-decode-entities)))
380 (defun mm-url-decode-entities-string (string)
383 (mm-url-decode-entities)
386 (defun mm-url-form-encode-xwfu (chunk)
387 "Escape characters in a string for application/x-www-form-urlencoded.
388 Blasphemous crap because someone didn't think %20 was good enough for encoding
389 spaces. Die Die Die."
390 ;; This will get rid of the 'attributes' specified by the file type,
391 ;; which are useless for an application/x-www-form-urlencoded form.
393 (setq chunk
(cdr chunk
)))
399 ((memq char mm-url-unreserved-chars
) (char-to-string char
))
400 (t (upcase (format "%%%02x" char
)))))
401 (mm-encode-coding-string chunk
402 (if (fboundp 'find-coding-systems-string
)
403 (car (find-coding-systems-string chunk
))
404 buffer-file-coding-system
))
407 (defun mm-url-encode-www-form-urlencoded (pairs)
408 "Return PAIRS encoded for forms."
411 (concat (mm-url-form-encode-xwfu (car data
)) "="
412 (mm-url-form-encode-xwfu (cdr data
))))
415 (autoload 'mml-compute-boundary
"mml")
417 (defun mm-url-encode-multipart-form-data (pairs &optional boundary
)
418 "Return PAIRS encoded in multipart/form-data."
420 ;; Get a good boundary
422 (setq boundary
(mml-compute-boundary '())))
424 ;; Start with the boundary
426 ;; Create name value pairs
429 ;; Delete any returned items that are empty
431 (mapcar (lambda (data)
432 (cond ((equal (car data
) "file")
436 "Content-Disposition: form-data; name=%S; filename=%S\r\nContent-Type: text/plain; charset=utf-8\r\nContent-Transfer-Encoding: binary\r\n\r\n%s"
437 (cdr (assoc "name" (cdr data
))) (cdr (assoc "filename" (cdr data
)))
438 (cond ((stringp (cdr (assoc "filedata" (cdr data
))))
439 (cdr (assoc "filedata" (cdr data
))))
440 ((integerp (cdr (assoc "filedata" (cdr data
))))
441 (number-to-string (cdr (assoc "filedata" (cdr data
))))))))
442 ((equal (car data
) "submit")
443 "Content-Disposition: form-data; name=\"submit\"\r\n\r\nSubmit\r\n")
446 "Content-Disposition: form-data;name=%S\r\n\r\n%s\r\n"
447 (car data
) (concat (mm-url-form-encode-xwfu (cdr data
)))
450 ;; use the boundary as a separator
451 (concat "\r\n--" boundary
"\r\n"))
452 ;; put a boundary at the end.
453 "--" boundary
"--\r\n"))
455 (defun mm-url-remove-markup ()
456 "Remove all HTML markup, leaving just plain text."
457 (goto-char (point-min))
458 (while (search-forward "<!--" nil t
)
459 (delete-region (match-beginning 0)
460 (or (search-forward "-->" nil t
)
462 (goto-char (point-min))
463 (while (re-search-forward "<[^>]+>" nil t
)
464 (replace-match "" t t
)))
468 ;;; mm-url.el ends here