File: test_cgi.ml

package info (click to toggle)
netstring 0.10.1-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,000 kB
  • ctags: 895
  • sloc: ml: 8,389; xml: 416; makefile: 188; sh: 103
file content (423 lines) | stat: -rw-r--r-- 10,068 bytes parent folder | download
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
413
414
415
416
417
418
419
420
421
422
423
#require "str";;
#directory "..";;
#load "netstring.cma";;


open Cgi;;

(**********************************************************************)
(* dest_form_encoded_parameters                                       *)
(**********************************************************************)

let t001 f =
  let r =
    f
      "blah blah
--snip
Content-Disposition: form-data; name=blupp

This is a text
--snip--
blah blah"
      "snip"
  in
  r = ["blupp", "text/plain", "This is a text"]
;;


let t002 f =
  let r =
    f
      "blah blah
--snip
Content-Disposition: form-data; name=blupp

This is a text
--snip--
blah blah"
      "snip"
  in
  r = ["blupp", "text/plain", "This is a text"]
;;


let t003 f =
  let r =
    f
      "--snip
Content-Disposition: form-data; name=blupp

This is a text
--snip--"
      "snip"
  in
  r = ["blupp", "text/plain", "This is a text"]
;;


let t004 f =
  let r =
    f
      "--snip
Content-Disposition: form-data; name=blupp

This is a text

--snip--"
      "snip"
  in
  r = ["blupp", "text/plain", "This is a text\013\n"]
;;


let t005 f =
  let r =
    f
      "--snip
Content-Disposition: form-data; name=blupp

This is a text

--snip--"
      "snip"
  in
  r = ["blupp", "text/plain", "This is a text\n"]
;;


let t006 f =
  let r =
    f
      "blah blah
--snip
Content-Disposition: form-data;name= \"blupp\"

This is a text
--snip--
blah blah"
      "snip"
  in
  r = ["blupp", "text/plain", "This is a text"]
;;


let t007 f =
  let r =
    f
      "blah blah
--snip
Content-Disposition: form-data;name= \"name=blupp\"

This is a text
--snip--
blah blah"
      "snip"
  in
  r = ["name=blupp", "text/plain", "This is a text"]
;;


let t008 f =
  let r =
    f
      "blah blah
--snip
Content-Disposition: form-data; strange=\"name=blop\"; name= \"blupp\"

This is a text
--snip--
blah blah"
      "snip"
  in
  r = ["blupp", "text/plain", "This is a text"]
;;


let t009 f =
  let r =
    f
      "blah blah
--snip
Content-Disposition: form-data; strange=\" name=blop \";  name=blupp

This is a text
--snip--
blah blah"
      "snip"
  in
  r = ["blupp", "text/plain", "This is a text"]
;;


let t010 f =
  (* There is a space after "octet-stream"! *)
  let r =
    f
      "--snip
Content-Disposition: form-data; name=blupp
Content-type:  application/octet-stream

This is a text
--snip--"
      "snip"
  in
  r = ["blupp", "application/octet-stream", "This is a text"]
;;


let t011 f =
  let r =
    f
      "blah blah
--snip
Content-Disposition: form-data; name=blupp

This is a text
--snip
Content-Disposition: form-data; name=blipp

Another line
--snip-- blah
blah blah"
      "snip"
  in
  r = ["blupp", "text/plain", "This is a text";
       "blipp", "text/plain", "Another line" ]
;;


let t012 f =
  (* A real example *)
   let r =
     f
"-----------------------------10843891265508332411092264958
Content-Disposition: form-data; name=\"line\"

aaa
-----------------------------10843891265508332411092264958
Content-Disposition: form-data; name=\"submit\"

Submit
-----------------------------10843891265508332411092264958--
"
      "---------------------------10843891265508332411092264958"
   in
   r = [ "line", "text/plain", "aaa";
	 "submit", "text/plain", "Submit";
       ]
;;


(**********************************************************************)
(* encode/decode                                                      *)
(**********************************************************************)

