-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsrc.lisp
1448 lines (1318 loc) · 51.7 KB
/
src.lisp
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;; -*- mode: common-lisp; package: puri -*-
;; Support for URIs
;; For general URI information see RFC2396.
;;
;; copyright (c) 1999-2002 Franz Inc, Berkeley, CA - All rights reserved.
;; copyright (c) 2002-2005 Franz Inc, Oakland, CA - All rights reserved.
;; copyright (c) 2003-2010 Kevin Rosenberg
;;
;; This code is free software; you can redistribute it and/or
;; modify it under the terms of the version 2.1 of
;; the GNU Lesser General Public License as published by
;; the Free Software Foundation, as clarified by the
;; preamble found here:
;; http://opensource.franz.com/preamble.html
;;
;; Versions ported from Franz's opensource release
;; uri.cl,v 2.3.6.4.2.1 2001/08/09 17:42:39 layer
;; uri.cl,v 2.9.84.1 2005/08/11 18:38:52 layer
;; This code 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
;; Lesser General Public License for more details.
(defpackage #:puri
(:use #:cl)
#-(or allegro zacl) (:nicknames #:net.uri)
(:export
#:uri ; the type and a function
#:uri-p
#:copy-uri
#:uri-scheme ; and slots
#:uri-host #:uri-port
#:uri-path
#:uri-query
#:uri-is-ip6
#:uri-fragment
#:uri-plist
#:uri-authority ; pseudo-slot accessor
#:urn ; class
#:urn-nid ; pseudo-slot accessor
#:urn-nss ; pseudo-slot accessor
#:*strict-parse*
#:parse-uri
#:merge-uris
#:enough-uri
#:uri-parsed-path
#:render-uri
#:make-uri-space ; interning...
#:uri-space
#:uri=
#:intern-uri
#:unintern-uri
#:do-all-uris
#:uri-parse-error ;; Added by KMR
))
(in-package #:puri)
(eval-when (:compile-toplevel) (declaim (optimize (speed 3))))
#-allegro
(defun parse-body (forms &optional env)
"Parses a body, returns (VALUES docstring declarations forms)"
(declare (ignore env))
;; fixme -- need to add parsing of multiple declarations
(let (docstring declarations)
(when (stringp (car forms))
(setq docstring (car forms))
(setq forms (cdr forms)))
(when (and (listp (car forms))
(symbolp (caar forms))
(string-equal (symbol-name '#:declare)
(symbol-name (caar forms))))
(setq declarations (car forms))
(setq forms (cdr forms)))
(values docstring declarations forms)))
(defun shrink-vector (str size)
#+allegro
(excl::.primcall 'sys::shrink-svector str size)
#+sbcl
(setq str (sb-kernel:shrink-vector str size))
#+cmu
(lisp::shrink-vector str size)
#+lispworks
(system::shrink-vector$vector str size)
#+scl
(common-lisp::shrink-vector str size)
#-(or allegro cmu lispworks sbcl scl)
(setq str (subseq str 0 size))
str)
;; KMR: Added new condition to handle cross-implementation variances
;; in the parse-error condition many implementations define
(define-condition uri-parse-error (parse-error)
((fmt-control :initarg :fmt-control :accessor fmt-control)
(fmt-arguments :initarg :fmt-arguments :accessor fmt-arguments ))
(:report (lambda (c stream)
(format stream "Parse error:")
(apply #'format stream (fmt-control c) (fmt-arguments c)))))
(defun .parse-error (fmt &rest args)
(error 'uri-parse-error :fmt-control fmt :fmt-arguments args))
#-allegro
(defun internal-reader-error (stream fmt &rest args)
(apply #'format stream fmt args))
#-allegro (defvar *current-case-mode* :case-insensitive-upper)
#+allegro (eval-when (:compile-toplevel :load-toplevel :execute)
(import '(excl:*current-case-mode*
excl:delimited-string-to-list
excl::parse-body
excl::internal-reader-error
excl:if*)))
#-allegro
(defmethod position-char (char (string string) start max)
(declare (optimize (speed 3) (safety 0) (space 0))
(fixnum start max) (string string))
(do* ((i start (1+ i)))
((= i max) nil)
(declare (fixnum i))
(when (char= char (char string i)) (return i))))
#-allegro
(defun delimited-string-to-list (string &optional (separator #\space)
skip-terminal)
(declare (optimize (speed 3) (safety 0) (space 0)
(compilation-speed 0))
(type string string)
(type character separator))
(do* ((len (length string))
(output '())
(pos 0)
(end (position-char separator string pos len)
(position-char separator string pos len)))
((null end)
(if (< pos len)
(push (subseq string pos) output)
(when (and (plusp len) (not skip-terminal))
(push "" output)))
(nreverse output))
(declare (type fixnum pos len)
(type (or null fixnum) end))
(push (subseq string pos end) output)
(setq pos (1+ end))))
#-allegro
(eval-when (:compile-toplevel :load-toplevel :execute)
(defvar if*-keyword-list '("then" "thenret" "else" "elseif"))
(defmacro if* (&rest args)
(do ((xx (reverse args) (cdr xx))
(state :init)
(elseseen nil)
(totalcol nil)
(lookat nil nil)
(col nil))
((null xx)
(cond ((eq state :compl)
`(cond ,@totalcol))
(t (error "if*: illegal form ~s" args))))
(cond ((and (symbolp (car xx))
(member (symbol-name (car xx))
if*-keyword-list
:test #'string-equal))
(setq lookat (symbol-name (car xx)))))
(cond ((eq state :init)
(cond (lookat (cond ((string-equal lookat "thenret")
(setq col nil
state :then))
(t (error
"if*: bad keyword ~a" lookat))))
(t (setq state :col
col nil)
(push (car xx) col))))
((eq state :col)
(cond (lookat
(cond ((string-equal lookat "else")
(cond (elseseen
(error
"if*: multiples elses")))
(setq elseseen t)
(setq state :init)
(push `(t ,@col) totalcol))
((string-equal lookat "then")
(setq state :then))
(t (error "if*: bad keyword ~s"
lookat))))
(t (push (car xx) col))))
((eq state :then)
(cond (lookat
(error
"if*: keyword ~s at the wrong place " (car xx)))
(t (setq state :compl)
(push `(,(car xx) ,@col) totalcol))))
((eq state :compl)
(cond ((not (string-equal lookat "elseif"))
(error "if*: missing elseif clause ")))
(setq state :init))))))
(defclass uri ()
(
;;;; external:
(scheme :initarg :scheme :initform nil :accessor uri-scheme)
(host :initarg :host :initform nil :accessor uri-host)
(port :initarg :port :initform nil :accessor uri-port)
(path :initarg :path :initform nil :accessor uri-path)
(query :initarg :query :initform nil :accessor uri-query)
(fragment :initarg :fragment :initform nil :accessor uri-fragment)
(plist :initarg :plist :initform nil :accessor uri-plist)
;;;; internal:
(escaped
;; used to prevent unnessary work, looking for chars to escape and
;; unescape.
:initarg :escaped :initform nil :accessor uri-escaped)
(string
;; the cached printable representation of the URI. It *might* be
;; different than the original string, though, because the user might
;; have escaped non-reserved chars--they won't be escaped when the URI
;; is printed.
:initarg :string :initform nil :accessor uri-string)
(parsed-path
;; the cached parsed representation of the URI path.
:initarg :parsed-path
:initform nil
:accessor .uri-parsed-path)
(is-ip6
:initarg :is-ip6
:initform nil
:accessor uri-is-ip6)
(hashcode
;; cached sxhash, so we don't have to compute it more than once.
:initarg :hashcode :initform nil :accessor uri-hashcode)))
(defclass urn (uri)
((nid :initarg :nid :initform nil :accessor urn-nid)
(nss :initarg :nss :initform nil :accessor urn-nss)))
(eval-when (:compile-toplevel :execute)
(defmacro clear-caching-on-slot-change (name)
`(defmethod (setf ,name) :around (new-value (self uri))
(declare (ignore new-value))
(prog1 (call-next-method)
(setf (uri-string self) nil)
,@(when (eq name 'uri-path) `((setf (.uri-parsed-path self) nil)))
(setf (uri-hashcode self) nil))))
)
(clear-caching-on-slot-change uri-scheme)
(clear-caching-on-slot-change uri-host)
(clear-caching-on-slot-change uri-port)
(clear-caching-on-slot-change uri-path)
(clear-caching-on-slot-change uri-query)
(clear-caching-on-slot-change uri-fragment)
(defmethod make-load-form ((self uri) &optional env)
(declare (ignore env))
`(make-instance ',(class-name (class-of self))
:scheme ,(uri-scheme self)
:host ,(uri-host self)
:port ,(uri-port self)
:path ',(uri-path self)
:query ,(uri-query self)
:fragment ,(uri-fragment self)
:plist ',(uri-plist self)
:string ,(uri-string self)
:parsed-path ',(.uri-parsed-path self)))
(defmethod uri-p ((thing uri)) t)
(defmethod uri-p ((thing t)) nil)
(defun copy-uri (uri
&key place
(scheme (when uri (uri-scheme uri)))
(host (when uri (uri-host uri)))
(port (when uri (uri-port uri)))
(path (when uri (uri-path uri)))
(parsed-path
(when uri (copy-list (.uri-parsed-path uri))))
(query (when uri (uri-query uri)))
(fragment (when uri (uri-fragment uri)))
(plist (when uri (copy-list (uri-plist uri))))
(class (when uri (class-of uri)))
&aux (escaped (when uri (uri-escaped uri))))
(if* place
then (setf (uri-scheme place) scheme)
(setf (uri-host place) host)
(setf (uri-port place) port)
(setf (uri-path place) path)
(setf (.uri-parsed-path place) parsed-path)
(setf (uri-query place) query)
(setf (uri-fragment place) fragment)
(setf (uri-plist place) plist)
(setf (uri-escaped place) escaped)
(setf (uri-string place) nil)
(setf (uri-hashcode place) nil)
place
elseif (eq 'uri class)
then ;; allow the compiler to optimize the call to make-instance:
(make-instance 'uri
:scheme scheme :host host :port port :path path
:parsed-path parsed-path
:query query :fragment fragment :plist plist
:escaped escaped :string nil :hashcode nil)
else (make-instance class
:scheme scheme :host host :port port :path path
:parsed-path parsed-path
:query query :fragment fragment :plist plist
:escaped escaped :string nil :hashcode nil)))
(defmethod uri-parsed-path ((uri uri))
(when (uri-path uri)
(when (null (.uri-parsed-path uri))
(setf (.uri-parsed-path uri)
(parse-path (uri-path uri) (uri-escaped uri))))
(.uri-parsed-path uri)))
(defmethod (setf uri-parsed-path) (path-list (uri uri))
(assert (and (consp path-list)
(or (member (car path-list) '(:absolute :relative)
:test #'eq))))
(setf (uri-path uri) (render-parsed-path path-list t))
(setf (.uri-parsed-path uri) path-list)
path-list)
(defun uri-authority (uri)
(when (uri-host uri)
(let ((*print-pretty* nil))
(format nil "~a~@[:~a~]" (uri-host uri) (uri-port uri)))))
(defun uri-nid (uri)
(if* (equalp "urn" (uri-scheme uri))
then (uri-host uri)
else (error "URI is not a URN: ~s." uri)))
(defun uri-nss (uri)
(if* (equalp "urn" (uri-scheme uri))
then (uri-path uri)
else (error "URI is not a URN: ~s." uri)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Parsing
(defparameter *excluded-characters*
(append
;; exclude control characters
(loop for i from 0 to #x1f
collect (code-char i))
'(;; `delims' (except #\%, because it's handled specially):
#\< #\> #\" #\space #\#
#\Rubout ;; (code-char #x7f)
;; `unwise':
#\{ #\} #\| #\\ #\^ #\[ #\] #\`))
"Excluded charcters from RFC2396 (http://www.ietf.org/rfc/rfc2396.txt 2.4.3)")
(defun reserved-char-vector (chars &key except)
(do* ((a (make-array 128 :element-type 'bit :initial-element 0))
(chars chars (cdr chars))
(c (car chars) (car chars)))
((null chars) a)
(if* (and except (member c except :test #'char=))
thenret
else (setf (sbit a (char-int c)) 1))))
(defparameter *reserved-characters*
(reserved-char-vector
(append *excluded-characters*
'(#\; #\/ #\? #\: #\@ #\& #\= #\+ #\$ #\, #\%))))
(defparameter *reserved-authority-characters*
(reserved-char-vector
(append *excluded-characters* '(#\; #\/ #\? #\: #\@))))
(defparameter *reserved-path-characters*
(reserved-char-vector
(append *excluded-characters*
'(#\; #\%
;;;;The rfc says this should be here, but it doesn't make sense.
;; #\=
#\/ #\?))))
(defparameter *reserved-fragment-characters*
(reserved-char-vector (remove #\# *excluded-characters*)))
(eval-when (:compile-toplevel :execute)
(defun gen-char-range-list (start end)
(do* ((res '())
(endcode (1+ (char-int end)))
(chcode (char-int start)
(1+ chcode))
(hyphen nil))
((= chcode endcode)
;; - has to be first, otherwise it signifies a range!
(if* hyphen
then (setq res (nreverse res))
(push #\- res)
res
else (nreverse res)))
(if* (= #.(char-int #\-) chcode)
then (setq hyphen t)
else (push (code-char chcode) res))))
)
(defparameter *valid-nid-characters*
(reserved-char-vector
'#.(nconc (gen-char-range-list #\a #\z)
(gen-char-range-list #\A #\Z)
(gen-char-range-list #\0 #\9)
'(#\- #\. #\+))))
(defparameter *reserved-nss-characters*
(reserved-char-vector
(append *excluded-characters* '(#\& #\~ #\/ #\?))))
(defparameter *illegal-characters*
(reserved-char-vector (set-difference *excluded-characters*
'(#\# #\[ #\]))))
(defparameter *strict-illegal-query-characters*
(reserved-char-vector (append '(#\?) (remove #\# *excluded-characters*))))
(defparameter *illegal-query-characters*
(reserved-char-vector
*excluded-characters* :except '(#\^ #\| #\#)))
(defparameter *valid-ip6-characters*
(reserved-char-vector
'#.(nconc (gen-char-range-list #\a #\f)
(gen-char-range-list #\A #\F)
(gen-char-range-list #\0 #\9)
'(#\: #\]))))
(defun parse-uri (thing &key (class 'uri) &aux escape)
(when (uri-p thing) (return-from parse-uri thing))
(setq escape (escape-p thing))
(multiple-value-bind (scheme host port path query fragment is-ip6)
(parse-uri-string thing)
(when scheme
(setq scheme
(intern (funcall
(case *current-case-mode*
((:case-insensitive-upper :case-sensitive-upper)
#'string-upcase)
((:case-insensitive-lower :case-sensitive-lower)
#'string-downcase))
(decode-escaped-encoding scheme escape))
(find-package :keyword))))
(when (and scheme (eq :urn scheme))
(return-from parse-uri
(make-instance 'urn :scheme scheme :nid host :nss path)))
(when host (setq host (decode-escaped-encoding host escape)))
(when port
(setq port (read-from-string port))
(when (not (numberp port)) (error "port is not a number: ~s." port))
(when (not (plusp port))
(error "port is not a positive integer: ~d." port))
(when (eql port (case scheme
(:http 80)
(:https 443)
(:ftp 21)
(:telnet 23)))
(setq port nil)))
(when (or (string= "" path)
(and ;; we canonicalize away a reference to just /:
scheme
(member scheme '(:http :https :ftp) :test #'eq)
(string= "/" path)))
(setq path nil))
(when path
(setq path
(decode-escaped-encoding path escape *reserved-path-characters*)))
(when query (setq query (decode-escaped-encoding query escape)))
(when fragment
(setq fragment
(decode-escaped-encoding fragment escape
*reserved-fragment-characters*)))
(if* (eq 'uri class)
then ;; allow the compiler to optimize the make-instance call:
(make-instance 'uri
:scheme scheme
:host host
:is-ip6 is-ip6
:port port
:path path
:query query
:fragment fragment
:escaped escape)
else ;; do it the slow way:
(make-instance class
:scheme scheme
:host host
:is-ip6 is-ip6
:port port
:path path
:query query
:fragment fragment
:escaped escape))))
(defmethod uri ((thing uri))
thing)
(defmethod uri ((thing string))
(parse-uri thing))
(defmethod uri ((thing t))
(error "Cannot coerce ~s to a uri." thing))
(defvar *strict-parse* t)
(defun parse-uri-string (string &aux (illegal-chars *illegal-characters*))
(declare (optimize (speed 3)))
;; Speed is important, so use a specialized state machine instead of
;; regular expressions for parsing the URI string. The regexp we are
;; simulating:
;; ^(([^:/?#]+):)?
;; (//([^/?#]*))?
;; May include a []-pair for ipv6
;; ([^?#]*)
;; (\?([^#]*))?
;; (#(.*))?
(let* ((state 0)
(start 0)
(end (length string))
(tokval nil)
(scheme nil)
(host nil)
(is-ip6 nil)
(port nil)
(path-components '())
(query nil)
(fragment nil)
;; namespace identifier, for urn parsing only:
(nid nil))
(declare (fixnum state start end))
(flet ((read-token (kind &optional legal-chars)
(setq tokval nil)
(if* (>= start end)
then :end
else (let ((sindex start)
(res nil)
c)
(declare (fixnum sindex))
(setq res
(loop
(when (>= start end) (return nil))
(setq c (char string start))
(let ((ci (char-int c)))
(if* legal-chars
then (if* (and (eq :colon kind) (eq c #\:))
then (return :colon)
elseif (= 0 (sbit legal-chars ci))
then (.parse-error
"~
URI ~s contains illegal character ~s at position ~d."
string c start))
elseif (and (< ci 128)
*strict-parse*
(= 1 (sbit illegal-chars ci)))
then (.parse-error "~
URI ~s contains illegal character ~s at position ~d."
string c start)))
(case kind
(:path (case c
(#\? (return :question))
(#\# (return :hash))))
(:query (case c (#\# (return :hash))))
(:ip6 (case c
(#\] (return :close-bracket))))
(:rest)
(t (case c
(#\: (return :colon))
(#\? (return :question))
(#\[ (return :open-bracket))
(#\] (return :close-bracket))
(#\# (return :hash))
(#\/ (return :slash)))))
(incf start)))
(if* (> start sindex)
then ;; we found some chars
;; before we stopped the parse
(setq tokval (subseq string sindex start))
:string
else ;; immediately stopped at a special char
(incf start)
res))))
(failure (&optional why)
(.parse-error "illegal URI: ~s [~d]~@[: ~a~]"
string state why))
(impossible ()
(.parse-error "impossible state: ~d [~s]" state string)))
(loop
(case state
(0 ;; starting to parse
(ecase (read-token t)
(:colon (failure))
(:question (setq state 7))
(:hash (setq state 8))
(:slash (setq state 3))
(:string (setq state 1))
(:end (setq state 9))))
(1 ;; seen <token><special char>
(let ((token tokval))
(ecase (read-token t)
(:colon (setq scheme token)
(if* (equalp "urn" scheme)
then (setq state 15)
else (setq state 2)))
(:question (push token path-components)
(setq state 7))
(:hash (push token path-components)
(setq state 8))
(:slash (push token path-components)
(push "/" path-components)
(setq state 6))
(:string (failure))
(:end (push token path-components)
(setq state 9)))))
(2 ;; seen <scheme>:
(ecase (read-token t)
(:colon (failure))
(:question (setq state 7))
(:hash (setq state 8))
(:slash (setq state 3))
(:string (setq state 10))
(:end (setq state 9))))
(10 ;; seen <scheme>:<token>
(let ((token tokval))
(ecase (read-token t)
(:colon (failure))
(:question (push token path-components)
(setq state 7))
(:hash (push token path-components)
(setq state 8))
(:slash (push token path-components)
(setq state 6))
(:string (failure))
(:end (push token path-components)
(setq state 9)))))
(3 ;; seen / or <scheme>:/
(ecase (read-token t)
(:colon (failure))
(:question (push "/" path-components)
(setq state 7))
(:hash (push "/" path-components)
(setq state 8))
(:slash (setq state 4))
(:string (push "/" path-components)
(push tokval path-components)
(setq state 6))
(:end (push "/" path-components)
(setq state 9))))
(66 ;; seen [<scheme>:]//[
(ecase (read-token :ip6 *valid-ip6-characters*)
(:string (setq host tokval)
(setq is-ip6 t)
(setq state 67))))
(67 ;; seen [<scheme>:]//[ip6]
(ecase (read-token t)
(:close-bracket (setq state 11))))
(4 ;; seen [<scheme>:]//
(ecase (read-token t)
(:colon (failure))
(:question (failure))
(:hash (failure))
(:open-bracket (setq state 66))
(:slash
(if* (and (equalp "file" scheme)
(null host))
then ;; file:///...
(push "/" path-components)
(setq state 6)
else (failure)))
(:string (setq host tokval)
(setq state 11))
(:end (failure))))
(11 ;; seen [<scheme>:]//<host>
(ecase (read-token t)
(:colon (setq state 5))
(:question (setq state 7))
(:hash (setq state 8))
(:slash (push "/" path-components)
(setq state 6))
(:string (impossible))
(:end (setq state 9))))
(5 ;; seen [<scheme>:]//<host>:
(ecase (read-token t)
(:colon (failure))
(:question (failure))
(:hash (failure))
(:slash (push "/" path-components)
(setq state 6))
(:string (setq port tokval)
(setq state 12))
(:end (failure))))
(12 ;; seen [<scheme>:]//<host>:[<port>]
(ecase (read-token t)
(:colon (failure))
(:question (setq state 7))
(:hash (setq state 8))
(:slash (push "/" path-components)
(setq state 6))
(:string (impossible))
(:end (setq state 9))))
(6 ;; seen /
(ecase (read-token :path)
(:question (setq state 7))
(:hash (setq state 8))
(:string (push tokval path-components)
(setq state 13))
(:end (setq state 9))))
(13 ;; seen path
(ecase (read-token :path)
(:question (setq state 7))
(:hash (setq state 8))
(:string (impossible))
(:end (setq state 9))))
(7 ;; seen ?
(setq illegal-chars
(if* *strict-parse*
then *strict-illegal-query-characters*
else *illegal-query-characters*))
(ecase (prog1 (read-token :query)
(setq illegal-chars *illegal-characters*))
(:hash (setq state 8))
(:string (setq query tokval)
(setq state 14))
(:end (setq state 9))))
(14 ;; query
(ecase (read-token :query)
(:hash (setq state 8))
(:string (impossible))
(:end (setq state 9))))
(8 ;; seen #
(ecase (read-token :rest)
(:string (setq fragment tokval)
(setq state 9))
(:end (setq state 9))))
(9 ;; done
(return
(values
scheme host port
(apply #'concatenate 'string (nreverse path-components))
query fragment is-ip6)))
;; URN parsing:
(15 ;; seen urn:, read nid now
(case (read-token :colon *valid-nid-characters*)
(:string (setq nid tokval)
(setq state 16))
(t (failure "missing namespace identifier"))))
(16 ;; seen urn:<nid>
(case (read-token t)
(:colon (setq state 17))
(t (failure "missing namespace specific string"))))
(17 ;; seen urn:<nid>:, rest is nss
(return (values scheme
nid
nil
(progn
(setq illegal-chars *reserved-nss-characters*)
(read-token :rest)
tokval))))
(t (.parse-error
"internal error in parse engine, wrong state: ~s." state)))))))
(defun escape-p (string)
(declare (optimize (speed 3)))
(do* ((i 0 (1+ i))
(max (the fixnum (length string))))
((= i max) nil)
(declare (fixnum i max))
(when (char= #\% (char string i))
(return t))))
(defun parse-path (path-string escape)
(do* ((xpath-list (delimited-string-to-list path-string #\/))
(path-list
(progn
(if* (string= "" (car xpath-list))
then (setf (car xpath-list) :absolute)
else (push :relative xpath-list))
xpath-list))
(pl (cdr path-list) (cdr pl))
segments)
((null pl) path-list)
(if* (cdr (setq segments
(if* (string= "" (car pl))
then '("")
else (delimited-string-to-list (car pl) #\;))))
then ;; there is a param
(setf (car pl)
(mapcar #'(lambda (s)
(decode-escaped-encoding s escape
;; decode all %xx:
nil))
segments))
else ;; no param
(setf (car pl)
(decode-escaped-encoding (car segments) escape
;; decode all %xx:
nil)))))
(defun decode-escaped-encoding (string escape
&optional (reserved-chars
*reserved-characters*))
;; Return a string with the real characters.
(when (null escape) (return-from decode-escaped-encoding string))
(do* ((i 0 (1+ i))
(max (length string))
(new-string (copy-seq string))
(new-i 0 (1+ new-i))
ch ch2 chc chc2)
((= i max)
(shrink-vector new-string new-i))
(if* (char= #\% (setq ch (char string i)))
then (when (> (+ i 3) max)
(.parse-error
"Unsyntactic escaped encoding in ~s." string))
(setq ch (char string (incf i)))
(setq ch2 (char string (incf i)))
(when (not (and (setq chc (digit-char-p ch 16))
(setq chc2 (digit-char-p ch2 16))))
(.parse-error
"Non-hexidecimal digits after %: %c%c." ch ch2))
(let ((ci (+ (* 16 chc) chc2)))
(if* (or (null reserved-chars)
(> ci 127) ; bug11527
(= 0 (sbit reserved-chars ci)))
then ;; ok as is
(setf (char new-string new-i)
(code-char ci))
else (setf (char new-string new-i) #\%)
(setf (char new-string (incf new-i)) ch)
(setf (char new-string (incf new-i)) ch2)))
else (setf (char new-string new-i) ch))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; Printing
(defun render-uri (uri stream
&aux (escape (uri-escaped uri))
(*print-pretty* nil))
(when (null (uri-string uri))
(setf (uri-string uri)
(let ((scheme (uri-scheme uri))
(host (uri-host uri))
(is-ip6 (uri-is-ip6 uri))
(port (uri-port uri))
(path (uri-path uri))
(query (uri-query uri))
(fragment (uri-fragment uri)))
(concatenate 'string
(when scheme
(encode-escaped-encoding
(string-downcase ;; for upper case lisps
(symbol-name scheme))
*reserved-characters* escape))
(when scheme ":")
(when (or host (eq :file scheme)) "//")
(when is-ip6 "[")
(when host
(encode-escaped-encoding
host *reserved-authority-characters* escape))
(when is-ip6 "]")
(when port ":")
(when port
#-allegro (format nil "~D" port)
#+allegro (with-output-to-string (s)
(excl::maybe-print-fast s port))
)
(encode-escaped-encoding (or path "/")
nil
;;*reserved-path-characters*
escape)
(when query "?")
(when query (encode-escaped-encoding query nil escape))
(when fragment "#")
(when fragment (encode-escaped-encoding fragment nil escape))))))
(if* stream
then (format stream "~a" (uri-string uri))
else (uri-string uri)))
(defun render-parsed-path (path-list escape)
(do* ((res '())
(first (car path-list))
(pl (cdr path-list) (cdr pl))
(pe (car pl) (car pl)))
((null pl)
(when res (apply #'concatenate 'string (nreverse res))))
(when (or (null first)
(prog1 (eq :absolute first)
(setq first nil)))
(push "/" res))
(if* (atom pe)
then (push
(encode-escaped-encoding pe *reserved-path-characters* escape)
res)
else ;; contains params
(push (encode-escaped-encoding
(car pe) *reserved-path-characters* escape)
res)
(dolist (item (cdr pe))
(push ";" res)
(push (encode-escaped-encoding
item *reserved-path-characters* escape)
res)))))
(defun render-urn (urn stream
&aux (*print-pretty* nil))
(when (null (uri-string urn))
(setf (uri-string urn)
(let ((nid (urn-nid urn))
(nss (urn-nss urn)))
(concatenate 'string "urn:" nid ":" nss))))
(if* stream
then (format stream "~a" (uri-string urn))
else (uri-string urn)))
(defparameter *escaped-encoding*
(vector #\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9 #\a #\b #\c #\d #\e #\f))
(defun encode-escaped-encoding (string reserved-chars escape)
(when (null escape) (return-from encode-escaped-encoding string))
;; Make a string as big as it possibly needs to be (3 times the original
;; size), and truncate it at the end.
(do* ((max (length string))
(new-max (* 3 max)) ;; worst case new size
(new-string (make-string new-max))
(i 0 (1+ i))
(new-i -1)
c ci)
((= i max)
(shrink-vector new-string (incf new-i)))
(setq ci (char-int (setq c (char string i))))
(if* (or (null reserved-chars)
(> ci 127)
(= 0 (sbit reserved-chars ci)))
then ;; ok as is
(incf new-i)
(setf (char new-string new-i) c)
else ;; need to escape it
(multiple-value-bind (q r) (truncate ci 16)
(setf (char new-string (incf new-i)) #\%)
(setf (char new-string (incf new-i)) (elt *escaped-encoding* q))
(setf (char new-string (incf new-i))
(elt *escaped-encoding* r))))))
(defmethod print-object ((uri uri) stream)
(if* *print-escape*
then (print-unreadable-object (uri stream :type t) (render-uri uri stream))
else (render-uri uri stream)))
(defmethod print-object ((urn urn) stream)
(if* *print-escape*
then (print-unreadable-object (urn stream :type t) (render-urn urn stream))
else (render-urn urn stream)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; merging and unmerging
(defmethod merge-uris ((uri string) (base string) &optional place)
(merge-uris (parse-uri uri) (parse-uri base) place))
(defmethod merge-uris ((uri uri) (base string) &optional place)
(merge-uris uri (parse-uri base) place))
(defmethod merge-uris ((uri string) (base uri) &optional place)
(merge-uris (parse-uri uri) base place))
(defmethod merge-uris ((uri uri) (base uri) &optional place)
;; See ../doc/rfc2396.txt for info on the algorithm we use to merge
;; URIs.
;;
(tagbody
;;;; step 2
(when (and (null (uri-parsed-path uri))
(null (uri-scheme uri))
(null (uri-host uri))
(null (uri-port uri))
(null (uri-query uri)))
(return-from merge-uris
(let ((new (copy-uri base :place place)))
(when (uri-query uri)
(setf (uri-query new) (uri-query uri)))