Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
48 changes: 32 additions & 16 deletions lib/Uri_IP.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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))
*)
39 changes: 33 additions & 6 deletions lib/Uri_IP.mli
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,38 @@
*
*)

type ipv4 = int32
(** Handle IPv4 and IPv6 addresses as used in URIs.

@author Richard Mortier <mort\@cantab.net>
*)

(** 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
18 changes: 9 additions & 9 deletions lib/uri.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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 *)
Expand All @@ -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 *)
Expand Down