let t100() =
  let s = String.create 256 in
  for i = 0 to 255 do s.[i] <- Char.chr i done;
  let r = encode s in
  r = ("%00%01%02%03%04%05%06%07%08%09%0A%0B%0C%0D%0E%0F" ^
       "%10%11%12%13%14%15%16%17%18%19%1A%1B%1C%1D%1E%1F" ^
       "+!%22%23$%25%26'()*%2B,-.%2F" ^
       "0123456789%3A%3B%3C%3D%3E%3F" ^
       "%40ABCDEFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_" ^
       "%60abcdefghijklmnopqrstuvwxyz%7B%7C%7D%7E%7F" ^
       "%80%81%82%83%84%85%86%87%88%89%8A%8B%8C%8D%8E%8F" ^
       "%90%91%92%93%94%95%96%97%98%99%9A%9B%9C%9D%9E%9F" ^
       "%A0%A1%A2%A3%A4%A5%A6%A7%A8%A9%AA%AB%AC%AD%AE%AF" ^
       "%B0%B1%B2%B3%B4%B5%B6%B7%B8%B9%BA%BB%BC%BD%BE%BF" ^
       "%C0%C1%C2%C3%C4%C5%C6%C7%C8%C9%CA%CB%CC%CD%CE%CF" ^
       "%D0%D1%D2%D3%D4%D5%D6%D7%D8%D9%DA%DB%DC%DD%DE%DF" ^
       "%E0%E1%E2%E3%E4%E5%E6%E7%E8%E9%EA%EB%EC%ED%EE%EF" ^
       "%F0%F1%F2%F3%F4%F5%F6%F7%F8%F9%FA%FB%FC%FD%FE%FF")
;;


let t101() =
  let r = String.create 256 in
  for i = 0 to 255 do r.[i] <- Char.chr i done;
  let s = decode
	    ("%00%01%02%03%04%05%06%07%08%09%0A%0B%0C%0D%0E%0F" ^
	     "%10%11%12%13%14%15%16%17%18%19%1A%1B%1C%1D%1E%1F" ^
	     "+!%22%23$%25%26'()*%2B,-.%2F" ^
	     "0123456789%3A%3B%3C%3D%3E%3F" ^
	     "%40ABCDEFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_" ^
	     "%60abcdefghijklmnopqrstuvwxyz%7B%7C%7D%7E%7F" ^
	     "%80%81%82%83%84%85%86%87%88%89%8A%8B%8C%8D%8E%8F" ^
	     "%90%91%92%93%94%95%96%97%98%99%9A%9B%9C%9D%9E%9F" ^
	     "%A0%A1%A2%A3%A4%A5%A6%A7%A8%A9%AA%AB%AC%AD%AE%AF" ^
	     "%B0%B1%B2%B3%B4%B5%B6%B7%B8%B9%BA%BB%BC%BD%BE%BF" ^
	     "%C0%C1%C2%C3%C4%C5%C6%C7%C8%C9%CA%CB%CC%CD%CE%CF" ^
	     "%D0%D1%D2%D3%D4%D5%D6%D7%D8%D9%DA%DB%DC%DD%DE%DF" ^
	     "%E0%E1%E2%E3%E4%E5%E6%E7%E8%E9%EA%EB%EC%ED%EE%EF" ^
	     "%F0%F1%F2%F3%F4%F5%F6%F7%F8%F9%FA%FB%FC%FD%FE%FF") in
  r = s
;;


let t102() =
  let r = String.create 256 in
  for i = 0 to 255 do r.[i] <- Char.chr i done;
  let s = decode
	    ((String.lowercase
		("%00%01%02%03%04%05%06%07%08%09%0A%0B%0C%0D%0E%0F" ^
		 "%10%11%12%13%14%15%16%17%18%19%1A%1B%1C%1D%1E%1F" ^
		 "+!%22%23$%25%26'()*%2B,-.%2F" ^
		 "0123456789%3A%3B%3C%3D%3E%3F")) ^
	     "%40ABCDEFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_" ^
	     (String.lowercase
		("%60abcdefghijklmnopqrstuvwxyz%7B%7C%7D%7E%7F" ^
		 "%80%81%82%83%84%85%86%87%88%89%8A%8B%8C%8D%8E%8F" ^
		 "%90%91%92%93%94%95%96%97%98%99%9A%9B%9C%9D%9E%9F" ^
		 "%A0%A1%A2%A3%A4%A5%A6%A7%A8%A9%AA%AB%AC%AD%AE%AF" ^
		 "%B0%B1%B2%B3%B4%B5%B6%B7%B8%B9%BA%BB%BC%BD%BE%BF" ^
		 "%C0%C1%C2%C3%C4%C5%C6%C7%C8%C9%CA%CB%CC%CD%CE%CF" ^
		 "%D0%D1%D2%D3%D4%D5%D6%D7%D8%D9%DA%DB%DC%DD%DE%DF" ^
		 "%E0%E1%E2%E3%E4%E5%E6%E7%E8%E9%EA%EB%EC%ED%EE%EF" ^
		 "%F0%F1%F2%F3%F4%F5%F6%F7%F8%F9%FA%FB%FC%FD%FE%FF"))) in
  r = s
