mm-url.el 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. ;;; mm-url.el --- a wrapper of url functions/commands for Gnus
  2. ;; Copyright (C) 2001-2015 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 code is 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. (require 'url)))
  243. ;;;###autoload
  244. (defun mm-url-insert-file-contents (url)
  245. "Insert file contents of URL.
  246. If `mm-url-use-external' is non-nil, use `mm-url-program'."
  247. (if mm-url-use-external
  248. (progn
  249. (if (string-match "^file:/+" url)
  250. (insert-file-contents (substring url (1- (match-end 0))))
  251. (mm-url-insert-file-contents-external url))
  252. (goto-char (point-min))
  253. (if (fboundp 'url-generic-parse-url)
  254. (setq url-current-object
  255. (url-generic-parse-url url)))
  256. (list url (buffer-size)))
  257. (mm-url-load-url)
  258. (let ((name buffer-file-name)
  259. (url-request-extra-headers
  260. ;; ISTM setting a Connection header was a workaround for
  261. ;; older versions of url included with w3, but it does more
  262. ;; harm than good with the one shipped with Emacs. --ansel
  263. (if (not (and (boundp 'url-version)
  264. (equal url-version "Emacs")))
  265. (list (cons "Connection" "Close"))))
  266. result)
  267. (setq result (url-insert-file-contents url))
  268. (save-excursion
  269. (goto-char (point-min))
  270. (while (re-search-forward "\r 1000\r ?" nil t)
  271. (replace-match "")))
  272. (setq buffer-file-name name)
  273. (if (and (fboundp 'url-generic-parse-url)
  274. (listp result))
  275. (setq url-current-object (url-generic-parse-url
  276. (car result))))
  277. result)))
  278. ;;;###autoload
  279. (defun mm-url-insert-file-contents-external (url)
  280. "Insert file contents of URL using `mm-url-program'."
  281. (let (program args)
  282. (if (symbolp mm-url-program)
  283. (let ((item (cdr (assq mm-url-program mm-url-predefined-programs))))
  284. (setq program (car item)
  285. args (append (cdr item) (list url))))
  286. (setq program mm-url-program
  287. args (append mm-url-arguments (list url))))
  288. (unless (eq 0 (apply 'call-process program nil t nil args))
  289. (error "Couldn't fetch %s" url))))
  290. (defvar mm-url-timeout 30
  291. "The number of seconds before timing out an URL fetch.")
  292. (defvar mm-url-retries 10
  293. "The number of retries after timing out when fetching an URL.")
  294. (defun mm-url-insert (url &optional follow-refresh)
  295. "Insert the contents from an URL in the current buffer.
  296. If FOLLOW-REFRESH is non-nil, redirect refresh url in META."
  297. (let ((times mm-url-retries)
  298. (done nil)
  299. (first t)
  300. result)
  301. (while (and (not (zerop (decf times)))
  302. (not done))
  303. (with-timeout (mm-url-timeout)
  304. (unless first
  305. (message "Trying again (%s)..." (- mm-url-retries times)))
  306. (setq first nil)
  307. (if follow-refresh
  308. (save-restriction
  309. (narrow-to-region (point) (point))
  310. (mm-url-insert-file-contents url)
  311. (goto-char (point-min))
  312. (when (re-search-forward
  313. "<meta[ \t\r\n]*http-equiv=\"Refresh\"[^>]*URL=\\([^\"]+\\)\"" nil t)
  314. (let ((url (match-string 1)))
  315. (delete-region (point-min) (point-max))
  316. (setq result (mm-url-insert url t)))))
  317. (setq result (mm-url-insert-file-contents url)))
  318. (setq done t)))
  319. result))
  320. (defun mm-url-decode-entities ()
  321. "Decode all HTML entities."
  322. (goto-char (point-min))
  323. (while (re-search-forward "&\\(#[0-9]+\\|#x[0-9a-f]+\\|[a-z]+[0-9]*\\);"
  324. nil t)
  325. (let* ((entity (match-string 1))
  326. (elem (if (eq (aref entity 0) ?\#)
  327. (let ((c
  328. ;; Hex number: &#x3212
  329. (if (eq (aref entity 1) ?x)
  330. (string-to-number (substring entity 2)
  331. 16)
  332. ;; Decimal number: &#23
  333. (string-to-number (substring entity 1)))))
  334. (setq c (or (cdr (assq c mm-extra-numeric-entities))
  335. (mm-ucs-to-char c)))
  336. (if (mm-char-or-char-int-p c) c ?#))
  337. (or (cdr (assq (intern entity)
  338. mm-url-html-entities))
  339. ?#))))
  340. (unless (stringp elem)
  341. (setq elem (char-to-string elem)))
  342. (replace-match elem t t))))
  343. (defun mm-url-decode-entities-nbsp ()
  344. "Decode all HTML entities and &nbsp; to a space."
  345. (let ((mm-url-html-entities (cons '(nbsp . 32) mm-url-html-entities)))
  346. (mm-url-decode-entities)))
  347. (defun mm-url-decode-entities-string (string)
  348. (with-temp-buffer
  349. (insert string)
  350. (mm-url-decode-entities)
  351. (buffer-string)))
  352. (defun mm-url-form-encode-xwfu (chunk)
  353. "Escape characters in a string for application/x-www-form-urlencoded.
  354. Blasphemous crap because someone didn't think %20 was good enough for encoding
  355. spaces. Die Die Die."
  356. ;; This will get rid of the 'attributes' specified by the file type,
  357. ;; which are useless for an application/x-www-form-urlencoded form.
  358. (if (consp chunk)
  359. (setq chunk (cdr chunk)))
  360. (if chunk
  361. (mapconcat
  362. (lambda (char)
  363. (cond
  364. ((= char ? ) "+")
  365. ((memq char mm-url-unreserved-chars) (char-to-string char))
  366. (t (upcase (format "%%%02x" char)))))
  367. (mm-encode-coding-string chunk
  368. (if (fboundp 'find-coding-systems-string)
  369. (car (find-coding-systems-string chunk))
  370. buffer-file-coding-system))
  371. "")))
  372. (defun mm-url-encode-www-form-urlencoded (pairs)
  373. "Return PAIRS encoded for forms."
  374. (mapconcat
  375. (lambda (data)
  376. (concat (mm-url-form-encode-xwfu (car data)) "="
  377. (mm-url-form-encode-xwfu (cdr data))))
  378. pairs "&"))
  379. (autoload 'mml-compute-boundary "mml")
  380. (defun mm-url-encode-multipart-form-data (pairs &optional boundary)
  381. "Return PAIRS encoded in multipart/form-data."
  382. ;; RFC1867
  383. ;; Get a good boundary
  384. (unless boundary
  385. (setq boundary (mml-compute-boundary '())))
  386. (concat
  387. ;; Start with the boundary
  388. "--" boundary "\r\n"
  389. ;; Create name value pairs
  390. (mapconcat
  391. 'identity
  392. ;; Delete any returned items that are empty
  393. (delq nil
  394. (mapcar (lambda (data)
  395. (cond ((equal (car data) "file")
  396. ;; For each pair
  397. (format
  398. ;; Encode the name
  399. "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"
  400. (cdr (assoc "name" (cdr data))) (cdr (assoc "filename" (cdr data)))
  401. (cond ((stringp (cdr (assoc "filedata" (cdr data))))
  402. (cdr (assoc "filedata" (cdr data))))
  403. ((integerp (cdr (assoc "filedata" (cdr data))))
  404. (number-to-string (cdr (assoc "filedata" (cdr data))))))))
  405. ((equal (car data) "submit")
  406. "Content-Disposition: form-data; name=\"submit\"\r\n\r\nSubmit\r\n")
  407. (t
  408. (format
  409. "Content-Disposition: form-data;name=%S\r\n\r\n%s\r\n"
  410. (car data) (concat (mm-url-form-encode-xwfu (cdr data)))
  411. ))))
  412. pairs))
  413. ;; use the boundary as a separator
  414. (concat "\r\n--" boundary "\r\n"))
  415. ;; put a boundary at the end.
  416. "--" boundary "--\r\n"))
  417. (defun mm-url-remove-markup ()
  418. "Remove all HTML markup, leaving just plain text."
  419. (goto-char (point-min))
  420. (while (search-forward "<!--" nil t)
  421. (delete-region (match-beginning 0)
  422. (or (search-forward "-->" nil t)
  423. (point-max))))
  424. (goto-char (point-min))
  425. (while (re-search-forward "<[^>]+>" nil t)
  426. (replace-match "" t t)))
  427. (provide 'mm-url)
  428. ;;; mm-url.el ends here