Merge branch 'master' into comment-cache
[emacs.git] / lisp / url / url-misc.el
blob3515febba205e61038f48ba57a7adcd559a98e51
1 ;;; url-misc.el --- Misc Uniform Resource Locator retrieval code
3 ;; Copyright (C) 1996-1999, 2002, 2004-2017 Free Software Foundation,
4 ;; Inc.
6 ;; Keywords: comm, data, processes
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Code:
25 (require 'url-vars)
26 (require 'url-parse)
27 (declare-function mm-disable-multibyte "mm-util" ())
28 (autoload 'Info-goto-node "info" "" t)
29 (autoload 'man "man" nil t)
31 ;;;###autoload
32 (defun url-man (url)
33 "Fetch a Unix manual page URL."
34 (man (url-filename url))
35 nil)
37 ;;;###autoload
38 (defun url-info (url)
39 "Fetch a GNU Info URL."
40 ;; Fetch an info node
41 (let* ((fname (url-filename url))
42 (node (url-unhex-string (or (url-target url) "Top"))))
43 (if (and fname node)
44 (Info-goto-node (concat "(" fname ")" node))
45 (error "Malformed url: %s" (url-recreate-url url)))
46 nil))
48 (defun url-do-terminal-emulator (type server port user)
49 (switch-to-buffer
50 (apply
51 'make-term
52 (format "%s%s" (if user (concat user "@") "") server)
53 (cond ((eq type 'rlogin) "rlogin")
54 ((eq type 'telnet) "telnet")
55 ((eq type 'tn3270) "tn3270")
56 (t (error "Unknown terminal emulator required: %s" type)))
57 nil
58 (cond ((eq type 'rlogin)
59 (if user (list server "-l" user) (list server)))
60 ((eq type 'telnet)
61 (if port (list server port) (list server)))
62 ((eq type 'tn3270)
63 (list server))))))
65 ;;;###autoload
66 (defun url-generic-emulator-loader (url)
67 (let* ((type (intern (downcase (url-type url))))
68 (server (url-host url))
69 (name (url-user url))
70 (port (number-to-string (url-port url))))
71 (url-do-terminal-emulator type server port name))
72 nil)
74 ;;;###autoload
75 (defalias 'url-rlogin 'url-generic-emulator-loader)
76 ;;;###autoload
77 (defalias 'url-telnet 'url-generic-emulator-loader)
78 ;;;###autoload
79 (defalias 'url-tn3270 'url-generic-emulator-loader)
81 ;; RFC 2397
82 ;;;###autoload
83 (defun url-data (url)
84 "Fetch a data URL (RFC 2397)."
85 (let ((mediatype nil)
86 ;; The mediatype may need to be hex-encoded too -- see the RFC.
87 (desc (url-unhex-string (url-filename url)))
88 (encoding "8bit")
89 (data nil))
90 (save-excursion
91 (if (not (string-match "\\([^,]*\\)?," desc))
92 (error "Malformed data URL: %s" desc)
93 (setq mediatype (match-string 1 desc)
94 data (url-unhex-string (substring desc (match-end 0))))
95 (if (and mediatype (string-match ";base64\\'" mediatype))
96 (setq mediatype (substring mediatype 0 (match-beginning 0))
97 encoding "base64"))
98 (if (or (null mediatype)
99 (eq ?\; (aref mediatype 0)))
100 (setq mediatype (concat "text/plain" mediatype))))
101 (set-buffer (generate-new-buffer " *url-data*"))
102 (mm-disable-multibyte)
103 (insert (format "Content-Length: %d\n" (length data))
104 "Content-Type: " mediatype "\n"
105 "Content-Transfer-Encoding: " encoding "\n"
106 "\n")
107 (if data (insert data))
108 (current-buffer))))
110 (provide 'url-misc)
112 ;;; url-misc.el ends here