url-rewrite.lisp
  1 ;;; -*- Mode: LISP; Syntax: COMMON-LISP; Package: CL-USER; Base: 10 -*-
2
3 ;;; Copyright (c) 2004-2010, Dr. Edmund Weitz. All rights reserved.
4
5 ;;; Redistribution and use in source and binary forms, with or without
6 ;;; modification, are permitted provided that the following conditions
7 ;;; are met:
8
9 ;;; * Redistributions of source code must retain the above copyright
10 ;;; notice, this list of conditions and the following disclaimer.
11
12 ;;; * Redistributions in binary form must reproduce the above
13 ;;; copyright notice, this list of conditions and the following
14 ;;; disclaimer in the documentation and/or other materials
15 ;;; provided with the distribution.
16
17 ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
18 ;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
21 ;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 ;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 ;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 (in-package :url-rewrite)
30
31 (defun starts-with-scheme-p (string)
32 "Checks whether the string STRING represents a URL which starts with
33 a scheme, i.e. something like 'https://' or 'mailto:'."
34 (loop with scheme-char-seen-p = nil
35 for c across string
36 when (or (char-not-greaterp #\a c #\z)
37 (digit-char-p c)
38 (member c '(#\+ #\- #\.) :test #'char=))
39 do (setq scheme-char-seen-p t)
40 else return (and scheme-char-seen-p
41 (char= c #\:))))
42
43 (defun url-encode (string)
44 "URL-encode a string."
45 (with-output-to-string (s)
46 (loop for c across string
47 do (cond ((or (char<= #\0 c #\9)
48 (char<= #\a c #\z)
49 (char<= #\A c #\Z)
50 (find c "$-_.!*'()," :test #'char=))
51 (write-char c s))
52 ((char= c #\Space)
53 (write-char #\+ s))
54 (t (format s "%~2,'0x" (char-code c)))))))
55
56 (defun add-get-param-to-url (url name value)
57 "URL is assumed to be a http URL. The pair of NAME and VALUE will be
58 added as a GET parameter to this URL. Assumes that there's no other
59 parameter of the same name. Only checks if #\? is part of the string
60 to decide how to attach the new parameter to the end of the string."
61 ;; possible bug: doesn't check for #\? which is written as, say,
62 ;; "&x3f;" - also, is there any other way a question mark could be a
63 ;; legitimate part of a URL?
64 (concatenate 'string
65 url
66 (if (find #\? url :test #'char=)
67 "&amp;"
68 "?")
69 name
70 "="
71 (url-encode value)))
72
73 (defun rewrite-urls (rewrite-fn &optional (test-fn (complement #'starts-with-scheme-p)))
74 "Reads an \(X)HTML document from *STANDARD-INPUT* and writes it back
75 to *STANDARD-OUTPUT*. Any attribute value which is in one of the
76 positions denoted by *URL-REWRITE-TAGS* is rewritten by REWRITE-FN if
77 it passes the test denoted by the optional function TEST-FN which
78 defaults to the complement of STARTS-WITH-SCHEME-P.
79
80 This function aims to yield correct results for correct \(X)HTML input
81 and it also tries hard to never signal an error although it may warn
82 if it encounters syntax errors. It will NOT detect any possible error
83 nor is there any warranty that it will work correctly with faulty
84 input."
85 (loop
86 ;; read (and write back) until we see a #\< which is a candidate
87 ;; for a tag or a markup declaration
88 (read-until "<")
89 ;; get next char without reading it
90 (let ((peek-char (peek-char*)))
91 (cond ((null peek-char)
92 ;; stop if EOF
93 (return-from rewrite-urls))
94 ((char= peek-char #\!)
95 ;; we've seen "<!" so this might be a markup declaration
96 ;; - first write #\! back
97 (write-char (read-char))
98 ;; peek at next char
99 (let ((peek-char (peek-char*)))
100 (cond ((null peek-char)
101 ;; stop if EOF
102 (return-from rewrite-urls))
103 ((eql peek-char #\>)
104 ;; "<!>" is nothing special, just write the
105 ;; char and go back to the start of the loop
106 (write-char (read-char)))
107 ((letterp peek-char)
108 ;; a letter, so this should be something like
109 ;; <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2
110 ;; Final//EN"> - we just check for names and
111 ;; delimited strings separated by whitespace
112 ;; until we see the next #\>
113 (read-name)
114 (skip-whitespace)
115 (block parameter-loop
116 (loop
117 (let ((peek-char (peek-char*)))
118 (cond ((null peek-char)
119 ;; stop if EOF
120 (warn "EOF in markup declaration")
121 (return-from rewrite-urls))
122 ((char= peek-char #\>)
123 ;; a #\> ends the markup
124 ;; declaration - write it back
125 ;; and exit the loop
126 (write-char (read-char))
127 (return-from parameter-loop))
128 ((or (letterp peek-char)
129 (find peek-char '(#\' #\") :test #'char=))
130 ;; a delimiter or a letter, so
131 ;; we expect a delimited string
132 (read-delimited-string)
133 (skip-whitespace))
134 ((comment-start-p)
135 ;; a comment - skip it and write it back
136 (skip-comment))
137 (t
138 ;; something else - this is an error
139 ;; so we warn and exit the loop
140 (warn "Unexpected character ~S in markup declaration"
141 peek-char)
142 (return-from parameter-loop)))))))
143 ((comment-start-p)
144 ;; we've seen "<!--" so this starts a comment declaration
145 ;; - we'll read comments which are possibly separated
146 ;; by whitespace
147 (block comment-loop
148 (loop
149 (skip-comment)
150 (skip-whitespace)
151 (let ((peek-char (peek-char*)))
152 (cond ((null peek-char)
153 ;; stop if EOF
154 (warn "EOF in comment declaration")
155 (return-from rewrite-urls))
156 ((char= peek-char #\>)
157 ;; a #\> ends the comment
158 ;; declaration - write it back
159 ;; and exit the loop
160 (write-char (read-char))
161 (return-from comment-loop))
162 ;; a comment - do nothing
163 ((comment-start-p))
164 (t
165 ;; something else - this is an error
166 ;; so we warn and exit the loop
167 (warn "Unexpected character ~S in comment declaration"
168 peek-char)
169 (return-from comment-loop)))))))
170 (t
171 ;; neither markup declaration nor comment declaration,
172 ;; so this was just "<!"
173 (write-char (read-char))))))
174 ((char= peek-char #\/)
175 (write-char (read-char))
176 (let ((peek-char (peek-char*)))
177 (cond ((null peek-char)
178 ;; stop if EOF
179 (warn "EOF in end-tag")
180 (return-from rewrite-urls))
181 ((letterp peek-char)
182 ;; a letter, so this is supposed to start a name -
183 ;; read it and skip whitespace following it
184 (let ((name (read-name :skip nil)))
185 (skip-whitespace)
186 (let ((peek-char (peek-char*)))
187 (cond ((null peek-char)
188 ;; stop if EOF
189 (warn "EOF after </~A" name)
190 (return-from rewrite-urls))
191 ((char/= (peek-char*) #\>)
192 ;; we expect to see #\> here - if not
193 ;; we warn but do nothing else
194 (warn "Expected #\> after </~A" name))
195 (t
196 ;; end of end tag, just consume the #\>
197 (write-char (read-char)))))))
198 (t
199 ;; not a letter, so this is an error -
200 ;; we warn and ignore this
201 (warn "Unexpected character ~S after </"
202 peek-char)))))
203 ((letterp peek-char)
204 ;; a letter so we expect a start tag, possibly followed by
205 ;; attributes - first read name, check if it's mentioned
206 ;; in *URL-REWRITE-TAGS*, and find the name of the
207 ;; corresponding attribute
208 (let* ((name (read-name :skip nil))
209 (rewrite-attribute (and name
210 (cdr (assoc name *url-rewrite-tags*
211 :test #'string-equal))))
212 attribute-found-p)
213 (flet ((maybe-write-attribute (&optional value
214 (rewrite-attribute
215 (and (not attribute-found-p)
216 (cdr (assoc name
217 *url-rewrite-fill-tags*
218 :test #'string-equal)))))
219 ;; write the name of the attribute
220 ;; REWRITE-ATTRIBUTE and its (rewritten)
221 ;; value VALUE to *STANDARD-OUTPUT* if DO-IT
222 ;; is true - the default value for DO-IT
223 ;; means to only write the attribute if it
224 ;; has to be added
225 (when rewrite-attribute
226 (unless attribute-found-p
227 (write-char #\Space))
228 (write-string rewrite-attribute)
229 (write-char #\=)
230 (let ((delimiter (if (find #\' value :test #'char=)
231 #\" #\')))
232 (write-char delimiter)
233 (write-string (funcall rewrite-fn value))
234 (write-char delimiter)))))
235 (skip-whitespace)
236 (block attribute-loop
237 (loop
238 (let ((peek-char (peek-char*)))
239 (cond ((null peek-char)
240 ;; stop if EOF
241 (warn "EOF before ~A tag was closed" name)
242 (return-from rewrite-urls))
243 ((eql peek-char #\>)
244 ;; end of tag - exit attribute loop
245 (maybe-write-attribute)
246 (write-char (read-char))
247 (return-from attribute-loop))
248 ((eql peek-char #\/)
249 ;; we've seen #\/, so this might be the XHTML way
250 ;; to end a stand-alone tag
251 (write-char (read-char))
252 (cond ((eql (peek-char*) #\>)
253 ;; yes, it is - exit this loop
254 (maybe-write-attribute)
255 (write-char (read-char)))
256 (t
257 ;; no, it's not - so this is an error
258 (warn "Unexpected #\/ in ~A tag" name)))
259 ;; exit attribute loop in any case
260 (return-from attribute-loop))
261 ((letterp peek-char)
262 ;; a letter - this should start an attribute
263 (multiple-value-bind (name value string)
264 ;; no need to cons up return values if we're
265 ;; not going to rewrite anyway
266 (read-attribute :skip (null rewrite-attribute)
267 :write-through (null rewrite-attribute))
268 (cond ((and rewrite-attribute
269 (string-equal name rewrite-attribute))
270 ;; remember that we've seen the
271 ;; attribute in question
272 (setq attribute-found-p t)
273 ;; if this an attribute which should be
274 ;; rewritten do it and write the whole
275 ;; stuff to *STANDARD-OUT* explicitly
276 (cond ((funcall test-fn value)
277 (maybe-write-attribute value name))
278 (t
279 ;; otherwise write it back
280 (write-string string))))
281 (rewrite-attribute
282 ;; we didn't rewrite this attribute but we
283 ;; have to write it back to *STANDARD-OUTPUT*
284 ;; because READ-ATTRIBUTE didn't do it
285 (write-string string))))
286 (skip-whitespace))
287 (t
288 ;; an error - exit the attribute loop
289 (warn "Unexpected character ~A after <~A" peek-char name)
290 (return-from attribute-loop)))))))))
291 (t
292 ;; anything else means this is just #\<, no markup
293 (write-char (read-char)))))))