]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - src/xtm/iri.lisp
Implement XTM role reification with native conformance evidence
[topic-maps.git] / src / xtm / iri.lisp
1 (in-package #:topic-maps.xtm)
2
3 ;; RFC 3986 5.2 algorithm, shared policy with CTM but no CTM dependency.
4 ;; Do not use PURI rendering here: it removes ports and rewrites locator spellings.
5
6 (defstruct iri-parts scheme authority (path "") query fragment)
7
8 (defun ascii-letter-p (c)
9 (or (char<= #\a c #\z) (char<= #\A c #\Z)))
10 (defun scheme-p (text)
11 (and (plusp (length text)) (ascii-letter-p (char text 0))
12 (every (lambda (c) (or (ascii-letter-p c) (find c "0123456789+-."))) text)))
13 (defun starts-with (prefix string)
14 (and (<= (length prefix) (length string))
15 (string= prefix string :end2 (length prefix))))
16
17 (defun split-iri (iri)
18 (let* ((fragment-position (position #\# iri))
19 (fragment (when fragment-position (subseq iri (1+ fragment-position))))
20 (remaining (subseq iri 0 fragment-position))
21 (query-position (position #\? remaining))
22 (query (when query-position (subseq remaining (1+ query-position))))
23 (head (subseq remaining 0 query-position))
24 (colon (position #\: head))
25 (scheme (when (and colon (scheme-p (subseq head 0 colon))) (subseq head 0 colon)))
26 (rest (if scheme (subseq head (1+ colon)) head))
27 (slash (when (starts-with "//" rest) (position #\/ rest :start 2)))
28 (authority (when (starts-with "//" rest) (subseq rest 2 slash)))
29 (path (if authority (if slash (subseq rest slash) "") rest)))
30 (make-iri-parts :scheme scheme :authority authority :path path :query query :fragment fragment)))
31
32 (defun join-iri (parts)
33 (with-output-to-string (out)
34 (when (iri-parts-scheme parts) (format out "~A:" (iri-parts-scheme parts)))
35 (when (iri-parts-authority parts) (format out "//~A" (iri-parts-authority parts)))
36 (write-string (iri-parts-path parts) out)
37 (when (iri-parts-query parts) (format out "?~A" (iri-parts-query parts)))
38 (when (iri-parts-fragment parts) (format out "#~A" (iri-parts-fragment parts)))))
39
40 (defun remove-dot-segments (path)
41 ;; RFC 3986 5.2.4, preserving empty segments and terminal slashes.
42 (let ((input path) (output ""))
43 (loop while (plusp (length input)) do
44 (cond ((starts-with "../" input) (setf input (subseq input 3)))
45 ((starts-with "./" input) (setf input (subseq input 2)))
46 ((starts-with "/./" input) (setf input (subseq input 2)))
47 ((string= input "/.") (setf input "/"))
48 ((or (starts-with "/../" input) (string= input "/.."))
49 (setf input (if (string= input "/..") "/" (subseq input 3))
50 output (subseq output 0 (or (position #\/ output :from-end t) 0))))
51 ((or (string= input ".") (string= input "..")) (setf input ""))
52 (t (let ((end (position #\/ input :start (if (char= (char input 0) #\/) 1 0))))
53 (setf output (concatenate 'string output (subseq input 0 end))
54 input (if end (subseq input end) ""))))))
55 output))
56
57 (defun resolve-iri (reference document node)
58 "XTM 4.3.4: percent UTF-8 decoding, then RFC 3986 resolution; never fetch."
59 (let* ((reference (validate-decoded-reference (unescape-reference reference node) node))
60 (r (split-iri reference)) (b (split-iri document))
61 (target (make-iri-parts :fragment (iri-parts-fragment r))))
62 (cond
63 ((iri-parts-scheme r)
64 (setf (iri-parts-scheme target) (iri-parts-scheme r)
65 (iri-parts-authority target) (iri-parts-authority r)
66 (iri-parts-path target) (remove-dot-segments (iri-parts-path r))
67 (iri-parts-query target) (iri-parts-query r)))
68 (t
69 (setf (iri-parts-scheme target) (iri-parts-scheme b))
70 (cond ((iri-parts-authority r)
71 (setf (iri-parts-authority target) (iri-parts-authority r)
72 (iri-parts-path target) (remove-dot-segments (iri-parts-path r))
73 (iri-parts-query target) (iri-parts-query r)))
74 (t
75 (setf (iri-parts-authority target) (iri-parts-authority b))
76 (if (zerop (length (iri-parts-path r)))
77 (setf (iri-parts-path target) (iri-parts-path b)
78 (iri-parts-query target) (or (iri-parts-query r) (iri-parts-query b)))
79 (setf (iri-parts-path target)
80 (remove-dot-segments
81 (if (starts-with "/" (iri-parts-path r))
82 (iri-parts-path r)
83 (concatenate 'string
84 (if (and (iri-parts-authority b) (zerop (length (iri-parts-path b))))
85 "/"
86 (subseq (iri-parts-path b) 0
87 (let ((slash (position #\/ (iri-parts-path b) :from-end t)))
88 (if slash (1+ slash) 0))))
89 (iri-parts-path r))))
90 (iri-parts-query target) (iri-parts-query r)))))))
91 (join-iri target)))
92
93 (defun unescape-reference (text node)
94 ;; XTM 4.3.4 explicitly decodes percent escapes before resolving, including
95 ;; reserved characters. Babel is an existing qualified CXML dependency.
96 (when (some (lambda (c) (or (<= (char-code c) 32) (find c "<>\"{}|^`\\"))) text)
97 (reject :invalid-iri "Illegal character in the original IRI reference." node))
98 (when (> (count #\# text) 1) (reject :invalid-iri "Multiple fragment delimiters." node))
99 (with-output-to-string (out)
100 (loop for i from 0 below (length text) do
101 (if (char= (char text i) #\%)
102 (let ((bytes nil))
103 (loop while (and (< i (length text)) (char= (char text i) #\%)) do
104 (unless (and (< (+ i 2) (length text))
105 (digit-char-p (char text (1+ i)) 16)
106 (digit-char-p (char text (+ i 2)) 16))
107 (reject :invalid-percent-escape "Expected two hexadecimal digits." node))
108 (push (+ (* 16 (digit-char-p (char text (1+ i)) 16))
109 (digit-char-p (char text (+ i 2)) 16)) bytes)
110 (incf i 3))
111 (handler-case
112 (write-string (babel:octets-to-string
113 (coerce (nreverse bytes) '(vector (unsigned-byte 8)))
114 :encoding :utf-8 :errorp t) out)
115 (babel:character-decoding-error ()
116 (reject :invalid-percent-utf8 "Percent escapes are not valid UTF-8." node)))
117 (decf i))
118 (write-char (char text i) out)))))
119
120 (defun require-document-iri (iri)
121 ;; Document IRI is already an absolute locator, not another escaped href.
122 (unless (and (stringp iri) (plusp (length iri))
123 (iri-parts-scheme (split-iri iri))
124 (not (find #\# iri))
125 (notany (lambda (c) (or (<= (char-code c) 32) (find c "<>\"{}|^`\\"))) iri))
126 (reject :invalid-document-iri "An explicit absolute, fragment-free Document IRI is required."))
127 iri)
128
129 (defun validate-decoded-reference (iri node)
130 ;; Percent-unescaping is required even for reserved characters. It must not
131 ;; introduce controls or malformed relative-path/fragment structure.
132 (let* ((parts (split-iri iri)) (path (iri-parts-path parts))
133 (first-segment (subseq path 0 (position #\/ path))))
134 (when (or (some (lambda (c) (or (< (char-code c) 32) (= (char-code c) 127))) iri)
135 (> (count #\# iri) 1)
136 (and (null (iri-parts-scheme parts)) (find #\: first-segment)))
137 (reject :invalid-iri "Invalid decoded IRI reference." node)))
138 iri)