;;;; uri.scm ;;;; Kon Lovett, Sep '06 ;; Issues ;; ;; - Modified for RFC 3986; specifically support for IPv6 address & sub-delims ;; ;; - Should support UTF8. Since uses byte oriented (via usual-integrations) ;; string & char-*? routines any UTF8 encoded character should be recognized ;; as a reserved character and URI encoded. ;; ;; - Some bug fixes (use srfi-1 srfi-13 regex extras posix coerce misc-extn-procs misc-extn) (eval-when (compile) (declare (usual-integrations) (no-procedure-checks-for-usual-bindings) (fixnum) (inline) (no-procedure-checks) (no-bound-checks) (export uri make-uri uri? uri-scheme uri-authority uri-path uri-query uri-fragment uri-userinfo uri-username uri-password uri-query->alist uri->string uri->tree uri-encode uri-decode uri:make-path uri:split-fields uri:decode-query uri:path->keys path->uri absolute-uri? absolute-path? null-directory? glob-pattern? html:anchor html:link html:base html:isindex parse-ftp-address ;; url compatibility url? url-scheme url-user url-password url-host url-port url-path url-typecode url->string url url-decode url-encode make-url ) ) ) ;;; (define-inline (proplist->alist pl) (map! (lambda (lst) (set-cdr! lst (cadr lst)) lst) (chop pl 2)) ) (define-inline (string-subst str . pairs) (string-translate* str (proplist->alist pairs)) ) (define-inline (string-ci->symbol str) (string->symbol (string-downcase str)) ) (define-inline (scan-list re str) (take-while! identity (cdr (or (string-match re str) '(())))) ) (define (%scan-set! re str setrs) (let ([num 0]) (let loop ([lst (scan-list re str)] [setrs setrs]) (if (null? setrs) num (if (null? lst) (loop '() (cdr setrs)) (begin ((car setrs) (car lst)) (set! num (add1 num)) (loop (cdr lst) (cdr setrs)) ) ) ) ) ) ) (define-macro (scan-set! str re . vars) (let loop ([vars vars] [setrs '()]) (if (null? vars) `(%scan-set! ,re ,str (list ,@(reverse! setrs))) (loop (cdr vars) (cons `(lambda (x) (set! ,(car vars) x)) setrs)) ) ) ) #;(define rfc3986-gen-delims ":/?#[]@") (define rfc3986-sub-delims "!$&'()*+,;=") (define rfc3986-pchar (string-append rfc3986-sub-delims ":@")) (define-inline (encode-char chr) (let ([str (number->string (char->integer chr) 16)]) (string-append "%" (if (= 1 (string-length str)) "0" "") str)) ) (define-inline (char-rfc3986-unreserved? chr) (or (char-alphabetic? chr) (char-numeric? chr) (string-index "-_.!~" chr)) ) ;;; ;;; "uri.scm" Construct and decode Uniform Resource Identifiers. -*-scheme-*- ; Copyright 1997, 1998, 2000, 2001 Aubrey Jaffer ; ;Permission to copy this software, to modify it, to redistribute it, ;to distribute modified versions, and to use it for any purpose is ;granted, subject to the following restrictions and understandings. ; ;1. Any copy made of this software must include this copyright notice ;in full. ; ;2. I have made no warranty or representation that the operation of ;this software will be error-free, and I am under no obligation to ;provide any services, by way of maintenance, update, or otherwise. ; ;3. In conjunction with products arising from the use of this ;material, there shall be no use of my name in any advertising, ;promotional, or sales literature without prior written consent in ;each case. ;;@code{(require 'uri)} ;;@ftindex uri ;; ;;@noindent Implements @dfn{Uniform Resource Identifiers} (URI) as ;;described in RFC 2396. ;;@ ;;@noindent @code{uric:} prefixes indicate procedures dealing with ;;URI-components. ;;@body Returns a copy of the string @1 in which all @dfn{unsafe} octets ;;(as defined in RFC 2396) have been @samp{%} @dfn{escaped}. ;;@code{uric:decode} decodes strings encoded by @0. (define (uric:encode uri-component . allows) (apply string-append (map (lambda (chr) (if (or (char-rfc3986-unreserved? chr) (any (cut string-index <> chr) allows)) (string chr) (encode-char chr))) (string->list uri-component)))) ;;@body Returns a copy of the string @1 in which each @samp{%} escaped ;;characters in @1 is replaced with the character it encodes. This ;;routine is useful for showing URI contents on error pages. (define (uric:decode uri-component) (define len (string-length uri-component)) (define (sub uri) (cond ((string-index uri #\%) => (lambda (idx) (if (and (< (+ 2 idx) len) (string->number (substring uri (add1 idx) (+ 2 idx)) 16) (string->number (substring uri (+ 2 idx) (+ 3 idx)) 16)) (string-append (substring uri 0 idx) (string (integer->char (string->number (substring uri (add1 idx) (+ 3 idx)) 16))) (sub (substring uri (+ 3 idx) (string-length uri))))))) (else uri))) (sub uri-component)) (define uri-encode uric:encode) (define uri-decode uric:decode) (define (uri:decode-path path-list base-path) (cond ((and (equal? "" (car path-list)) (not (equal? '("") path-list))) path-list) (base-path (let* ((cpath0 (append (butlast base-path 1) path-list)) (cpath1 (let remove ((l cpath0) (result '())) (cond ((null? l) (reverse result)) ((not (equal? "." (car l))) (remove (cdr l) (cons (car l) result))) ((null? (cdr l)) (reverse (cons "" result))) (else (remove (cdr l) result))))) (cpath2 (let remove ((l cpath1) (result '())) (cond ((null? l) (reverse result)) ((not (equal? ".." (car l))) (remove (cdr l) (cons (car l) result))) ((or (null? result) (equal? "" (car result))) (warning 'uri:decode-path "null path" cpath1) (append (reverse result) l)) ((null? (cdr l)) (reverse (cons "" (cdr result)))) (else (remove (cdr l) (cdr result))))))) cpath2)) (else path-list))) ;; (userinfo host port) where any can be #f. (define (uri:decode-authority authority) (let* ([idx-at (string-index-right authority #\@)] [userinfo (and idx-at (uric:decode (substring authority 0 idx-at)))] [hostport (if idx-at (substring authority (add1 idx-at) (string-length authority)) authority)] [ipv6-end (string-index-right hostport #\])] [cdx (if ipv6-end (begin (unless (string-index hostport #\[) (error 'uri:decode-authority "invalid IPv6 address" authority)) (string-index hostport #\: ipv6-end)) (string-index-right hostport #\:))] [host (if cdx (substring hostport 0 cdx) hostport)] [port (and cdx (substring hostport (add1 cdx) (string-length hostport)))]) (if (or userinfo port) (list userinfo host (or (string->number port) port)) (list #f host #f) ) ) ) ;;@args txt chr ;;Returns a list of @1 split at each occurrence of @2. @2 does not ;;appear in the returned list of strings. (define (uri:split-fields txt chr) (let ([idx (string-index txt chr)]) (if idx (cons (substring txt 0 (if (and (positive? idx) (char=? #\return (string-ref txt (sub1 idx)))) (sub1 idx) idx)) (uri:split-fields (substring txt (add1 idx) (string-length txt)) chr)) (list txt) ) ) ) ;;@body @1 is a path-list as returned by @code{uri:split-fields}. @0 ;;returns a list of items returned by @code{uri:decode-path}, coerced ;;to types @2. (define (uri:path->keys path-list ptypes) (and (not (null? path-list)) (not (equal? '("") path-list)) (let ((path (uri:decode-path (map uric:decode path-list) #f))) (and (= (length path) (length ptypes)) (map coerce path ptypes))))) ;;@body Converts a @dfn{URI} encoded @1 to a query-alist. (define (uri:decode-query query-string) (set! query-string (string-subst query-string " " "" "+" " ")) (do ((lst '()) (edx (string-index query-string #\=) (string-index query-string #\=))) ((not edx) lst) (let* ((rxt (substring query-string (add1 edx) (string-length query-string))) (adx (string-index rxt #\&)) (urid (uric:decode (substring rxt 0 (or adx (string-length rxt))))) (name (string-ci->symbol (uric:decode (substring query-string 0 edx))))) (set! lst (append lst (if (equal? "" urid) '() (map (lambda (value) (list name value)) (uri:split-fields urid #\newline))))) (set! query-string (if adx (substring rxt (add1 adx) (string-length rxt)) ""))))) ;;@body ;;Returns a URI string combining the components of list @1. (define (uri:make-path path) (apply string-append (uric:encode (car path) rfc3986-pchar) (map (lambda (pth) (string-append "/" (uric:encode pth rfc3986-pchar))) (cdr path)))) ;;@args ;;@args fragment ;;@args query fragment ;;@args path query fragment ;;@args authority path query fragment ;;@args scheme authority path query fragment ;; ;;Returns a Uniform Resource Identifier string from component arguments. (define (*make-uri . args) (let ([nargs (length args)] [args (reverse! args)]) (let ((fragment (if (>= nargs 1) (car args) #f)) (query (if (>= nargs 2) (cadr args) #f)) (path (if (>= nargs 3) (caddr args) #f)) (authority (if (>= nargs 4) (cadddr args) #f)) (scheme (if (>= nargs 5) (list-ref args 4) #f))) (string-append (if scheme (format "~A:" scheme) "") (cond ((string? authority) (format "//~A" (uric:encode authority rfc3986-sub-delims ":@[]"))) ((list? authority) (apply (lambda (userinfo host port) (cond ((and userinfo port) (format "//address@hidden:~A" (uric:encode userinfo rfc3986-sub-delims ":") host port)) (userinfo (format "//address@hidden" (uric:encode userinfo rfc3986-sub-delims ":") host)) (port (format "//~A:~A" host port)) (else host))) authority)) (else (or authority ""))) (cond ((string? path) (uric:encode path rfc3986-pchar "/")) ((null? path) "") ((list? path) (uri:make-path path)) (else (or path ""))) (if query (format "?~A" (uric:encode query rfc3986-pchar "/?")) "") (if fragment (format "#~A" (uric:encode fragment rfc3986-pchar "/?")) "")) ) ) ) ;;@args uri-reference base-tree ;;@args uri-reference ;; ;;Returns a list of 5 elements corresponding to the parts ;;(@var{scheme} @var{authority} @var{path} @var{query} @var{fragment}) ;;of string @1. Elements corresponding to absent parts are #f. ;; ;;The @var{path} is a list of strings. If the first string is empty, ;;then the path is absolute; otherwise relative. The optional @2 is a ;;tree as returned by @0; and is used as the base address for relative ;;URIs. ;; ;;If the @var{authority} component is a ;;@dfn{Server-based Naming Authority}, then it is a list of the ;;@var{userinfo}, @var{host}, and @var{port} strings (or #f). For other ;;types of @var{authority} components the @var{authority} will be a ;;string. ;; ;;@example ;;(uri->tree "http://www.ics.uci.edu/pub/ietf/uri/#Related") ;;@result{} ;;(http "www.ics.uci.edu" ("" "pub" "ietf" "uri" "") #f "Related") ;;@end example (define uri:scheme? (let ([re (regexp "([a-zA-Z0-9+.-]+)?[ \t\n]*([^ \t\n]+)?" #f #f #t)]) (lambda (str) (let ((lst (scan-list re str))) (and (eqv? 1 (length lst)) (char-alphabetic? (string-ref str 0))) ) ) ) ) (define (uri:split uri-reference) (let ([len (string-length uri-reference)] [idx-sharp (string-index uri-reference #\#)]) (let ([fragment (and idx-sharp (substring uri-reference (add1 idx-sharp) len))] [uri (if idx-sharp (and (not (zero? idx-sharp)) (substring uri-reference 0 idx-sharp)) uri-reference)]) (if uri (let* ((len (string-length uri)) (idx-? (string-index uri #\?)) (query (and idx-? (substring uri (add1 idx-?) len))) (front (if idx-? (and (not (zero? idx-?)) (substring uri 0 idx-?)) uri))) (if front (let* ((len (string-length front)) (cdx (string-index front #\:)) (scheme (and cdx (substring front 0 cdx))) (path (if cdx (substring front (add1 cdx) len) front))) (cond ((eqv? 0 (string-contains path "//")) (set! len (string-length path)) (set! path (substring path 2 len)) (set! len (+ -2 len)) (let* ((idx-/ (string-index path #\/)) (authority (substring path 0 (or idx-/ len))) (path (if idx-/ (substring path idx-/ len) ""))) (list scheme authority path query fragment))) (else (list scheme #f path query fragment)))) (list #f #f "" query fragment))) (list #f #f "" #f fragment) ) ) ) ) (define (uri->tree uri-reference . base-tree) (let ([split (uri:split uri-reference)]) (apply (lambda (b-scheme b-authority b-path b-query b-fragment) (apply (lambda (scheme authority path query fragment) (define uri-empty? (and (equal? "" path) (not scheme) (not authority) (not query))) (list (if (and scheme (uri:scheme? scheme)) (string-ci->symbol scheme) (cond ((not scheme) b-scheme) (else (warning 'uri->tree "bad scheme" scheme) b-scheme))) (if authority (uri:decode-authority authority) b-authority) (if uri-empty? (or b-path '("")) (uri:decode-path (map uric:decode (uri:split-fields path #\/)) (and (not authority) (not scheme) b-path))) (if uri-empty? b-query query) (or (and fragment (uric:decode fragment)) (and uri-empty? b-fragment)))) split)) (if (or (car split) (null? base-tree)) '(#f #f #f #f #f) (car base-tree) ) ) ) ) (define (split-userinfo userinfo) (if userinfo (let ([idx-colon (string-index-right userinfo #\:)]) (if idx-colon (list (substring userinfo 0 idx-colon) (substring userinfo (add1 idx-colon))) (list userinfo #f) ) ) '(#f #f) ) ) (define-record-type uri (%make-uri scheme authority path query fragment) uri? (scheme uri-scheme) (authority uri-authority) (path uri-path) (query uri-query) (fragment uri-fragment) ) (define-record-printer (uri x port) (fprintf port "#,(uri ~S ~S ~S ~S ~S)" (uri-scheme x) (uri-authority x) (uri-path x) (uri-query x) (uri-fragment x)) ) (define (uri-userinfo uri) (let ([ui (split-userinfo (first (uri-authority uri)))]) (values (first ui) (second ui)) ) ) (define (uri-username uri) (let-values (([un pw] (uri-userinfo uri))) un ) ) (define (uri-password uri) (let-values (([un pw] (uri-userinfo uri))) pw ) ) (define (make-uri #!rest args #!key scheme authority path query fragment) (let ([args (filter-rest-argument! args '(#:scheme #:authority #:path #:query #:fragment))]) (if (null? args) (%make-uri scheme authority path query fragment) (apply *make-uri args) ) ) ) (define (uri str #!optional default) (let ([lst (uri->tree str)]) (if (equal? lst '(#f #f #f #f #f)) (or default (error 'uri "not a valid URI" str)) (%make-uri (first lst) (second lst) (third lst) (fourth lst) (fifth lst)) ) ) ) (define (uri->string uri) (*make-uri (uri-scheme uri) (uri-authority uri) (uri-path uri) (uri-query uri) (uri-fragment uri)) ) (define (uri-query->alist uri) (map! (lambda (pair) (let ([value (cdr pair)]) (when (and (proper-list? value) (length=1? value)) (set-cdr! pair (car value)))) pair) (uri:decode-query (or (uri-query uri) ""))) ) ;;@subheading File-system Locators and Predicates ;;@body Returns #t if @1 is a fully specified pathname (does not ;;depend on the current working directory); otherwise returns #f. (define (absolute-path? file-name) (and (string? file-name) (positive? (string-length file-name)) (memv (string-ref file-name 0) '(#\\ #\/)))) ;;@body Returns a URI-string for @1 on the local host. (define (path->uri path) (if (absolute-path? path) (format "file://~A" path) (format "file://~A/~A" (current-directory) path))) ;;@body Returns #t if @1 is an absolute-URI as indicated by a ;;syntactically valid (per RFC 2396) @dfn{scheme}; otherwise returns ;;#f. (define absolute-uri? (let ([re (regexp "([a-zA-Z0-9+.-]+)?:([^ \t\n]+)?" #f #f #t)]) (lambda (str) (let ((lst (scan-list re str))) (and (eqv? 2 (length lst)) (char-alphabetic? (string-ref str 0))) ) ) ) ) ;;@body Returns #t if changing directory to @1 would leave the current ;;directory unchanged; otherwise returns #f. (define (null-directory? str) (member str '("" "." "./" ".\\"))) ;;@body Returns #t if the string @1 contains characters used for ;;specifying glob patterns, namely @samp{*}, @samp{?}, or @samp{[}. (define (glob-pattern? str) (let loop ((idx (sub1 (string-length str)))) (if (negative? idx) #f (case (string-ref str idx) ((#\* #\[ #\?) #t) (else (loop (sub1 idx))))))) ;;@noindent ;;Before RFC 2396, the @dfn{File Transfer Protocol} (FTP) served a ;;similar purpose. ;;@body ;;Returns a list of the decoded FTP @1; or #f if indecipherable. FTP ;;@dfn{Uniform Resource Locator}, @dfn{ange-ftp}, and @dfn{getit} ;;formats are handled. The returned list has four elements which are ;;strings or #f: ;; ;;@enumerate 0 ;;@item ;;username ;;@item ;;password ;;@item ;;remote-site ;;@item ;;remote-directory ;;@end enumerate (define parse-ftp-address (let ( [re1 (regexp "[ \t\n]*ftp://([^ \t\n]+)?[ \t\n]*([^ \t\n]+)?" #f #f #t)] [re2 (regexp "([^/]+)?/(address@hidden)?([^ \t\n]+)?" #f #f #t)] [re3 (regexp "(address@hidden)?@(address@hidden)?([^ \t\n]+)?" #f #f #t)] [re4 (regexp "([^:]+)?:([^@/]+)?([^ \t\n]+)?" #f #f #t)] [re5 (regexp "[ \t\n]*([^:]+)?([:]+)?(address@hidden)?[ \t\n]*([^ \t\n]+)?" #f #f #t)] [re6 (regexp "/([^@/]+)?@(address@hidden)?([^ \t\n]+)?" #f #f #t)] [re7 (regexp "([^@/]+)?@(address@hidden)?([^ \t\n]+)?" #f #f #t)] [re8 (regexp "@(address@hidden)?([^ \t\n]+)?" #f #f #t)] [re9 (regexp "[ \t\n]*([^@/]+)?[ \t\n]*([^ \t\n]+)?" #f #f #t)]) (lambda (uri) (let ((length? (lambda (len lst) (and (eqv? len (length lst)) lst)))) (cond ((not uri) #f) ((length? 1 (scan-list re1 uri)) => (lambda (host) (let ((login #f) (path #f) (dross #f)) (scan-set! (car host) re2 login path dross) (and login (append (cond ((length? 2 (scan-list re3 login)) => (lambda (address@hidden) (append (cond ((length? 2 (scan-list re4 (car address@hidden)))) (else (list (car address@hidden) #f))) (cdr address@hidden)))) (else (list "anonymous" #f login))) (list path)))))) (else (let ((address@hidden #f) (colon #f) (path #f) (dross #f)) (case (scan-set! uri re5 address@hidden colon path dross) ((2 3) (let ((user #f) (site #f)) (cond ((or (eqv? 2 (scan-set! address@hidden re6 user site dross)) (eqv? 2 (scan-set! address@hidden re7 user site dross))) (list user #f site path)) ((eqv? 1 (scan-set! address@hidden re8 site dross)) (list #f #f site path)) (else (list #f #f address@hidden path))))) (else (let ((site (scan-list re9 uri))) (and (length? 1 site) (list #f #f (car site) #f)))))))) ) ) ) ) ;;@body Returns a string which defines this location in the (HTML) file ;;as @1. The hypertext @samp{} will link to this point. ;; ;;@example ;;(html:anchor "(section 7)") ;;@result{} ;;"" ;;@end example (define (html:anchor name) (format "" (uric:encode name "#?/:@;="))) ;;@body Returns a string which links the @2 text to @1. ;; ;;@example ;;(html:link (make-uri "(section 7)") "section 7") ;;@result{} ;;"section 7" ;;@end example (define (html:link uri highlighted) (format "~A" uri highlighted)) ;;@body Returns a string specifying the @dfn{base} @1 of a document, for ;;inclusion in the HEAD of the document (@pxref{HTML, head}). (define (html:base uri) (format "" uri)) ;;@body Returns a string specifying the search @1 of a document, for ;;inclusion in the HEAD of the document (@pxref{HTML, head}). (define (html:isindex prompt) (format "" prompt)) ;;; ;;url compatibility (define url? uri?) (define url uri) (define url->string uri->string) (define url-decode uri-decode) ;slightly different? (define url-encode uri-encode) ;slightly different? (define (url-scheme x) (symbol->string (uri-scheme x))) (define url-user uri-username) (define url-password uri-password) (define (url-host x) (cadr (uri-authority x))) (define (url-port x) (caddr (uri-authority x))) (define (url-path x) (string-intersperse (cdr (uri-path x)) "/")) (define (url-typecode x) #f) ;not easy to support (define (make-url #!key scheme user password host port path typecode) (let ([scheme (string->symbol scheme)] [authority (list (and user (if password (string-append user ":" password) user)) host port)] [path (cons "" (if path (string-split path "/" #t) '()))] [query #f] [fragment #f]) (%make-uri scheme authority path query fragment)))