14
|
1 ;;; url-misc.el --- Misc Uniform Resource Locator retrieval code
|
|
2 ;; Author: wmperry
|
16
|
3 ;; Created: 1997/01/21 21:14:56
|
|
4 ;; Version: 1.9
|
14
|
5 ;; Keywords: comm, data, processes
|
|
6
|
|
7 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
8 ;;; Copyright (c) 1993 - 1996 by William M. Perry (wmperry@cs.indiana.edu)
|
16
|
9 ;;; Copyright (c) 1996, 1997 Free Software Foundation, Inc.
|
14
|
10 ;;;
|
|
11 ;;; This file is not part of GNU Emacs, but the same permissions apply.
|
|
12 ;;;
|
|
13 ;;; GNU Emacs is free software; you can redistribute it and/or modify
|
|
14 ;;; it under the terms of the GNU General Public License as published by
|
|
15 ;;; the Free Software Foundation; either version 2, or (at your option)
|
|
16 ;;; any later version.
|
|
17 ;;;
|
|
18 ;;; GNU Emacs is distributed in the hope that it will be useful,
|
|
19 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
20 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
21 ;;; GNU General Public License for more details.
|
|
22 ;;;
|
|
23 ;;; You should have received a copy of the GNU General Public License
|
|
24 ;;; along with GNU Emacs; see the file COPYING. If not, write to the
|
|
25 ;;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
26 ;;; Boston, MA 02111-1307, USA.
|
|
27 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
28
|
|
29 (require 'url-vars)
|
|
30 (require 'url-parse)
|
|
31 (autoload 'Info-goto-node "info" "" t)
|
|
32
|
|
33 (defun url-info (url)
|
|
34 ;; Fetch an info node
|
|
35 (if (get-buffer url-working-buffer)
|
|
36 (kill-buffer url-working-buffer))
|
|
37 (let* ((data (url-generic-parse-url url))
|
|
38 (fname (url-filename data))
|
|
39 (node (or (url-target data) "Top")))
|
|
40 (if (and fname node)
|
|
41 (Info-goto-node (concat "(" fname ")" node))
|
|
42 (error "Malformed url: %s" url))))
|
|
43
|
|
44 (defun url-finger (url)
|
|
45 ;; Find a finger reference
|
|
46 (setq url-current-mime-headers '(("content-type" . "text/html"))
|
|
47 url-current-mime-type "text/html")
|
|
48 (set-buffer (get-buffer-create url-working-buffer))
|
|
49 (let* ((urlobj (if (vectorp url) url
|
|
50 (url-generic-parse-url url)))
|
|
51 (host (or (url-host urlobj) "localhost"))
|
|
52 (port (or (url-port urlobj)
|
|
53 (cdr-safe (assoc "finger" url-default-ports))))
|
|
54 (user (url-unhex-string (url-filename urlobj)))
|
|
55 (proc (url-open-stream "finger" url-working-buffer host
|
|
56 (string-to-int port))))
|
|
57 (if (stringp proc)
|
|
58 (message "%s" proc)
|
|
59 (process-kill-without-query proc)
|
|
60 (if (= (string-to-char user) ?/)
|
|
61 (setq user (substring user 1 nil)))
|
|
62 (goto-char (point-min))
|
|
63 (insert "<html>\n"
|
|
64 " <head>\n"
|
|
65 " <title>Finger information for " user "@" host "</title>\n"
|
|
66 " </head>\n"
|
|
67 " <body>\n"
|
|
68 " <h1>Finger information for " user "@" host "</h1>\n"
|
|
69 " <hr>\n"
|
|
70 " <pre>\n")
|
|
71 (process-send-string proc (concat user "\r\n"))
|
|
72 (while (memq (url-process-status proc) '(run open))
|
|
73 (url-after-change-function)
|
|
74 (url-accept-process-output proc))
|
|
75 (goto-char (point-min))
|
|
76 (url-replace-regexp "^Process .* exited .*code .*$" "")
|
|
77 (goto-char (point-max))
|
|
78 (insert " </pre>\n"
|
|
79 " </body>\n"
|
|
80 "</html>\n"))))
|
|
81
|
16
|
82 (defun url-do-terminal-emulator (type server port user)
|
|
83 (terminal-emulator
|
|
84 (generate-new-buffer (format "%s%s" (if user (concat user "@") "") server))
|
|
85 (case type
|
|
86 (rlogin "rlogin")
|
|
87 (telnet "telnet")
|
|
88 (tn3270 "tn3270")
|
|
89 (otherwise
|
|
90 (error "Unknown terminal emulator required: %s" type)))
|
|
91 (if user
|
|
92 (case type
|
|
93 (rlogin
|
|
94 (list server "-l" user))
|
|
95 (telnet
|
|
96 (if user (message "Please log in as user: %s" user))
|
|
97 (if port
|
|
98 (list server port)
|
|
99 (list server)))
|
|
100 (tn3270
|
|
101 (if user (message "Please log in as user: %s" user))
|
|
102 (list server))))))
|
14
|
103
|
16
|
104 (defun url-generic-emulator-loader (url)
|
14
|
105 (if (get-buffer url-working-buffer)
|
|
106 (kill-buffer url-working-buffer))
|
16
|
107 (or (string-match "^\\([^:]+\\):/*\\(.*@\\)*\\([^/]*\\)/*" url)
|
|
108 (error "Invalid URL: %s" url))
|
|
109 (let* ((type (intern (downcase (match-string 1 url))))
|
|
110 (server (match-string 3 url))
|
|
111 (name (if (match-beginning 2)
|
|
112 (substring url (match-beginning 2) (1- (match-end 2)))))
|
|
113 (port (if (string-match ":" server)
|
14
|
114 (prog1
|
16
|
115 (substring server (match-end 0))
|
|
116 (setq server (substring server 0 (match-beginning 0)))))))
|
|
117 (url-do-terminal-emulator type server port name)))
|
14
|
118
|
16
|
119 (fset 'url-rlogin 'url-generic-emulator-loader)
|
|
120 (fset 'url-telnet 'url-generic-emulator-loader)
|
|
121 (fset 'url-tn3270 'url-generic-emulator-loader)
|
14
|
122
|
|
123 (defun url-proxy (url)
|
|
124 ;; Retrieve URL from a proxy.
|
|
125 ;; Expects `url-using-proxy' to be bound to the specific proxy to use."
|
|
126 (let (
|
|
127 (urlobj (url-generic-parse-url url))
|
|
128 (proxyobj (url-generic-parse-url url-using-proxy)))
|
|
129 (url-http url-using-proxy url)
|
|
130 (setq url-current-type (url-type urlobj)
|
|
131 url-current-user (url-user urlobj)
|
|
132 url-current-port (or (url-port urlobj)
|
|
133 (cdr-safe (assoc url-current-type
|
|
134 url-default-ports)))
|
|
135 url-current-server (url-host urlobj)
|
|
136 url-current-file (url-filename urlobj))))
|
|
137
|
|
138 (defun url-x-exec (url)
|
|
139 ;; Handle local execution of scripts.
|
|
140 (set-buffer (get-buffer-create url-working-buffer))
|
|
141 (erase-buffer)
|
|
142 (string-match "x-exec:/+\\([^/]+\\)\\(/.*\\)" url)
|
|
143 (let ((process-environment process-environment)
|
|
144 (executable (url-match url 1))
|
|
145 (path-info (url-match url 2))
|
|
146 (query-string nil)
|
|
147 (safe-paths url-local-exec-path)
|
|
148 (found nil)
|
|
149 (y nil)
|
|
150 )
|
|
151 (setq url-current-server executable
|
|
152 url-current-file path-info)
|
|
153 (if (string-match "\\(.*\\)\\?\\(.*\\)" path-info)
|
|
154 (setq query-string (url-match path-info 2)
|
|
155 path-info (url-match path-info 1)))
|
|
156 (while (and safe-paths (not found))
|
|
157 (setq y (expand-file-name executable (car safe-paths))
|
|
158 found (and (file-exists-p y) (file-executable-p y) y)
|
|
159 safe-paths (cdr safe-paths)))
|
|
160 (if (not found)
|
|
161 (url-retrieve (concat "www://error/nofile/" executable))
|
|
162 (setq process-environment
|
|
163 (append
|
|
164 (list
|
|
165 "SERVER_SOFTWARE=x-exec/1.0"
|
|
166 (concat "SERVER_NAME=" (system-name))
|
|
167 "GATEWAY_INTERFACE=CGI/1.1"
|
|
168 "SERVER_PROTOCOL=HTTP/1.0"
|
|
169 "SERVER_PORT="
|
|
170 (concat "REQUEST_METHOD=" url-request-method)
|
|
171 (concat "HTTP_ACCEPT="
|
|
172 (mapconcat
|
|
173 (function
|
|
174 (lambda (x)
|
|
175 (cond
|
|
176 ((= x ?\n) (setq y t) "")
|
|
177 ((= x ?:) (setq y nil) ",")
|
|
178 (t (char-to-string x))))) url-mime-accept-string
|
|
179 ""))
|
|
180 (concat "PATH_INFO=" (url-unhex-string path-info))
|
|
181 (concat "PATH_TRANSLATED=" (url-unhex-string path-info))
|
|
182 (concat "SCRIPT_NAME=" executable)
|
|
183 (concat "QUERY_STRING=" (url-unhex-string query-string))
|
|
184 (concat "REMOTE_HOST=" (system-name)))
|
|
185 (if (assoc "content-type" url-request-extra-headers)
|
|
186 (concat "CONTENT_TYPE=" (cdr
|
|
187 (assoc "content-type"
|
|
188 url-request-extra-headers))))
|
|
189 (if url-request-data
|
|
190 (concat "CONTENT_LENGTH=" (length url-request-data)))
|
|
191 process-environment))
|
|
192 (and url-request-data (insert url-request-data))
|
|
193 (setq y (call-process-region (point-min) (point-max) found t t))
|
|
194 (goto-char (point-min))
|
|
195 (delete-region (point) (progn (skip-chars-forward " \t\n") (point)))
|
|
196 (cond
|
|
197 ((url-mime-response-p) nil) ; Its already got an HTTP/1.0 header
|
|
198 ((null y) ; Weird exit status, whassup?
|
|
199 (insert "HTTP/1.0 404 Not Found\n"
|
|
200 "Server: " url-package-name "/x-exec\n"))
|
|
201 ((= 0 y) ; The shell command was successful
|
|
202 (insert "HTTP/1.0 200 Document follows\n"
|
|
203 "Server: " url-package-name "/x-exec\n"))
|
|
204 (t ; Non-zero exit status is bad bad bad
|
|
205 (insert "HTTP/1.0 404 Not Found\n"
|
|
206 "Server: " url-package-name "/x-exec\n"))))))
|
|
207
|
16
|
208 ;; ftp://ietf.org/internet-drafts/draft-masinter-url-data-02.txt
|
|
209 (defun url-data (url)
|
|
210 (set-buffer (get-buffer-create url-working-buffer))
|
|
211 (let ((content-type nil)
|
|
212 (encoding nil)
|
|
213 (data nil))
|
|
214 (cond
|
|
215 ((string-match "^data:\\([^;,]*\\);*\\([^,]*\\)," url)
|
|
216 (setq content-type (match-string 1 url)
|
|
217 encoding (match-string 2 url)
|
|
218 data (url-unhex-string (substring url (match-end 0))))
|
|
219 (if (= 0 (length content-type)) (setq content-type "text/plain"))
|
|
220 (if (= 0 (length encoding)) (setq encoding "8bit")))
|
|
221 (t nil))
|
|
222 (setq url-current-content-length (length data)
|
|
223 url-current-mime-type content-type
|
|
224 url-current-mime-encoding encoding
|
|
225 url-current-mime-headers (list (cons "content-type" content-type)
|
|
226 (cons "content-encoding" encoding)))
|
|
227 (and data (insert data))))
|
|
228
|
14
|
229 (provide 'url-misc)
|