package mcp-kit

  1. Overview
  2. Docs

Source file fmt_uri.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
(** Format validators: uri, uri-reference, iri, iri-reference, uri-template. *)

let has_scheme s =
  (* scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) ":" *)
  let len = String.length s in
  if len = 0 then false
  else
    let c = s.[0] in
    if not ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) then false
    else
      try
        let colon = String.index s ':' in
        let ok = ref true in
        for i = 1 to colon - 1 do
          let c = s.[i] in
          if
            not
              ((c >= 'a' && c <= 'z')
              || (c >= 'A' && c <= 'Z')
              || (c >= '0' && c <= '9')
              || c = '+' || c = '-' || c = '.')
          then ok := false
        done;
        !ok && colon > 0
      with Not_found -> false

let is_hex c =
  (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')

let is_digit c = c >= '0' && c <= '9'

let check_percent_encoding s =
  let len = String.length s in
  let ok = ref true in
  let i = ref 0 in
  while !i < len do
    if s.[!i] = '%' then
      begin if !i + 2 >= len then ok := false
      else if not (is_hex s.[!i + 1] && is_hex s.[!i + 2]) then ok := false
      else i := !i + 2
      end;
    incr i
  done;
  !ok

let is_forbidden_ascii_delimiter = function
  | '<' | '>' | '"' | '{' | '}' | '|' | '\\' | '^' | '`' -> true
  | _ -> false

let no_invalid_chars s =
  let ok = ref true in
  String.iter
    (fun c ->
      let code = Char.code c in
      if code <= 0x20 || code = 0x7F || is_forbidden_ascii_delimiter c then
        ok := false)
    s;
  !ok

let is_ascii_only s =
  let ok = ref true in
  String.iter (fun c -> if Char.code c > 127 then ok := false) s;
  !ok

let has_prefix s prefix =
  let prefix_len = String.length prefix in
  String.length s >= prefix_len && String.sub s 0 prefix_len = prefix

let authority_start s =
  if has_prefix s "//" then Some 2
  else
    match String.index_opt s ':' with
    | Some colon
      when has_scheme s
           && colon + 2 < String.length s
           && s.[colon + 1] = '/'
           && s.[colon + 2] = '/' ->
        Some (colon + 3)
    | _ -> None

let authority_end s start =
  let len = String.length s in
  let rec find_end i =
    if i >= len then len
    else match s.[i] with '/' | '?' | '#' -> i | _ -> find_end (i + 1)
  in
  find_end start

let authority_bounds s =
  match authority_start s with
  | None -> None
  | Some start -> Some (start, authority_end s start)

let authority_of_string s =
  match authority_bounds s with
  | None -> None
  | Some (start, stop) -> Some (String.sub s start (stop - start))

let host_port_of_authority authority =
  let start =
    match String.rindex_opt authority '@' with Some i -> i + 1 | None -> 0
  in
  String.sub authority start (String.length authority - start)

let all_digits s =
  let ok = ref true in
  String.iter (fun c -> if not (is_digit c) then ok := false) s;
  !ok

let is_alpha c = (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')

let is_unreserved c =
  is_alpha c || is_digit c || c = '-' || c = '.' || c = '_' || c = '~'

let is_sub_delim = function
  | '!' | '$' | '&' | '\'' | '(' | ')' | '*' | '+' | ',' | ';' | '=' -> true
  | _ -> false

let valid_ipvfuture literal =
  let len = String.length literal in
  if len < 4 || not (literal.[0] = 'v' || literal.[0] = 'V') then false
  else
    let i = ref 1 in
    while !i < len && is_hex literal.[!i] do
      incr i
    done;
    !i > 1
    && !i + 1 < len
    && literal.[!i] = '.'
    &&
    let ok = ref true in
    for j = !i + 1 to len - 1 do
      let c = literal.[j] in
      if not (is_unreserved c || is_sub_delim c || c = ':') then ok := false
    done;
    !ok

let valid_ip_literal literal =
  match Ipaddr.V6.of_string literal with
  | Ok _ -> true
  | Error _ -> valid_ipvfuture literal

let valid_port_suffix suffix =
  if suffix = "" then true
  else if suffix.[0] <> ':' then false
  else
    let port = String.sub suffix 1 (String.length suffix - 1) in
    all_digits port

let check_bracketed_host_port host_port =
  match String.index_opt host_port ']' with
  | None -> false
  | Some close ->
      let literal = String.sub host_port 1 (close - 1) in
      let suffix =
        String.sub host_port (close + 1) (String.length host_port - close - 1)
      in
      valid_ip_literal literal && valid_port_suffix suffix

let count_char s needle =
  String.fold_left (fun acc c -> if c = needle then acc + 1 else acc) 0 s

let check_plain_host_port host_port =
  if String.contains host_port '[' || String.contains host_port ']' then false
  else
    match String.rindex_opt host_port ':' with
    | None -> true
    | Some colon_pos ->
        let host = String.sub host_port 0 colon_pos in
        let port =
          String.sub host_port (colon_pos + 1)
            (String.length host_port - colon_pos - 1)
        in
        count_char host ':' = 0 && all_digits port

let check_authority s =
  match authority_of_string s with
  | None -> true
  | Some authority ->
      let host_port = host_port_of_authority authority in
      if host_port = "" then true
      else if host_port.[0] = '[' then check_bracketed_host_port host_port
      else check_plain_host_port host_port

let bracket_positions_valid s =
  let allowed_range =
    match authority_bounds s with
    | None -> None
    | Some (start, stop) ->
        let authority = String.sub s start (stop - start) in
        let host_start =
          start
          +
          match String.rindex_opt authority '@' with
          | Some i -> i + 1
          | None -> 0
        in
        let host_port = String.sub s host_start (stop - host_start) in
        if String.length host_port > 0 && host_port.[0] = '[' then
          match String.index_opt host_port ']' with
          | Some close when check_bracketed_host_port host_port ->
              Some (host_start, host_start + close)
          | _ -> None
        else None
  in
  let ok = ref true in
  String.iteri
    (fun i c ->
      if c = '[' || c = ']' then
        match allowed_range with
        | Some (first, last) when i >= first && i <= last -> ()
        | _ -> ok := false)
    s;
  !ok

let relative_reference_path_ok s =
  let len = String.length s in
  if len = 0 || has_scheme s || has_prefix s "//" then true
  else
    match s.[0] with
    | '/' | '?' | '#' -> true
    | _ ->
        let rec segment_end i =
          if i = len then len
          else match s.[i] with '/' | '?' | '#' -> i | _ -> segment_end (i + 1)
        in
        let first_segment = String.sub s 0 (segment_end 0) in
        not (String.contains first_segment ':')

let check_uri s =
  String.length s > 0
  && has_scheme s && no_invalid_chars s && is_ascii_only s
  && check_percent_encoding s && check_authority s && bracket_positions_valid s

let check_uri_reference s =
  String.length s >= 0
  && no_invalid_chars s && is_ascii_only s && check_percent_encoding s
  && check_authority s && bracket_positions_valid s
  && relative_reference_path_ok s

let no_invalid_iri_chars s =
  let ok = ref true in
  String.iter
    (fun c ->
      let code = Char.code c in
      if code <= 0x20 || code = 0x7F || is_forbidden_ascii_delimiter c then
        ok := false)
    s;
  !ok

let check_iri s =
  String.length s > 0
  && has_scheme s && no_invalid_iri_chars s && check_percent_encoding s
  && check_authority s && bracket_positions_valid s

let check_iri_reference s =
  no_invalid_iri_chars s && check_percent_encoding s && check_authority s
  && bracket_positions_valid s
  && relative_reference_path_ok s

let is_uri_template_operator = function
  | '+' | '#' | '.' | '/' | ';' | '?' | '&' | '=' | ',' | '!' | '@' | '|' ->
      true
  | _ -> false

let pct_encoded_at s i =
  i + 2 < String.length s && s.[i] = '%' && is_hex s.[i + 1] && is_hex s.[i + 2]

let literal_char_end s i =
  let c = s.[i] in
  let code = Char.code c in
  if c = '%' then if pct_encoded_at s i then Some (i + 3) else None
  else if code >= 128 then Some (i + 1)
  else
    let allowed =
      code = 0x21
      || (code >= 0x23 && code <= 0x24)
      || code = 0x26
      || (code >= 0x28 && code <= 0x3B)
      || code = 0x3D
      || (code >= 0x3F && code <= 0x5B)
      || code = 0x5D || code = 0x5F
      || (code >= 0x61 && code <= 0x7A)
      || code = 0x7E
    in
    if allowed then Some (i + 1) else None

let varchar_end s i =
  if i >= String.length s then None
  else
    let c = s.[i] in
    if is_alpha c || is_digit c || c = '_' then Some (i + 1)
    else if pct_encoded_at s i then Some (i + 3)
    else None

let varname_end s start close =
  let rec loop i =
    if i >= close then Some i
    else if s.[i] = '.' then
      match varchar_end s (i + 1) with
      | Some j when j <= close -> loop j
      | _ -> None
    else
      match varchar_end s i with
      | Some j when j <= close -> loop j
      | _ -> Some i
  in
  match varchar_end s start with Some i when i <= close -> loop i | _ -> None

let prefix_end s i close =
  if i >= close then None
  else if s.[i] < '1' || s.[i] > '9' then None
  else
    let j = ref (i + 1) in
    let digits = ref 1 in
    while !j < close && !digits < 4 && is_digit s.[!j] do
      incr j;
      incr digits
    done;
    Some !j

let varspec_end s start close =
  match varname_end s start close with
  | None -> None
  | Some i when i < close && s.[i] = '*' -> Some (i + 1)
  | Some i when i < close && s.[i] = ':' -> prefix_end s (i + 1) close
  | Some i -> Some i

let variable_list_valid s start close =
  let rec loop i =
    if i >= close then false
    else
      match varspec_end s i close with
      | None -> false
      | Some j when j = close -> true
      | Some j when s.[j] = ',' -> loop (j + 1)
      | Some _ -> false
  in
  loop start

let expression_end s open_pos =
  let len = String.length s in
  let rec find_close i =
    if i >= len then None
    else match s.[i] with '{' -> None | '}' -> Some i | _ -> find_close (i + 1)
  in
  match find_close (open_pos + 1) with
  | None -> None
  | Some close when close = open_pos + 1 -> None
  | Some close ->
      let first = s.[open_pos + 1] in
      let list_start =
        if is_uri_template_operator first then open_pos + 2 else open_pos + 1
      in
      if list_start >= close then None
      else if variable_list_valid s list_start close then Some (close + 1)
      else None

let check_uri_template s =
  let len = String.length s in
  let rec loop i =
    if i >= len then true
    else
      match s.[i] with
      | '{' -> (
          match expression_end s i with Some j -> loop j | None -> false)
      | '}' -> false
      | _ -> (
          match literal_char_end s i with Some j -> loop j | None -> false)
  in
  loop 0