package mcp-kit
Source file fmt_hostname.ml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81(** Format validators: hostname, idn-hostname. Uses ocaml-idna for IDNA2008 validation. *) let check_hostname_rfc1123 s = let len = String.length s in if len = 0 || len > 253 then false else if s.[len - 1] = '.' then false else if s.[0] = '.' then false (* Must be pure ASCII for hostname format *) else if String.length s <> Jsonschema_core.Instance.utf8_length s then false else let valid_label label = let len = String.length label in let is_ldh = function | 'A' .. 'Z' | 'a' .. 'z' | '0' .. '9' | '-' -> true | _ -> false in len > 0 && len <= 63 && label.[0] <> '-' && label.[len - 1] <> '-' && String.for_all is_ldh label in String.split_on_char '.' s |> List.for_all valid_label let check_hostname s = let len = String.length s in if len = 0 || len > 253 then false else if s.[len - 1] = '.' then false else if s.[0] = '.' then false (* Must be pure ASCII for hostname format *) else if String.length s <> Jsonschema_core.Instance.utf8_length s then false else Idna.Registration.is_valid_hostname s (** Replace Unicode dot separators with ASCII dot. U+3002 IDEOGRAPHIC FULL STOP, U+FF0E FULLWIDTH FULL STOP, U+FF61 HALFWIDTH IDEOGRAPHIC FULL STOP. *) let normalize_dots s = let buf = Buffer.create (String.length s) in let len = String.length s in let i = ref 0 in while !i < len do let b = Char.code s.[!i] in if b = 0xE3 && !i + 2 < len && Char.code s.[!i + 1] = 0x80 && Char.code s.[!i + 2] = 0x82 then begin (* U+3002 = E3 80 82 *) Buffer.add_char buf '.'; i := !i + 3 end else if b = 0xEF && !i + 2 < len then begin let b1 = Char.code s.[!i + 1] in let b2 = Char.code s.[!i + 2] in if b1 = 0xBC && b2 = 0x8E then begin (* U+FF0E = EF BC 8E *) Buffer.add_char buf '.'; i := !i + 3 end else if b1 = 0xBD && b2 = 0xA1 then begin (* U+FF61 = EF BD A1 *) Buffer.add_char buf '.'; i := !i + 3 end else begin Buffer.add_char buf s.[!i]; incr i end end else begin Buffer.add_char buf s.[!i]; incr i end done; Buffer.contents buf let check_idn_hostname s = let s = normalize_dots s in let len = String.length s in if len = 0 || len > 253 then false else if s.[len - 1] = '.' then false else if s.[0] = '.' then false else Idna.Registration.is_valid_hostname s