aboutsummaryrefslogtreecommitdiff
path: root/emms-url.el
blob: aee894d2e11efd1f2900fd6de201a430c08de063 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
;;; emms-url.el --- Make URL and EMMS work together well

;; Copyright (C) 2006, 2007 Free Software Foundation, Inc.

;; This file is part of EMMS.

;; EMMS is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;;
;; EMMS is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with EMMS; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
;; Boston, MA 02110-1301, USA.

;;; Commentary:

;; These routines sanify input to URL and parse data returned by URL.

;;; Code:

(require 'url)
(require 'emms-compat)

(defun emms-url-quote (s &optional safe)
  "Replace special characters in S using the `%xx' escape.
This is useful for escaping parts of URLs, but not entire URLs.

Characters in [a-zA-Z_.-/] and SAFE(default is \"\") will never be
quoted.
e.g.,
    (url-quote \"abc def\") => \"abc%20def\"."
  (or safe (setq safe ""))
  (save-match-data
    (mapconcat (lambda (c)
                 (if (if (string-match "]" safe)
                         ;; ] should be place at the beginning inside []
                         (string-match
                          (format "[]a-zA-Z_.-/%s]"
                                  (emms-replace-regexp-in-string "]" "" safe))
                          (char-to-string c))
                       (string-match (format "[a-zA-Z_.-/%s]" safe)
                                     (char-to-string c)))
                     (char-to-string c)
                   (format "%%%02x" c)))
               (string-to-list (encode-coding-string s 'utf-8))
               "")))

(defun emms-url-quote-plus (s &optional safe)
  "Run (emms-url-quote s \" \"), then replace ` ' with `+'."
  (emms-replace-regexp-in-string
   " " "+" (emms-url-quote s (concat safe " "))))

(defun emms-http-content-coding ()
  (save-match-data
    (and (boundp 'url-http-content-type)
         (stringp url-http-content-type)
         (string-match ";\\s-*charset=\\([^;[:space:]]+\\)"
                       url-http-content-type)
         (intern-soft (downcase (match-string 1 url-http-content-type))))))

(defun emms-http-decode-buffer (&optional buffer)
  "Recode the buffer with `url-retrieve's contents. Else the
buffer would contain multibyte chars like \\123\\456."
  (with-current-buffer (or buffer (current-buffer))
    (let* ((default (or (car default-process-coding-system) 'utf-8))
           (coding  (or (emms-http-content-coding) default)))
      (when coding
        ;; (pop-to-buffer (current-buffer))
        ;; (message "content-type: %s" url-http-content-type)
        ;; (message "coding: %S [default: %S]" coding default)
        (set-buffer-multibyte t)
        (decode-coding-region (point-min) (point-max) coding)))))

(provide 'emms-url)
;;; emms-url.el ends here