| 1 | ;;; emms-url.el --- Make URL and EMMS work together well -*- lexical-binding: t; -*- |
| 2 | |
| 3 | ;; Copyright (C) 2006, 2007, 2008, 2009 Free Software Foundation, Inc. |
| 4 | |
| 5 | ;; This file is part of EMMS. |
| 6 | |
| 7 | ;; EMMS is free software; you can redistribute it and/or modify |
| 8 | ;; it under the terms of the GNU General Public License as published by |
| 9 | ;; the Free Software Foundation; either version 3, or (at your option) |
| 10 | ;; any later version. |
| 11 | ;; |
| 12 | ;; EMMS is distributed in the hope that it will be useful, |
| 13 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | ;; GNU General Public License for more details. |
| 16 | ;; |
| 17 | ;; You should have received a copy of the GNU General Public License |
| 18 | ;; along with EMMS; see the file COPYING. If not, write to the |
| 19 | ;; Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, |
| 20 | ;; Boston, MA 02110-1301, USA. |
| 21 | |
| 22 | ;;; Commentary: |
| 23 | |
| 24 | ;; These routines sanify input to URL and parse data returned by URL. |
| 25 | |
| 26 | ;;; Code: |
| 27 | |
| 28 | (require 'url) |
| 29 | (require 'emms-compat) |
| 30 | |
| 31 | (defvar emms-url-specials-entire |
| 32 | '((?\ . "%20") |
| 33 | (?\n . "%0D%0A")) |
| 34 | "*An alist of characters which must be represented specially in URLs. |
| 35 | The transformation is the key of the pair. |
| 36 | |
| 37 | This is used by `emms-url-quote-entire'.") |
| 38 | |
| 39 | (defun emms-url-quote-entire (url) |
| 40 | "Escape specials conservatively in an entire URL. |
| 41 | |
| 42 | The specials to escape are specified by the `emms-url-specials-entire' |
| 43 | variable. |
| 44 | |
| 45 | If you want to escape parts of URLs thoroughly, then use |
| 46 | `emms-url-quote' instead." |
| 47 | (apply (function concat) |
| 48 | (mapcar |
| 49 | (lambda (ch) |
| 50 | (let ((repl (assoc ch emms-url-specials-entire))) |
| 51 | (if (null repl) |
| 52 | (char-to-string ch) |
| 53 | (cdr repl)))) |
| 54 | (append url nil)))) |
| 55 | |
| 56 | (defun emms-url-quote (s &optional safe) |
| 57 | "Replace special characters in S using the `%xx' escape. |
| 58 | This is useful for escaping parts of URLs, but not entire URLs. |
| 59 | |
| 60 | Characters in [a-zA-Z_.-/] and SAFE(default is \"\") will never be |
| 61 | quoted. |
| 62 | e.g., |
| 63 | (emms-url-quote \"abc def\") => \"abc%20def\"." |
| 64 | (if (not (stringp s)) |
| 65 | "" |
| 66 | (or safe (setq safe "")) |
| 67 | (save-match-data |
| 68 | (let ((re (if (string-match "]" safe) |
| 69 | ;; `]' should be placed at the beginning inside [] |
| 70 | (format "[]a-zA-Z_.-/%s]" |
| 71 | (emms-replace-regexp-in-string "]" "" safe)) |
| 72 | (format "[a-zA-Z_.-/%s]" safe)))) |
| 73 | (mapconcat |
| 74 | (lambda (c) |
| 75 | (let ((s1 (char-to-string c))) |
| 76 | (if (string-match re s1) |
| 77 | s1 |
| 78 | (format "%%%02x" c)))) |
| 79 | (string-to-list (encode-coding-string s 'utf-8)) |
| 80 | ""))))) |
| 81 | |
| 82 | (defun emms-url-quote-plus (s &optional safe) |
| 83 | "Run (emms-url-quote s \" \"), then replace ` ' with `+'." |
| 84 | (emms-replace-regexp-in-string |
| 85 | " " "+" (emms-url-quote s (concat safe " ")))) |
| 86 | |
| 87 | (defun emms-url-quote-underscore (s &optional safe) |
| 88 | "Run (emms-url-quote s \" \"), then replace ` ' with `_'." |
| 89 | (emms-replace-regexp-in-string |
| 90 | " " "_" (emms-url-quote s (concat safe " ")))) |
| 91 | |
| 92 | (defun emms-http-content-coding () |
| 93 | (save-match-data |
| 94 | (and (boundp 'url-http-content-type) |
| 95 | (stringp url-http-content-type) |
| 96 | (string-match ";\\s-*charset=\\([^;[:space:]]+\\)" |
| 97 | url-http-content-type) |
| 98 | (intern-soft (downcase (match-string 1 url-http-content-type)))))) |
| 99 | |
| 100 | (defun emms-http-decode-buffer (&optional buffer) |
| 101 | "Recode the buffer with `url-retrieve's contents. Else the |
| 102 | buffer would contain multibyte chars like \\123\\456." |
| 103 | (with-current-buffer (or buffer (current-buffer)) |
| 104 | (let* ((default (or (car default-process-coding-system) 'utf-8)) |
| 105 | (coding (or (emms-http-content-coding) default))) |
| 106 | (when coding |
| 107 | ;; (pop-to-buffer (current-buffer)) |
| 108 | ;; (message "content-type: %s" url-http-content-type) |
| 109 | ;; (message "coding: %S [default: %S]" coding default) |
| 110 | (set-buffer-multibyte t) |
| 111 | (decode-coding-region (point-min) (point-max) coding))))) |
| 112 | |
| 113 | (provide 'emms-url) |
| 114 | ;;; emms-url.el ends here |