From 76a77d87b43c2d55a92901d9909c2b941a4e70de Mon Sep 17 00:00:00 2001 From: Richard Mortier Date: Wed, 14 Mar 2012 13:36:01 +0000 Subject: [PATCH] tweak docs; tweak IP support --- Makefile | 7 ++----- lib/Uri_IP.ml | 48 ++++++++++++++++++++++++++++++++---------------- lib/Uri_IP.mli | 39 +++++++++++++++++++++++++++++++++------ lib/uri.mli | 18 +++++++++--------- 4 files changed, 76 insertions(+), 36 deletions(-) diff --git a/Makefile b/Makefile index 76eed7a..0ed4d94 100644 --- a/Makefile +++ b/Makefile @@ -1,16 +1,13 @@ .PHONY: all clean install build -all: build test doc +all: build doc NAME=uri -OFLAGS= J=4 export OCAMLRUNPARAM=b setup.bin: setup.ml - ocamlopt.opt $(OFLAGS) -o $@ $< \ - || ocamlopt $(OFLAGS) -o $@ $< \ - || ocamlc $(OFLAGS) -o $@ $< + ocamlopt.opt -o $@ $< || ocamlopt -o $@ $< || ocamlc -o $@ $< $(RM) setup.cmx setup.cmi setup.o setup.cmo setup.data: setup.bin diff --git a/lib/Uri_IP.ml b/lib/Uri_IP.ml index 4ee7fe4..43b97df 100644 --- a/lib/Uri_IP.ml +++ b/lib/Uri_IP.ml @@ -24,21 +24,33 @@ let (<<<) x y = Int32.shift_left x y let (>>>) x y = Int32.shift_right_logical x y let sp = Printf.sprintf -type ipv4 = int32 -type ipv6 = int32 * int32 * int32 * int32 +type byte = char +let byte_to_int32 b = b |> int_of_char |> Int32.of_int + +type bytes = string +let bytes (s:string) : bytes = s +let bytes_to_string (bs:bytes) : string = + let s = ref [] in + let l = String.length bs in + for i = 0 to (l-1) do + s := (Printf.sprintf "%02lx" (byte_to_int32 bs.[i])) :: !s + done; + String.concat "." !s -let int32_of_byte b = b |> int_of_char |> Int32.of_int +type ipv4 = Int32.t -let string_of_ipv4 i = +let ipv4_to_string i = sp "%ld.%ld.%ld.%ld" ((i &&& 0x0_ff000000_l) >>> 24) ((i &&& 0x0_00ff0000_l) >>> 16) ((i &&& 0x0_0000ff00_l) >>> 8) ((i &&& 0x0_000000ff_l) ) -let ipv4_of_bytes bs = - ((bs.[0] |> int32_of_byte <<< 24) ||| (bs.[1] |> int32_of_byte <<< 16) - ||| (bs.[2] |> int32_of_byte <<< 8) ||| (bs.[3] |> int32_of_byte)) +let bytes_to_ipv4 bs = + ((bs.[0] |> byte_to_int32 <<< 24) ||| (bs.[1] |> byte_to_int32 <<< 16) + ||| (bs.[2] |> byte_to_int32 <<< 8) ||| (bs.[3] |> byte_to_int32)) + +type ipv6 = int32 * int32 * int32 * int32 -let string_of_ipv6 i = +let ipv6_to_string i = (* TODO should make this rfc 5952 compliant *) let i1, i2, i3, i4 = i in let s = sp "%lx:%lx:%lx:%lx:%lx:%lx:%lx:%lx" @@ -49,12 +61,16 @@ let string_of_ipv6 i = in s -let ipv6_of_bytes bs = - (ipv4_of_bytes (String.sub bs 0 4), ipv4_of_bytes (String.sub bs 4 4), - ipv4_of_bytes (String.sub bs 8 4), ipv4_of_bytes (String.sub bs 12 4)) +let bytes_to_ipv6 bs = + (bytes_to_ipv4 (String.sub bs 0 4), bytes_to_ipv4 (String.sub bs 4 4), + bytes_to_ipv4 (String.sub bs 8 4), bytes_to_ipv4 (String.sub bs 12 4)) + + + + + + + + + -(* -let _ = - Printf.printf "++ %s" - (string_of_ipv6 (0xde000000_l, 0xdeadbeef_l, 0x00000000_l, 0xdeadbeef_l)) -*) diff --git a/lib/Uri_IP.mli b/lib/Uri_IP.mli index d05fdb0..791494c 100644 --- a/lib/Uri_IP.mli +++ b/lib/Uri_IP.mli @@ -15,11 +15,38 @@ * *) -type ipv4 = int32 +(** Handle IPv4 and IPv6 addresses as used in URIs. + + @author Richard Mortier +*) + +(** Type alias for a byte. *) +type byte + +(** Convert {! byte} to {! Int32}. *) +val byte_to_int32 : byte -> int32 + +(** Type alias for a sequence of bytes. *) +type bytes +val bytes : string -> bytes +val bytes_to_string : bytes -> string + +(** Simple representation of an IPv4 address as {! Int32}. *) +type ipv4 = Int32.t + +(** Standard dotted quad string representation of an IPv4 address. *) +val ipv4_to_string : ipv4 -> string + +(** Generate numeric IPv4 address from a packed bytestring. *) +val bytes_to_ipv4 : bytes -> ipv4 + +(** Simple representation of an IPv6 address (128 bits). *) type ipv6 = int32 * int32 * int32 * int32 -val int32_of_byte : char -> int32 -val string_of_ipv4 : int32 -> string -val ipv4_of_bytes : string -> int32 -val string_of_ipv6 : int32 * int32 * int32 * int32 -> string -val ipv6_of_bytes : string -> int32 * int32 * int32 * int32 +(** Standard string representation of an IPv6 address. + Note this is not yet canonicalised per RFC 5952. *) +val ipv6_to_string : ipv6 -> string + +(** Generate numeric -- 4-tuple of Int32, as above -- IPv6 address from packed + bytestring. *) +val bytes_to_ipv6 : bytes -> ipv6 diff --git a/lib/uri.mli b/lib/uri.mli index dfb3f66..bf4b7ed 100644 --- a/lib/uri.mli +++ b/lib/uri.mli @@ -17,14 +17,14 @@ type t -(** This absract type represents a set of safe characters allowed in - a portion of a URI. Anything not allowed will be percent-encoded. - Note that different portions of the URI permit a different set of - allowed characters. *) +(** This abstract type represents a set of safe characters allowed in a + portion of a URI. Anything not allowed will be percent-encoded. Note that + different portions of the URI permit a different set of allowed + characters. *) type safe_chars -(** This represents the minimal set of safe characters allowed in - a URI. `[A-Z][a-z]._-` *) +(** This represents the minimal set of safe characters allowed in a URI. + `[A-Z][a-z]._-` *) val safe_chars : safe_chars (** This is the set allowed for the path component *) @@ -33,9 +33,9 @@ val safe_chars_for_path : safe_chars (** This is the set allowed for the user info component *) val safe_chars_for_userinfo : safe_chars -(** Percent-encode a string. The [safe_chars] argument defaults to the - set of characters for a path component, and should be set differently - for other URI components *) +(** Percent-encode a string. The [safe_chars] argument defaults to the set of + characters for a path component, and should be set differently for other + URI components *) val pct_encode : ?safe_chars:safe_chars -> string -> string (** Percent-decode a percent-encoded string *)