Unix.getpeername (Unix.descr_of_out_channel outchan)
let peername_of_in_channel inchan =
Unix.getpeername (Unix.descr_of_in_channel inchan)
+let sockname_of_out_channel outchan =
+ Unix.getsockname (Unix.descr_of_out_channel outchan)
+let sockname_of_in_channel inchan =
+ Unix.getsockname (Unix.descr_of_in_channel inchan)
+
+let buf_of_inchan ?limit ic =
+ let buf = Buffer.create 10240 in
+ let tmp = String.make 1024 '\000' in
+ let rec buf_of_inchan' limit =
+ (match limit with
+ | None ->
+ let bytes = input ic tmp 0 1024 in
+ if bytes > 0 then begin
+ Buffer.add_substring buf tmp 0 bytes;
+ buf_of_inchan' None
+ end
+ | Some lim -> (* TODO what about using a single really_input call? *)
+ let bytes = input ic tmp 0 (min lim 1024) in
+ if bytes > 0 then begin
+ Buffer.add_substring buf tmp 0 bytes;
+ buf_of_inchan' (Some (lim - bytes))
+ end)
+ in
+ (try buf_of_inchan' limit with End_of_file -> ());
+ buf
+
+let list_assoc_all key pairs =
+ snd (List.split (List.filter (fun (k, v) -> k = key) pairs))
val peername_of_out_channel: out_channel -> Unix.sockaddr
(** as above but works on in_channels *)
val peername_of_in_channel: in_channel -> Unix.sockaddr
+ (** given an out_channel build on top of a socket, return sockname related to
+ that socket *)
+val sockname_of_out_channel: out_channel -> Unix.sockaddr
+ (** as above but works on in_channels *)
+val sockname_of_in_channel: in_channel -> Unix.sockaddr
+
+ (** reads from an input channel till it End_of_file and returns what has been
+ read; if limit is given returned buffer will contains at most first 'limit'
+ bytes read from input channel *)
+val buf_of_inchan: ?limit: int -> in_channel -> Buffer.t
+
+ (** like List.assoc but return all bindings of a given key instead of the
+ leftmost one only *)
+val list_assoc_all: 'a -> ('a * 'b) list -> 'b list