text
stringlengths
12
786k
let algorithm_of_header h = h . alg
let typ_of_header h = h . typ
let kid_of_header h = h . kid
let json_of_header header = ` Assoc ( ( " alg " , ` String ( string_of_algorithm ( algorithm_of_header header ) ) ) :: ( ( match typ_of_header header with | Some typ -> [ ( " typ " , ` String typ ) ] | None -> [ ] ) ( @ match kid_of_header header with | Some k...
let string_of_header header = let json = json_of_header header in Yojson . Basic . to_string json
let header_of_json json = let alg = Yojson . Basic . Util . to_string ( Yojson . Basic . Util . member " alg " json ) in let typ = Yojson . Basic . Util . to_string_option ( Yojson . Basic . Util . member " typ " json ) in let kid = Yojson . Basic . Util . to_string_opt...
let header_of_string str = header_of_json ( Yojson . Basic . from_string str )
let claim c = c
let string_of_claim c = c
type payload = ( claim * string ) list
let add_claim claim value payload = ( claim , value ) :: payload
let find_claim claim payload = let ( _ , value ) = List . find ( fun ( c , _v ) -> ( string_of_claim c ) = ( string_of_claim claim ) ) payload in value
let map f p = List . map f p
let payload_of_json json = List . map ( fun x -> match x with | ( claim , ` String value ) -> ( claim , value ) | ( claim , ` Int value ) -> ( claim , string_of_int value ) | ( claim , value ) -> ( claim , Yojson . Basic . to_string value ) ) ( Yojson . Basi...
let payload_of_string str = payload_of_json ( Yojson . Basic . from_string str )
let json_of_payload payload = let members = map ( fun ( claim , value ) -> match string_of_claim claim with | " exp " | " iat " -> ( ( string_of_claim claim ) , ` Int ( int_of_string value ) ) | _ -> ( ( string_of_claim claim ) , ` String value ) ) payload in ` A...
let string_of_payload payload = Yojson . Basic . to_string ( json_of_payload payload )
type t = { header : header ; payload : payload ; signature : string ; unsigned_token : string }
let b64_url_encode str = let r = Base64 . encode ~ pad : false ~ alphabet : Base64 . uri_safe_alphabet str in match r with | Ok s -> s | Error _ -> failwith ( Printf . sprintf " Something wrong happened while encoding % s " str )
let b64_url_decode str = let r = Base64 . decode ~ pad : false ~ alphabet : Base64 . uri_safe_alphabet str in match r with | Ok s -> s | Error _ -> failwith ( Printf . sprintf " Something wrong happened while decoding % s " str )
let unsigned_token_of_header_and_payload header payload = let b64_header = ( b64_url_encode ( string_of_header header ) ) in let b64_payload = ( b64_url_encode ( string_of_payload payload ) ) in b64_header ^ " . " ^ b64_payload
let t_of_header_and_payload header payload = let algo = fn_of_algorithm ( algorithm_of_header header ) in let unsigned_token = unsigned_token_of_header_and_payload header payload in let signature = algo unsigned_token in { header ; payload ; signature ; unsigned_token }
let header_of_t t = t . header
let payload_of_t t = t . payload
let signature_of_t t = t . signature
let unsigned_token_of_t t = t . unsigned_token
let token_of_t t = let b64_header = ( b64_url_encode ( string_of_header ( header_of_t t ) ) ) in let b64_payload = ( b64_url_encode ( string_of_payload ( payload_of_t t ) ) ) in let b64_signature = ( b64_url_encode ( signature_of_t t ) ) in b64_header ^ " . " ^ b64_payload ^...
let t_of_token token = try let token_splitted = Re . Str . split_delim ( Re . Str . regexp_string " . " ) token in match token_splitted with | [ header_encoded ; payload_encoded ; signature_encoded ] -> let header = header_of_string ( b64_url_decode header_encoded ) in let payload = ...
let asn1_sha256 = Cstruct . of_string " \ x30 \ x31 \ x30 \ x0d \ x06 \ x09 \ x60 \ x86 \ x48 \ x01 \ x65 \ x03 \ x04 \ x02 \ x01 \ x05 \ x00 \ x04 \ x20 "
let pkcs1_sig header body = let hlen = Cstruct . len header in if Cstruct . check_bounds body hlen then match Cstruct . split ~ start : 0 body hlen with | a , b when Cstruct . equal a header -> Some b | _ -> None else None
let find_jwk ~ jwks alg kid kty = let module J = Yojson . Basic . Util in let find_matching key = J . member " alg " key = ` String alg && J . member " kid " key = ` String kid && J . member " kty " key = ` String kty in match List . find find_matching @@ J . to_list jwks with ...
let rs256 n e signature unsigned_token = let open Nocrypto in let open Numeric in let sign = Cstruct . of_string signature in let n = Z . of_cstruct_be @@ Cstruct . of_string n in let e = Z . of_cstruct_be @@ Cstruct . of_string e in match Rsa . PKCS1 . sig_decode ~ key : Rsa . { n ; e } s...
let verify ~ alg ~ jwks t = assert ( alg = " RS256 " ) ; let header = header_of_t t in let payload = payload_of_t t in let signature = signature_of_t t in let unsigned_token = unsigned_token_of_t t in let module J = Yojson . Basic . Util in typ_of_header header = Some " JWT " && algorithm_o...
type message = | Message of topic * partition * offset * string * string option | PartitionEnd of topic * partition * offset
type error = | BAD_MSG | BAD_COMPRESSION | DESTROY | FAIL | TRANSPORT | CRIT_SYS_RESOURCE | RESOLVE | MSG_TIMED_OUT | UNKNOWN_PARTITION | FS | UNKNOWN_TOPIC | ALL_BROKERS_DOWN | INVALID_ARG | TIMED_OUT | QUEUE_FULL | ISR_INSUFF | UNKNOWN | OFFSET_OUT_OF_RANGE | INVALID_MSG | UNKNOWN_TOPIC_OR_PART ...
let _ = Callback . register_exception " kafka . error " ( Error ( UNKNOWN , " msg string " ) ) ; ? delivery_callback ( : msg_id -> error option -> unit ) -> ( string * string ) list -> handler = " ocaml_kafka_new_producer " ? partitioner_callback ( : int -> string -> ...
let poll_events ( ? timeout_ms = 1000 ) handler = poll handler timeout_ms
let wait_delivery ( ? timeout_ms = 100 ) ( ? max_outq_len = 0 ) handler = let rec loop ( ) = if outq_len handler > max_outq_len then ( ignore ( poll_events ~ timeout_ms handler ) ; loop ( ) ) else ( ) in loop ( )
let offset_tail n = Int64 . sub ( - 2000L ) ( Int64 . of_int n )
module Metadata = struct type topic_metadata = { topic_name : string ; topic_partitions : partition list ; } end
let topic_metadata ( ? timeout_ms = 1000 ) handler topic = get_topic_metadata handler topic timeout_ms
let local_topics_metadata ( ? timeout_ms = 1000 ) handler = get_topics_metadata handler false timeout_ms
let all_topics_metadata ( ? timeout_ms = 1000 ) handler = get_topics_metadata handler true timeout_ms
let librdkafka_version = get_librdkafka_version ( )
let pending_table = Int . Table . create ~ size ( : 8 * 1024 )
type ' a response = ( ' a , Kafka . error * string ) result
type producer = { handler : Kafka . handler ; pending_msg : unit Ivar . t Int . Table . t ; stop_poll : unit Ivar . t ; }
type consumer = { handler : Kafka . handler ; start_poll : unit Ivar . t ; stop_poll : unit Ivar . t ; subscriptions : Kafka . message Pipe . Writer . t String . Table . t ; }
let next_msg_id = let n = ref 1 in fun ( ) -> let id = ! n in n := id + 1 ; id
let poll_interval = Time . Span . of_ms 50 . Kafka . topic -> ? partition : Kafka . partition -> ? key : string -> msg_id : Kafka . msg_id -> string -> unit response = " ocaml_kafka_async_produce "
let produce ( t : producer ) topic ? partition ? key msg = let msg_id = next_msg_id ( ) in let ivar = Ivar . create ( ) in Int . Table . add_exn t . pending_msg ~ key : msg_id ~ data : ivar ; match produce ' topic ? partition ? key ~ msg_id msg with | Error _ as e -> Int . Tabl...
let handle_producer_response pending_msg msg_id _maybe_error = match Int . Table . find_and_remove pending_msg msg_id with | Some ivar -> Ivar . fill ivar ( ) | None -> ( )
let new_producer xs = let open Result . Let_syntax in let pending_msg = pending_table ( ) in let stop_poll = Ivar . create ( ) in let % bind handler = new_producer ' ( handle_producer_response pending_msg ) xs in every ~ stop ( : Ivar . read stop_poll ) poll_interval ( fun ( ) -> i...
let handle_incoming_message subscriptions = function | None | Some ( Kafka . PartitionEnd _ ) -> ( ) | Some ( Kafka . Message ( topic , _ , _ , _ , _ ) as msg ) -> ( let topic_name = Kafka . topic_name topic in match String . Table . find subscriptions topic_name with | ...
let new_consumer xs = let open Result . Let_syntax in let subscriptions = String . Table . create ~ size ( : 8 * 1024 ) ( ) in let stop_poll = Ivar . create ( ) in let start_poll = Ivar . create ( ) in let % bind handler = new_consumer ' xs in every ~ start ( : Ivar . read s...
let consume consumer ~ topic = let open Result . Let_syntax in match String . Table . mem consumer . subscriptions topic with | true -> Error ( Kafka . FAIL , " Already subscribed to this topic " ) | false -> Ivar . fill_if_empty consumer . start_poll ( ) ; let subscribe_error = ref...
let new_topic ( producer : producer ) name opts = match Kafka . new_topic producer . handler name opts with | v -> Ok v | exception Kafka . Error ( e , msg ) -> Error ( e , msg )
let destroy_consumer consumer = Ivar . fill consumer . stop_poll ( ) ; Kafka . destroy_handler consumer . handler
let destroy_producer ( producer : producer ) = Ivar . fill producer . stop_poll ( ) ; Kafka . destroy_handler producer . handler
let protect ~ finally f x = let module E = struct type ' a t = Left of ' a | Right of exn end in let res = try E . Left ( f x ) with e -> E . Right e in let ( ) = finally ( ) in match res with | E . Left r -> r | E . Right e -> raise e
let fold_partition ( ? consumer_props = [ " metadata . broker . list " , " localhost : 9092 " ] ) ( ? topic_props = [ ] ) ( ? timeout_ms = 1000 ) ( ? stop_at_end = false ) topic_name partition update offset seed = let consumer_props = ( " enable . partition . e...
let fold_queue_for_ever queue timeout_ms update seed = let rec loop acc = match Kafka . consume_queue ~ timeout_ms queue with | Kafka . Message _ as msg -> loop ( update acc msg ) | Kafka . PartitionEnd _ as msg -> loop ( update acc msg ) | exception Kafka . Error ( Kafka . TIMED_OUT , _...
module Partition = struct type t = int let compare a b = a - b end
module PartitionSet : Set . S with type elt = int = Set . Make ( Partition )
let fold_queue_upto_end queue timeout_ms update partitions seed = let rec loop ( partition_set , acc ) = match Kafka . consume_queue ~ timeout_ms queue with | Kafka . Message ( _ , partition , _ , _ , _ ) as msg -> let partition_set = PartitionSet . add partition partition_set in loop ...
let find_offset partition_offsets partition = try List . assoc partition partition_offsets with Not_found -> 0L
let fold_topic ( ? consumer_props = [ " metadata . broker . list " , " localhost : 9092 " ] ) ( ? topic_props = [ ] ) ( ? timeout_ms = 1000 ) ( ? stop_at_end = false ) topic_name partitions update partition_offsets seed = let consumer_props = ( " enable . partiti...
module TopicMap : Map . S with type key = string = Map . Make ( String )
let fold_queue ( ? consumer_props = [ " metadata . broker . list " , " localhost : 9092 " ] ) ( ? topic_props = [ ] ) ( ? timeout_ms = 1000 ) ? stop_at_end ( : _stop_at_end = false ) topic_partition_pairs update topic_partition_offsets seed = let consumer_props = ( ...
let consume ( ? timeout_ms = 1000 ) topic partition = Lwt_unix . run_job ( consume_job topic partition timeout_ms )
let consume_queue ( ? timeout_ms = 1000 ) queue = Lwt_unix . run_job ( consume_queue_job queue timeout_ms )
let consume_batch ( ? timeout_ms = 1000 ) ( ? msg_count = 1024 ) topic partition = Lwt_unix . run_job ( consume_batch_job topic partition timeout_ms msg_count )
let consume_batch_queue ( ? timeout_ms = 1000 ) ( ? msg_count = 1024 ) queue = Lwt_unix . run_job ( consume_batch_queue_job queue timeout_ms msg_count )
let pending_msg = Hashtbl . create ( 8 * 1024 )
let next_msg_id = let n = ref 1 in let get_next ( ) = let id = ! n in n := id + 1 ; id in get_next
let produce topic ? partition ? key msg = let msg_id = next_msg_id ( ) in let waiter , wakener = Lwt . wait ( ) in Hashtbl . add pending_msg msg_id wakener ; Kafka . produce topic ? partition ? key ~ msg_id msg ; waiter
let delivery_callback msg_id error = try let wakener = Hashtbl . find pending_msg msg_id in Hashtbl . remove pending_msg msg_id ; match error with | None -> Lwt . wakeup wakener ( ) | Some error -> Lwt . wakeup_exn wakener ( Kafka . Error ( error , " Failed to produce message " ) ) w...
let poll_delivery period_ms producer = let timeout_s = ( float_of_int period_ms ) . / 1000 . 0 in let rec loop ( ) = Lwt . ( Lwt_unix . sleep timeout_s >>= fun ( ) -> Kafka . poll_events ~ timeout_ms : 0 producer |> fun _ -> loop ( ) ) in loop
let set_option options option value = let options = List . remove_assoc option options in ( option , value ) :: options
let new_producer ( ? delivery_check_period_ms = 100 ) options = let options = set_option options " delivery . report . only . error " " false " in let producer = Kafka . new_producer ~ delivery_callback options in Lwt . async ( poll_delivery delivery_check_period_ms producer ) ; produ...
let wait_delivery ( ? timeout_ms = 100 ) ( ? max_outq_len = 0 ) producer = let timeout_s = ( float_of_int timeout_ms ) . / 1000 . 0 in let rec loop ( ) = Lwt . ( if Kafka . outq_len producer > max_outq_len then Lwt_unix . sleep timeout_s >>= loop else return_unit ) in loop ( ...
type ( ' a , ' b ) sink = unit -> ( ' a -> unit ) * ( unit -> ' b )
type ' a iterable = ( ' a -> unit ) -> unit
let stream_to : ( ' a , ' b ) sink -> ' a iterable -> ' b = fun open_sink iterable -> let ( push , close ) = open_sink ( ) in try iterable push ; close ( ) with error -> ignore ( close ( ) ) ; raise error
type ' a push_error_handler = ( ' a -> unit ) -> ' a -> exn -> unit
let retry_on_error push msg _error = push msg
let raise_on_error _push _msg error = raise error
let partition_sink ( ? producer_props = [ " metadata . broker . list " , " localhost : 9092 " ] ) ( ? topic_props = [ ] ) ( ? delivery_error_handler = raise_on_error ) topic_name ~ partition let producer = Kafka . new_producer producer_props in let topic = Kafka . new_to...
let topic_sink ( ? producer_props = [ " metadata . broker . list " , " localhost : 9092 " ] ) ( ? topic_props = [ ] ) ( ? delivery_error_handler = raise_on_error ) topic_name let producer = Kafka . new_producer producer_props in let topic = Kafka . new_topic producer topi...
let rec super m = function Term ( _ , sons ) as n -> let rec collate n = function [ ] -> [ ] | son :: rest -> List . map ( fun ( u , subst ) -> ( n :: u , subst ) ) ( super m son ) @ collate ( n + 1 ) rest in let insides = collate 1 sons in begin try ( [ ] , ...
let super_strict m = function Term ( _ , sons ) -> let rec collate n = function [ ] -> [ ] | son :: rest -> List . map ( fun ( u , subst ) -> ( n :: u , subst ) ) ( super m son ) @ collate ( n + 1 ) rest in collate 1 sons | _ -> [ ]
let critical_pairs ( l1 , r1 ) ( l2 , r2 ) = let mk_pair ( u , subst ) = substitute subst ( replace l2 u r1 ) , substitute subst r2 in List . map mk_pair ( super l1 l2 )
let strict_critical_pairs ( l1 , r1 ) ( l2 , r2 ) = let mk_pair ( u , subst ) = substitute subst ( replace l2 u r1 ) , substitute subst r2 in List . map mk_pair ( super_strict l1 l2 )
let mutual_critical_pairs eq1 eq2 = ( strict_critical_pairs eq1 eq2 ) @ ( critical_pairs eq2 eq1 )
let rename n ( t1 , t2 ) = let rec ren_rec = function Var k -> Var ( k + n ) | Term ( op , sons ) -> Term ( op , List . map ren_rec sons ) in ( ren_rec t1 , ren_rec t2 )
let deletion_message rule = print_string " Rule " ; print_int rule . number ; print_string " deleted " ; print_newline ( )
let non_orientable ( m , n ) = pretty_term m ; print_string " = " ; pretty_term n ; print_newline ( )
let rec partition p = function [ ] -> ( [ ] , [ ] ) | x :: l -> let ( l1 , l2 ) = partition p l in if p x then ( x :: l1 , l2 ) else ( l1 , x :: l2 )
let rec get_rule n = function [ ] -> raise Not_found | r :: l -> if n = r . number then r else get_rule n l
let kb_completion greater = let rec kbrec j rules = let rec process failures ( k , l ) eqs = match eqs with [ ] -> if k < l then next_criticals failures ( k + 1 , l ) else if l < j then next_criticals failures ( 1 , l + 1 ) else begin match failures with [ ] -> rules | _ -> prin...