uri.scm 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. ;;;; (web uri) --- URI manipulation tools
  2. ;;;;
  3. ;;;; Copyright (C) 1997,2001,2002,2010,2011,2012 Free Software Foundation, Inc.
  4. ;;;;
  5. ;;;; This library is free software; you can redistribute it and/or
  6. ;;;; modify it under the terms of the GNU Lesser General Public
  7. ;;;; License as published by the Free Software Foundation; either
  8. ;;;; version 3 of the License, or (at your option) any later version.
  9. ;;;;
  10. ;;;; This library is distributed in the hope that it will be useful,
  11. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. ;;;; Lesser General Public License for more details.
  14. ;;;;
  15. ;;;; You should have received a copy of the GNU Lesser General Public
  16. ;;;; License along with this library; if not, write to the Free Software
  17. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. ;;;;
  19. ;;; Commentary:
  20. ;; A data type for Universal Resource Identifiers, as defined in RFC
  21. ;; 3986.
  22. ;;; Code:
  23. (define-module (web uri)
  24. #:use-module (srfi srfi-9)
  25. #:use-module (ice-9 regex)
  26. #:use-module (ice-9 rdelim)
  27. #:use-module (ice-9 control)
  28. #:use-module (rnrs bytevectors)
  29. #:use-module (ice-9 binary-ports)
  30. #:export (uri?
  31. uri-scheme uri-userinfo uri-host uri-port
  32. uri-path uri-query uri-fragment
  33. build-uri
  34. declare-default-port!
  35. string->uri uri->string
  36. uri-decode uri-encode
  37. split-and-decode-uri-path
  38. encode-and-join-uri-path))
  39. (define-record-type <uri>
  40. (make-uri scheme userinfo host port path query fragment)
  41. uri?
  42. (scheme uri-scheme)
  43. (userinfo uri-userinfo)
  44. (host uri-host)
  45. (port uri-port)
  46. (path uri-path)
  47. (query uri-query)
  48. (fragment uri-fragment))
  49. (define (absolute-uri? x)
  50. (and (uri? x) (uri-scheme x) #t))
  51. (define (uri-error message . args)
  52. (throw 'uri-error message args))
  53. (define (positive-exact-integer? port)
  54. (and (number? port) (exact? port) (integer? port) (positive? port)))
  55. (define (validate-uri scheme userinfo host port path query fragment)
  56. (cond
  57. ((not (symbol? scheme))
  58. (uri-error "Expected a symbol for the URI scheme: ~s" scheme))
  59. ((and (or userinfo port) (not host))
  60. (uri-error "Expected a host, given userinfo or port"))
  61. ((and port (not (positive-exact-integer? port)))
  62. (uri-error "Expected port to be an integer: ~s" port))
  63. ((and host (or (not (string? host)) (not (valid-host? host))))
  64. (uri-error "Expected valid host: ~s" host))
  65. ((and userinfo (not (string? userinfo)))
  66. (uri-error "Expected string for userinfo: ~s" userinfo))
  67. ((not (string? path))
  68. (uri-error "Expected string for path: ~s" path))
  69. ((and host (not (string-null? path))
  70. (not (eqv? (string-ref path 0) #\/)))
  71. (uri-error "Expected path of absolute URI to start with a /: ~a" path))))
  72. (define* (build-uri scheme #:key userinfo host port (path "") query fragment
  73. (validate? #t))
  74. "Construct a URI object. SCHEME should be a symbol, PORT
  75. either a positive, exact integer or ‘#f’, and the rest of the
  76. fields are either strings or ‘#f’. If VALIDATE? is true,
  77. also run some consistency checks to make sure that the constructed URI
  78. is valid."
  79. (if validate?
  80. (validate-uri scheme userinfo host port path query fragment))
  81. (make-uri scheme userinfo host port path query fragment))
  82. ;; See RFC 3986 #3.2.2 for comments on percent-encodings, IDNA (RFC
  83. ;; 3490), and non-ASCII host names.
  84. ;;
  85. (define ipv4-regexp
  86. (make-regexp "^([0-9.]+)$"))
  87. (define ipv6-regexp
  88. (make-regexp "^([0-9a-fA-F:.]+)$"))
  89. (define domain-label-regexp
  90. (make-regexp "^[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?$"))
  91. (define top-label-regexp
  92. (make-regexp "^[a-zA-Z]([a-zA-Z0-9-]*[a-zA-Z0-9])?$"))
  93. (define (valid-host? host)
  94. (cond
  95. ((regexp-exec ipv4-regexp host)
  96. (false-if-exception (inet-pton AF_INET host)))
  97. ((regexp-exec ipv6-regexp host)
  98. (false-if-exception (inet-pton AF_INET6 host)))
  99. (else
  100. (let lp ((start 0))
  101. (let ((end (string-index host #\. start)))
  102. (if end
  103. (and (regexp-exec domain-label-regexp
  104. (substring host start end))
  105. (lp (1+ end)))
  106. (regexp-exec top-label-regexp host start)))))))
  107. (define userinfo-pat
  108. "[a-zA-Z0-9_.!~*'();:&=+$,-]+")
  109. (define host-pat
  110. "[a-zA-Z0-9.-]+")
  111. (define ipv6-host-pat
  112. "[0-9a-fA-F:.]+")
  113. (define port-pat
  114. "[0-9]*")
  115. (define authority-regexp
  116. (make-regexp
  117. (format #f "^//((~a)@)?((~a)|(\\[(~a)\\]))(:(~a))?$"
  118. userinfo-pat host-pat ipv6-host-pat port-pat)))
  119. (define (parse-authority authority fail)
  120. (if (equal? authority "//")
  121. ;; Allow empty authorities: file:///etc/hosts is a synonym of
  122. ;; file:/etc/hosts.
  123. (values #f #f #f)
  124. (let ((m (regexp-exec authority-regexp authority)))
  125. (if (and m (valid-host? (or (match:substring m 4)
  126. (match:substring m 6))))
  127. (values (match:substring m 2)
  128. (or (match:substring m 4)
  129. (match:substring m 6))
  130. (let ((port (match:substring m 8)))
  131. (and port (not (string-null? port))
  132. (string->number port))))
  133. (fail)))))
  134. ;;; RFC 3986, #3.
  135. ;;;
  136. ;;; URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
  137. ;;;
  138. ;;; hier-part = "//" authority path-abempty
  139. ;;; / path-absolute
  140. ;;; / path-rootless
  141. ;;; / path-empty
  142. (define scheme-pat
  143. "[a-zA-Z][a-zA-Z0-9+.-]*")
  144. (define authority-pat
  145. "[^/?#]*")
  146. (define path-pat
  147. "[^?#]*")
  148. (define query-pat
  149. "[^#]*")
  150. (define fragment-pat
  151. ".*")
  152. (define uri-pat
  153. (format #f "^((~a):)?(//~a)?(~a)(\\?(~a))?(#(~a))?$"
  154. scheme-pat authority-pat path-pat query-pat fragment-pat))
  155. (define uri-regexp
  156. (make-regexp uri-pat))
  157. (define (string->uri* string)
  158. "Parse STRING into a URI object. Return ‘#f’ if the string
  159. could not be parsed."
  160. (% (let ((m (regexp-exec uri-regexp string)))
  161. (if (not m) (abort))
  162. (let ((scheme (let ((str (match:substring m 2)))
  163. (and str (string->symbol (string-downcase str)))))
  164. (authority (match:substring m 3))
  165. (path (match:substring m 4))
  166. (query (match:substring m 6))
  167. (fragment (match:substring m 7)))
  168. (call-with-values
  169. (lambda ()
  170. (if authority
  171. (parse-authority authority abort)
  172. (values #f #f #f)))
  173. (lambda (userinfo host port)
  174. (make-uri scheme userinfo host port path query fragment)))))
  175. (lambda (k)
  176. #f)))
  177. (define (string->uri string)
  178. "Parse STRING into a URI object. Return ‘#f’ if the string
  179. could not be parsed."
  180. (let ((uri (string->uri* string)))
  181. (and uri (uri-scheme uri) uri)))
  182. (define *default-ports* (make-hash-table))
  183. (define (declare-default-port! scheme port)
  184. "Declare a default port for the given URI scheme."
  185. (hashq-set! *default-ports* scheme port))
  186. (define (default-port? scheme port)
  187. (or (not port)
  188. (eqv? port (hashq-ref *default-ports* scheme))))
  189. (declare-default-port! 'http 80)
  190. (declare-default-port! 'https 443)
  191. (define (uri->string uri)
  192. "Serialize URI to a string. If the URI has a port that is the
  193. default port for its scheme, the port is not included in the
  194. serialization."
  195. (let* ((scheme (uri-scheme uri))
  196. (userinfo (uri-userinfo uri))
  197. (host (uri-host uri))
  198. (port (uri-port uri))
  199. (path (uri-path uri))
  200. (query (uri-query uri))
  201. (fragment (uri-fragment uri)))
  202. (string-append
  203. (if scheme
  204. (string-append (symbol->string scheme) ":")
  205. "")
  206. (if host
  207. (string-append "//"
  208. (if userinfo (string-append userinfo "@")
  209. "")
  210. (if (string-index host #\:)
  211. (string-append "[" host "]")
  212. host)
  213. (if (default-port? (uri-scheme uri) port)
  214. ""
  215. (string-append ":" (number->string port))))
  216. "")
  217. path
  218. (if query
  219. (string-append "?" query)
  220. "")
  221. (if fragment
  222. (string-append "#" fragment)
  223. ""))))
  224. ;; like call-with-output-string, but actually closes the port (doh)
  225. (define (call-with-output-string* proc)
  226. (let ((port (open-output-string)))
  227. (proc port)
  228. (let ((str (get-output-string port)))
  229. (close-port port)
  230. str)))
  231. (define (call-with-output-bytevector* proc)
  232. (call-with-values
  233. (lambda ()
  234. (open-bytevector-output-port))
  235. (lambda (port get-bytevector)
  236. (proc port)
  237. (let ((bv (get-bytevector)))
  238. (close-port port)
  239. bv))))
  240. (define (call-with-encoded-output-string encoding proc)
  241. (if (string-ci=? encoding "utf-8")
  242. (string->utf8 (call-with-output-string* proc))
  243. (call-with-output-bytevector*
  244. (lambda (port)
  245. (set-port-encoding! port encoding)
  246. (proc port)))))
  247. (define (encode-string str encoding)
  248. (if (string-ci=? encoding "utf-8")
  249. (string->utf8 str)
  250. (call-with-encoded-output-string encoding
  251. (lambda (port)
  252. (display str port)))))
  253. (define (decode-string bv encoding)
  254. (if (string-ci=? encoding "utf-8")
  255. (utf8->string bv)
  256. (let ((p (open-bytevector-input-port bv)))
  257. (set-port-encoding! p encoding)
  258. (let ((res (read-delimited "" p)))
  259. (close-port p)
  260. res))))
  261. ;; A note on characters and bytes: URIs are defined to be sequences of
  262. ;; characters in a subset of ASCII. Those characters may encode a
  263. ;; sequence of bytes (octets), which in turn may encode sequences of
  264. ;; characters in other character sets.
  265. ;;
  266. ;; Return a new string made from uri-decoding STR. Specifically,
  267. ;; turn ‘+’ into space, and hex-encoded ‘%XX’ strings into
  268. ;; their eight-bit characters.
  269. ;;
  270. (define hex-chars
  271. (string->char-set "0123456789abcdefABCDEF"))
  272. (define* (uri-decode str #:key (encoding "utf-8"))
  273. "Percent-decode the given STR, according to ENCODING,
  274. which should be the name of a character encoding.
  275. Note that this function should not generally be applied to a full URI
  276. string. For paths, use split-and-decode-uri-path instead. For query
  277. strings, split the query on ‘&’ and ‘=’ boundaries, and decode
  278. the components separately.
  279. Note also that percent-encoded strings encode @emph{bytes}, not
  280. characters. There is no guarantee that a given byte sequence is a valid
  281. string encoding. Therefore this routine may signal an error if the
  282. decoded bytes are not valid for the given encoding. Pass ‘#f’ for
  283. ENCODING if you want decoded bytes as a bytevector directly.
  284. @xref{Ports, ‘set-port-encoding!’}, for more information on
  285. character encodings.
  286. Returns a string of the decoded characters, or a bytevector if
  287. ENCODING was ‘#f’."
  288. (let* ((len (string-length str))
  289. (bv
  290. (call-with-output-bytevector*
  291. (lambda (port)
  292. (let lp ((i 0))
  293. (if (< i len)
  294. (let ((ch (string-ref str i)))
  295. (cond
  296. ((eqv? ch #\+)
  297. (put-u8 port (char->integer #\space))
  298. (lp (1+ i)))
  299. ((and (< (+ i 2) len) (eqv? ch #\%)
  300. (let ((a (string-ref str (+ i 1)))
  301. (b (string-ref str (+ i 2))))
  302. (and (char-set-contains? hex-chars a)
  303. (char-set-contains? hex-chars b)
  304. (string->number (string a b) 16))))
  305. => (lambda (u8)
  306. (put-u8 port u8)
  307. (lp (+ i 3))))
  308. ((< (char->integer ch) 128)
  309. (put-u8 port (char->integer ch))
  310. (lp (1+ i)))
  311. (else
  312. (uri-error "Invalid character in encoded URI ~a: ~s"
  313. str ch))))))))))
  314. (if encoding
  315. (decode-string bv encoding)
  316. ;; Otherwise return raw bytevector
  317. bv)))
  318. (define ascii-alnum-chars
  319. (string->char-set
  320. "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"))
  321. ;; RFC 3986, #2.2.
  322. (define gen-delims
  323. (string->char-set ":/?#[]@"))
  324. (define sub-delims
  325. (string->char-set "!$&'()*+,l="))
  326. (define reserved-chars
  327. (char-set-union gen-delims sub-delims))
  328. ;; RFC 3986, #2.3
  329. (define unreserved-chars
  330. (char-set-union ascii-alnum-chars
  331. (string->char-set "-._~")))
  332. ;; Return a new string made from uri-encoding STR, unconditionally
  333. ;; transforming any characters not in UNESCAPED-CHARS.
  334. ;;
  335. (define* (uri-encode str #:key (encoding "utf-8")
  336. (unescaped-chars unreserved-chars))
  337. "Percent-encode any character not in the character set,
  338. UNESCAPED-CHARS.
  339. The default character set includes alphanumerics from ASCII, as well as
  340. the special characters @samp{-}, @samp{.}, @samp{_}, and @samp{~}. Any
  341. other character will be percent-encoded, by writing out the character to
  342. a bytevector within the given ENCODING, then encoding each byte as
  343. ‘%HH’, where HH is the hexadecimal representation of
  344. the byte."
  345. (define (needs-escaped? ch)
  346. (not (char-set-contains? unescaped-chars ch)))
  347. (if (string-index str needs-escaped?)
  348. (call-with-output-string*
  349. (lambda (port)
  350. (string-for-each
  351. (lambda (ch)
  352. (if (char-set-contains? unescaped-chars ch)
  353. (display ch port)
  354. (let* ((bv (encode-string (string ch) encoding))
  355. (len (bytevector-length bv)))
  356. (let lp ((i 0))
  357. (if (< i len)
  358. (let ((byte (bytevector-u8-ref bv i)))
  359. (display #\% port)
  360. (when (< byte 16)
  361. (display #\0 port))
  362. (display (number->string byte 16) port)
  363. (lp (1+ i))))))))
  364. str)))
  365. str))
  366. (define (split-and-decode-uri-path path)
  367. "Split PATH into its components, and decode each component,
  368. removing empty components.
  369. For example, ‘\"/foo/bar%20baz/\"’ decodes to the two-element list,
  370. ‘(\"foo\" \"bar baz\")’."
  371. (filter (lambda (x) (not (string-null? x)))
  372. (map uri-decode (string-split path #\/))))
  373. (define (encode-and-join-uri-path parts)
  374. "URI-encode each element of PARTS, which should be a list of
  375. strings, and join the parts together with ‘/’ as a delimiter.
  376. For example, the list ‘(\"scrambled eggs\" \"biscuits&gravy\")’
  377. encodes as ‘\"scrambled%20eggs/biscuits%26gravy\"’."
  378. (string-join (map uri-encode parts) "/"))