mm-url.el 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. ;;; mm-url.el --- a wrapper of url functions/commands for Gnus
  2. ;; Copyright (C) 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
  4. ;; This file is part of GNU Emacs.
  5. ;; GNU Emacs is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; GNU Emacs is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;; Some codes are stolen from w3 and url packages. Some are moved from
  17. ;; nnweb.
  18. ;; TODO: Support POST, cookie.
  19. ;;; Code:
  20. (eval-when-compile (require 'cl))
  21. (require 'mm-util)
  22. (require 'gnus)
  23. (defvar url-current-object)
  24. (defvar url-package-name)
  25. (defvar url-package-version)
  26. (defgroup mm-url nil
  27. "A wrapper of url package and external url command for Gnus."
  28. :group 'gnus)
  29. (defcustom mm-url-use-external (not
  30. (condition-case nil
  31. (require 'url)
  32. (error nil)))
  33. "*If non-nil, use external grab program `mm-url-program'."
  34. :version "22.1"
  35. :type 'boolean
  36. :group 'mm-url)
  37. (defvar mm-url-predefined-programs
  38. '((wget "wget" "--user-agent=mm-url" "-q" "-O" "-")
  39. (w3m "w3m" "-dump_source")
  40. (lynx "lynx" "-source")
  41. (curl "curl" "--silent" "--user-agent" "mm-url" "--location")))
  42. (defcustom mm-url-program
  43. (cond
  44. ((executable-find "wget") 'wget)
  45. ((executable-find "w3m") 'w3m)
  46. ((executable-find "lynx") 'lynx)
  47. ((executable-find "curl") 'curl)
  48. (t "GET"))
  49. "The url grab program.
  50. Likely values are `wget', `w3m', `lynx' and `curl'."
  51. :version "22.1"
  52. :type '(choice
  53. (symbol :tag "wget" wget)
  54. (symbol :tag "w3m" w3m)
  55. (symbol :tag "lynx" lynx)
  56. (symbol :tag "curl" curl)
  57. (string :tag "other"))
  58. :group 'mm-url)
  59. (defcustom mm-url-arguments nil
  60. "The arguments for `mm-url-program'."
  61. :version "22.1"
  62. :type '(repeat string)
  63. :group 'mm-url)
  64. ;;; Internal variables
  65. ;; Stolen from w3.
  66. (defvar mm-url-html-entities
  67. '(
  68. ;;(excl . 33)
  69. (quot . 34)
  70. ;;(num . 35)
  71. ;;(dollar . 36)
  72. ;;(percent . 37)
  73. (amp . 38)
  74. (rsquo . 39) ; should be U+8217
  75. ;;(apos . 39)
  76. ;;(lpar . 40)
  77. ;;(rpar . 41)
  78. ;;(ast . 42)
  79. ;;(plus . 43)
  80. ;;(comma . 44)
  81. ;;(period . 46)
  82. ;;(colon . 58)
  83. ;;(semi . 59)
  84. (lt . 60)
  85. ;;(equals . 61)
  86. (gt . 62)
  87. ;;(quest . 63)
  88. ;;(commat . 64)
  89. ;;(lsqb . 91)
  90. ;;(rsqb . 93)
  91. (uarr . 94) ; should be U+8593
  92. ;;(lowbar . 95)
  93. (lsquo . 96) ; should be U+8216
  94. (lcub . 123)
  95. ;;(verbar . 124)
  96. (rcub . 125)
  97. (tilde . 126)
  98. (nbsp . 160)
  99. (iexcl . 161)
  100. (cent . 162)
  101. (pound . 163)
  102. (curren . 164)
  103. (yen . 165)
  104. (brvbar . 166)
  105. (sect . 167)
  106. (uml . 168)
  107. (copy . 169)
  108. (ordf . 170)
  109. (laquo . 171)
  110. (not . 172)
  111. (shy . 173)
  112. (reg . 174)
  113. (macr . 175)
  114. (deg . 176)
  115. (plusmn . 177)
  116. (sup2 . 178)
  117. (sup3 . 179)
  118. (acute . 180)
  119. (micro . 181)
  120. (para . 182)
  121. (middot . 183)
  122. (cedil . 184)
  123. (sup1 . 185)
  124. (ordm . 186)
  125. (raquo . 187)
  126. (frac14 . 188)
  127. (frac12 . 189)
  128. (frac34 . 190)
  129. (iquest . 191)
  130. (Agrave . 192)
  131. (Aacute . 193)
  132. (Acirc . 194)
  133. (Atilde . 195)
  134. (Auml . 196)
  135. (Aring . 197)
  136. (AElig . 198)
  137. (Ccedil . 199)
  138. (Egrave . 200)
  139. (Eacute . 201)
  140. (Ecirc . 202)
  141. (Euml . 203)
  142. (Igrave . 204)
  143. (Iacute . 205)
  144. (Icirc . 206)
  145. (Iuml . 207)
  146. (ETH . 208)
  147. (Ntilde . 209)
  148. (Ograve . 210)
  149. (Oacute . 211)
  150. (Ocirc . 212)
  151. (Otilde . 213)
  152. (Ouml . 214)
  153. (times . 215)
  154. (Oslash . 216)
  155. (Ugrave . 217)
  156. (Uacute . 218)
  157. (Ucirc . 219)
  158. (Uuml . 220)
  159. (Yacute . 221)
  160. (THORN . 222)
  161. (szlig . 223)
  162. (agrave . 224)
  163. (aacute . 225)
  164. (acirc . 226)
  165. (atilde . 227)
  166. (auml . 228)
  167. (aring . 229)
  168. (aelig . 230)
  169. (ccedil . 231)
  170. (egrave . 232)
  171. (eacute . 233)
  172. (ecirc . 234)
  173. (euml . 235)
  174. (igrave . 236)
  175. (iacute . 237)
  176. (icirc . 238)
  177. (iuml . 239)
  178. (eth . 240)
  179. (ntilde . 241)
  180. (ograve . 242)
  181. (oacute . 243)
  182. (ocirc . 244)
  183. (otilde . 245)
  184. (ouml . 246)
  185. (divide . 247)
  186. (oslash . 248)
  187. (ugrave . 249)
  188. (uacute . 250)
  189. (ucirc . 251)
  190. (uuml . 252)
  191. (yacute . 253)
  192. (thorn . 254)
  193. (yuml . 255)
  194. ;; Special handling of these
  195. (frac56 . "5/6")
  196. (frac16 . "1/6")
  197. (frac45 . "4/5")
  198. (frac35 . "3/5")
  199. (frac25 . "2/5")
  200. (frac15 . "1/5")
  201. (frac23 . "2/3")
  202. (frac13 . "1/3")
  203. (frac78 . "7/8")
  204. (frac58 . "5/8")
  205. (frac38 . "3/8")
  206. (frac18 . "1/8")
  207. ;; The following 5 entities are not mentioned in the HTML 2.0
  208. ;; standard, nor in any other HTML proposed standard of which I
  209. ;; am aware. I am not even sure they are ISO entity names. ***
  210. ;; Hence, some arrangement should be made to give a bad HTML
  211. ;; message when they are seen.
  212. (ndash . 45)
  213. (mdash . 45)
  214. (emsp . 32)
  215. (ensp . 32)
  216. (sim . 126)
  217. (le . "<=")
  218. (agr . "alpha")
  219. (rdquo . "''")
  220. (ldquo . "``")
  221. (trade . "(TM)")
  222. ;; To be done
  223. ;; (shy . ????) ; soft hyphen
  224. )
  225. "*An assoc list of entity names and how to actually display them.")
  226. (defconst mm-url-unreserved-chars
  227. '(
  228. ?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
  229. ?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
  230. ?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9
  231. ?- ?_ ?. ?! ?~ ?* ?' ?\( ?\))
  232. "A list of characters that are _NOT_ reserved in the URL spec.
  233. This is taken from RFC 2396.")
  234. (defun mm-url-load-url ()
  235. "Load `url-insert-file-contents'."
  236. (unless (condition-case ()
  237. (progn
  238. (require 'url-handlers)
  239. (require 'url-parse)
  240. (require 'url-vars))
  241. (error nil))
  242. ;; w3-4.0pre0.46 or earlier version.
  243. (require 'w3-vars)
  244. (require 'url)))
  245. ;;;###autoload
  246. (defun mm-url-insert-file-contents (url)
  247. "Insert file contents of URL.
  248. If `mm-url-use-external' is non-nil, use `mm-url-program'."
  249. (if mm-url-use-external
  250. (progn
  251. (if (string-match "^file:/+" url)
  252. (insert-file-contents (substring url (1- (match-end 0))))
  253. (mm-url-insert-file-contents-external url))
  254. (goto-char (point-min))
  255. (if (fboundp 'url-generic-parse-url)
  256. (setq url-current-object
  257. (url-generic-parse-url url)))
  258. (list url (buffer-size)))
  259. (mm-url-load-url)
  260. (let ((name buffer-file-name)
  261. (url-request-extra-headers
  262. ;; ISTM setting a Connection header was a workaround for
  263. ;; older versions of url included with w3, but it does more
  264. ;; harm than good with the one shipped with Emacs. --ansel
  265. (if (not (and (boundp 'url-version)
  266. (equal url-version "Emacs")))
  267. (list (cons "Connection" "Close"))))
  268. result)
  269. (setq result (url-insert-file-contents url))
  270. (save-excursion
  271. (goto-char (point-min))
  272. (while (re-search-forward "\r 1000\r ?" nil t)
  273. (replace-match "")))
  274. (setq buffer-file-name name)
  275. (if (and (fboundp 'url-generic-parse-url)
  276. (listp result))
  277. (setq url-current-object (url-generic-parse-url
  278. (car result))))
  279. result)))
  280. ;;;###autoload
  281. (defun mm-url-insert-file-contents-external (url)
  282. "Insert file contents of URL using `mm-url-program'."
  283. (let (program args)
  284. (if (symbolp mm-url-program)
  285. (let ((item (cdr (assq mm-url-program mm-url-predefined-programs))))
  286. (setq program (car item)
  287. args (append (cdr item) (list url))))
  288. (setq program mm-url-program
  289. args (append mm-url-arguments (list url))))
  290. (unless (eq 0 (apply 'call-process program nil t nil args))
  291. (error "Couldn't fetch %s" url))))
  292. (defvar mm-url-timeout 30
  293. "The number of seconds before timing out an URL fetch.")
  294. (defvar mm-url-retries 10
  295. "The number of retries after timing out when fetching an URL.")
  296. (defun mm-url-insert (url &optional follow-refresh)
  297. "Insert the contents from an URL in the current buffer.
  298. If FOLLOW-REFRESH is non-nil, redirect refresh url in META."
  299. (let ((times mm-url-retries)
  300. (done nil)
  301. (first t)
  302. result)
  303. (while (and (not (zerop (decf times)))
  304. (not done))
  305. (with-timeout (mm-url-timeout)
  306. (unless first
  307. (message "Trying again (%s)..." (- mm-url-retries times)))
  308. (setq first nil)
  309. (if follow-refresh
  310. (save-restriction
  311. (narrow-to-region (point) (point))
  312. (mm-url-insert-file-contents url)
  313. (goto-char (point-min))
  314. (when (re-search-forward
  315. "<meta[ \t\r\n]*http-equiv=\"Refresh\"[^>]*URL=\\([^\"]+\\)\"" nil t)
  316. (let ((url (match-string 1)))
  317. (delete-region (point-min) (point-max))
  318. (setq result (mm-url-insert url t)))))
  319. (setq result (mm-url-insert-file-contents url)))
  320. (setq done t)))
  321. result))
  322. (defun mm-url-decode-entities ()
  323. "Decode all HTML entities."
  324. (goto-char (point-min))
  325. (while (re-search-forward "&\\(#[0-9]+\\|#x[0-9a-f]+\\|[a-z]+[0-9]*\\);"
  326. nil t)
  327. (let* ((entity (match-string 1))
  328. (elem (if (eq (aref entity 0) ?\#)
  329. (let ((c
  330. ;; Hex number: &#x3212
  331. (if (eq (aref entity 1) ?x)
  332. (string-to-number (substring entity 2)
  333. 16)
  334. ;; Decimal number: &#23
  335. (string-to-number (substring entity 1)))))
  336. (setq c (or (cdr (assq c mm-extra-numeric-entities))
  337. (mm-ucs-to-char c)))
  338. (if (mm-char-or-char-int-p c) c ?#))
  339. (or (cdr (assq (intern entity)
  340. mm-url-html-entities))
  341. ?#))))
  342. (unless (stringp elem)
  343. (setq elem (char-to-string elem)))
  344. (replace-match elem t t))))
  345. (defun mm-url-decode-entities-nbsp ()
  346. "Decode all HTML entities and &nbsp; to a space."
  347. (let ((mm-url-html-entities (cons '(nbsp . 32) mm-url-html-entities)))
  348. (mm-url-decode-entities)))
  349. (defun mm-url-decode-entities-string (string)
  350. (with-temp-buffer
  351. (insert string)
  352. (mm-url-decode-entities)
  353. (buffer-string)))
  354. (defun mm-url-form-encode-xwfu (chunk)
  355. "Escape characters in a string for application/x-www-form-urlencoded.
  356. Blasphemous crap because someone didn't think %20 was good enough for encoding
  357. spaces. Die Die Die."
  358. ;; This will get rid of the 'attributes' specified by the file type,
  359. ;; which are useless for an application/x-www-form-urlencoded form.
  360. (if (consp chunk)
  361. (setq chunk (cdr chunk)))
  362. (mapconcat
  363. (lambda (char)
  364. (cond
  365. ((= char ? ) "+")
  366. ((memq char mm-url-unreserved-chars) (char-to-string char))
  367. (t (upcase (format "%%%02x" char)))))
  368. (mm-encode-coding-string chunk
  369. (if (fboundp 'find-coding-systems-string)
  370. (car (find-coding-systems-string chunk))
  371. buffer-file-coding-system))
  372. ""))
  373. (defun mm-url-encode-www-form-urlencoded (pairs)
  374. "Return PAIRS encoded for forms."
  375. (mapconcat
  376. (lambda (data)
  377. (concat (mm-url-form-encode-xwfu (car data)) "="
  378. (mm-url-form-encode-xwfu (cdr data))))
  379. pairs "&"))
  380. (autoload 'mml-compute-boundary "mml")
  381. (defun mm-url-encode-multipart-form-data (pairs &optional boundary)
  382. "Return PAIRS encoded in multipart/form-data."
  383. ;; RFC1867
  384. ;; Get a good boundary
  385. (unless boundary
  386. (setq boundary (mml-compute-boundary '())))
  387. (concat
  388. ;; Start with the boundary
  389. "--" boundary "\r\n"
  390. ;; Create name value pairs
  391. (mapconcat
  392. 'identity
  393. ;; Delete any returned items that are empty
  394. (delq nil
  395. (mapcar (lambda (data)
  396. (when (car data)
  397. ;; For each pair
  398. (concat
  399. ;; Encode the name
  400. "Content-Disposition: form-data; name=\""
  401. (car data) "\"\r\n"
  402. "Content-Type: text/plain; charset=utf-8\r\n"
  403. "Content-Transfer-Encoding: binary\r\n\r\n"
  404. (cond ((stringp (cdr data))
  405. (cdr data))
  406. ((integerp (cdr data))
  407. (int-to-string (cdr data))))
  408. "\r\n")))
  409. pairs))
  410. ;; use the boundary as a separator
  411. (concat "--" boundary "\r\n"))
  412. ;; put a boundary at the end.
  413. "--" boundary "--\r\n"))
  414. (defun mm-url-fetch-form (url pairs)
  415. "Fetch a form from URL with PAIRS as the data using the POST method."
  416. (mm-url-load-url)
  417. (let ((url-request-data (mm-url-encode-www-form-urlencoded pairs))
  418. (url-request-method "POST")
  419. (url-request-extra-headers
  420. '(("Content-type" . "application/x-www-form-urlencoded"))))
  421. (url-insert-file-contents url)
  422. (setq buffer-file-name nil))
  423. t)
  424. (defun mm-url-fetch-simple (url content)
  425. (mm-url-load-url)
  426. (let ((url-request-data content)
  427. (url-request-method "POST")
  428. (url-request-extra-headers
  429. '(("Content-type" . "application/x-www-form-urlencoded"))))
  430. (url-insert-file-contents url)
  431. (setq buffer-file-name nil))
  432. t)
  433. (defun mm-url-remove-markup ()
  434. "Remove all HTML markup, leaving just plain text."
  435. (goto-char (point-min))
  436. (while (search-forward "<!--" nil t)
  437. (delete-region (match-beginning 0)
  438. (or (search-forward "-->" nil t)
  439. (point-max))))
  440. (goto-char (point-min))
  441. (while (re-search-forward "<[^>]+>" nil t)
  442. (replace-match "" t t)))
  443. (provide 'mm-url)
  444. ;;; mm-url.el ends here