]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - src/ctm/iri.lisp
docs(ctm): preserve Template Tail grammar investigation and evidence
[topic-maps.git] / src / ctm / iri.lisp
1 (in-package #:topic-maps.ctm)
2
3 (defstruct iri-parts scheme authority (path "") query fragment)
4
5 (defun ascii-letter-p (c)
6 (or (char<= #\a c #\z) (char<= #\A c #\Z)))
7 (defun scheme-p (text)
8 (and (plusp (length text)) (ascii-letter-p (char text 0))
9 (every (lambda (c) (or (ascii-letter-p c) (find c "0123456789+-."))) text)))
10 (defun starts-with (prefix string)
11 (and (<= (length prefix) (length string))
12 (string= prefix string :end2 (length prefix))))
13
14 (defun split-iri (iri)
15 (let* ((fragment-position (position #\# iri))
16 (fragment (when fragment-position (subseq iri (1+ fragment-position))))
17 (remaining (subseq iri 0 fragment-position))
18 (query-position (position #\? remaining))
19 (query (when query-position (subseq remaining (1+ query-position))))
20 (head (subseq remaining 0 query-position))
21 (colon (position #\: head))
22 (scheme (when (and colon (scheme-p (subseq head 0 colon))) (subseq head 0 colon)))
23 (rest (if scheme (subseq head (1+ colon)) head))
24 (slash (when (starts-with "//" rest) (position #\/ rest :start 2)))
25 (authority (when (starts-with "//" rest) (subseq rest 2 slash)))
26 (path (if authority (if slash (subseq rest slash) "") rest)))
27 (make-iri-parts :scheme scheme :authority authority :path path :query query :fragment fragment)))
28
29 (defun join-iri (parts)
30 (with-output-to-string (out)
31 (when (iri-parts-scheme parts) (format out "~A:" (iri-parts-scheme parts)))
32 (when (iri-parts-authority parts) (format out "//~A" (iri-parts-authority parts)))
33 (write-string (iri-parts-path parts) out)
34 (when (iri-parts-query parts) (format out "?~A" (iri-parts-query parts)))
35 (when (iri-parts-fragment parts) (format out "#~A" (iri-parts-fragment parts)))))
36
37 (defun remove-dot-segments (path)
38 ;; RFC 3986 5.2.4, preserving empty segments and terminal slashes.
39 (let ((input path) (output ""))
40 (loop while (plusp (length input)) do
41 (cond ((starts-with "../" input) (setf input (subseq input 3)))
42 ((starts-with "./" input) (setf input (subseq input 2)))
43 ((starts-with "/./" input) (setf input (subseq input 2)))
44 ((string= input "/.") (setf input "/"))
45 ((or (starts-with "/../" input) (string= input "/.."))
46 (setf input (if (string= input "/..") "/" (subseq input 3))
47 output (subseq output 0 (or (position #\/ output :from-end t) 0))))
48 ((or (string= input ".") (string= input "..")) (setf input ""))
49 (t (let ((end (position #\/ input :start (if (char= (char input 0) #\/) 1 0))))
50 (setf output (concatenate 'string output (subseq input 0 end))
51 input (if end (subseq input end) ""))))))
52 output))
53
54 (defun validate-iri-characters (iri position)
55 (loop for c across iri for index from 0 do
56 (when (or (<= (char-code c) 32) (= (char-code c) 127)
57 (find c "<>\"{}|^`\\") (<= #xd800 (char-code c) #xdfff))
58 (problem 'ctm-rejection :invalid-iri (+ position index) "Illegal IRI character.")))
59 (let* ((parts (split-iri iri)) (path (iri-parts-path parts))
60 (first-segment (subseq path 0 (position #\/ path))))
61 (when (or (> (count #\# iri) 1)
62 (and (null (iri-parts-scheme parts)) (find #\: first-segment)))
63 (problem 'ctm-rejection :invalid-iri position "Invalid relative path or fragment syntax.")))
64 iri)
65
66 (defun require-document-iri (iri)
67 (unless (stringp iri)
68 (problem 'ctm-rejection :invalid-document-iri 0 "An explicit absolute Document IRI is required."))
69 (validate-iri-characters iri 0)
70 (let ((parts (split-iri iri)))
71 (unless (and (iri-parts-scheme parts) (null (iri-parts-fragment parts)))
72 (problem 'ctm-rejection :invalid-document-iri 0
73 "Document IRI must be absolute and fragment-free (RFC 3987 absolute-IRI).")))
74 iri)
75
76 (defun resolve-iri (reference document position &key (unescape t))
77 "CTM 3.3.3 / RFC 3986 5.2. No fetching or case normalization.
78 UNESCAPE is NIL for existing locator strings in Include aliasing (3.16.2)."
79 (let* ((reference (validate-iri-characters (if unescape (unescape-iri reference position) reference) position))
80 (r (split-iri reference)) (b (split-iri document))
81 (target (make-iri-parts :fragment (iri-parts-fragment r))))
82 (cond
83 ((iri-parts-scheme r)
84 (setf (iri-parts-scheme target) (iri-parts-scheme r)
85 (iri-parts-authority target) (iri-parts-authority r)
86 (iri-parts-path target) (remove-dot-segments (iri-parts-path r))
87 (iri-parts-query target) (iri-parts-query r)))
88 (t
89 (setf (iri-parts-scheme target) (iri-parts-scheme b))
90 (cond ((iri-parts-authority r)
91 (setf (iri-parts-authority target) (iri-parts-authority r)
92 (iri-parts-path target) (remove-dot-segments (iri-parts-path r))
93 (iri-parts-query target) (iri-parts-query r)))
94 (t
95 (setf (iri-parts-authority target) (iri-parts-authority b))
96 (if (zerop (length (iri-parts-path r)))
97 (setf (iri-parts-path target) (iri-parts-path b)
98 (iri-parts-query target) (or (iri-parts-query r) (iri-parts-query b)))
99 (setf (iri-parts-path target)
100 (remove-dot-segments
101 (if (starts-with "/" (iri-parts-path r))
102 (iri-parts-path r)
103 (concatenate 'string
104 (if (and (iri-parts-authority b) (zerop (length (iri-parts-path b))))
105 "/"
106 (subseq (iri-parts-path b) 0
107 (let ((slash (position #\/ (iri-parts-path b) :from-end t)))
108 (if slash (1+ slash) 0))))
109 (iri-parts-path r))))
110 (iri-parts-query target) (iri-parts-query r)))))))
111 (join-iri target)))