-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
wget.el
202 lines (139 loc) · 5.89 KB
/
wget.el
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
;; Copyright (C) 1997-2000 Free Software Foundation, Inc.
;; Author: Kevin A. Burton ([email protected])
;; Maintainer: Kevin A. Burton ([email protected])
;; Location: http://relativity.yi.org
;; Keywords: wget
;; Version: 1.1.0
;; This file is [not yet] part of GNU Emacs.
;; This program 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 2 of the License, or any later version.
;;
;; This program 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
;; this program; if not, write to the Free Software Foundation, Inc., 59 Temple
;; Place - Suite 330, Boston, MA 02111-1307, USA.
;;; History:
;;
;; - Fri Nov 02 2001 01:43 AM ([email protected]): it now uses 100%
;; elisp.. IE no external wget binary needed.
;;
;; - Tue Oct 30 2001 08:22 PM ([email protected]): enable html-mode if we
;; are viewing an html file.
;;
;; - Mon Oct 29 2001 07:04 PM ([email protected]): modernized for Emacs 21.
;; Version 1.0.1:
;;
;; - Correctly paying attention to stderr. Invalid URLs will not be detected
;; and the user notified.
;;
;; - Create a new *wget* buffer for each new URL and dont' reuse the same
;; buffer.
;;
;; - supports --save-headers from wget
;;
;; - http:// is now specified as the init for input
;;
;; - have wget-mode that highlights the firsts
;; lines of the buffer until the regexp ^$
;; Version 1.0:
;; init.
;;; Commentary:
;; This is an interface for wget. Basically it allows you to pull down URLs and
;; then view the output in a buffer. This is a too to help develop/debug web
;; applications.
;;; TODO:
;; - This is NOT synchronous AKA if we have a server that is remove and times
;; out Emacs will allow the user to type things and the metaphor becomes VERY
;; confusing! This needs to be fixed!
;;
;; - FIXME: why are we getting ^M chars in the output?
;;
;; - I think I need to set my character conversion
;; - include a mechanism to support posting content to a webserver. This would
;; prompt for the URL, method (get|post), and any specific HTTP headers to send.
;; It should save this URL and all its params in memory for future use. The the
;; user can repeat this again.
(require 'cl)
;;; Code:
(defface wget-headers-face '((t (:foreground "GoldenRod" :bold t)))
"Face used to highlight the HTTP headers in the buffer.")
(defvar wget-current-url nil "Current URL for local buffer.")
(make-variable-buffer-local 'wget-current-url)
(defun wget(url)
"Fetch the source for the given URL using wget and view it in a buffer."
(interactive
(list
(read-string "URL: " "")))
(let((inhibit-read-only t)
(buffer-name nil)
(hostname nil)
(protocol nil)
(port nil)
(stream nil))
(save-excursion
(setq buffer-name (concat "*wget " url "*"))
(if (get-buffer buffer-name)
(kill-buffer (get-buffer buffer-name)))
(set-buffer (get-buffer-create buffer-name))
(message "Fetching %s..." url)
(assert (string-match "^\\(.*\\)://\\([^/:]+\\)\\(:\\([0-9]+\\)\\)?" url)
nil "Unable to determine protocol and host.")
(setq protocol (match-string 1 url))
(when (null protocol)
(error "Unable to determine protocol"))
(setq host (match-string 2 url))
(setq port (match-string 4 url))
(if (string-equal protocol "http")
(progn
;;provide a default port if we need it.
(if (null port)
(setq port 80)))
(error (format "Unknown or unsupported protocol '%s'" protocol)))
(setq stream (open-network-stream "wget" buffer-name host port))
(setq wget-current-url url)
;;do the HTTP get now... we need a better way to plugin protocols here.
(set-process-sentinel stream 'wget-process-sentinel)
(wget--protocol-http-do url stream))))
(defun wget--protocol-http-do(url stream)
"Handle http protocol interaction."
(save-excursion
(set-buffer (process-buffer stream))
(process-send-string stream (format "GET %s HTTP/1.0\n\n" url))
(accept-process-output stream 30)))
(defun wget-process-sentinel(process event)
"Run after wget to update misc items."
(if (and (string-equal (process-name process) "wget")
(or (string-match "^exited" event)
(string-match "^finished" event)))
(progn
(set-buffer (process-buffer process))
(beginning-of-buffer)
;;(wget-highlight-buffer buffer-name)
(message "Fetching %s...done" wget-current-url)
;;(wget-highlight-buffer (current-buffer))
(wget-html-enable)
(display-buffer (process-buffer process)))))
(defun wget-highlight-buffer(buffer-name)
"Highlight misc information in the wget buffer."
(save-excursion
(set-buffer (get-buffer buffer-name))
(beginning-of-buffer)
(insert "--------------------------------------------------------------------------------\n")
(assert (re-search-forward "^\n" nil t)
nil "Could not find end of HTTP section.")
(insert "--------------------------------------------------------------------------------\n")
(end-of-line)
(setq current-overlay (make-overlay 1 (point)))
(overlay-put current-overlay 'face 'wget-headers-face)
(overlay-put current-overlay 'window (get-buffer-window (current-buffer)))))
(defun wget-html-enable()
"If the current buffer is html content. Turn on `html-mode'."
(if (re-search-forward "^Content-Type: text/html" nil t)
(html-mode)))
(provide 'wget)
;;; wget.el ends here