mirror of
https://github.com/c-cube/tiny_httpd.git
synced 2025-12-06 11:15:35 -05:00
feat: add Route.rest to match the rest of the path
This commit is contained in:
parent
aa66e172e8
commit
2ed9131bdc
2 changed files with 12 additions and 1 deletions
|
|
@ -688,9 +688,11 @@ module Route = struct
|
||||||
|
|
||||||
type (_, _) t =
|
type (_, _) t =
|
||||||
| Fire : ('b, 'b) t
|
| Fire : ('b, 'b) t
|
||||||
|
| Rest : (string -> 'b, 'b) t
|
||||||
| Compose: ('a, 'b) comp * ('b, 'c) t -> ('a, 'c) t
|
| Compose: ('a, 'b) comp * ('b, 'c) t -> ('a, 'c) t
|
||||||
|
|
||||||
let return = Fire
|
let return = Fire
|
||||||
|
let rest = Rest
|
||||||
let (@/) a b = Compose (a,b)
|
let (@/) a b = Compose (a,b)
|
||||||
let string = String
|
let string = String
|
||||||
let string_urlencoded = String_urlencoded
|
let string_urlencoded = String_urlencoded
|
||||||
|
|
@ -703,6 +705,9 @@ module Route = struct
|
||||||
begin match path, route with
|
begin match path, route with
|
||||||
| [], Fire -> Some f
|
| [], Fire -> Some f
|
||||||
| _, Fire -> None
|
| _, Fire -> None
|
||||||
|
| _, Rest ->
|
||||||
|
let whole_path = String.concat "/" path in
|
||||||
|
Some (f whole_path)
|
||||||
| (c1 :: path'), Compose (comp, route') ->
|
| (c1 :: path'), Compose (comp, route') ->
|
||||||
begin match comp with
|
begin match comp with
|
||||||
| Int ->
|
| Int ->
|
||||||
|
|
@ -730,6 +735,7 @@ module Route = struct
|
||||||
: type a b. Buffer.t -> (a,b) t -> unit
|
: type a b. Buffer.t -> (a,b) t -> unit
|
||||||
= fun out -> function
|
= fun out -> function
|
||||||
| Fire -> bpf out "/"
|
| Fire -> bpf out "/"
|
||||||
|
| Rest -> bpf out "<rest>"
|
||||||
| Compose (Exact s, tl) -> bpf out "%s/%a" s pp_ tl
|
| Compose (Exact s, tl) -> bpf out "%s/%a" s pp_ tl
|
||||||
| Compose (Int, tl) -> bpf out "<int>/%a" pp_ tl
|
| Compose (Int, tl) -> bpf out "<int>/%a" pp_ tl
|
||||||
| Compose (String, tl) -> bpf out "<str>/%a" pp_ tl
|
| Compose (String, tl) -> bpf out "<str>/%a" pp_ tl
|
||||||
|
|
|
||||||
|
|
@ -386,7 +386,7 @@ module Route : sig
|
||||||
(** Matches an integer. *)
|
(** Matches an integer. *)
|
||||||
|
|
||||||
val string : (string -> 'a, 'a) comp
|
val string : (string -> 'a, 'a) comp
|
||||||
(** Matches a string and binds it as is. *)
|
(** Matches a string not containing ['/'] and binds it as is. *)
|
||||||
|
|
||||||
val string_urlencoded : (string -> 'a, 'a) comp
|
val string_urlencoded : (string -> 'a, 'a) comp
|
||||||
(** Matches a URL-encoded string, and decodes it. *)
|
(** Matches a URL-encoded string, and decodes it. *)
|
||||||
|
|
@ -397,6 +397,11 @@ module Route : sig
|
||||||
val return : ('a, 'a) t
|
val return : ('a, 'a) t
|
||||||
(** Matches the empty path. *)
|
(** Matches the empty path. *)
|
||||||
|
|
||||||
|
val rest : (string -> 'a, 'a) t
|
||||||
|
(** Matches a string, even containing ['/']. This will match
|
||||||
|
the entirety of the remaining route.
|
||||||
|
@since NEXT_RELEASE *)
|
||||||
|
|
||||||
val (@/) : ('a, 'b) comp -> ('b, 'c) t -> ('a, 'c) t
|
val (@/) : ('a, 'b) comp -> ('b, 'c) t -> ('a, 'c) t
|
||||||
(** [comp / route] matches ["foo/bar/…"] iff [comp] matches ["foo"],
|
(** [comp / route] matches ["foo/bar/…"] iff [comp] matches ["foo"],
|
||||||
and [route] matches ["bar/…"]. *)
|
and [route] matches ["bar/…"]. *)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue