amall / src / urilex.mll

  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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
{
open Uri_type;;

let failwith fmt = Printf.ksprintf failwith fmt;;

open Cd_All;;
open Strings.Latin1;;

let query_param_add k v acc =
  if k = ""
  then acc
  else if List.Assoc.mem ~keq:String.eq k acc
  then acc
  else (k, v) :: acc
;;

let add_query_char_unescaped buf c =
  let c0 = c.[0] in
  let c =
    if c0 = '%'
    then begin
      match String.(decode_hex_opt c.[1], decode_hex_opt c.[2]) with
      | Some hi, Some lo -> Char.chr ((hi lsl 4) lor lo)
      | None, (None | Some _) | Some _, None -> assert false
    end else
      c0
  in
  Buffer.add_char buf c
;;
}

(* common part from rfc2616 *)

let    OCTET          = _
let    CHAR           = [ '\000' - '\127' ]
let    UPALPHA        = [ 'A' - 'Z' ]
let    LOALPHA        = [ 'a' - 'z' ]
let    ALPHA          = UPALPHA | LOALPHA
let    DIGIT          = [ '0' - '9' ]
let    CTL            = [ '\000' - '\031' '\127' ]
let    CR             = '\013'
let    LF             = '\010'
let    SP             = '\032'
let    HT             = '\009'

(* addon *)

let    DBLQ           = '\034'
let    HEXDIG         = DIGIT | ['a'-'f' 'A'-'F']

let    none           = ( _ # _ )?

(* uri *)

let scheme        = ALPHA ( ALPHA | DIGIT | '+' | '-' | '.' )*
let pct_encoded   = '%' HEXDIG HEXDIG
let unreserved    = ALPHA | DIGIT | '-' | '.' | '_' | '~'
let sub_delims    = [ '!' '$' '&' '\'' '(' ')'
                      '*' '+' ',' ';' '=' ]
let pchar         = unreserved | pct_encoded | sub_delims | ':' | '@'
let query_char    = ( pchar | '/' | '?' )
let query         = query_char*
let fragment      = ( pchar | '/' | '?' )*

let segment       = pchar*
let segment_nz    = pchar+
let segment_nz_nc = ( unreserved | pct_encoded | sub_delims | '@' )+

let path_abempty  = ( '/' segment )*
let path_absolute = '/' ( segment_nz ( '/' segment )* )?
let path_noscheme = segment_nz_nc ( '/' segment )*
let path_rootless = segment_nz ( '/' segment )*
let path_empty    = none  (* <0>pchar *)

let userinfo      = ( unreserved | pct_encoded | sub_delims | ':' )*
let port          = DIGIT*

let reg_name      = ( unreserved | pct_encoded | sub_delims )*

let dec_octet     =   DIGIT                (* 0-9 *)
                    | ['1'-'9'] DIGIT      (* 10-99 *)
                    | '1' DIGIT DIGIT      (* 100-199 *)
                    | '2' ['0'-'4'] DIGIT  (* 200-249 *)
                    | "25" ['0'-'5'] DIGIT (* 250-255 *)

let IPv4address   = dec_octet '.' dec_octet '.' dec_octet '.' dec_octet
let IPvFuture     = 'v' HEXDIG+ '.' ( unreserved | sub_delims | ':' )+

let h16           = HEXDIG HEXDIG? HEXDIG? HEXDIG?
let hc            = h16 ':'
let ls32          = ( hc h16 ) | IPv4address
let IPv6address   =
                                     hc hc hc hc hc hc ls32
  |                            "::"     hc hc hc hc hc ls32
  | (                   h16 )? "::"        hc hc hc hc ls32
  | ( hc                h16 )? "::"           hc hc hc ls32
  | ( hc hc             h16 )? "::"              hc hc ls32
  | ( hc hc hc          h16 )? "::"                 hc ls32
  | ( hc hc hc hc       h16 )? "::"                    ls32
  | ( hc hc hc hc hc    h16 )? "::"                         h16
  | ( hc hc hc hc hc hc h16 )? "::"

let IP_literal    = "[" ( IPv6address | IPvFuture  ) "]"


(* not an uri, but http *)

let g_space = ['\x20' '\x09']

let g_ctl = ['\x00' - '\x1F' '\x7F']

let g_separator =
  [ '(' ')' '<' '>' '@'
    ',' ';' ':' '\\' '\"'
    '/' '[' ']' '?' '='
    '{' '}' '\x20' '\x09'
  ]

let token = (_ # g_ctl # g_separator)+

let g_text = (_ # g_ctl)

let g_char = ['\x00' - '\x7F']

(* cookies *)

let g_cookie_octet =
  [ '\x21' '\x23'-'\x2B' '\x2D'-'\x3A' '\x3C'-'\x5B' '\x5D'-'\x7E' ]

(* query params *)

let qp_pair_delim = [ '&' ';' ]

(* rules *)

rule uri = parse
  (scheme as scheme) ":"
    {
(* let () = print_string "1\n" in *)
      let (authority_opt, (path, (query, fragment))) =
        hier_part_qf lexbuf in
      { scheme = Some scheme
      ; authority = authority_opt
      ; path = path
      ; query = query
      ; fragment = fragment
      }
    }
| none
    {
      let (path, (query, fragment)) =
        path_query_fragment lexbuf
      in
      { scheme = None
      ; authority = None
      ; path = path
      ; query = query
      ; fragment = fragment
      }
    }

and hier_part_qf = parse
  "//"
    {
(* let () = print_string "2a\n" in *)
      authority_path_abempty_qf lexbuf
    }
| none
    {
      (None, path_query_fragment lexbuf)
    }

and path_query_fragment = parse
  (path_absolute | path_rootless | path_empty) as path
    { (path, query_fragment lexbuf)
    }

and host_portopt = parse
  ( ( IP_literal  (none as is_lit)
    | IPv4address (none as is_ipv4)
    | reg_name    (none as is_reg_name)
    )
      as host
  )
  ( ( ":" (port as port_opt_txt) ) ?)
    {
(* let () = print_string "3\n" in *)
      let host_kind =
        (match (is_lit, is_ipv4, is_reg_name) with
           Some _, None, None -> IP_literal
         | None, Some _, None -> IPv4address
         | None, None, Some _ -> Reg_name
         | _ -> assert false
        )
      in
      let port_opt =
        (match port_opt_txt with
           None -> None
         | Some txt ->
             if txt = ""
             then None
             else (try Some (int_of_string txt) with _ -> None)
        )
      in
        (host_kind, host, port_opt)
    }

and authority_path_abempty_qf = parse
  ((userinfo as userinfo_opt) '@')?
    {
      let (host_kind, host, port_opt) = host_portopt lexbuf in
      let path = path_abempty_qf lexbuf in
      ( Some
        { host_kind = host_kind
        ; host = host
        ; port = port_opt
        ; userinfo = userinfo_opt
        }
      , path
      )
    }

     
and path_abempty_qf = parse
  path_abempty
    { let path = Lexing.lexeme lexbuf in
      (path, query_fragment lexbuf)
    }

and query_fragment = parse
  ('?' (query as query_opt))?
    { (query_opt, fragment lexbuf) }

and fragment = parse
  ('#' (fragment as fragment_opt))?
    { fragment_opt }


(*
   hier-part     = "//" authority path-abempty
                 / path-absolute
                 / path-rootless
                 / path-empty

   URI-reference = URI / relative-ref

   absolute-URI  = scheme ":" hier-part [ "?" query ]

   relative-ref  = relative-part [ "?" query ] [ "#" fragment ]

   relative-part = "//" authority path-abempty
                 / path-absolute
                 / path-noscheme
                 / path-empty



   path          = path-abempty    ; begins with "/" or is empty
                 / path-absolute   ; begins with "/" but not "//"
                 / path-noscheme   ; begins with a non-colon segment
                 / path-rootless   ; begins with a segment
                 / path-empty      ; zero characters


   reserved      = gen-delims / sub-delims
   gen-delims    = ":" / "/" / "?" / "#" / "[" / "]" / "@"




*)


(* not an uri, but http *)

and dump_rest = parse
  ((_ *) as s)
    { s }

and content_type = parse
  g_space+
    { content_type lexbuf }
| (token as mtype) '/' (token as msubtype) g_space*
    { try
        let pars = parameters [] lexbuf in
        `Ok (mtype, msubtype, pars)
      with
      | Failure msg -> `Error msg
      | e -> `Error (Printexc.to_string e)
    }

| ""
    { `Error (Printf.sprintf "bad Content-Type: %S" (dump_rest lexbuf)) }

and parameters acc = parse
  eof 
    { List.rev acc }
| ""
    { let p = parameter lexbuf in
      parameters (p :: acc) lexbuf
    }

and parameter = parse
  ';' g_space* (token as par_name) '='
    { let v = parameter_value lexbuf in
      (par_name, v)
    }

| ""
    { failwith "expected parameter, found %S" (dump_rest lexbuf) }

and parameter_value = parse
  (token as t) g_space*
    { t }
| '"'
    { inside_quoted_string (Buffer.create 40) lexbuf }
| ""
    { failwith "expected parameter's value, found %S" (dump_rest lexbuf) }

and inside_quoted_string buf = parse
  '"' g_space*
    { Buffer.contents buf }
| '\\'
    { let c = escaped_char lexbuf in
      let () = Buffer.add_char buf c in
      inside_quoted_string buf lexbuf
    }
| ((g_text # ['"' '\\'])+ as txt)
    { let () = Buffer.add_string buf txt in
      inside_quoted_string buf lexbuf
    }
| ""
    { failwith "expected HTTP's TEXT, found %S" (dump_rest lexbuf) }

and escaped_char = parse
  g_char as c
    { c }
| (_ as c)
    { failwith "expected \\x00..\\x7F after backslash, found %C" c }


(* cookies *)

and cookie_string = parse
  SP* ""
    { cookie_pair [] lexbuf }

and cookie_pair acc = parse
  (token as cn) '='
    { let cv = cookie_value lexbuf in
      cookie_pair_cont ((cn, cv) :: acc) lexbuf
    }

and cookie_pair_cont acc = parse
  SP* eof
    { List.rev acc }
| ";" SP
    { cookie_pair acc lexbuf }

and cookie_name = parse
  token
    { Lexing.lexeme lexbuf }

and cookie_value = parse
  '"' (g_cookie_octet* as v) '"'
    { v }
| (g_cookie_octet* as v)
    { v }




and query_params acc buf = parse

  (* first rules have priority over query_char case *)
  qp_pair_delim
    { let k = Buffer.contents buf in
      let acc = query_param_add k "" acc in
      query_params acc buf lexbuf
    }

| '='
    { let k = Buffer.contents buf in
      Buffer.clear buf;
      let v = query_param_value buf lexbuf in
      Buffer.clear buf;
      let acc = query_param_add k v acc in
      query_params acc buf lexbuf
    }

| (* can't use query_char* because of "longest match" and unescaping *)
  query_char
    { let c = Lexing.lexeme lexbuf in
      add_query_char_unescaped buf c;
      query_params acc buf lexbuf
    }

| eof | ""
    { let k = Buffer.contents buf in
      List.rev (query_param_add k "" acc)
    }

and query_param_value buf = parse

  qp_pair_delim | eof | ""
    { Buffer.contents buf }

| query_char
    { let c = Lexing.lexeme lexbuf in
      add_query_char_unescaped buf c;
      query_param_value buf lexbuf
    }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.