1 ;; -*- mode: common-lisp; package: puri -*-
2 ;; Support for URIs in Allegro.
3 ;; For general URI information see RFC2396.
5 ;; copyright (c) 1999-2001 Franz Inc, Berkeley, CA - All rights reserved.
6 ;; copyright (c) 2003 Kevin Rosenberg (porting changes)
8 ;; The software, data and information contained herein are proprietary
9 ;; to, and comprise valuable trade secrets of, Franz, Inc. They are
10 ;; given in confidence by Franz, Inc. pursuant to a written license
11 ;; agreement, and may be stored and used only in accordance with the terms
14 ;; Restricted Rights Legend
15 ;; ------------------------
16 ;; Use, duplication, and disclosure of the software, data and information
17 ;; contained herein by any agency, department or entity of the U.S.
18 ;; Government are subject to restrictions of Restricted Rights for
19 ;; Commercial Software developed at private expense as specified in
20 ;; DOD FAR Supplement 52.227-7013 (c) (1) (ii), as applicable.
22 ;; Original version from ACL 6.1:
23 ;; uri.cl,v 2.3.6.4.2.1 2001/08/09 17:42:39 layer
25 ;; $Id: src.lisp,v 1.9 2003/07/20 18:51:48 kevin Exp $
30 #:uri ; the type and a function
34 #:uri-scheme ; and slots
40 #:uri-authority ; pseudo-slot accessor
43 #:urn-nid ; pseudo-slot accessor
44 #:urn-nss ; pseudo-slot accessor
53 #:make-uri-space ; interning...
60 #:uri-parse-error ;; Added by KMR
65 (eval-when (:compile-toplevel) (declaim (optimize (speed 3))))
69 (defun parse-body (forms &optional env)
70 "Parses a body, returns (VALUES docstring declarations forms)"
71 (declare (ignore env))
72 ;; fixme -- need to add parsing of multiple declarations
73 (let (docstring declarations)
74 (when (stringp (car forms))
75 (setq docstring (car forms))
76 (setq forms (cdr forms)))
77 (when (and (listp (car forms))
78 (symbolp (caar forms))
79 (string-equal (symbol-name '#:declare)
80 (symbol-name (caar forms))))
81 (setq declarations (car forms))
82 (setq forms (cdr forms)))
83 (values docstring declarations forms)))
86 (defun shrink-vector (str size)
88 (excl::.primcall 'sys::shrink-svector str size)
90 (sb-kernel:shrink-vector str size)
92 (lisp::shrink-vector str size)
94 (system::shrink-vector$vector str size)
95 #+(or allegro cmu sbcl lispworks)
97 #-(or allegro cmu sbcl lispworks)
101 ;; KMR: Added new condition to handle cross-implementation variances
102 ;; in the parse-error condition many implementations define
104 (define-condition uri-parse-error (parse-error)
105 ((fmt-control :initarg :fmt-control :accessor fmt-control)
106 (fmt-arguments :initarg :fmt-arguments :accessor fmt-arguments ))
107 (:report (lambda (c stream)
108 (format stream "Parse error:")
109 (apply #'format stream (fmt-control c) (fmt-arguments c)))))
111 (defun .parse-error (fmt &rest args)
112 (error 'uri-parse-error :fmt-control fmt :fmt-arguments args))
115 (defun internal-reader-error (stream fmt &rest args)
116 (apply #'format stream fmt args))
118 #-allegro (defvar *current-case-mode* :case-insensitive-upper)
119 #+allegro (eval-when (:compile-toplevel :load-toplevel :execute)
120 (import '(excl:*current-case-mode*
121 excl:delimited-string-to-list
123 excl::internal-reader-error
127 (defun position-char (char string start max)
128 (declare (optimize (speed 3) (safety 0) (space 0))
129 (fixnum start max) (simple-string string))
130 (do* ((i start (1+ i)))
133 (when (char= char (schar string i)) (return i))))
136 (defun delimited-string-to-list (string &optional (separator #\space)
138 (declare (optimize (speed 3) (safety 0) (space 0)
139 (compilation-speed 0))
141 (type character separator))
142 (do* ((len (length string))
145 (end (position-char separator string pos len)
146 (position-char separator string pos len)))
149 (push (subseq string pos) output)
150 (when (or (not skip-terminal) (zerop len))
153 (declare (type fixnum pos len)
154 (type (or null fixnum) end))
155 (push (subseq string pos end) output)
156 (setq pos (1+ end))))
159 (eval-when (:compile-toplevel :load-toplevel :execute)
160 (defvar if*-keyword-list '("then" "thenret" "else" "elseif"))
162 (defmacro if* (&rest args)
163 (do ((xx (reverse args) (cdr xx))
170 (cond ((eq state :compl)
172 (t (error "if*: illegal form ~s" args))))
173 (cond ((and (symbolp (car xx))
174 (member (symbol-name (car xx))
176 :test #'string-equal))
177 (setq lookat (symbol-name (car xx)))))
179 (cond ((eq state :init)
180 (cond (lookat (cond ((string-equal lookat "thenret")
184 "if*: bad keyword ~a" lookat))))
187 (push (car xx) col))))
190 (cond ((string-equal lookat "else")
193 "if*: multiples elses")))
196 (push `(t ,@col) totalcol))
197 ((string-equal lookat "then")
199 (t (error "if*: bad keyword ~s"
201 (t (push (car xx) col))))
205 "if*: keyword ~s at the wrong place " (car xx)))
206 (t (setq state :compl)
207 (push `(,(car xx) ,@col) totalcol))))
209 (cond ((not (string-equal lookat "elseif"))
210 (error "if*: missing elseif clause ")))
211 (setq state :init))))))
217 (scheme :initarg :scheme :initform nil :accessor uri-scheme)
218 (host :initarg :host :initform nil :accessor uri-host)
219 (port :initarg :port :initform nil :accessor uri-port)
220 (path :initarg :path :initform nil :accessor uri-path)
221 (query :initarg :query :initform nil :accessor uri-query)
222 (fragment :initarg :fragment :initform nil :accessor uri-fragment)
223 (plist :initarg :plist :initform nil :accessor uri-plist)
227 ;; used to prevent unnessary work, looking for chars to escape and
229 :initarg :escaped :initform nil :accessor uri-escaped)
231 ;; the cached printable representation of the URI. It *might* be
232 ;; different than the original string, though, because the user might
233 ;; have escaped non-reserved chars--they won't be escaped when the URI
235 :initarg :string :initform nil :accessor uri-string)
237 ;; the cached parsed representation of the URI path.
238 :initarg :parsed-path
240 :accessor .uri-parsed-path)
242 ;; cached sxhash, so we don't have to compute it more than once.
243 :initarg :hashcode :initform nil :accessor uri-hashcode)))
246 ((nid :initarg :nid :initform nil :accessor urn-nid)
247 (nss :initarg :nss :initform nil :accessor urn-nss)))
249 (eval-when (:compile-toplevel :execute)
250 (defmacro clear-caching-on-slot-change (name)
251 `(defmethod (setf ,name) :around (new-value (self uri))
252 (declare (ignore new-value))
253 (prog1 (call-next-method)
254 (setf (uri-string self) nil)
255 ,@(when (eq name 'uri-path) `((setf (.uri-parsed-path self) nil)))
256 (setf (uri-hashcode self) nil))))
259 (clear-caching-on-slot-change uri-scheme)
260 (clear-caching-on-slot-change uri-host)
261 (clear-caching-on-slot-change uri-port)
262 (clear-caching-on-slot-change uri-path)
263 (clear-caching-on-slot-change uri-query)
264 (clear-caching-on-slot-change uri-fragment)
267 (defmethod make-load-form ((self uri) &optional env)
268 (declare (ignore env))
269 `(make-instance ',(class-name (class-of self))
270 :scheme ,(uri-scheme self)
271 :host ,(uri-host self)
272 :port ,(uri-port self)
273 :path ',(uri-path self)
274 :query ,(uri-query self)
275 :fragment ,(uri-fragment self)
276 :plist ',(uri-plist self)
277 :string ,(uri-string self)
278 :parsed-path ',(.uri-parsed-path self)))
280 (defmethod uri-p ((thing uri)) t)
281 (defmethod uri-p ((thing t)) nil)
285 (scheme (when uri (uri-scheme uri)))
286 (host (when uri (uri-host uri)))
287 (port (when uri (uri-port uri)))
288 (path (when uri (uri-path uri)))
290 (when uri (copy-list (.uri-parsed-path uri))))
291 (query (when uri (uri-query uri)))
292 (fragment (when uri (uri-fragment uri)))
293 (plist (when uri (copy-list (uri-plist uri))))
294 (class (when uri (class-of uri)))
295 &aux (escaped (when uri (uri-escaped uri))))
297 then (setf (uri-scheme place) scheme)
298 (setf (uri-host place) host)
299 (setf (uri-port place) port)
300 (setf (uri-path place) path)
301 (setf (.uri-parsed-path place) parsed-path)
302 (setf (uri-query place) query)
303 (setf (uri-fragment place) fragment)
304 (setf (uri-plist place) plist)
305 (setf (uri-escaped place) escaped)
306 (setf (uri-string place) nil)
307 (setf (uri-hashcode place) nil)
309 elseif (eq 'uri class)
310 then ;; allow the compiler to optimize the call to make-instance:
312 :scheme scheme :host host :port port :path path
313 :parsed-path parsed-path
314 :query query :fragment fragment :plist plist
315 :escaped escaped :string nil :hashcode nil)
316 else (make-instance class
317 :scheme scheme :host host :port port :path path
318 :parsed-path parsed-path
319 :query query :fragment fragment :plist plist
320 :escaped escaped :string nil :hashcode nil)))
322 (defmethod uri-parsed-path ((uri uri))
324 (when (null (.uri-parsed-path uri))
325 (setf (.uri-parsed-path uri)
326 (parse-path (uri-path uri) (uri-escaped uri))))
327 (.uri-parsed-path uri)))
329 (defmethod (setf uri-parsed-path) (path-list (uri uri))
330 (assert (and (consp path-list)
331 (or (member (car path-list) '(:absolute :relative)
333 (setf (uri-path uri) (render-parsed-path path-list t))
334 (setf (.uri-parsed-path uri) path-list)
337 (defun uri-authority (uri)
339 (let ((*print-pretty* nil))
340 (format nil "~a~@[:~a~]" (uri-host uri) (uri-port uri)))))
343 (if* (equalp "urn" (uri-scheme uri))
345 else (error "URI is not a URN: ~s." uri)))
348 (if* (equalp "urn" (uri-scheme uri))
350 else (error "URI is not a URN: ~s." uri)))
352 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
355 (defparameter *excluded-characters*
356 '(;; `delims' (except #\%, because it's handled specially):
357 #\< #\> #\" #\space #\#
359 #\{ #\} #\| #\\ #\^ #\[ #\] #\`))
361 (defun reserved-char-vector (chars &key except)
362 (do* ((a (make-array 127 :element-type 'bit :initial-element 0))
363 (chars chars (cdr chars))
364 (c (car chars) (car chars)))
366 (if* (and except (member c except :test #'char=))
368 else (setf (sbit a (char-int c)) 1))))
370 (defparameter *reserved-characters*
371 (reserved-char-vector
372 (append *excluded-characters*
373 '(#\; #\/ #\? #\: #\@ #\& #\= #\+ #\$ #\, #\%))))
374 (defparameter *reserved-authority-characters*
375 (reserved-char-vector
376 (append *excluded-characters* '(#\; #\/ #\? #\: #\@))))
377 (defparameter *reserved-path-characters*
378 (reserved-char-vector
379 (append *excluded-characters*
381 ;;;;The rfc says this should be here, but it doesn't make sense.
384 (defparameter *reserved-path-characters2*
385 ;; These are the same characters that are in
386 ;; *reserved-path-characters*, minus #\/. Why? Because the parsed
387 ;; representation of the path can contain the %2f converted into a /.
388 ;; That's the whole point of having the parsed representation, so that
389 ;; lisp programs can deal with the path element data in the most
391 (reserved-char-vector
392 (append *excluded-characters*
394 ;;;;The rfc says this should be here, but it doesn't make sense.
397 (defparameter *reserved-fragment-characters*
398 (reserved-char-vector (remove #\# *excluded-characters*)))
400 (eval-when (:compile-toplevel :execute)
401 (defun gen-char-range-list (start end)
403 (endcode (1+ (char-int end)))
404 (chcode (char-int start)
408 ;; - has to be first, otherwise it signifies a range!
410 then (setq res (nreverse res))
413 else (nreverse res)))
414 (if* (= #.(char-int #\-) chcode)
416 else (push (code-char chcode) res))))
419 (defparameter *valid-nid-characters*
420 (reserved-char-vector
421 '#.(nconc (gen-char-range-list #\a #\z)
422 (gen-char-range-list #\A #\Z)
423 (gen-char-range-list #\0 #\9)
425 (defparameter *reserved-nss-characters*
426 (reserved-char-vector
427 (append *excluded-characters* '(#\& #\~ #\/ #\?))))
429 (defparameter *illegal-characters*
430 (reserved-char-vector (remove #\# *excluded-characters*)))
431 (defparameter *strict-illegal-query-characters*
432 (reserved-char-vector (append '(#\?) (remove #\# *excluded-characters*))))
433 (defparameter *illegal-query-characters*
434 (reserved-char-vector
435 *excluded-characters* :except '(#\^ #\| #\#)))
438 (defun parse-uri (thing &key (class 'uri) &aux escape)
439 (when (uri-p thing) (return-from parse-uri thing))
441 (setq escape (escape-p thing))
442 (multiple-value-bind (scheme host port path query fragment)
443 (parse-uri-string thing)
447 (case *current-case-mode*
448 ((:case-insensitive-upper :case-sensitive-upper)
450 ((:case-insensitive-lower :case-sensitive-lower)
452 (decode-escaped-encoding scheme escape))
453 (find-package :keyword))))
455 (when (and scheme (eq :urn scheme))
456 (return-from parse-uri
457 (make-instance 'urn :scheme scheme :nid host :nss path)))
459 (when host (setq host (decode-escaped-encoding host escape)))
461 (setq port (read-from-string port))
462 (when (not (numberp port)) (error "port is not a number: ~s." port))
463 (when (not (plusp port))
464 (error "port is not a positive integer: ~d." port))
465 (when (eql port (case scheme
471 (when (or (string= "" path)
472 (and ;; we canonicalize away a reference to just /:
474 (member scheme '(:http :https :ftp) :test #'eq)
479 (decode-escaped-encoding path escape *reserved-path-characters*)))
480 (when query (setq query (decode-escaped-encoding query escape)))
483 (decode-escaped-encoding fragment escape
484 *reserved-fragment-characters*)))
486 then ;; allow the compiler to optimize the make-instance call:
495 else ;; do it the slow way:
505 (defmethod uri ((thing uri))
508 (defmethod uri ((thing string))
511 (defmethod uri ((thing t))
512 (error "Cannot coerce ~s to a uri." thing))
514 (defvar *strict-parse* t)
516 (defun parse-uri-string (string &aux (illegal-chars *illegal-characters*))
517 (declare (optimize (speed 3)))
518 ;; Speed is important, so use a specialized state machine instead of
519 ;; regular expressions for parsing the URI string. The regexp we are
528 (end (length string))
533 (path-components '())
536 ;; namespace identifier, for urn parsing only:
538 (declare (fixnum state start end))
539 (flet ((read-token (kind &optional legal-chars)
543 else (let ((sindex start)
546 (declare (fixnum sindex))
549 (when (>= start end) (return nil))
550 (setq c (schar string start))
551 (let ((ci (char-int c)))
553 then (if* (and (eq :colon kind) (eq c #\:))
555 elseif (= 0 (sbit legal-chars ci))
558 URI ~s contains illegal character ~s at position ~d."
560 elseif (and (< ci 128)
562 (= 1 (sbit illegal-chars ci)))
563 then (.parse-error "~
564 URI ~s contains illegal character ~s at position ~d."
568 (#\? (return :question))
569 (#\# (return :hash))))
570 (:query (case c (#\# (return :hash))))
573 (#\: (return :colon))
574 (#\? (return :question))
576 (#\/ (return :slash)))))
578 (if* (> start sindex)
579 then ;; we found some chars
580 ;; before we stopped the parse
581 (setq tokval (subseq string sindex start))
583 else ;; immediately stopped at a special char
586 (failure (&optional why)
587 (.parse-error "illegal URI: ~s [~d]~@[: ~a~]"
590 (.parse-error "impossible state: ~d [~s]" state string)))
593 (0 ;; starting to parse
594 (ecase (read-token t)
596 (:question (setq state 7))
597 (:hash (setq state 8))
598 (:slash (setq state 3))
599 (:string (setq state 1))
600 (:end (setq state 9))))
601 (1 ;; seen <token><special char>
602 (let ((token tokval))
603 (ecase (read-token t)
604 (:colon (setq scheme token)
605 (if* (equalp "urn" scheme)
607 else (setq state 2)))
608 (:question (push token path-components)
610 (:hash (push token path-components)
612 (:slash (push token path-components)
613 (push "/" path-components)
616 (:end (push token path-components)
619 (ecase (read-token t)
621 (:question (setq state 7))
622 (:hash (setq state 8))
623 (:slash (setq state 3))
624 (:string (setq state 10))
625 (:end (setq state 9))))
626 (10 ;; seen <scheme>:<token>
627 (let ((token tokval))
628 (ecase (read-token t)
630 (:question (push token path-components)
632 (:hash (push token path-components)
634 (:slash (push token path-components)
637 (:end (push token path-components)
639 (3 ;; seen / or <scheme>:/
640 (ecase (read-token t)
642 (:question (push "/" path-components)
644 (:hash (push "/" path-components)
646 (:slash (setq state 4))
647 (:string (push "/" path-components)
648 (push tokval path-components)
650 (:end (push "/" path-components)
652 (4 ;; seen [<scheme>:]//
653 (ecase (read-token t)
655 (:question (failure))
658 (:string (setq host tokval)
661 (11 ;; seen [<scheme>:]//<host>
662 (ecase (read-token t)
663 (:colon (setq state 5))
664 (:question (setq state 7))
665 (:hash (setq state 8))
666 (:slash (push "/" path-components)
668 (:string (impossible))
669 (:end (setq state 9))))
670 (5 ;; seen [<scheme>:]//<host>:
671 (ecase (read-token t)
673 (:question (failure))
675 (:slash (push "/" path-components)
677 (:string (setq port tokval)
680 (12 ;; seen [<scheme>:]//<host>:[<port>]
681 (ecase (read-token t)
683 (:question (setq state 7))
684 (:hash (setq state 8))
685 (:slash (push "/" path-components)
687 (:string (impossible))
688 (:end (setq state 9))))
690 (ecase (read-token :path)
691 (:question (setq state 7))
692 (:hash (setq state 8))
693 (:string (push tokval path-components)
695 (:end (setq state 9))))
697 (ecase (read-token :path)
698 (:question (setq state 7))
699 (:hash (setq state 8))
700 (:string (impossible))
701 (:end (setq state 9))))
705 then *strict-illegal-query-characters*
706 else *illegal-query-characters*))
707 (ecase (prog1 (read-token :query)
708 (setq illegal-chars *illegal-characters*))
709 (:hash (setq state 8))
710 (:string (setq query tokval)
712 (:end (setq state 9))))
714 (ecase (read-token :query)
715 (:hash (setq state 8))
716 (:string (impossible))
717 (:end (setq state 9))))
719 (ecase (read-token :rest)
720 (:string (setq fragment tokval)
722 (:end (setq state 9))))
727 (apply #'concatenate 'simple-string (nreverse path-components))
730 (15 ;; seen urn:, read nid now
731 (case (read-token :colon *valid-nid-characters*)
732 (:string (setq nid tokval)
734 (t (failure "missing namespace identifier"))))
735 (16 ;; seen urn:<nid>
737 (:colon (setq state 17))
738 (t (failure "missing namespace specific string"))))
739 (17 ;; seen urn:<nid>:, rest is nss
740 (return (values scheme
744 (setq illegal-chars *reserved-nss-characters*)
748 "internal error in parse engine, wrong state: ~s." state)))))))
750 (defun escape-p (string)
751 (declare (optimize (speed 3)))
753 (max (the fixnum (length string))))
755 (declare (fixnum i max))
756 (when (char= #\% (schar string i))
759 (defun parse-path (path-string escape)
760 (do* ((xpath-list (delimited-string-to-list path-string #\/))
763 (if* (string= "" (car xpath-list))
764 then (setf (car xpath-list) :absolute)
765 else (push :relative xpath-list))
767 (pl (cdr path-list) (cdr pl))
769 ((null pl) path-list)
770 (if* (cdr (setq segments (delimited-string-to-list (car pl) #\;)))
771 then ;; there is a param
772 ;;; (setf (car pl) segments)
774 (mapcar #'(lambda (s)
775 (decode-escaped-encoding
776 s escape *reserved-path-characters2*))
779 ;;; (setf (car pl) (car segments))
781 (decode-escaped-encoding
782 (car segments) escape *reserved-path-characters2*)))))
784 (defun decode-escaped-encoding (string escape
785 &optional (reserved-chars
786 *reserved-characters*))
787 ;; Return a string with the real characters.
788 (when (null escape) (return-from decode-escaped-encoding string))
790 (max (length string))
791 (new-string (copy-seq string))
795 (shrink-vector new-string new-i))
796 (if* (char= #\% (setq ch (schar string i)))
797 then (when (> (+ i 3) max)
799 "Unsyntactic escaped encoding in ~s." string))
800 (setq ch (schar string (incf i)))
801 (setq ch2 (schar string (incf i)))
802 (when (not (and (setq chc (digit-char-p ch 16))
803 (setq chc2 (digit-char-p ch2 16))))
805 "Non-hexidecimal digits after %: %c%c." ch ch2))
806 (let ((ci (+ (* 16 chc) chc2)))
807 (if* (or (null reserved-chars)
808 (= 0 (sbit reserved-chars ci)))
810 (setf (schar new-string new-i)
812 else (setf (schar new-string new-i) #\%)
813 (setf (schar new-string (incf new-i)) ch)
814 (setf (schar new-string (incf new-i)) ch2)))
815 else (setf (schar new-string new-i) ch))))
817 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
820 (defun render-uri (uri stream
821 &aux (escape (uri-escaped uri))
822 (*print-pretty* nil))
823 (when (null (uri-string uri))
824 (setf (uri-string uri)
825 (let ((scheme (uri-scheme uri))
826 (host (uri-host uri))
827 (port (uri-port uri))
828 (path (uri-path uri))
829 (query (uri-query uri))
830 (fragment (uri-fragment uri)))
831 (concatenate 'simple-string
833 (encode-escaped-encoding
834 (string-downcase ;; for upper case lisps
835 (symbol-name scheme))
836 *reserved-characters* escape))
840 (encode-escaped-encoding
841 host *reserved-authority-characters* escape))
844 #-allegro (format nil "~D" port)
845 #+allegro (with-output-to-string (s)
846 (excl::maybe-print-fast s port))
849 (encode-escaped-encoding path
851 ;;*reserved-path-characters*
854 (when query (encode-escaped-encoding query nil escape))
856 (when fragment (encode-escaped-encoding fragment nil escape))))))
858 then (format stream "~a" (uri-string uri))
859 else (uri-string uri)))
861 (defun render-parsed-path (path-list escape)
863 (first (car path-list))
864 (pl (cdr path-list) (cdr pl))
865 (pe (car pl) (car pl)))
867 (when res (apply #'concatenate 'simple-string (nreverse res))))
868 (when (or (null first)
869 (prog1 (eq :absolute first)
874 (encode-escaped-encoding pe *reserved-path-characters* escape)
876 else ;; contains params
877 (push (encode-escaped-encoding
878 (car pe) *reserved-path-characters* escape)
880 (dolist (item (cdr pe))
882 (push (encode-escaped-encoding
883 item *reserved-path-characters* escape)
886 (defun render-urn (urn stream
887 &aux (*print-pretty* nil))
888 (when (null (uri-string urn))
889 (setf (uri-string urn)
890 (let ((nid (urn-nid urn))
892 (concatenate 'simple-string "urn:" nid ":" nss))))
894 then (format stream "~a" (uri-string urn))
895 else (uri-string urn)))
897 (defparameter *escaped-encoding*
898 (vector #\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9 #\a #\b #\c #\d #\e #\f))
900 (defun encode-escaped-encoding (string reserved-chars escape)
901 (when (null escape) (return-from encode-escaped-encoding string))
902 ;; Make a string as big as it possibly needs to be (3 times the original
903 ;; size), and truncate it at the end.
904 (do* ((max (length string))
905 (new-max (* 3 max)) ;; worst case new size
906 (new-string (make-string new-max))
911 (shrink-vector new-string (incf new-i)))
912 (setq ci (char-int (setq c (schar string i))))
913 (if* (or (null reserved-chars)
915 (= 0 (sbit reserved-chars ci)))
918 (setf (schar new-string new-i) c)
919 else ;; need to escape it
920 (multiple-value-bind (q r) (truncate ci 16)
921 (setf (schar new-string (incf new-i)) #\%)
922 (setf (schar new-string (incf new-i)) (elt *escaped-encoding* q))
923 (setf (schar new-string (incf new-i))
924 (elt *escaped-encoding* r))))))
926 (defmethod print-object ((uri uri) stream)
928 then (format stream "#<~a ~a>" 'uri (render-uri uri nil))
929 else (render-uri uri stream)))
931 (defmethod print-object ((urn urn) stream)
933 then (format stream "#<~a ~a>" 'uri (render-urn urn nil))
934 else (render-urn urn stream)))
936 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
937 ;; merging and unmerging
939 (defmethod merge-uris ((uri string) (base string) &optional place)
940 (merge-uris (parse-uri uri) (parse-uri base) place))
942 (defmethod merge-uris ((uri uri) (base string) &optional place)
943 (merge-uris uri (parse-uri base) place))
945 (defmethod merge-uris ((uri string) (base uri) &optional place)
946 (merge-uris (parse-uri uri) base place))
948 (defmethod merge-uris ((uri uri) (base uri) &optional place)
949 ;; The following is from
950 ;; http://info.internet.isi.edu/in-notes/rfc/files/rfc2396.txt
951 ;; and is algorithm we use to merge URIs.
953 ;; For more information, see section 5.2 of the RFC.
957 (when (and (null (uri-parsed-path uri))
958 (null (uri-scheme uri))
959 (null (uri-host uri))
960 (null (uri-port uri))
961 (null (uri-query uri)))
962 (return-from merge-uris
963 (let ((new (copy-uri base :place place)))
964 (when (uri-query uri)
965 (setf (uri-query new) (uri-query uri)))
966 (when (uri-fragment uri)
967 (setf (uri-fragment new) (uri-fragment uri)))
970 (setq uri (copy-uri uri :place place))
973 (when (uri-scheme uri)
974 (return-from merge-uris uri))
975 (setf (uri-scheme uri) (uri-scheme base))
978 (when (uri-host uri) (go :done))
979 (setf (uri-host uri) (uri-host base))
980 (setf (uri-port uri) (uri-port base))
983 (let ((p (uri-parsed-path uri)))
984 (when (and p (eq :absolute (car p)))
985 (when (equal '(:absolute "") p)
986 ;; Canonicalize the way parsing does:
987 (setf (uri-path uri) nil))
992 (or (uri-parsed-path base)
993 ;; needed because we canonicalize away a path of just `/':
995 (path (uri-parsed-path uri))
997 (when (not (eq :absolute (car base-path)))
998 (error "Cannot merge ~a and ~a, since latter is not absolute."
1003 (append (butlast base-path)
1004 (if* path then (cdr path) else '(""))))
1007 (let ((last (last new-path-list)))
1008 (if* (atom (car last))
1009 then (when (string= "." (car last))
1010 (setf (car last) ""))
1011 else (when (string= "." (caar last))
1012 (setf (caar last) ""))))
1014 (delete "." new-path-list :test #'(lambda (a b)
1020 (let ((npl (cdr new-path-list))
1023 (string= ".." (let ((l (car (last npl))))
1030 :test #'(lambda (a b)
1035 (when (null index) (return))
1037 ;; The RFC says, in 6g, "that the implementation may handle
1038 ;; this error by retaining these components in the resolved
1039 ;; path, by removing them from the resolved path, or by
1040 ;; avoiding traversal of the reference." The examples in C.2
1041 ;; imply that we should do the first thing (retain them), so
1042 ;; that's what we'll do.
1045 then (setq npl (cddr npl))
1047 (dotimes (x (- index 2)) (setq tmp (cdr tmp)))
1048 (setf (cdr tmp) (cdddr tmp))))
1049 (setf (cdr new-path-list) npl)
1050 (when fix-tail (setq new-path-list (nconc new-path-list '("")))))
1053 ;; don't complain if new-path-list starts with `..'. See comment
1054 ;; above about this step.
1057 (when (or (equal '(:absolute "") new-path-list)
1058 (equal '(:absolute) new-path-list))
1059 (setq new-path-list nil))
1060 (setf (uri-path uri)
1061 (render-parsed-path new-path-list
1062 ;; don't know, so have to assume:
1067 (return-from merge-uris uri)))
1069 (defmethod enough-uri ((uri string) (base string) &optional place)
1070 (enough-uri (parse-uri uri) (parse-uri base) place))
1072 (defmethod enough-uri ((uri uri) (base string) &optional place)
1073 (enough-uri uri (parse-uri base) place))
1075 (defmethod enough-uri ((uri string) (base uri) &optional place)
1076 (enough-uri (parse-uri uri) base place))
1078 (defmethod enough-uri ((uri uri) (base uri) &optional place)
1079 (let ((new-scheme nil)
1082 (new-parsed-path nil))
1084 (when (or (and (uri-scheme uri)
1085 (not (equalp (uri-scheme uri) (uri-scheme base))))
1087 (not (equalp (uri-host uri) (uri-host base))))
1088 (not (equalp (uri-port uri) (uri-port base))))
1089 (return-from enough-uri uri))
1091 (when (null (uri-host uri))
1092 (setq new-host (uri-host base)))
1093 (when (null (uri-port uri))
1094 (setq new-port (uri-port base)))
1096 (when (null (uri-scheme uri))
1097 (setq new-scheme (uri-scheme base)))
1099 ;; Now, for the hard one, path.
1100 ;; We essentially do here what enough-namestring does.
1101 (do* ((base-path (uri-parsed-path base))
1102 (path (uri-parsed-path uri))
1103 (bp base-path (cdr bp))
1105 ((or (null bp) (null p))
1106 ;; If p is nil, that means we have something like
1107 ;; (enough-uri "/foo/bar" "/foo/bar/baz.htm"), so
1108 ;; new-parsed-path will be nil.
1110 (setq new-parsed-path (copy-list p))
1111 (when (not (symbolp (car new-parsed-path)))
1112 (push :relative new-parsed-path))))
1113 (if* (equal (car bp) (car p))
1115 else (setq new-parsed-path (copy-list p))
1116 (when (not (symbolp (car new-parsed-path)))
1117 (push :relative new-parsed-path))
1121 (when new-parsed-path
1122 (render-parsed-path new-parsed-path
1123 ;; don't know, so have to assume:
1125 (new-query (uri-query uri))
1126 (new-fragment (uri-fragment uri))
1127 (new-plist (copy-list (uri-plist uri))))
1128 (if* (and (null new-scheme)
1132 (null new-parsed-path)
1134 (null new-fragment))
1135 then ;; can't have a completely empty uri!
1137 :class (class-of uri)
1142 :class (class-of uri)
1148 :parsed-path new-parsed-path
1150 :fragment new-fragment
1151 :plist new-plist)))))
1153 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1154 ;; support for interning URIs
1156 (defun make-uri-space (&rest keys &key (size 777) &allow-other-keys)
1158 (apply #'make-hash-table :size size
1159 :hash-function 'uri-hash
1160 :test 'uri= :values nil keys)
1162 (apply #'make-hash-table :size size keys))
1164 (defun gethash-uri (uri table)
1165 #+allegro (gethash uri table)
1167 (let* ((hash (uri-hash uri))
1168 (existing (gethash hash table)))
1169 (dolist (u existing)
1171 (return-from gethash-uri (values u t))))
1174 (defun puthash-uri (uri table)
1175 #+allegro (excl:puthash-key uri table)
1177 (let ((existing (gethash (uri-hash uri) table)))
1178 (dolist (u existing)
1180 (return-from puthash-uri u)))
1181 (setf (gethash (uri-hash uri) table)
1182 (cons uri existing))
1186 (defun uri-hash (uri)
1187 (if* (uri-hashcode uri)
1189 else (setf (uri-hashcode uri)
1192 (render-uri uri nil)
1195 (render-uri uri nil))))))
1197 (defvar *uris* (make-uri-space))
1199 (defun uri-space () *uris*)
1201 (defun (setf uri-space) (new-val)
1202 (setq *uris* new-val))
1204 ;; bootstrapping (uri= changed from function to method):
1205 (when (fboundp 'uri=) (fmakunbound 'uri=))
1207 (defgeneric uri= (uri1 uri2))
1208 (defmethod uri= ((uri1 uri) (uri2 uri))
1209 (when (not (eq (uri-scheme uri1) (uri-scheme uri2)))
1210 (return-from uri= nil))
1211 ;; RFC2396 says: a URL with an explicit ":port", where the port is
1212 ;; the default for the scheme, is the equivalent to one where the
1213 ;; port is elided. Hmmmm. This means that this function has to be
1214 ;; scheme dependent. Grrrr.
1215 (let ((default-port (case (uri-scheme uri1)
1220 (and (equalp (uri-host uri1) (uri-host uri2))
1221 (eql (or (uri-port uri1) default-port)
1222 (or (uri-port uri2) default-port))
1223 (string= (uri-path uri1) (uri-path uri2))
1224 (string= (uri-query uri1) (uri-query uri2))
1225 (string= (uri-fragment uri1) (uri-fragment uri2)))))
1227 (defmethod uri= ((urn1 urn) (urn2 urn))
1228 (when (not (eq (uri-scheme urn1) (uri-scheme urn2)))
1229 (return-from uri= nil))
1230 (and (equalp (urn-nid urn1) (urn-nid urn2))
1231 (urn-nss-equal (urn-nss urn1) (urn-nss urn2))))
1233 (defun urn-nss-equal (nss1 nss2 &aux len)
1234 ;; Return t iff the nss values are the same.
1235 ;; %2c and %2C are equivalent.
1236 (when (or (null nss1) (null nss2)
1237 (not (= (setq len (length nss1))
1239 (return-from urn-nss-equal nil))
1244 (setq c1 (schar nss1 i))
1245 (setq c2 (schar nss2 i))
1248 (if* (and (char= #\% c1) (char= #\% c2))
1249 then (setq state :percent+1)
1250 elseif (char/= c1 c2)
1253 (when (char-not-equal c1 c2) (return nil))
1254 (setq state :percent+2))
1256 (when (char-not-equal c1 c2) (return nil))
1257 (setq state :char)))))
1259 (defmethod intern-uri ((xuri uri) &optional (uri-space *uris*))
1260 (let ((uri (gethash-uri xuri uri-space)))
1263 else (puthash-uri xuri uri-space))))
1265 (defmethod intern-uri ((uri string) &optional (uri-space *uris*))
1266 (intern-uri (parse-uri uri) uri-space))
1268 (defun unintern-uri (uri &optional (uri-space *uris*))
1270 then (clrhash uri-space)
1272 then (remhash uri uri-space)
1273 else (error "bad uri: ~s." uri)))
1275 (defmacro do-all-uris ((var &optional uri-space result-form)
1278 "do-all-uris (var [[uri-space] result-form])
1279 {declaration}* {tag | statement}*
1280 Executes the forms once for each uri with var bound to the current uri"
1283 (g-uri-space (gensym))
1284 (body (third (parse-body forms env))))
1285 `(let ((,g-uri-space (or ,uri-space *uris*)))
1287 (flet ((,f (,var &optional ,g-ignore)
1288 (declare (ignore-if-unused ,var ,g-ignore))
1290 (maphash #',f ,g-uri-space))
1291 (return ,result-form)))))
1293 (defun sharp-u (stream chr arg)
1294 (declare (ignore chr arg))
1295 (let ((arg (read stream nil nil t)))
1299 then (parse-uri arg)
1302 (internal-reader-error
1304 "#u takes a string or list argument: ~s" arg)))))
1310 (locally (declare (special std-lisp-readtable))
1311 (let ((*readtable* std-lisp-readtable))
1312 (set-dispatch-macro-character #\# #\u #'puri::sharp-u)))
1314 (set-dispatch-macro-character #\# #\u #'puri::sharp-u)
1316 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1320 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1322 ;; (don't run under emacs with M-x fi:common-lisp)
1325 (eval-when (:compile-toplevel :load-toplevel :execute)
1329 (defun gc (&rest options)
1330 (declare (ignore options))
1335 (defun time-uri-module ()
1336 (declare (optimize (speed 3) (safety 0) (debug 0)))
1337 (let ((uri "http://www.franz.com/a/b;x;y;z/c/foo?bar=baz&xxx#foo")
1338 (uri2 "http://www.franz.com/a/b;x;y;z/c/%2ffoo?bar=baz&xxx#foo"))
1339 (gc t) (gc :tenure) (gc :tenure) (gc :tenure)
1340 (format t "~&;;; starting timing testing 1...~%")
1341 (time (dotimes (i 100000) (parse-uri uri)))
1343 (gc t) (gc :tenure) (gc :tenure) (gc :tenure)
1344 (format t "~&;;; starting timing testing 2...~%")
1345 (let ((uri (parse-uri uri)))
1346 (time (dotimes (i 100000)
1347 ;; forces no caching of the printed representation:
1348 (setf (uri-string uri) nil)
1349 (format nil "~a" uri))))
1351 (gc t) (gc :tenure) (gc :tenure) (gc :tenure)
1352 (format t "~&;;; starting timing testing 3...~%")
1355 (dotimes (i 100000) (parse-uri uri2))
1356 (let ((uri (parse-uri uri)))
1358 ;; forces no caching of the printed representation:
1359 (setf (uri-string uri) nil)
1360 (format nil "~a" uri)))))))
1362 ;;******** reference output (ultra, modified 5.0.1):
1363 ;;; starting timing testing 1...
1364 ; cpu time (non-gc) 13,710 msec user, 0 msec system
1365 ; cpu time (gc) 600 msec user, 10 msec system
1366 ; cpu time (total) 14,310 msec user, 10 msec system
1367 ; real time 14,465 msec
1369 ; 1,804,261 cons cells, 7 symbols, 41,628,832 other bytes, 0 static bytes
1370 ;;; starting timing testing 2...
1371 ; cpu time (non-gc) 27,500 msec user, 0 msec system
1372 ; cpu time (gc) 280 msec user, 20 msec system
1373 ; cpu time (total) 27,780 msec user, 20 msec system
1374 ; real time 27,897 msec
1376 ; 1,900,463 cons cells, 0 symbols, 17,693,712 other bytes, 0 static bytes
1377 ;;; starting timing testing 3...
1378 ; cpu time (non-gc) 52,290 msec user, 10 msec system
1379 ; cpu time (gc) 1,290 msec user, 30 msec system
1380 ; cpu time (total) 53,580 msec user, 40 msec system
1381 ; real time 54,062 msec
1383 ; 7,800,205 cons cells, 0 symbols, 81,697,496 other bytes, 0 static bytes
1385 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1386 ;;; after improving decode-escaped-encoding/encode-escaped-encoding:
1388 ;;; starting timing testing 1...
1389 ; cpu time (non-gc) 14,520 msec user, 0 msec system
1390 ; cpu time (gc) 400 msec user, 0 msec system
1391 ; cpu time (total) 14,920 msec user, 0 msec system
1392 ; real time 15,082 msec
1394 ; 1,800,270 cons cells, 0 symbols, 41,600,160 other bytes, 0 static bytes
1395 ;;; starting timing testing 2...
1396 ; cpu time (non-gc) 27,490 msec user, 10 msec system
1397 ; cpu time (gc) 300 msec user, 0 msec system
1398 ; cpu time (total) 27,790 msec user, 10 msec system
1399 ; real time 28,025 msec
1401 ; 1,900,436 cons cells, 0 symbols, 17,693,712 other bytes, 0 static bytes
1402 ;;; starting timing testing 3...
1403 ; cpu time (non-gc) 47,900 msec user, 20 msec system
1404 ; cpu time (gc) 920 msec user, 10 msec system
1405 ; cpu time (total) 48,820 msec user, 30 msec system
1406 ; real time 49,188 msec
1408 ; 3,700,215 cons cells, 0 symbols, 81,707,144 other bytes, 0 static bytes