sema-url 0.1.0
URL parsing, building, resolving, and query-string manipulation
Exported declarations from this release. Parameter names and descriptions come directly from its source.
No matching exports. Try a function name or a word from its description.
(url/absolute? url)Return #t when url is absolute, i.e. starts with a scheme followed by ://.
View source definition
(define (url/absolute? url)
"Return #t when url is absolute, i.e. starts with a scheme followed by ://."
(regex/match? #"^[A-Za-z][A-Za-z0-9+.-]*://" url))(url/build parts)Build a URL string from a parts map ({:scheme :userinfo :host :port :path :query :fragment}). nil parts are omitted; :query and :fragment are emitted verbatim (already-encoded).
View source definition
(define (url/build parts)
"Build a URL string from a parts map ({:scheme :userinfo :host :port :path :query :fragment}). nil parts are omitted; :query and :fragment are emitted verbatim (already-encoded)."
(let ((result ""))
(when (get parts :scheme)
(set! result (string-append result (get parts :scheme) "://")))
(when (get parts :userinfo)
(set! result (string-append result (get parts :userinfo) "@")))
(when (get parts :host)
(set! result (string-append result (get parts :host))))
(when (get parts :port)
(set! result (string-append result ":" (str (get parts :port)))))
(when (get parts :path)
(set! result (string-append result (get parts :path))))
(when (get parts :query)
(set! result (string-append result "?" (get parts :query))))
(when (get parts :fragment)
(set! result (string-append result "#" (get parts :fragment))))
result))(url/decode s)Percent-decode a URL-encoded string. %XX sequences are decoded as UTF-8 bytes, + becomes a space, and a % not followed by two hex digits is kept literally.
View source definition
(define (url/decode s)
"Percent-decode a URL-encoded string. %XX sequences are decoded as UTF-8 bytes, + becomes a space, and a % not followed by two hex digits is kept literally."
(define (decode-loop i bytes)
(if (>= i (string-length s))
(utf8/to-string (list/to-bytevector (reverse bytes)))
(let ((ch (substring s i (+ i 1))))
(cond
((and (= ch "%") (<= (+ i 3) (string-length s)))
(let ((hi (hex-digit (substring s (+ i 1) (+ i 2))))
(lo (hex-digit (substring s (+ i 2) (+ i 3)))))
(if (or (nil? hi) (nil? lo))
(decode-loop (+ i 1) (cons 37 bytes)) ; literal "%"
(decode-loop (+ i 3) (cons (+ (* hi 16) lo) bytes)))))
((= ch "+")
(decode-loop (+ i 1) (cons 32 bytes))) ; space
(#t
(decode-loop (+ i 1)
(foldl (fn (acc b) (cons b acc))
bytes
(bytevector/to-list (string/to-utf8 ch)))))))))
(decode-loop 0 (list)))(url/encode s)Percent-encode a string for use in a URL component. Everything outside RFC 3986 unreserved characters is encoded; non-ASCII characters are encoded as their UTF-8 bytes (e.g. "é" → "%C3%A9").
View source definition
(define (url/encode s)
"Percent-encode a string for use in a URL component. Everything outside RFC 3986 unreserved characters is encoded; non-ASCII characters are encoded as their UTF-8 bytes (e.g. \"é\" → \"%C3%A9\")."
(string/join
(map (fn (b)
(if (unreserved-byte? b)
(char->string (integer->char b))
(string-append "%" (to-hex2 b))))
(bytevector/to-list (string/to-utf8 s)))
""))(url/join base ref)Resolve ref against base, RFC 3986 style. An absolute ref replaces base; an absolute path replaces base's path; a relative path resolves against the directory of base's path (with ./.. normalization). Base's query and fragment are dropped; ref's are kept.
View source definition
(define (url/join base ref)
"Resolve ref against base, RFC 3986 style. An absolute ref replaces base; an absolute path replaces base's path; a relative path resolves against the directory of base's path (with ./.. normalization). Base's query and fragment are dropped; ref's are kept."
(cond
((= ref "") base)
((url/absolute? ref) ref)
((string/starts-with? ref "//")
(let ((scheme (get (url/parse base) :scheme)))
(if (nil? scheme) ref (string-append scheme ":" ref))))
(#t
(let ((b (url/parse base))
(r-path ref) (r-query nil) (r-frag nil))
(when (string/contains? r-path "#")
(let ((idx (string/index-of r-path "#")))
(set! r-frag (substring r-path (+ idx 1) (string-length r-path)))
(set! r-path (substring r-path 0 idx))))
(when (string/contains? r-path "?")
(let ((idx (string/index-of r-path "?")))
(set! r-query (substring r-path (+ idx 1) (string-length r-path)))
(set! r-path (substring r-path 0 idx))))
(let ((new-path
(cond
((= r-path "") (get b :path))
((string/starts-with? r-path "/") (remove-dot-segments r-path))
(#t
(let* ((bp (get b :path))
(dir (if (string/contains? bp "/")
(substring bp 0 (+ (string/last-index-of bp "/") 1))
"/")))
(remove-dot-segments (string-append dir r-path)))))))
(url/build (assoc b :path new-path :query r-query :fragment r-frag))))))))(url/parse url)Parse a URL into {:scheme :userinfo :host :port :path :query :fragment}. Absent parts are nil; path defaults to "/". :port is an integer — a non-numeric port raises an error. IPv6 host literals are not supported.
View source definition
(define (url/parse url)
"Parse a URL into {:scheme :userinfo :host :port :path :query :fragment}. Absent parts are nil; path defaults to \"/\". :port is an integer — a non-numeric port raises an error. IPv6 host literals are not supported."
(let ((scheme nil) (userinfo nil) (host nil) (port nil)
(path "") (query nil) (fragment nil)
(rest-str url))
;; Fragment
(when (string/contains? rest-str "#")
(let ((idx (string/index-of rest-str "#")))
(set! fragment (substring rest-str (+ idx 1) (string-length rest-str)))
(set! rest-str (substring rest-str 0 idx))))
;; Query
(when (string/contains? rest-str "?")
(let ((idx (string/index-of rest-str "?")))
(set! query (substring rest-str (+ idx 1) (string-length rest-str)))
(set! rest-str (substring rest-str 0 idx))))
;; Scheme
(when (string/contains? rest-str "://")
(let ((idx (string/index-of rest-str "://")))
(set! scheme (substring rest-str 0 idx))
(set! rest-str (substring rest-str (+ idx 3) (string-length rest-str)))))
;; Split authority from path
(if (string/contains? rest-str "/")
(let ((idx (string/index-of rest-str "/")))
(set! path (substring rest-str idx (string-length rest-str)))
(set! rest-str (substring rest-str 0 idx)))
(set! path "/"))
;; Userinfo (user or user:pass before @)
(when (string/contains? rest-str "@")
(let ((idx (string/index-of rest-str "@")))
(set! userinfo (substring rest-str 0 idx))
(set! rest-str (substring rest-str (+ idx 1) (string-length rest-str)))))
;; Port
(if (string/contains? rest-str ":")
(let* ((idx (string/index-of rest-str ":"))
(port-str (substring rest-str (+ idx 1) (string-length rest-str))))
(set! host (substring rest-str 0 idx))
(if (regex/match? #"^[0-9]+$" port-str)
(set! port (string->number port-str))
(error (format "url/parse: invalid port ~a in ~a — port must be digits" port-str url))))
(set! host rest-str))
(hash-map :scheme scheme :userinfo userinfo :host host :port port
:path path :query query :fragment fragment)))(url/query-build m)Build a query string from a map, percent-encoding keys and values. Keys are emitted in sorted order; non-string values go through str.
View source definition
(define (url/query-build m)
"Build a query string from a map, percent-encoding keys and values. Keys are emitted in sorted order; non-string values go through str."
(string/join
(map (fn (key)
(string-append (url/encode key) "=" (url/encode (str (get m key)))))
(keys m))
"&"))(url/query-parse qs)Parse a query string like "a=1&b=2" into a map of decoded strings. nil or "" gives {}. A key without = gets value ""; when a key repeats, the last value wins.
View source definition
(define (url/query-parse qs)
"Parse a query string like \"a=1&b=2\" into a map of decoded strings. nil or \"\" gives {}. A key without = gets value \"\"; when a key repeats, the last value wins."
(if (or (nil? qs) (= qs ""))
(hash-map)
(foldl (fn (acc pair)
(cond
((= pair "") acc)
((string/contains? pair "=")
(let ((idx (string/index-of pair "=")))
(assoc acc
(url/decode (substring pair 0 idx))
(url/decode (substring pair (+ idx 1) (string-length pair))))))
(#t (assoc acc (url/decode pair) ""))))
(hash-map)
(string/split qs "&"))))(url/with-query url params)Return url with its query string replaced by the encoded params map. An empty map removes the query; the fragment is preserved.
View source definition
(define (url/with-query url params)
"Return url with its query string replaced by the encoded params map. An empty map removes the query; the fragment is preserved."
(let ((q (url/query-build params)))
(url/build (assoc (url/parse url) :query (if (= q "") nil q)))))About this reference
Generated with sema-reader 1.37.0 without executing package code. These are source declarations; dynamic exports, aliases, and runtime changes are not inferred.
Archive SHA-256 0103c8ed137cc8dcc97affe2e1ebc3390c575edc280f0c664d87b28d3d6e85a8