mirror of
https://github.com/c-cube/tiny_httpd.git
synced 2025-12-06 03:05:29 -05:00
feat: easy accessor to the query parameters in path
This commit is contained in:
parent
057d5c9f0c
commit
130608d924
5 changed files with 25 additions and 1 deletions
|
|
@ -351,6 +351,11 @@ module Request = struct
|
|||
let path self = self.path
|
||||
let body self = self.body
|
||||
|
||||
let query self =
|
||||
match Tiny_httpd_util.(parse_query @@ get_query self.path) with
|
||||
| Ok l -> l
|
||||
| Error e -> bad_reqf 400 "invalid query: %s" e
|
||||
|
||||
let get_header ?f self h = Headers.get ?f h self.headers
|
||||
let get_header_int self h = match get_header self h with
|
||||
| Some x -> (try Some (int_of_string x) with _ -> None)
|
||||
|
|
|
|||
|
|
@ -233,6 +233,10 @@ module Request : sig
|
|||
val path : _ t -> string
|
||||
(** Request path. *)
|
||||
|
||||
val query : _ t -> (string*string) list
|
||||
(** Decode the query part of the {!path} field
|
||||
@since NEXT_RELEASE *)
|
||||
|
||||
val body : 'b t -> 'b
|
||||
(** Request body, possibly empty. *)
|
||||
|
||||
|
|
|
|||
|
|
@ -66,6 +66,11 @@ let percent_decode (s:string) : _ option =
|
|||
|
||||
exception Invalid_query
|
||||
|
||||
let get_query s : string =
|
||||
match String.index s '?' with
|
||||
| i -> String.sub s (i+1) (String.length s-i-1)
|
||||
| exception Not_found -> ""
|
||||
|
||||
let parse_query s : (_ list, string) result=
|
||||
let pairs = ref [] in
|
||||
let is_sep_ = function '&' | ';' -> true | _ -> false in
|
||||
|
|
|
|||
|
|
@ -13,6 +13,10 @@ val percent_decode : string -> string option
|
|||
(** Inverse operation of {!percent_encode}.
|
||||
Can fail since some strings are not valid percent encodings. *)
|
||||
|
||||
val get_query : string -> string
|
||||
(** Obtain the query part of a path
|
||||
@since NEXT_RELEASE *)
|
||||
|
||||
val parse_query : string -> ((string*string) list, string) result
|
||||
(** Parse a query as a list of ['&'] or [';'] separated [key=value] pairs.
|
||||
The order might not be preserved.
|
||||
|
|
|
|||
|
|
@ -16,7 +16,13 @@ let () =
|
|||
"/hello/%s@/" (fun name _req -> S.Response.make_string (Ok ("hello " ^name ^"!\n")));
|
||||
(* echo request *)
|
||||
S.add_path_handler server
|
||||
"/echo" (fun req -> S.Response.make_string (Ok (Format.asprintf "echo:@ %a@." S.Request.pp req)));
|
||||
"/echo" (fun req ->
|
||||
let q =
|
||||
S.Request.query req |> List.map (fun (k,v) -> Printf.sprintf "%S = %S" k v)
|
||||
|> String.concat ";"
|
||||
in
|
||||
S.Response.make_string
|
||||
(Ok (Format.asprintf "echo:@ %a@ (query: %s)@." S.Request.pp req q)));
|
||||
S.add_path_handler ~meth:`PUT server
|
||||
"/upload/%s" (fun path req ->
|
||||
S._debug (fun k->k "start upload %S\n%!" path);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue