text
stringlengths 12
786k
|
---|
let canonicalize uri = let uri = resolve " " empty uri in let module Scheme = ( val ( module_of_scheme ( uncast_opt uri . scheme ) ) : Scheme ) in { uri with port = Scheme . canonicalize_port uri . port ; path = Scheme . canonicalize_path uri . path ; } |
let pp ppf uri = Format . pp_print_string ppf ( to_string uri ) |
let pp_hum ppf uri = Format . pp_print_string ppf ( to_string uri ) |
module Parser = struct open Angstrom let string_of_char = String . make 1 let string_of_char_list chars = String . concat " " ( List . map string_of_char chars ) let scheme = lift ( fun s -> Some ( Pct . decode ( Pct . cast_encoded s ) ) ) ( take_while ( fun c -> c <> ' ' : && c <> ' ' / && c <> ' ' ? && c <> ' ' ) # <* char ' ' ) : <|> return None let is_digit = function ' 0 ' . . ' 9 ' -> true | _ -> false let hex_digit = satisfy ( function | ' 0 ' . . ' 9 ' | ' A ' . . ' F ' | ' a ' . . ' f ' -> true | _ -> false ) let hexadecimal = lift string_of_char_list ( many hex_digit ) let c_dot = char ' . ' let c_at = char ' ' @ let c_colon = char ' ' : let dec_octet = take_while1 ( function ' 0 ' . . ' 9 ' -> true | _ -> false ) >>= fun num -> if int_of_string num < 256 then return num else fail " invalid octect " let ipv4_address = lift2 ( fun three one -> String . concat " . " three ^ " . " ^ one ) ( count 3 ( dec_octet <* c_dot ) ) dec_octet let after_double_colon = fix ( fun f -> list [ ipv4_address ] <|> lift2 ( fun x y -> x :: y ) hexadecimal ( c_colon *> f <|> return [ ] ) ) let double_colon count = after_double_colon >>= ( fun rest -> let filler_length = 8 - count - List . length rest in if filler_length <= 0 then fail " too many parts in IPv6 address " else return ( " " :: rest ) ) <|> return [ " " ] let rec part = function | 7 -> lift ( fun x -> [ x ] ) hexadecimal | 6 -> list [ ipv4_address ] <|> hex_part 6 | n -> hex_part n and hex_part n = lift2 ( fun x y -> x :: y ) hexadecimal ( c_colon *> ( c_colon *> double_colon ( n + 1 ) <|> part ( n + 1 ) ) ) let rec split_with f xs = match xs with | [ ] -> [ ] , [ ] | y :: ys -> if f y then let zs , ts = split_with f ys in y :: zs , ts else [ ] , xs let ipv6 = let format_addr segments = let before_double_colon , after_double_colon = split_with ( fun segment -> segment <> " " ) segments in let before = String . concat " " : before_double_colon in let res = match after_double_colon with | " " :: xs -> before ^ " " :: ^ String . concat " " : xs | _ -> before in res in lift format_addr ( c_colon *> c_colon *> double_colon 0 <|> part 0 ) let ipv6_address = lift3 ( fun lb ip rb -> String . concat " " [ string_of_char lb ; ip ; string_of_char rb ] ) ( char ' [ ' ) ipv6 ( char ' ] ' ) let pct_encoded = lift2 ( fun pct digits -> string_of_char_list ( pct :: digits ) ) ( char ' ' ) % ( count 2 hex_digit ) let sub_delims = satisfy ( function | ' ' ! | ' ' $ | ' ' & | ' ' ' \ | ' ( ' | ' ) ' | ' ' * | ' ' + | ' , ' | ' ; ' | ' ' = -> true | _ -> false ) let unreserved = satisfy ( function | ' A ' . . ' Z ' | ' a ' . . ' z ' | ' 0 ' . . ' 9 ' | ' ' - | ' . ' | ' _ ' | ' ' ~ -> true | _ -> false ) let reg_name = lift ( String . concat " " ) ( many ( choice [ string_of_char <$> unreserved ; pct_encoded ; string_of_char <$> sub_delims ] ) ) let host = lift ( fun s -> Pct . decode ( Pct . cast_encoded s ) ) ( choice [ reg_name ; ipv4_address ; ipv6_address ] ) let userinfo = lift ( fun x -> let s = String . concat " " x in Some ( Userinfo . userinfo_of_encoded s ) ) ( many ( choice [ string_of_char <$> unreserved ; pct_encoded ; string_of_char <$> sub_delims ; string_of_char <$> c_colon ] ) <* c_at ) <|> return None let port = peek_char >>= function | Some ' ' : -> c_colon *> take_while is_digit >>| fun port -> let decoded = Pct . decode ( Pct . cast_encoded port ) in ( try Some ( int_of_string ( Pct . uncast_decoded decoded ) ) with _ -> None ) | Some _ | None -> return None let authority = string " " // *> lift3 ( fun userinfo host port -> userinfo , Some host , port ) userinfo host port <|> return ( None , None , None ) let path = lift Path . path_of_encoded ( take_while ( function ' ' ? | ' ' # -> false | _ -> true ) ) let query = lift Query . of_raw ( char ' ' ? *> take_till ( function ' ' # -> true | _ -> false ) ) <|> return ( Query . Raw ( None , Lazy . from_val [ ] ) ) let fragment = lift ( fun s -> Some ( Pct . decode ( Pct . cast_encoded s ) ) ) ( char ' ' # *> take_while ( fun _ -> true ) ) <|> return None let _uri_reference = lift4 ( fun scheme ( userinfo , host , port ) path query fragment -> normalize scheme { scheme ; userinfo ; host ; port ; path ; query ; fragment } ) scheme authority path query <*> fragment let uri_reference = take_while ( function | ' \ n ' -> false | _ -> true ) >>| fun s -> match Angstrom . parse_string ~ consume : All _uri_reference s with | Ok t -> t | Error _ -> empty end |
let of_string s = match Angstrom . parse_string ~ consume : Prefix Parser . uri_reference s with | Ok t -> t | Error _ -> empty |
type t = string [ @@ deriving_inline yojson ] |
let _ = fun ( _ : t ) -> ( ) |
let t_of_yojson = ( string_of_yojson : Ppx_yojson_conv_lib . Yojson . Safe . t -> t ) |
let yojson_of_t = ( yojson_of_string : t -> Ppx_yojson_conv_lib . Yojson . Safe . t ) |
let _ = yojson_of_t [ @@@ end ] |
let to_string uri = uri |
let proto = match Sys . win32 with | true -> " file " :/// | false -> " file " :// |
let to_path ( uri : t ) = let path = match String . drop_prefix ~ prefix : proto uri with | Some path -> path | None -> uri in path |> String . replace_all ~ pattern " " :\\ ~ with_ " " :/ |> String . replace_all ~ pattern " :% 3A " ~ with_ " " :: |> String . replace_all ~ pattern " :% 5C " ~ with_ " " :/ |
let of_path ( path : string ) = let path = path |> String . replace_all ~ pattern " " :\\ ~ with_ " " :/ |> String . replace_all ~ pattern " " :: ~ with_ " :% 3A " in proto ^ path |
let pp fmt uri = Format . fprintf fmt " % s " uri |
module Private = struct module Heap = Heap end |
module type FLAGS = sig type t = private int val of_int : int -> t val ( + ) : t -> t -> t val mem : t -> t -> bool end |
module Flags = struct type t = int let empty = 0 let of_int x = x let ( + ) = ( lor ) let mem a b = ( a land b ) = a end |
module Open_flags = struct include Flags let rdonly = Config . o_rdonly let wronly = Config . o_wronly let rdwr = Config . o_rdwr let creat = Config . o_creat let excl = Config . o_excl let noctty = Config . o_noctty let trunc = Config . o_trunc let append = Config . o_append let nonblock = Config . o_nonblock let dsync = Config . o_dsync let direct = Config . o_direct let largefile = Config . o_largefile let directory = Config . o_directory let nofollow = Config . o_nofollow let noatime = Config . o_noatime let cloexec = Config . o_cloexec let sync = Config . o_sync let path = Config . o_path let tmpfile = Config . o_tmpfile end |
module Resolve = struct include Flags let no_xdev = 0x01 let no_magiclinks = 0x02 let no_symlinks = 0x04 let beneath = 0x08 let in_root = 0x10 let cached = 0x20 end |
module Poll_mask = struct include Flags let pollin = Config . pollin let pollout = Config . pollout let pollerr = Config . pollerr let pollhup = Config . pollhup end |
module Sockaddr = struct type t external of_unix : Unix . sockaddr -> t = " ocaml_uring_make_sockaddr " external get : t -> Unix . sockaddr = " ocaml_uring_extract_sockaddr " let dummy_addr = Unix . ADDR_UNIX " " - let create ( ) = of_unix dummy_addr end |
module Open_how = struct type t external make : int -> Unix . file_perm -> int -> string -> t = " ocaml_uring_make_open_how " let v ~ open_flags ~ perm ~ resolve path = make open_flags perm resolve path end |
module Iovec = struct module Check : sig [ @@@ warning " - 34 " ] type t = private { buffer : ( char , Bigarray . int8_unsigned_elt , Bigarray . c_layout ) Bigarray . Array1 . t ; off : int ; len : int ; } end = Cstruct type iovec type t = iovec * int * Cstruct . t list external make_iovec : Cstruct . t list -> int -> iovec = " ocaml_uring_make_iovec " let make buffers = let len = List . length buffers in let iovec = make_iovec buffers len in ( iovec , len , buffers ) end |
module Msghdr = struct type msghdr type t = msghdr * Sockaddr . t option * Iovec . t external make_msghdr : int -> Unix . file_descr list -> Sockaddr . t option -> Iovec . t -> msghdr = " ocaml_uring_make_msghdr " external get_msghdr_fds : msghdr -> Unix . file_descr list = " ocaml_uring_get_msghdr_fds " let get_fds ( hdr , _ , _ ) = get_msghdr_fds hdr let create_with_addr ~ n_fds ~ fds ? addr buffs = let iovs = Iovec . make buffs in make_msghdr n_fds fds addr iovs , addr , iovs let create ( ? n_fds = 0 ) ? addr buffs = create_with_addr ~ n_fds ~ fds [ ] : ? addr buffs end |
type ' a job = ' a Heap . entry |
module Uring = struct type t external create : int -> int option -> t = " ocaml_uring_setup " external exit : t -> unit = " ocaml_uring_exit " external unregister_buffers : t -> unit = " ocaml_uring_unregister_buffers " external register_bigarray : t -> Cstruct . buffer -> unit = " ocaml_uring_register_ba " external submit : t -> int = " ocaml_uring_submit " type id = Heap . ptr type offset = Optint . Int63 . t external submit_nop : t -> id -> bool = " ocaml_uring_submit_nop " [ @@ noalloc ] external submit_poll_add : t -> Unix . file_descr -> id -> Poll_mask . t -> bool = " ocaml_uring_submit_poll_add " [ @@ noalloc ] external submit_readv : t -> Unix . file_descr -> id -> Iovec . t -> offset -> bool = " ocaml_uring_submit_readv " [ @@ noalloc ] external submit_writev : t -> Unix . file_descr -> id -> Iovec . t -> offset -> bool = " ocaml_uring_submit_writev " [ @@ noalloc ] external submit_readv_fixed : t -> Unix . file_descr -> id -> Cstruct . buffer -> int -> int -> offset -> bool = " ocaml_uring_submit_readv_fixed_byte " " ocaml_uring_submit_readv_fixed_native " [ @@ noalloc ] external submit_writev_fixed : t -> Unix . file_descr -> id -> Cstruct . buffer -> int -> int -> offset -> bool = " ocaml_uring_submit_writev_fixed_byte " " ocaml_uring_submit_writev_fixed_native " [ @@ noalloc ] external submit_close : t -> Unix . file_descr -> id -> bool = " ocaml_uring_submit_close " [ @@ noalloc ] external submit_splice : t -> id -> Unix . file_descr -> Unix . file_descr -> int -> bool = " ocaml_uring_submit_splice " [ @@ noalloc ] external submit_connect : t -> id -> Unix . file_descr -> Sockaddr . t -> bool = " ocaml_uring_submit_connect " [ @@ noalloc ] external submit_accept : t -> id -> Unix . file_descr -> Sockaddr . t -> bool = " ocaml_uring_submit_accept " [ @@ noalloc ] external submit_cancel : t -> id -> id -> bool = " ocaml_uring_submit_cancel " [ @@ noalloc ] external submit_openat2 : t -> id -> Unix . file_descr -> Open_how . t -> bool = " ocaml_uring_submit_openat2 " [ @@ noalloc ] external submit_send_msg : t -> id -> Unix . file_descr -> Msghdr . t -> bool = " ocaml_uring_submit_send_msg " [ @@ noalloc ] external submit_recv_msg : t -> id -> Unix . file_descr -> Msghdr . t -> bool = " ocaml_uring_submit_recv_msg " [ @@ noalloc ] type cqe_option = private | Cqe_none | Cqe_some of { user_data_id : id ; res : int } [ @@ ocaml . warning " - 37 " ] external wait_cqe : t -> cqe_option = " ocaml_uring_wait_cqe " external wait_cqe_timeout : float -> t -> cqe_option = " ocaml_uring_wait_cqe_timeout " external peek_cqe : t -> cqe_option = " ocaml_uring_peek_cqe " external error_of_errno : int -> Unix . error = " ocaml_uring_error_of_errno " end |
type ' a t = { id : < ; > uring : Uring . t ; mutable fixed_iobuf : Cstruct . buffer ; data : ' a Heap . t ; queue_depth : int ; mutable dirty : bool ; } |
module Generic_ring = struct type ring = T : ' a t -> ring type t = ring let compare ( T a ) ( T b ) = compare a . id b . id end |
let gc_roots = Atomic . make Ring_set . empty |
let rec update_gc_roots fn = let old_set = Atomic . get gc_roots in let new_set = fn old_set in if not ( Atomic . compare_and_set gc_roots old_set new_set ) then update_gc_roots fn |
let register_gc_root t = update_gc_roots ( Ring_set . add ( Generic_ring . T t ) ) |
let unregister_gc_root t = update_gc_roots ( Ring_set . remove ( Generic_ring . T t ) ) |
let create ? polling_timeout ~ queue_depth ( ) = if queue_depth < 1 then Fmt . invalid_arg " Non - positive queue depth : % d " queue_depth ; let uring = Uring . create queue_depth polling_timeout in let data = Heap . create queue_depth in let id = object end in let fixed_iobuf = Cstruct . empty . buffer in let t = { id ; uring ; fixed_iobuf ; data ; dirty = false ; queue_depth } in register_gc_root t ; t |
let ensure_idle t op = match Heap . in_use t . data with | 0 -> ( ) | n -> Fmt . invalid_arg " % s : % d request ( s ) still active " ! op n |
let set_fixed_buffer t iobuf = ensure_idle t " set_fixed_buffer " ; if Bigarray . Array1 . dim t . fixed_iobuf > 0 then Uring . unregister_buffers t . uring ; t . fixed_iobuf <- iobuf ; if Bigarray . Array1 . dim iobuf > 0 then ( match Uring . register_bigarray t . uring iobuf with | ( ) -> Ok ( ) | exception Unix . Unix_error ( Unix . ENOMEM , " io_uring_register_buffers " , " " ) -> Error ` ENOMEM ) else Ok ( ) |
let exit t = ensure_idle t " exit " ; Uring . exit t . uring ; unregister_gc_root t |
let with_id_full : type a . a t -> ( Heap . ptr -> bool ) -> a -> extra_data ' : b -> a job option = fun t fn datum ~ extra_data -> match Heap . alloc t . data datum ~ extra_data with | exception Heap . No_space -> None | entry -> let ptr = Heap . ptr entry in let has_space = fn ptr in if has_space then ( t . dirty <- true ; Some entry ) else ( ignore ( Heap . free t . data ptr : a ) ; None ) |
let with_id t fn a = with_id_full t fn a ~ extra_data ( ) : |
let noop t user_data = with_id t ( fun id -> Uring . submit_nop t . uring id ) user_data |
let at_fdcwd : Unix . file_descr = Obj . magic Config . at_fdcwd |
let openat2 t ~ access ~ flags ~ perm ~ resolve ( ? fd = at_fdcwd ) path user_data = let open_flags = flags lor match access with | ` R -> Open_flags . rdonly | ` W -> Open_flags . wronly | ` RW -> Open_flags . rdwr in let open_how = Open_how . v ~ open_flags ~ perm ~ resolve path in with_id_full t ( fun id -> Uring . submit_openat2 t . uring id fd open_how ) user_data ~ extra_data : open_how |
let readv t ~ file_offset fd buffers user_data = let iovec = Iovec . make buffers in with_id_full t ( fun id -> Uring . submit_readv t . uring fd id iovec file_offset ) user_data ~ extra_data : iovec |
let read_fixed t ~ file_offset fd ~ off ~ len user_data = with_id t ( fun id -> Uring . submit_readv_fixed t . uring fd id t . fixed_iobuf off len file_offset ) user_data |
let read_chunk ? len t ~ file_offset fd chunk user_data = let { Cstruct . buffer ; off ; len } = Region . to_cstruct ? len chunk in if buffer != t . fixed_iobuf then invalid_arg " Chunk does not belong to ring " ; ! with_id t ( fun id -> Uring . submit_readv_fixed t . uring fd id t . fixed_iobuf off len file_offset ) user_data |
let write_fixed t ~ file_offset fd ~ off ~ len user_data = with_id t ( fun id -> Uring . submit_writev_fixed t . uring fd id t . fixed_iobuf off len file_offset ) user_data |
let write_chunk ? len t ~ file_offset fd chunk user_data = let { Cstruct . buffer ; off ; len } = Region . to_cstruct ? len chunk in if buffer != t . fixed_iobuf then invalid_arg " Chunk does not belong to ring " ; ! with_id t ( fun id -> Uring . submit_writev_fixed t . uring fd id t . fixed_iobuf off len file_offset ) user_data |
let writev t ~ file_offset fd buffers user_data = let iovec = Iovec . make buffers in with_id_full t ( fun id -> Uring . submit_writev t . uring fd id iovec file_offset ) user_data ~ extra_data : iovec |
let poll_add t fd poll_mask user_data = with_id t ( fun id -> Uring . submit_poll_add t . uring fd id poll_mask ) user_data |
let close t fd user_data = with_id t ( fun id -> Uring . submit_close t . uring fd id ) user_data |
let splice t ~ src ~ dst ~ len user_data = with_id t ( fun id -> Uring . submit_splice t . uring id src dst len ) user_data |
let connect t fd addr user_data = let addr = Sockaddr . of_unix addr in with_id_full t ( fun id -> Uring . submit_connect t . uring id fd addr ) user_data ~ extra_data : addr |
let accept t fd addr user_data = with_id_full t ( fun id -> Uring . submit_accept t . uring id fd addr ) user_data ~ extra_data : addr |
let send_msg ( ? fds [ ] ) = ? dst t fd buffers user_data = let addr = Option . map Sockaddr . of_unix dst in let n_fds = List . length fds in let msghdr = Msghdr . create_with_addr ~ n_fds ~ fds ? addr buffers in with_id_full t ( fun id -> Uring . submit_send_msg t . uring id fd msghdr ) user_data ~ extra_data : msghdr |
let recv_msg t fd msghdr user_data = with_id_full t ( fun id -> Uring . submit_recv_msg t . uring id fd msghdr ) user_data ~ extra_data : msghdr |
let cancel t job user_data = ignore ( Heap . ptr job : Uring . id ) ; with_id t ( fun id -> Uring . submit_cancel t . uring id ( Heap . ptr job ) ) user_data |
let submit t = if t . dirty then begin t . dirty <- false ; Uring . submit t . uring end else 0 |
type ' a completion_option = | None | Some of { result : int ; data : ' a } |
let fn_on_ring fn t = match fn t . uring with | Uring . Cqe_none -> None | Uring . Cqe_some { user_data_id ; res } -> let data = Heap . free t . data user_data_id in Some { result = res ; data } |
let peek t = fn_on_ring Uring . peek_cqe t |
let wait ? timeout t = match timeout with | None -> fn_on_ring Uring . wait_cqe t | Some timeout -> fn_on_ring ( Uring . wait_cqe_timeout timeout ) t |
let queue_depth { queue_depth ; _ } = queue_depth |
let buf { fixed_iobuf ; _ } = fixed_iobuf |
let error_of_errno e = Uring . error_of_errno ( abs e ) |
module Uri_re = struct open Re module Raw = struct let ( ) + a b = seq [ a ; b ] let ( ) / a b = alt [ a ; b ] let gen_delims = Posix . re " [ [ ] ] " :/?#\\\\@ let sub_delims = Posix . re " [ ' ( ) , ; ] " !$&*+= let c_at = char ' ' @ let c_colon = char ' ' : let c_slash = char ' ' / let c_slash2 = Posix . re " " // let c_dot = char ' . ' let c_question = char ' ' ? let c_hash = char ' ' # let reserved = gen_delims / sub_delims let unreserved = Posix . re " [ A - Za - z0 - 9 . - _ ] " ~ let hexdig = Posix . re " [ 0 - 9A - Fa - f ] " let pct_encoded = ( char ' ' ) % + hexdig + hexdig let dec_octet = Posix . re " 25 [ 0 - 5 ] | 2 [ 0 - 4 ] [ 0 - 9 ] [ | 01 ] [ ? 0 - 9 ] [ 0 - 9 ] " ? let ipv4_address = ( repn ( dec_octet + c_dot ) 3 ( Some 3 ) ) + dec_octet let zone_id = unreserved / pct_encoded let ipv6_address = let ( ) =| n a = repn a n ( Some n ) in let ( ) <| n a = repn a 0 ( Some n ) in let h16 = repn hexdig 1 ( Some 4 ) in let h16c = h16 + c_colon in let cc = c_colon + c_colon in let ls32 = ( h16c + h16 ) / ipv4_address in ( char ' [ ' + ( ( ( 6 =| h16c ) + ls32 ) / ( cc + ( 5 =| h16c ) + ls32 ) / ( ( 1 <| h16 ) + cc + ( 4 =| h16c ) + ls32 ) / ( ( 1 ( ( <| 1 <| h16c ) + h16 ) ) + cc + ( 3 =| h16c ) + ls32 ) / ( ( 1 ( ( <| 2 <| h16c ) + h16 ) ) + cc + ( 2 =| h16c ) + ls32 ) / ( ( 1 ( ( <| 3 <| h16c ) + h16 ) ) + cc + h16c + ls32 ) / ( ( 1 ( ( <| 4 <| h16c ) + h16 ) ) + cc + ls32 ) / ( ( 1 ( ( <| 5 <| h16c ) + h16 ) ) + cc + h16 ) / ( ( 1 ( ( <| 6 <| h16c ) + h16 ) ) + cc ) ) + ( opt ( Posix . re " % 25 " + rep1 zone_id ) ) + char ' ] ' ) let reg_name = rep ( unreserved / pct_encoded / sub_delims ) let host = ipv6_address / ipv4_address / reg_name let userinfo = rep ( unreserved / pct_encoded / sub_delims / c_colon ) let port = Posix . re " [ 0 - 9 ] " * let authority = ( opt ( ( group userinfo ) + c_at ) ) + ( group host ) + ( opt ( c_colon + ( group port ) ) ) let null_authority = ( group empty ) + ( group empty ) + ( group empty ) let pchar = unreserved / pct_encoded / sub_delims / c_colon / c_at let segment = rep pchar let segment_nz = rep1 pchar let segment_nz_nc = repn ( unreserved / pct_encoded / sub_delims / c_at ) 1 None let path_abempty = rep ( c_slash + segment ) let path_absolute = c_slash + ( opt ( segment_nz + ( rep ( c_slash + segment ) ) ) ) let path_noscheme = segment_nz_nc + ( rep ( c_slash + segment ) ) let path_rootless = segment_nz + ( rep ( c_slash + segment ) ) let path_empty = empty let path = path_abempty / path_absolute / path_noscheme / path_rootless / path_empty let hier_part = ( c_slash2 + authority + path_abempty ) / ( path_absolute / path_rootless / path_empty ) let scheme = Posix . re " [ A - Za - z ] [ A - Za - z0 - 9 . ] " +\\\\-\\* let query = group ( rep ( pchar / c_slash / c_question ) ) let fragment = group ( rep ( pchar / c_slash / c_question ) ) let absolute_uri = scheme + c_colon + hier_part + ( opt ( c_question + query ) ) let uri = scheme + c_colon + hier_part + ( opt ( c_question + query ) ) + ( opt ( c_hash + fragment ) ) let relative_part = ( c_slash2 + authority + path_abempty ) / ( path_absolute / path_noscheme / path_empty ) let relative_ref = relative_part + ( opt ( c_question + query ) ) + ( opt ( c_hash + fragment ) ) let uri_reference = Posix . re " ( ( [ ] ) ) ( ( [ ] ) ) ( [ ] ) ( ( [ ] ) ) ( ( . ) ) " ^^:/?#+:?//^/?#*?^?#*\\?^#*?#*? end let ipv4_address = Posix . compile Raw . ipv4_address let ipv6_address = Posix . compile Raw . ipv6_address let uri_reference = Posix . compile Raw . uri_reference let authority = Posix . compile Raw . authority let host = Posix . compile Raw . host end |
type component = [ | ` Scheme | ` Authority | ` Userinfo | ` Host | ` Path | ` Query | ` Query_key | ` Query_value | ` Fragment ] |
let rec iter_concat fn sep buf = function | last [ ] :: -> fn buf last | el :: rest -> fn buf el ; Buffer . add_string buf sep ; iter_concat fn sep buf rest | [ ] -> ( ) |
let rev_interject e lst = let rec aux acc = function | [ ] -> acc | x :: xs -> aux ( x :: e :: acc ) xs in match lst with | [ ] -> [ ] | h :: t -> aux [ h ] t |
let compare_opt c t t ' = match t , t ' with | None , None -> 0 | Some _ , None -> 1 | None , Some _ -> - 1 | Some a , Some b -> c a b |
let rec compare_list f t t ' = match t , t ' with | [ ] , [ ] -> 0 | _ :: _ , [ ] -> 1 | [ ] , _ :: _ -> - 1 | x :: xs , y :: ys -> match f x y with 0 -> compare_list f xs ys | c -> c |
type safe_chars = bool array |
module type Scheme = sig val safe_chars_for_component : component -> safe_chars val normalize_host : string option -> string option val canonicalize_port : int option -> int option val canonicalize_path : string list -> string list end |
module Generic : Scheme = struct let sub_delims a = let subd = " ' ( ) , ; " !$&*+= in for i = 0 to String . length subd - 1 do let c = Char . code subd . [ i ] in a . ( c ) <- true done ; a let safe_chars : safe_chars = let a = Array . make 256 false in let always_safe = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_ . " -~ in for i = 0 to String . length always_safe - 1 do let c = Char . code always_safe . [ i ] in a . ( c ) <- true done ; a let pchar : safe_chars = let a = sub_delims ( Array . copy safe_chars ) in a . ( Char . code ' ' ) : <- true ; a . ( Char . code ' ' ) @ <- true ; a let safe_chars_for_scheme : safe_chars = let a = Array . copy safe_chars in a . ( Char . code ' ' ) + <- true ; a let safe_chars_for_path : safe_chars = let a = sub_delims ( Array . copy pchar ) in a . ( Char . code ' ' ) / <- false ; a let safe_chars_for_query : safe_chars = let a = Array . copy pchar in a . ( Char . code ' ' ) / <- true ; a . ( Char . code ' ' ) ? <- true ; a . ( Char . code ' ' ) & <- false ; a . ( Char . code ' ; ' ) <- false ; a . ( Char . code ' ' ) + <- false ; a let safe_chars_for_query_key : safe_chars = let a = Array . copy safe_chars_for_query in a . ( Char . code ' ' ) = <- false ; a let safe_chars_for_query_value : safe_chars = let a = Array . copy safe_chars_for_query in a . ( Char . code ' , ' ) <- false ; a let safe_chars_for_fragment : safe_chars = safe_chars_for_query let safe_chars_for_userinfo : safe_chars = let a = Array . copy safe_chars in a . ( Char . code ' ' ) : <- false ; a let safe_chars_for_component = function | ` Path -> safe_chars_for_path | ` Userinfo -> safe_chars_for_userinfo | ` Query -> safe_chars_for_query | ` Query_key -> safe_chars_for_query_key | ` Query_value -> safe_chars_for_query_value | ` Fragment -> safe_chars_for_fragment | ` Scheme -> safe_chars_for_scheme | _ -> safe_chars let normalize_host hso = hso let canonicalize_port port = port let canonicalize_path path = path end |
module Http : Scheme = struct include Generic let normalize_host = function | Some hs -> Some ( String . lowercase_ascii hs ) | None -> None let canonicalize_port = function | None -> None | Some 80 -> None | Some x -> Some x let canonicalize_path = function | [ ] -> [ " " ] / | x -> x end |
module Https : Scheme = struct include Http let canonicalize_port = function | None -> None | Some 443 -> None | Some x -> Some x end |
module File : Scheme = struct include Generic let normalize_host = function | Some hs -> let hs = String . lowercase_ascii hs in if hs " = localhost " then Some " " else Some hs | None -> None end |
module Urn : Scheme = struct include Generic end |
let module_of_scheme = function | Some s -> begin match String . lowercase_ascii s with | " http " -> ( module Http : Scheme ) | " https " -> ( module Https : Scheme ) | " file " -> ( module File : Scheme ) | " urn " -> ( module Urn : Scheme ) | _ -> ( module Generic : Scheme ) end | None -> ( module Generic : Scheme ) |
module Pct : sig type encoded type decoded val encode : ? scheme : string -> ? component : component -> decoded -> encoded val decode : encoded -> decoded val empty_decoded : decoded val cast_encoded : string -> encoded val cast_decoded : string -> decoded val uncast_encoded : encoded -> string val uncast_decoded : decoded -> string val lift_encoded : ( encoded -> encoded ) -> string -> string val lift_decoded : ( decoded -> decoded ) -> string -> string val unlift_encoded : ( string -> string ) -> encoded -> encoded val unlift_decoded : ( string -> string ) -> decoded -> decoded val unlift_decoded2 : ( string -> string -> ' a ) -> decoded -> decoded -> ' a type encoded = string type decoded = string let cast_encoded x = x let cast_decoded x = x let empty_decoded = " " let uncast_decoded x = x let uncast_encoded x = x let lift_encoded f = f let lift_decoded f = f let unlift_encoded f = f let unlift_decoded f = f let unlift_decoded2 f = f let encode ? scheme ( ? component ` = Path ) b = let module Scheme = ( val ( module_of_scheme scheme ) : Scheme ) in let safe_chars = Scheme . safe_chars_for_component component in let len = String . length b in let buf = Buffer . create len in let rec scan start cur = if cur >= len then begin Buffer . add_substring buf b start ( cur - start ) ; end else begin let c = Char . code b . [ cur ] in if safe_chars . ( c ) then scan start ( cur + 1 ) else begin if cur > start then Buffer . add_substring buf b start ( cur - start ) ; Buffer . add_string buf ( Printf . sprintf " %%% 02X " c ) ; scan ( cur + 1 ) ( cur + 1 ) end end in scan 0 0 ; Buffer . contents buf let int_of_hex_char c = let c = int_of_char ( Char . uppercase_ascii c ) - 48 in if c > 9 then if c > 16 && c < 23 then c - 7 else failwith " int_of_hex_char " else if c >= 0 then c else failwith " int_of_hex_char " let decode b = let len = String . length b in let buf = Buffer . create len in let rec scan start cur = if cur >= len then Buffer . add_substring buf b start ( cur - start ) else if b . [ cur ] = ' ' % then begin Buffer . add_substring buf b start ( cur - start ) ; let cur = cur + 1 in if cur >= len then Buffer . add_char buf ' ' % else match int_of_hex_char b . [ cur ] with | exception _ -> Buffer . add_char buf ' ' ; % scan cur cur | highbits -> begin let cur = cur + 1 in if cur >= len then begin Buffer . add_char buf ' ' ; % Buffer . add_char buf b . [ cur - 1 ] end else begin let start_at = match int_of_hex_char b . [ cur ] with | lowbits -> Buffer . add_char buf ( Char . chr ( highbits lsl 4 + lowbits ) ) ; cur + 1 | exception _ -> Buffer . add_char buf ' ' ; % Buffer . add_char buf b . [ cur - 1 ] ; cur in scan start_at start_at end end end else scan start ( cur + 1 ) in scan 0 0 ; Buffer . contents buf end |
let pct_encode ? scheme ( ? component ` = Path ) s = Pct . ( uncast_encoded ( encode ? scheme ~ component ( cast_decoded s ) ) ) |
let pct_decode s = Pct . ( uncast_decoded ( decode ( cast_encoded s ) ) ) |
module Userinfo = struct type t = string * string option let compare ( u , p ) ( u ' , p ' ) = match String . compare u u ' with | 0 -> compare_opt String . compare p p ' | c -> c let userinfo_of_encoded us = match Stringext . split ~ max : 2 ~ on ' ' :: us with | [ ] -> ( " " , None ) | [ u ] -> ( pct_decode u , None ) | u :: p :: _ -> ( pct_decode u , Some ( pct_decode p ) ) let encoded_of_userinfo ? scheme ( u , po ) = let len = String . ( 1 + ( length u ) + ( match po with None -> 0 | Some p -> length p ) ) in let buf = Buffer . create len in Buffer . add_string buf ( pct_encode ? scheme ~ component ` : Userinfo u ) ; begin match po with None -> ( ) ; | Some p -> Buffer . add_char buf ' ' ; : Buffer . add_string buf ( pct_encode ? scheme ~ component ` : Userinfo p ) end ; Pct . cast_encoded ( Buffer . contents buf ) end |
let encoded_of_userinfo ? scheme = Userinfo . encoded_of_userinfo ? scheme |
module Path = struct type t = string list let compare = compare_list String . compare let path_of_encoded ps = let tokl = Stringext . full_split ps ~ on ' ' :/ in List . map pct_decode tokl let remove_dot_segments p = let revp = List . rev p in let rec loop ascension outp = function | " " " . . " /:::: r | " . . " :: r -> loop ( ascension + 1 ) outp r | " " " . " /:::: r | " . " :: r -> loop ascension outp r | " " [ ] /:: | [ ] when List . ( length p > 0 && hd p = " " ) / -> " " /:: outp | [ ] when ascension > 0 -> List . rev_append ( " " ( /:: rev_interject " " / Array . ( to_list ( make ascension " . . " ) ) ) ) outp | [ ] -> List . ( if length outp > 0 && hd outp = " " / then tl outp else outp ) | " " " " /::/:: r when ascension > 0 -> loop ( ascension - 1 ) outp ( " " /:: r ) | " " /:: _ :: r when ascension > 0 -> loop ( ascension - 1 ) outp r | s :: r -> loop 0 ( s :: outp ) r in loop 0 [ ] revp let encoded_of_path ? scheme p = let len = List . fold_left ( fun c tok -> String . length tok + c ) 0 p in let buf = Buffer . create len in iter_concat ( fun buf -> function | " " / -> Buffer . add_char buf ' ' / | seg -> Buffer . add_string buf ( pct_encode ? scheme ~ component ` : Path seg ) ) " " buf p ; Pct . cast_encoded ( Buffer . contents buf ) let merge bhost bpath relpath = match bhost , List . rev bpath with | Some _ , [ ] -> " " /:: relpath | _ , ( " " /:: rbpath | _ " " ::/:: rbpath ) -> List . rev_append ( " " /:: rbpath ) relpath | _ , _ -> relpath end |
let encoded_of_path ? scheme = Path . encoded_of_path ? scheme |
module Query = struct type kv = ( string * string list ) list type t = | KV of kv | Raw of string option * kv Lazy . t let compare x y = match x , y with | KV kvl , KV kvl ' | Raw ( _ , lazy kvl ) , KV kvl ' | KV kvl , Raw ( _ , lazy kvl ' ) -> compare_list ( fun ( k , vl ) ( k ' , vl ' ) -> match String . compare k k ' with | 0 -> compare_list String . compare vl vl ' | c -> c ) kvl kvl ' | Raw ( raw , _ ) , Raw ( raw ' , _ ) -> compare_opt String . compare raw raw ' let find q k = try Some ( List . assoc k q ) with Not_found -> None let split_query qs = let els = Stringext . split ~ on ' ' :& qs in let plus_to_space s = let s = Bytes . unsafe_of_string s in for i = 0 to Bytes . length s - 1 do if Bytes . get s i = ' ' + then Bytes . set s i ' ' done ; Bytes . unsafe_to_string s in let rec loop acc = function | ( k :: v :: _ ) :: tl -> let n = plus_to_space k , ( match Stringext . split ~ on ' , ' : ( plus_to_space v ) with | [ ] -> [ " " ] | l -> l ) in loop ( n :: acc ) tl | [ k ] :: tl -> let n = plus_to_space k , [ ] in loop ( n :: acc ) tl | [ ] :: tl -> loop ( ( " " , [ ] ) :: acc ) tl | [ ] -> acc in match els with | [ ] -> [ " " , [ ] ] | els -> loop [ ] ( List . rev_map ( fun el -> Stringext . split ~ on ' ' := el ~ max : 2 ) els ) let query_of_encoded qs = List . map ( fun ( k , v ) -> ( pct_decode k , List . map pct_decode v ) ) ( split_query qs ) let encoded_of_query ? scheme l = let len = List . fold_left ( fun a ( k , v ) -> a + ( String . length k ) + ( List . fold_left ( fun a s -> a ( + String . length s ) + 1 ) 0 v ) + 2 ) ( - 1 ) l in let buf = Buffer . create len in iter_concat ( fun buf ( k , v ) -> Buffer . add_string buf ( pct_encode ? scheme ~ component ` : Query_key k ) ; if v <> [ ] then ( Buffer . add_char buf ' ' ; = iter_concat ( fun buf s -> Buffer . add_string buf ( pct_encode ? scheme ~ component ` : Query_value s ) ) " , " buf v ) ) " " & buf l ; Buffer . contents buf let of_raw qs = let lazy_query = Lazy . from_fun ( fun ( ) -> query_of_encoded qs ) in Raw ( Some qs , lazy_query ) let kv = function Raw ( _ , lazy kv ) | KV kv -> kv end |
let encoded_of_query ? scheme = Query . encoded_of_query ? scheme |
type t = { scheme : Pct . decoded option ; userinfo : Userinfo . t option ; host : Pct . decoded option ; port : int option ; path : Path . t ; query : Query . t ; fragment : Pct . decoded option ; } |
let empty = { scheme = None ; userinfo = None ; host = None ; port = None ; path = [ ] ; query = Query . Raw ( None , Lazy . from_val [ ] ) ; fragment = None ; } |
let compare_decoded = Pct . unlift_decoded2 String . compare |
let compare_decoded_opt = compare_opt compare_decoded |
let compare t t ' = ( match compare_decoded_opt t . host t ' . host with | 0 -> ( match compare_decoded_opt t . scheme t ' . scheme with | 0 -> ( match compare_opt ( fun p p ' -> if p < p ' then - 1 else if p > p ' then 1 else 0 ) t . port t ' . port with | 0 -> ( match compare_opt Userinfo . compare t . userinfo t ' . userinfo with | 0 -> ( match Path . compare t . path t ' . path with | 0 -> ( match Query . compare t . query t ' . query with | 0 -> compare_decoded_opt t . fragment t ' . fragment | c -> c ) | c -> c ) | c -> c ) | c -> c ) | c -> c ) | c -> c ) |
let equal t t ' = compare t t ' = 0 |
let uncast_opt = function | Some h -> Some ( Pct . uncast_decoded h ) | None -> None |
let cast_opt = function | Some h -> Some ( Pct . cast_decoded h ) | None -> None |
let normalize schem uri = let module Scheme = ( val ( module_of_scheme ( uncast_opt schem ) ) : Scheme ) in let dob f = function | Some x -> Some ( Pct . unlift_decoded f x ) | None -> None in { uri with scheme = dob String . lowercase_ascii uri . scheme ; host = cast_opt ( Scheme . normalize_host ( uncast_opt uri . host ) ) } |
let make ? scheme ? userinfo ? host ? port ? path ? query ? fragment ( ) = let decode = function | Some x -> Some ( Pct . cast_decoded x ) | None -> None in let host = match userinfo , host , port with | _ , Some _ , _ | None , None , None -> host | Some _ , None , _ | _ , None , Some _ -> Some " " in let userinfo = match userinfo with | None -> None | Some u -> Some ( userinfo_of_encoded u ) in let path = match path with | None -> [ ] | Some p -> let path = path_of_encoded p in match host , path with | None , _ | Some _ , " " /:: _ | Some _ , [ ] -> path | Some _ , _ -> " " /:: path in let query = match query with | None -> Query . KV [ ] | Some p -> Query . KV p in let scheme = decode scheme in normalize scheme { scheme ; userinfo ; host = decode host ; port ; path ; query ; fragment = decode fragment } |
let of_string s = let get_opt_encoded s n = try Some ( Pct . cast_encoded ( Re . Group . get s n ) ) with Not_found -> None in let get_opt s n = try let pct = Pct . cast_encoded ( Re . Group . get s n ) in Some ( Pct . decode pct ) with Not_found -> None in let subs = Re . exec Uri_re . uri_reference s in let scheme = get_opt subs 2 in let userinfo , host , port = match get_opt_encoded subs 4 with | None -> None , None , None | Some a -> let subs ' = Re . exec Uri_re . authority ( Pct . uncast_encoded a ) in let userinfo = match get_opt_encoded subs ' 1 with | Some x -> Some ( Userinfo . userinfo_of_encoded ( Pct . uncast_encoded x ) ) | None -> None in let host = get_opt subs ' 2 in let port = match get_opt subs ' 3 with | None -> None | Some x -> ( try Some ( int_of_string ( Pct . uncast_decoded x ) ) with _ -> None ) in userinfo , host , port in let path = match get_opt_encoded subs 5 with | Some x -> Path . path_of_encoded ( Pct . uncast_encoded x ) | None -> [ ] in let query = match get_opt_encoded subs 7 with | Some x -> Query . of_raw ( Pct . uncast_encoded x ) | None -> Query . Raw ( None , Lazy . from_val [ ] ) in let fragment = get_opt subs 9 in normalize scheme { scheme ; userinfo ; host ; port ; path ; query ; fragment } |
let to_string uri = let scheme = match uri . scheme with | Some s -> Some ( Pct . uncast_decoded s ) | None -> None in let buf = Buffer . create 128 in let add_pct_string ( ? component ` = Path ) x = Buffer . add_string buf ( Pct . uncast_encoded ( Pct . encode ? scheme ~ component x ) ) in ( match uri . scheme with | None -> ( ) | Some x -> add_pct_string ~ component ` : Scheme x ; Buffer . add_char buf ' ' : ) ; if ( match uri . userinfo , uri . host , uri . port with | Some _ , _ , _ | _ , Some _ , _ | _ , _ , Some _ -> true | _ -> false ) then Buffer . add_string buf " " ; // ( match uri . userinfo with | None -> ( ) | Some userinfo -> Buffer . add_string buf ( Pct . uncast_encoded ( encoded_of_userinfo ? scheme userinfo ) ) ; Buffer . add_char buf ' ' @ ) ; ( match uri . host with | None -> ( ) | Some host -> add_pct_string ~ component ` : Host host ; ) ; ( match uri . port with | None -> ( ) | Some port -> Buffer . add_char buf ' ' ; : Buffer . add_string buf ( string_of_int port ) ) ; ( match uri . path with | [ ] -> ( ) | " " /:: _ -> Buffer . add_string buf ( Pct . uncast_encoded ( encoded_of_path ? scheme uri . path ) ) | first_segment :: _ -> ( match uri . host with | Some _ -> Buffer . add_char buf ' ' / | None -> match Stringext . find_from first_segment ~ pattern " " :: with | None -> ( ) | Some _ -> match scheme with | Some _ -> ( ) | None -> Buffer . add_string buf " . " / ) ; Buffer . add_string buf ( Pct . uncast_encoded ( encoded_of_path ? scheme uri . path ) ) ) ; Query . ( match uri . query with | Raw ( None , _ ) | KV [ ] -> ( ) | Raw ( _ , lazy q ) | KV q -> Buffer . add_char buf ' ' ; ? Buffer . add_string buf ( encoded_of_query ? scheme q ) ) ; ( match uri . fragment with | None -> ( ) | Some f -> Buffer . add_char buf ' ' ; # add_pct_string ~ component ` : Fragment f ) ; Buffer . contents buf |
let get_decoded_opt = function None -> None | Some x -> Some ( Pct . uncast_decoded x ) |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.