;;; -*- Mode: LISP; Syntax: COMMON-LISP; Package: CL-USER; Base: 10 -*- ;;; $Header: /usr/local/cvsrep/url-rewrite/url-rewrite.lisp,v 1.15 2006/01/03 18:40:23 edi Exp $ ;;; Copyright (c) 2004-2006, Dr. Edmund Weitz. All rights reserved. ;;; Redistribution and use in source and binary forms, with or without ;;; modification, are permitted provided that the following conditions ;;; are met: ;;; * Redistributions of source code must retain the above copyright ;;; notice, this list of conditions and the following disclaimer. ;;; * Redistributions in binary form must reproduce the above ;;; copyright notice, this list of conditions and the following ;;; disclaimer in the documentation and/or other materials ;;; provided with the distribution. ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED ;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY ;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE ;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS ;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. (in-package #:url-rewrite) (defun starts-with-scheme-p (string) "Checks whether the string STRING represents a URL which starts with a scheme, i.e. something like 'https://' or 'mailto:'." (loop with scheme-char-seen-p = nil for c across string when (or (char-not-greaterp #\a c #\z) (digit-char-p c) (member c '(#\+ #\- #\.) :test #'char=)) do (setq scheme-char-seen-p t) else return (and scheme-char-seen-p (char= c #\:)))) (defun url-encode (string) "URL-encode a string." ;; does this work correctly with IE or does it expect UTF-8 based ;; URL-encoding? (with-output-to-string (s) (loop for c across string do (cond ((or (char<= #\0 c #\9) (char<= #\a c #\z) (char<= #\A c #\Z) (find c "$-_.!*'()," :test #'char=)) (write-char c s)) ((char= c #\Space) (write-char #\+ s)) (t (format s "%~2,'0x" (char-code c))))))) (defun add-get-param-to-url (url name value) "URL is assumed to be a http URL. The pair of NAME and VALUE will be added as a GET parameter to this URL. Assumes that there's no other parameter of the same name. Only checks if #\? is part of the string to decide how to attach the new parameter to the end of the string." ;; possible bug: doesn't check for #\? which is written as, say, ;; "&x3f;" - also, is there any other way a question mark could be a ;; legitimate part of a URL? (concatenate 'string url (if (find #\? url :test #'char=) "&" "?") name "=" (url-encode value))) (defun rewrite-urls (rewrite-fn &optional (test-fn (complement #'starts-with-scheme-p))) "Reads an \(X)HTML document from *STANDARD-INPUT* and writes it back to *STANDARD-OUTPUT*. Any attribute value which is in one of the positions denoted by *URL-REWRITE-TAGS* is rewritten by REWRITE-FN if it passes the test denoted by the optional function TEST-FN which defaults to the complement of STARTS-WITH-SCHEME-P. This function aims to yield correct results for correct \(X)HTML input and it also tries hard to never signal an error although it may warn if it encounters syntax errors. It will NOT detect any possible error nor is there any warranty that it will work correctly with faulty input." (loop ;; read (and write back) until we see a #\< which is a candidate ;; for a tag or a markup declaration (read-until "<") ;; get next char without reading it (let ((peek-char (peek-char*))) (cond ((null peek-char) ;; stop if EOF (return-from rewrite-urls)) ((char= peek-char #\!) ;; we've seen ") ;; "" is nothing special, just write the ;; char and go back to the start of the loop (write-char (read-char))) ((letterp peek-char) ;; a letter, so this should be something like ;; - we just check for names and ;; delimited strings separated by whitespace ;; until we see the next #\> (read-name) (skip-whitespace) (block parameter-loop (loop (let ((peek-char (peek-char*))) (cond ((null peek-char) ;; stop if EOF (warn "EOF in markup declaration") (return-from rewrite-urls)) ((char= peek-char #\>) ;; a #\> ends the markup ;; declaration - write it back ;; and exit the loop (write-char (read-char)) (return-from parameter-loop)) ((or (letterp peek-char) (find peek-char '(#\' #\") :test #'char=)) ;; a delimiter or a letter, so ;; we expect a delimited string (read-delimited-string) (skip-whitespace)) ((comment-start-p) ;; a comment - skip it and write it back (skip-comment)) (t ;; something else - this is an error ;; so we warn and exit the loop (warn "Unexpected character ~S in markup declaration" peek-char) (return-from parameter-loop))))))) ((comment-start-p) ;; we've seen "