On this page:
request-headers-ref
request-headers-ref*
request-ip-address
request-json
request-path
request-reroot
bindings-ref
bindings-ref-bytes
bindings-ref-number
bindings-ref-symbol
url-scrub
url-path*
url-has-path?

14 HTTP🔗

 (require koyo/http) package: koyo-lib

This module provides utilities for working with HTTP-related data structures.

procedure

(request-headers-ref req header)  (or/c #f bytes?)

  req : request?
  header : bytes?
Returns the value of header in req, if available.

Added in version 0.29 of package koyo-lib.

procedure

(request-headers-ref* req header)  (or/c #f string?)

  req : request?
  header : bytes?
Like request-headers-ref, but decodes the result value.

Added in version 0.29 of package koyo-lib.

procedure

(request-ip-address req)  string?

  req : request?
Returns the user agent’s IP address, parsing it from headers like x-forwarded-for and x-real-ip if provided. Falls back to request-client-ip.

Added in version 0.29 of package koyo-lib.

procedure

(request-json req)  jsexpr?

  req : request?
Returns the JSON data corresponding to req, if any.

Added in version 0.29 of package koyo-lib.

procedure

(request-path req)  string?

  req : request?
Returns the absolute request path for req, scrubbed of path params.

procedure

(request-reroot req root)  (or/c #f request?)

  req : request?
  root : url?
Returns a copy of req with its request-uri changed to drop the given root prefix. Returns #f when req’s URI is not prefixed by root.

Examples:
> (require koyo/http
           koyo/testing
           net/url
           web-server/http)
> (url->string
   (request-uri
    (request-reroot
     (make-test-request #:path "/a/b/c")
     (string->url "/a/b"))))

"http://127.0.0.1:80/c"

> (request-reroot
   (make-test-request #:path "/d")
   (string->url "/a/b"))

#f

Added in version 0.28 of package koyo-lib.

procedure

(bindings-ref bindings name [default])  (or/c #f string?)

  bindings : (listof binding?)
  name : symbol?
  default : (or/c #f string?) = #f

procedure

(bindings-ref-bytes bindings name [default])  (or/c #f bytes?)

  bindings : (listof binding?)
  name : symbol?
  default : (or/c #f bytes?) = #f

procedure

(bindings-ref-number bindings name [default])  (or/c #f number?)

  bindings : (listof binding?)
  name : symbol?
  default : (or/c #f number?) = #f

procedure

(bindings-ref-symbol bindings name [default])  (or/c #f symbol?)

  bindings : (listof binding?)
  name : symbol?
  default : (or/c #f symbol?) = #f
Finds the first binding in bindings whose name is name and returns its value or default.

procedure

(url-scrub u)  url?

  u : url?
Removes all the path params from u, while leaving its path intact. This is used by the default continuation mismatch handler to strip the current URL of its continuation id.

> (require koyo/http
           net/url)
> (url->string
   (url-scrub
    (string->url "https://127.0.0.1/foo/bar;(\"k\" . \"123\")/baz")))

"https://127.0.0.1/foo/bar/baz"

procedure

(url-path* u)  string?

  u : url?
Joins the scrubbed path params in u and returns the resulting string.

Examples:
> (require koyo/http
           net/url)
> (url-path* (string->url "https://example.com"))

"/"

> (url-path* (string->url "https://example.com/"))

"/"

> (url-path* (string->url "https://example.com/a/b/c"))

"/a/b/c"

Added in version 0.21 of package koyo-lib.

procedure

(url-has-path? u p)  boolean?

  u : url?
  p : string?
Returns #t when the scrubbed path of u matches p.

Examples:
> (require koyo/http
           net/url)
> (url-has-path? (string->url "https://example.com") "")

#t

> (url-has-path? (string->url "https://example.com") "/")

#t

> (url-has-path? (string->url "https://example.com/a") "/a")

#t

> (url-has-path? (string->url "https://example.com/a/b/c") "/a/b/c")

#t

> (url-has-path? (string->url "https://example.com/a/b/c") "a/b/c")

#t

Added in version 0.21 of package koyo-lib.