;;

(**********************************************************************)
(* dest_url_encoded_parameters                                        *)
(**********************************************************************)

let t200() =
  let r = dest_url_encoded_parameters "a=b&c=d" in
  r = ["a", "b"; "c", "d" ]
;;


let t201() =
  let r = dest_url_encoded_parameters "a=&c=d" in
  r = ["a", ""; "c", "d" ]
;;


let t202() =
  let r = dest_url_encoded_parameters "a=&c=" in
  r = ["a", ""; "c", "" ]
;;


let t203() =
  let r = dest_url_encoded_parameters "" in
  r = []
;;


let t204() =
  let r = dest_url_encoded_parameters "%41=%42" in
  r = ["A", "B"]
;;


(**********************************************************************)

let test f n =
  if f() then
    print_endline ("Test " ^ n ^ " ok")
  else
    print_endline ("Test " ^ n ^ " FAILED!!!!");
  flush stdout
;;


let test_dest_form_encoded_parameters f n =
  let dest s b =
    let args = dest_form_encoded_parameters s b default_config in
    List.map
      (fun a -> arg_name a, arg_mimetype a, arg_value a)
      args
  in
  if f dest then
    print_endline ("Test dest_form_encoded_parameters " ^ n ^ " ok")
  else
    print_endline ("Test dest_form_encoded_parameters " ^ n ^ " FAILED!!!!");
  flush stdout
;;


let fill_stream s =
  (* Returns a channel that reads from string s.
   * This requires forking.
   *)
  let rd, wr = Unix.pipe() in
  let pid = Unix.fork() in
  if pid = 0 then begin
    Unix.close rd;
    let out = Unix.out_channel_of_descr wr in
    output_string out s;
    close_out out;
    exit(0);
  end;
  Unix.close wr;
  Unix.in_channel_of_descr rd
;;


let test_dest_form_encoded_parameters_from_netstream f n =
  let dest s b =
    let fd = fill_stream s in
    let bs = String.length b * 2 in
    let stream = Netstream.create_from_channel fd None bs in
    let args = dest_form_encoded_parameters_from_netstream
		 stream b default_config in

(*
    List.iter
      (fun a ->
	 Printf.printf "name=%s mimetype=%s value=%s\n"
	   (arg_name a) (arg_mimetype a) (arg_value a))
      args;
*)
    List.map
      (fun a -> arg_name a, arg_mimetype a, arg_value a)
      args
  in
  if f dest then
    Printf.printf
      "Test dest_form_encoded_parameters_from_netstream %s ok\n"
      n
  else
    print_endline ("Test dest_form_encoded_parameters_from_netstream " ^ n ^ " FAILED!!!!");
  flush stdout
;;



test_dest_form_encoded_parameters t001 "001";;
test_dest_form_encoded_parameters t002 "002";;
test_dest_form_encoded_parameters t003 "003";;
test_dest_form_encoded_parameters t004 "004";;
test_dest_form_encoded_parameters t005 "005";;
test_dest_form_encoded_parameters t006 "006";;
test_dest_form_encoded_parameters t007 "007";;
test_dest_form_encoded_parameters t008 "008";;
test_dest_form_encoded_parameters t009 "009";;
test_dest_form_encoded_parameters t010 "010";;
test_dest_form_encoded_parameters t011 "011";;
test_dest_form_encoded_parameters t012 "012";;

test_dest_form_encoded_parameters_from_netstream t001 "001";;
test_dest_form_encoded_parameters_from_netstream t002 "002";;
test_dest_form_encoded_parameters_from_netstream t003 "003";;
test_dest_form_encoded_parameters_from_netstream t004 "004";;
test_dest_form_encoded_parameters_from_netstream t005 "005";;
test_dest_form_encoded_parameters_from_netstream t006 "006";;
test_dest_form_encoded_parameters_from_netstream t007 "007";;
test_dest_form_encoded_parameters_from_netstream t008 "008";;
test_dest_form_encoded_parameters_from_netstream t009 "009";;
test_dest_form_encoded_parameters_from_netstream t010 "010";;
test_dest_form_encoded_parameters_from_netstream t011 "011";;
test_dest_form_encoded_parameters_from_netstream t012 "012";;

test t100 "100";;
test t101 "101";;
test t102 "102";;

test t200 "200";;
test t201 "201";;
test t202 "202";;
test t203 "203";;
test t204 "204";;