;; uri-generic version based on the abnf egg ;; Needs lexgen v3.5+ and abnf v3.1+ (provide 'uri-generic) (module uri-generic (uri-reference update-uri update-authority uri-reference? uri-auth uri-authority uri-scheme uri-path uri-query uri-fragment uri-host uri-port uri-username uri-password authority? authority-host authority-port authority-username authority-password uri? absolute-uri absolute-uri? uri->string uri->list relative-ref? uri-relative-to uri-relative-from uri-decode-string uri-encode-string uri-normalize-case uri-normalize-path-segments uri-path-absolute? uri-path-relative? char-set:gen-delims char-set:sub-delims char-set:uri-reserved char-set:uri-unreserved) (import chicken scheme extras data-structures ports) (require-library regex) (import irregex) (require-library lexgen) (import (prefix lexgen lg:)) (require-extension abnf-charlist abnf-consumers defstruct srfi-1 srfi-4 srfi-13 srfi-14) ;; What to do with these? #;(cond-expand (utf8-strings (use utf8-srfi-13 utf8-srfi-14)) (else (use srfi-13 srfi-14))) (defstruct URI scheme authority path query fragment) (defstruct URIAuth username password host port) (define-record-printer (URI x out) (fprintf out "#(URI scheme=~S authority=~A path=~S query=~S fragment=~S)" (URI-scheme x) (URI-authority x) (URI-path x) (URI-query x) (URI-fragment x))) (define-record-printer (URIAuth x out) (fprintf out "#(URIAuth host=~S port=~A)" (URIAuth-host x) (URIAuth-port x))) (define uri-reference? URI?) (define uri-auth URI-authority ) (define uri-authority URI-authority ) (define uri-scheme URI-scheme ) (define uri-path URI-path ) (define uri-query URI-query ) (define uri-fragment URI-fragment ) (define (uri-host x) (let ((auth (URI-authority x))) (and auth (URIAuth-host auth)))) (define (uri-port x) (let ((auth (URI-authority x))) (and auth (URIAuth-port auth)))) (define (uri-username x) (let ((auth (URI-authority x))) (and auth (URIAuth-username auth)))) (define (uri-password x) (let ((auth (URI-authority x))) (and auth (URIAuth-password auth)))) (define authority? URIAuth?) (define authority-host URIAuth-host) (define authority-port URIAuth-port) (define authority-username URIAuth-username) (define authority-password URIAuth-password) (define update-authority update-URIAuth) (define update-uri (let ((unset (list 'unset))) (lambda (uri . key/values) (apply (lambda (#!key (scheme (URI-scheme uri)) (path (URI-path uri)) (query (URI-query uri)) (fragment (URI-fragment uri)) (auth unset) (authority unset)) (let* ((base-auth (or (cond ((not (eq? unset auth)) auth) ((not (eq? unset authority)) authority) (else (URI-authority uri))) (make-URIAuth))) (updated-auth (apply update-authority base-auth key/values)) (final-auth (if (equal? (make-URIAuth) updated-auth) #f updated-auth))) (make-URI scheme: scheme path: path query: query fragment: fragment authority: final-auth))) key/values)))) (define (uri? u) (and (uri-reference? u) (uri-scheme u) #t)) (define (relative-ref? u) (and (uri-reference? u) (not (uri-scheme u)))) (define (absolute-uri? u) (and (uri-reference? u) (not (relative-ref? u)) (not (uri-fragment u)))) ;; RFC3986, section 2.2 ;; ;; Reserved characters. ;; (define char-set:gen-delims (string->char-set ":/?#[]@")) (define char-set:sub-delims (string->char-set "!$&'()*+,;=")) (define char-set:uri-reserved (char-set-union char-set:gen-delims char-set:sub-delims)) (define sub-delims (set char-set:sub-delims)) ;; RFC3986, section 2.3 ;; ;; "Unreserved" characters. ;; (define char-set:uri-unreserved (char-set-union char-set:letter+digit (string->char-set "-_.~"))) (define unreserved (set char-set:uri-unreserved)) ;; Turns a URI into a string. ;; ;; Uses a supplied function to map the userinfo part of the URI. ;; (define (uri->string uri . rest) (let-optionals rest ((userinfomap (lambda (u pw) (string-append u ":******" )))) (with-output-to-string (lambda () (display-fragments `(,(and-let* ((scheme (uri-scheme uri))) (list scheme ":")) ,(and-let* ((auth (URI-authority uri)) (host (URIAuth-host auth))) (let ((username (URIAuth-username auth))) (list "//" (and username (list (userinfomap username (URIAuth-password auth)) "@")) host (and (URIAuth-port auth) (list ":" (URIAuth-port auth)))))) ,(path->string (uri-path uri)) ,(and-let* ((query (uri-query uri))) (list "?" query)) ,(and-let* ((fragment (uri-fragment uri))) (list "#" fragment)))))))) (define uri-decode-string (let ((re (irregex `(seq #\% hex-digit hex-digit)))) (lambda (str #!optional (char-set char-set:full)) (irregex-replace/all re str (lambda (match) (let* ((encoded (irregex-match-substring match)) (decoded (integer->char (string->number (string-drop encoded 1) 16)))) (if (char-set-contains? char-set decoded) (string decoded) encoded))))))) (define (display-fragments b) (let loop ((fragments b)) (cond ((null? fragments) (begin)) ((not (car fragments)) (loop (cdr fragments) )) ((null? (car fragments)) (loop (cdr fragments) )) ((pair? (car fragments)) (begin (loop (car fragments)) (loop (cdr fragments) ))) (else (display (car fragments)) (loop (cdr fragments) ))))) (define (path->string path) (cond ((null? path) "") ((eq? '/ (car path)) (string-append "/" (join-segments (cdr path)))) ((protect? (car path)) (join-segments (cons "." path))) (else (join-segments path)))) (define (join-segments segments) (string-intersperse (map (lambda (segment) (list) (uri-encode-string segment (char-set #\/))) segments) "/")) ;; Section 4.2; if the first segment contains a colon, it must be prefixed "./" (define (protect? sa) (string-index sa #\:)) ; specific: ((uri-authority uri) (uri-path uri) (uri-query uri)). (define (uri->list uri . rest) (let-optionals rest ((userinfomap (lambda (u pw) (string-append u ":******" )))) `(,(URI-scheme uri) (,(uri-auth->list (URI-authority uri) userinfomap) ,(URI-path uri) ,(URI-query uri)) ,(URI-fragment uri)))) (define (uri-auth->list uri-auth userinfomap) (and uri-auth `(,(and-let* ((user (URIAuth-username uri-auth)) (pass (URIAuth-password uri-auth))) (userinfomap user pass)) ,(URIAuth-host uri-auth) ,(URIAuth-port uri-auth)))) (define (uri-normalize-case uri) (let* ((normalized-uri (uri-reference (normalize-pct-encoding (uri->string uri (lambda (user pass) (conc user ":" pass)))))) (scheme (string->symbol (string-downcase (->string (uri-scheme uri))))) (host (normalize-pct-encoding (string-downcase (uri-host uri))))) (update-uri normalized-uri scheme: scheme host: host))) ;; RFC3986, section 2.2 ;; ;; Percent encoding ;; (define pct-encoded (concatenation (char #\%) hexadecimal hexadecimal)) ;; RFC 3986, section 2.1 ;; ;; Returns a 'pct-encoded' sequence of octets. ;; (define (uri-encode-string str #!optional (char-set (char-set-complement char-set:uri-unreserved))) (define (hex-digit i) (and (>= i 0) (< i 16) (car (string->list (string-upcase (number->string i 16)))))) (define (pct-encode c) (let ((i (char->integer c))) `(#\% ,(hex-digit (quotient i 16)) ,(hex-digit (remainder i 16))))) (list->string (string-fold-right (lambda (c res) (if (char-set-contains? char-set c) (append (pct-encode c) res) (cons c res))) '() str))) (define normalize-pct-encoding (let ((re (irregex `(seq #\% hex-digit hex-digit))) (char-set char-set:uri-unreserved)) (lambda (str) (irregex-replace/all re str (lambda (match) (let* ((encoded (irregex-match-substring match)) (decoded (integer->char (string->number (string-drop encoded 1) 16)))) (if (char-set-contains? char-set decoded) (string decoded) (string-upcase encoded)))))))) (define (alist->uri contents) (make-URI scheme: (alist-ref 'scheme contents) authority: (let ((user (alist-ref 'username contents)) (pass (alist-ref 'password contents)) (host (alist-ref 'host contents)) (port (alist-ref 'port contents))) (and (or user pass host port) (make-URIAuth username: user password: pass host: host port: port))) path: (alist-ref 'path contents) query: (alist-ref 'query contents) fragment: (alist-ref 'fragment contents))) ;; Reference, Relative and Absolute URI forms ;; ;; RFC3986, section 4.1 (define (uri-reference s) (and-let* ((decoded (uri-decode-string s char-set:uri-unreserved)) (res (or (lg:lex uri (lambda _ (list)) decoded) (lg:lex relative-ref (lambda _ (list)) decoded))) ((null? (cadr res))) (alist (car res))) (alist->uri alist))) ;; RFC3986, section 3.2.2 ;; ;; host = IP-literal / IPv4address / reg-name ;; ;; IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet ;; dec-octet = DIGIT ; 0-9 ;; / %x31-39 DIGIT ; 10-99 ;; / "1" 2DIGIT ; 100-199 ;; / "2" %x30-34 DIGIT ; 200-249 ;; / "25" %x30-35 ; 250-255 (define dec-octet (alternatives decimal (concatenation (range #x31 #x39) decimal) (concatenation (char #\1) decimal decimal) (concatenation (char #\2) (range #x30 #x34) decimal) (concatenation (char #\2) (char #\5) (range #x30 #x35)))) (define ipv4-address (concatenation dec-octet (char #\.) dec-octet (char #\.) dec-octet (char #\.) dec-octet)) ;; IPv6address = 6( h16 ":" ) ls32 ;; / "::" 5( h16 ":" ) ls32 ;; / [ h16 ] "::" 4( h16 ":" ) ls32 ;; / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32 ;; / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32 ;; / [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32 ;; / [ *4( h16 ":" ) h16 ] "::" ls32 ;; / [ *5( h16 ":" ) h16 ] "::" h16 ;; / [ *6( h16 ":" ) h16 ] "::" ;; ls32 = ( h16 ":" h16 ) / IPv4address ;; ; least-significant 32 bits of address ;; h16 = 1*4HEXDIG ;; ; 16 bits of address represented in hexadecimal (define h16 (repetition-n 4 hexadecimal)) (define ls32 (alternatives (concatenation h16 (char #\:) h16) ipv4-address)) (define ipv6-address (alternatives (concatenation (repetition-n 6 (concatenation h16 (char #\:))) ls32) (concatenation (lit "::") (repetition-n 5 (concatenation h16 (char #\:))) ls32) (longest (concatenation (optional-sequence h16) (lit "::") (repetition-n 4 (concatenation h16 (char #\:))) ls32)) (longest (concatenation (optional-sequence (repetition-n 1 (concatenation h16 (char #\:)))) (lit "::") (repetition-n 3 (concatenation h16 (char #\:))) ls32)) (longest (concatenation (optional-sequence (repetition-n 2 (concatenation h16 (char #\:)))) (lit "::") (repetition-n 2 (concatenation h16 (char #\:))) ls32)) (longest (concatenation (optional-sequence (repetition-n 3 (concatenation h16 (char #\:)))) (lit "::") (concatenation h16 (char #\:)) ls32)) (longest (concatenation (optional-sequence (repetition-n 4 (concatenation h16 (char #\:)))) (lit "::") ls32)) (longest (concatenation (optional-sequence (repetition-n 5 (concatenation h16 (char #\:)))) (lit "::") h16)) (longest (concatenation (optional-sequence (repetition-n 6 (concatenation h16 (char #\:)))) (lit "::"))))) ;; IPvFuture = "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" ) (define ipv-future (concatenation (char #\v) (longest (repetition1 hexadecimal)) (char #\.) (longest (repetition1 (alternatives unreserved sub-delims (char #\:)))))) ;; IP-literal = "[" ( IPv6address / IPvFuture ) "]" (define ip-literal (concatenation (char #\[) (alternatives ipv6-address ipv-future) (char #\]))) (define reg-name (longest (repetition (alternatives unreserved pct-encoded sub-delims)))) (define host (bind (lambda (contents) `((host . ,(car (consumed-chars->string contents))))) (alternatives ip-literal ipv4-address reg-name))) (define port (bind (lambda (contents) `((port . ,(string->number (car (consumed-chars->string contents)))))) (longest (repetition decimal)))) ;; RFC3986, section 3.2 ;; ;; userinfo = *( unreserved / pct-encoded / sub-delims / ":" ) ;; ;; We split this up in the leading part without colons ("username") and ;; everything after that ("password"), including extra colons. ;; ;; The RFC is not very clear, but it does mention this: ;; "The userinfo subcomponent may consist of a user name and, ;; optionally, scheme-specific information about how to gain ;; authorization to access the resource." ;; ;; The grammar allows multiple colons, and the RFC then continues: ;; "Applications should not render as clear text any data after ;; the first colon (":") character found within a userinfo ;; subcomponent unless the data after the colon is the empty ;; string (indicating no password)." (define userinfo0 (bind (lambda (contents) `((username . ,(car (consumed-chars->string contents))))) (longest (repetition (alternatives unreserved pct-encoded sub-delims))))) (define userinfo1 (bind (lambda (contents) `((password . ,(car (consumed-chars->string contents))))) (longest (repetition (alternatives unreserved pct-encoded sub-delims (char #\:)))))) (define userinfo (longest (concatenation userinfo0 (optional-sequence (concatenation (drop-consumed (char #\:)) userinfo1))))) ;; authority = [ userinfo "@" ] host [ ":" port ] (define authority (concatenation (optional-sequence (concatenation userinfo (drop-consumed (char #\@)))) host (optional-sequence (concatenation (drop-consumed (char #\:)) port)))) ;; RFC3986, section 3 ;; (define scheme (bind (lambda (contents) `((scheme . ,(car (consumed-chars->symbol contents))))) (longest (concatenation alpha (repetition (alternatives alpha decimal (set-from-string "+-."))))))) ;; hier-part = "//" authority path-abempty ;; / path-absolute ;; / path-rootless ;; / path-empty ;; ;; ;; path-abempty = *( "/" segment ) ;; path-absolute = "/" [ segment-nz *( "/" segment ) ] ;; path-noscheme = segment-nz-nc *( "/" segment ) ;; path-rootless = segment-nz *( "/" segment ) ;; path-empty = 0 ;; ;; segment = *pchar ;; segment-nz = 1*pchar ;; segment-nz-nc = 1*( unreserved / pct-encoded / sub-delims / "@" ) ;; ; non-zero-length segment without any colon ":" ;; pchar = unreserved / pct-encoded / sub-delims / ":" / "@" (define pchar (alternatives unreserved pct-encoded sub-delims (char #\:) (char #\@))) (define pchar-nc ;; Our own invention, not in ABNF of RFC 3986 (alternatives unreserved pct-encoded sub-delims (char #\@))) (define segment (bind consumed-chars->string (longest (repetition pchar)))) (define segment-nz (bind consumed-chars->string (longest (repetition1 pchar)))) (define segment-nz-nc (bind consumed-chars->string (longest (repetition1 pchar-nc)))) (define slash-segments ;; Also our own invention (longest (repetition (concatenation (char #\/) segment)))) (define path-empty (bind (lambda (contents) `((path . ()))) pass)) ;; Always succeeds. "0" in the ABNF (define path-safe-chars (char-set-union char-set:uri-unreserved (char-set #\/))) (define (slashed-strings->path contents) (let loop ((strs contents) (res (if (and (not (null? contents)) (equal? (car contents) #\/)) '("") '()))) (cond ((null? strs) res) ((equal? (car strs) #\/) (loop (cdr strs) res)) (else (loop (cdr strs) (cons (uri-decode-string (car strs) path-safe-chars) res)))))) (define path-noscheme (bind (lambda (contents) `((path . ,(slashed-strings->path contents)))) (concatenation segment-nz-nc slash-segments))) (define path-abempty (bind (lambda (contents) (if (null? contents) `((path . ())) `((path . (/ . ,(slashed-strings->path contents)))))) slash-segments)) (define path-rootless (bind (lambda (contents) `((path . ,(slashed-strings->path contents)))) (concatenation segment-nz slash-segments))) (define path-absolute (bind (lambda (contents) `((path . (/ . ,(slashed-strings->path contents))))) (longest (concatenation (char #\/) (optional-sequence (concatenation segment-nz slash-segments)))))) (define hier-part (alternatives (concatenation (drop-consumed (lit "//")) authority path-abempty) path-absolute path-rootless path-empty)) ;; RFC3986 section 3.4 ;; ;; query = *( pchar / "/" / "?" ) (define query (bind (lambda (contents) `((query . ,(car (consumed-chars->string contents))))) (longest (repetition (alternatives pchar (char #\/) (char #\?)))))) ;; RFC3986 section 3.5 ;; ;; fragment = *( pchar / "/" / "?" ) (define fragment (bind (lambda (contents) `((fragment . ,(car (consumed-chars->string contents))))) (longest (repetition (alternatives pchar (char #\/) (char #\?)))))) ;; RFC3986 section 3 ;; ;; URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ] ;; (define uri (longest (concatenation scheme (drop-consumed (char #\:)) hier-part (optional-sequence (concatenation (drop-consumed (char #\?)) query)) (optional-sequence (concatenation (drop-consumed (char #\#)) fragment))))) ;; RFC3986, section 4.2 ;; ;; relative-ref = relative-part [ "?" query ] [ "#" fragment ] ;; ;; relative-part = "//" authority path-abempty ;; / path-absolute ;; / path-noscheme ;; / path-empty (define relative-part (longest (alternatives (concatenation (drop-consumed (lit "//")) authority path-abempty) path-absolute path-noscheme path-empty))) (define relative-ref (longest (concatenation relative-part (optional-sequence (concatenation (drop-consumed (char #\?)) query)) (optional-sequence (concatenation (drop-consumed (char #\#)) fragment))))) ;; RFC3986, section 4.3 ;; absolute-URI = scheme ":" hier-part [ "?" query ] (define abs-uri (longest (concatenation scheme (drop-consumed (char #\:)) hier-part (optional-sequence (concatenation (drop-consumed (char #\?)) query))))) (define (absolute-uri s) (let ((ref (uri-reference s))) (when (uri-fragment ref) (error 'absolute-uri "fragments are not permitted in absolute URI")) (unless (uri-scheme ref) (error 'absolute-uri "no scheme found in URI string")) ref)) ;; ;; Resolving a relative URI relative to a base URI ;; ;; Returns a new URI which represents the value of the first URI ;; interpreted as relative to the second URI. ;; ;; For example: ;; ;; (uri->string (relative-to (uri-reference "foo") (uri "http://bar.org/")) ) ;; => "http://bar.org/foo" ;; ;; Algorithm from RFC3986, section 5.2.2 ;; (define (uri-relative-to ref base) (and (uri-reference? ref) (uri-reference? base) (cond ((uri-scheme ref) (update-URI ref path: (just-segments ref))) ((uri-authority ref) (update-URI ref path: (just-segments ref) scheme: (uri-scheme base))) ((let ((p (uri-path ref))) (and (not (null? p)) p)) => (lambda (ref-path) (if (and (pair? ref-path) (eq? '/ (car ref-path))) (update-URI ref scheme: (uri-scheme base) authority: (uri-auth base) path: (just-segments ref)) (update-URI ref scheme: (uri-scheme base) authority: (uri-auth base) path: (merge-paths base ref-path))))) ((uri-query ref) (update-URI ref scheme: (uri-scheme base) authority: (uri-auth base) path: (merge-paths base (list "")))) (else (update-URI ref path: (URI-path base) scheme: (URI-scheme base) authority: (URI-authority base) query: (URI-query base)))))) ;; ;; Finding a URI relative to a base URI ;; ;; Returns a new URI which represents the relative location of the ;; first URI with respect to the second URI. Thus, the values ;; supplied are expected to be absolute URIs, and the result returned ;; may be a relative URI. ;; ;; Example: ;; ;; (uri->string ;; (uri-relative-from (uri "http://example.com/Root/sub1/name2#frag") ;; (uri "http://example.com/Root/sub2/name2#frag"))) ;; ==> "../sub1/name2#frag" ;; (define (uri-relative-from uabs base) (cond ((ucdiff? uri-scheme uabs base) (update-URI uabs)) ((ucdiff? uri-authority uabs base) (update-URI uabs scheme: #f)) ;; Special case: no relative representation for http://a/ -> http://a ;; ....unless that should be a path of ("..") ((null? (uri-path uabs)) (update-URI uabs scheme: #f)) ((ucdiff? uri-path uabs base) (update-URI uabs scheme: #f authority: #f path: (rel-path-from (remove-dot-segments (uri-path uabs)) (remove-dot-segments (uri-path base))))) ((ucdiff? uri-query uabs base) (update-URI uabs scheme: #f authority: #f path: (list))) (else (update-URI uabs scheme: #f authority: #f query: #f path: (list))))) (define (ucdiff? sel u1 u2) (let ((s1 (sel u1)) (s2 (sel u2))) (not (cond ((and (URIAuth? s1) (URIAuth? s2)) (not (or (ucdiff? uri-username u1 u2) (ucdiff? uri-host u1 u2) (ucdiff? uri-port u1 u2)))) ((and (list? s1) (list? s2)) (equal? s1 s2)) ((and (string? s1) (string? s2)) (string=? s1 s2)) (else (eq? s1 s2)))))) (define (rel-path-from pabs base) (cond ((or (null? base) (null? pabs)) pabs) ;; Construct a relative path segment if the paths share a ;; leading segment other than a leading '/' ((and (eq? (car pabs) '/) (eq? (car base) '/)) (make-rel-path (if (string=? (cadr pabs) (cadr base)) (rel-path-from1 (cdr pabs) (cdr base)) pabs))) (else (error 'rel-path-from "Both URI paths must be absolute" pabs base)))) (define (make-rel-path x) (if (or (eq? (car x) '/) (string=? (car x) ".") (string=? (car x) "..")) x (cons "." x))) ;; rel-path-from1 strips off trailing names from the supplied paths, (define (rel-path-from1 pabs base) (let* ((rpabs (reverse pabs)) (rbase (reverse base)) (rp (rel-segs-from (reverse (cdr rpabs)) (reverse (cdr rbase))))) (if (null? rp) (if (string=? (car rpabs) (car rbase)) (list) (list (car rpabs))) (append rp (list (car rpabs)))))) ;; rel-segs-from discards any common leading segments from both paths, ;; then invokes dif-segs-from to calculate a relative path from the end ;; of the base path to the end of the target path. The final name is ;; handled separately, so this deals only with "directory" segments. (define (rel-segs-from sabs base) (cond ((and (null? sabs) (null? base)) (list)) ((or (null? sabs) (null? base)) (dif-segs-from sabs base)) (else (if (string=? (car sabs) (car base)) (rel-segs-from (cdr sabs) (cdr base)) (dif-segs-from sabs base))))) ;; dif-segs-from calculates a path difference from base to target, ;; not including the final name at the end of the path (i.e. results ;; always ends with '/') ;; ;; This function operates under the invariant that the supplied value ;; of sabs is the desired path relative to the beginning of base. ;; Thus, when base is empty, the desired path has been found. (define (dif-segs-from sabs base) (if (null? base) sabs (dif-segs-from (cons ".." sabs) (cdr base)))) ;; Path segment normalization; cf. RFC3986 section 6.2.2.4 (define (uri-normalize-path-segments uri) (update-URI uri path: (just-segments uri))) (define (merge0 pb pr) (let* ((rpb (reverse pb)) (pb1 (reverse (if (pair? rpb) ; RFC3986, section 5.2.3, second bullet (cdr rpb) rpb)))) (append pb1 pr))) ; It is assumed we never get here if pr is empty! (define (merge-paths b pr) ; pr is a relative path, *not* a URI object (let ((ba (uri-authority b)) (pb (uri-path b))) (let ((mp (if (and ba (null? pb)) (cons '/ pr) ; RFC3986, section 5.2.3, first bullet (merge0 pb pr)))) (remove-dot-segments mp)))) (define (just-segments u) (remove-dot-segments (uri-path u))) ;; Remove dot segments, but protect leading '/' symbol (define (remove-dot-segments ps) (if (and (pair? ps) (eq? (car ps) '/)) (cons '/ (elim-dots (cdr ps))) (elim-dots ps))) (define (elim-dots ps) (let loop ((ps ps) (trailing-slash? #f) (lst (list))) (cond ((null? ps) (reverse (if trailing-slash? (cons "" lst) lst))) ((equal? (car ps) ".") (loop (cdr ps) #t lst)) ((equal? (car ps) "..") (loop (cdr ps) #t (if (pair? lst) (cdr lst) lst))) (else (loop (cdr ps) #f (cons (car ps) lst)))))) (define (uri-path-absolute? uri) (let ((path (uri-path uri))) (and (pair? path) (eq? '/ (car path))))) (define (uri-path-relative? uri) (not (uri-path-absolute? uri))) )