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 <> ' ' ...
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...
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 = Co...
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 ...
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_...
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_...
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 . ...
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...
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 i...
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...
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 . ...
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 ....
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...
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 =...
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 ...
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 ...
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 Generi...
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 un...
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 | [ ] ...
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 | " " " ...
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 , ...
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 -> ( ...
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 ...
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 , _ | ...
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 Ur...
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 ~ componen...
let get_decoded_opt = function None -> None | Some x -> Some ( Pct . uncast_decoded x )