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