File: eliomexamples.ml

package info (click to toggle)
ocsigen 1.3.3-1squeeze1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 3,488 kB
  • ctags: 4,784
  • sloc: ml: 35,847; makefile: 1,450; sh: 772; ansic: 29
file content (1567 lines) | stat: -rw-r--r-- 49,992 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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
(* Ocsigen
 * http://www.ocsigen.org
 * Module eliomexamples.ml
 * Copyright (C) 2007 Vincent Balat
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, with linking exception;
 * either version 2.1 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *)


(* Other examples for Eliom, and various tests *)

open Tutoeliom
open XHTML.M
open Eliom_predefmod.Xhtmlcompact
open Eliom_predefmod
open Eliom_services
open Eliom_parameters
open Eliom_sessions
open Lwt


(******)
(* unregistering services *)
let unregister_example =
  Eliom_predefmod.Xhtml.register_new_service
    ~path:["unregister"]
    ~get_params:Eliom_parameters.unit
    (fun sp () () ->
       let s1 = Eliom_predefmod.Xhtml.register_new_service
         ~sp
         ~path:["unregister1"]
         ~get_params:Eliom_parameters.unit
         (fun sp () () -> failwith "s1")
       in
       let s2 = Eliom_predefmod.Xhtml.register_new_coservice
         ~sp
         ~fallback:s1
         ~get_params:Eliom_parameters.unit
         (fun sp () () -> failwith "s2")
       in
       let s3 = Eliom_predefmod.Xhtml.register_new_coservice'
         ~sp
         ~get_params:Eliom_parameters.unit
         (fun sp () () -> failwith "s3")
       in
       Eliom_predefmod.Xhtml.register_for_session
         ~sp
         ~service:s1
         (fun sp () () -> failwith "s4");
       Eliom_services.unregister ~sp s1;
       Eliom_services.unregister ~sp s2;
       Eliom_services.unregister ~sp s3;
       Eliom_services.unregister_for_session ~sp s1;
       Lwt.return
         (html
            (head (title (pcdata "Unregistering services")) [])
            (body [p [pcdata 
                        "These services have been registered and unregistered"];
                   p [a s1 sp [pcdata "regular service"] ();
                      pcdata ", ";
                      a s2 sp [pcdata "coservice"] ();
                      pcdata ", ";
                      a s3 sp [pcdata "non attached coservice"] ();
                      pcdata ", ";
                      a s1 sp [pcdata "session service"] ();
                     ]]))
    )


(******)
(* CSRF GET *)

let csrfsafe_get_example =
  Eliom_services.new_service
    ~path:["csrfget"]
    ~get_params:Eliom_parameters.unit
    ()

let csrfsafe_example_get =
  Eliom_services.new_coservice
    ~csrf_safe:true
    ~timeout:10.
    ~fallback:csrfsafe_get_example
    ~get_params:Eliom_parameters.unit
    ()

let _ =
  let page sp () () =
    let l3 = Eliom_predefmod.Xhtml.get_form csrfsafe_example_get sp
        (fun _ -> [p [Eliom_predefmod.Xhtml.string_input
                        ~input_type:`Submit
                        ~value:"Click" ()]])
    in
    return
      (html
       (head (title (pcdata "CSRF safe service example")) [])
       (body [p [pcdata "A new coservice will be created each time this form is displayed"];
              l3]))
  in
  Eliom_predefmod.Xhtml.register csrfsafe_get_example page;
  Eliom_predefmod.Xhtml.register csrfsafe_example_get
    (fun sp () () ->
       Lwt.return
         (html
            (head (title (pcdata "CSRF safe service")) [])
            (body [p [pcdata "This is a GET CSRF safe service"]])))

(******)
(* CSRF POST on CSRF GET coservice *)

let csrfsafe_postget_example =
  Eliom_services.new_service
    ~path:["csrfpostget"]
    ~get_params:Eliom_parameters.unit
    ()

let csrfsafe_example_post =
  Eliom_services.new_post_coservice
    ~csrf_safe:true
    ~timeout:10.
    ~fallback:csrfsafe_example_get (* !!! *)
    ~post_params:Eliom_parameters.unit
    ()

let _ =
  let page sp () () =
    let l3 = Eliom_predefmod.Xhtml.post_form csrfsafe_example_post sp
        (fun _ -> [p [Eliom_predefmod.Xhtml.string_input
                        ~input_type:`Submit
                        ~value:"Click" ()]]) ()
    in
    return
      (html
       (head (title (pcdata "CSRF safe service example")) [])
       (body [p [pcdata "A new coservice will be created each time this form is displayed"];
              l3]))
  in
  Eliom_predefmod.Xhtml.register csrfsafe_postget_example page;
  Eliom_predefmod.Xhtml.register csrfsafe_example_post
    (fun sp () () ->
       Lwt.return
         (html
            (head (title (pcdata "CSRF safe service")) [])
            (body [p [pcdata "This is a POST CSRF safe service, combined with a GET CSRF safe service"]])))


(******)
(* CSRF for_session *)

let csrfsafe_session_example =
  Eliom_services.new_service
    ~path:["csrfsession"]
    ~get_params:Eliom_parameters.unit
    ()

let csrfsafe_example_session =
  Eliom_services.new_post_coservice'
    ~csrf_safe:true
    ~csrf_session_name:"plop"
    ~csrf_secure_session:true
    ~timeout:10.
    ~post_params:Eliom_parameters.unit
    ()

let _ =
  let page sp () () =
    Eliom_predefmod.Xhtml.register_for_session
      ~session_name:"plop"
      ~secure:true
      ~sp
      ~service:csrfsafe_example_session
      (fun sp () () ->
         Lwt.return
           (html
              (head (title (pcdata "CSRF safe service")) [])
              (body [p [pcdata "This is a POST CSRF safe service"]])));
    let l3 = Eliom_predefmod.Xhtml.post_form csrfsafe_example_session sp
        (fun _ -> [p [Eliom_predefmod.Xhtml.string_input
                        ~input_type:`Submit
                        ~value:"Click" ()]])
        ()
    in
    return
      (html
       (head (title (pcdata "CSRF safe service example")) [])
       (body [p [pcdata "A new coservice will be created each time this form is displayed"];
              l3]))
  in
  Eliom_predefmod.Xhtml.register csrfsafe_session_example page



(******)
(* optional suffix parameters *)

let optsuf =
  register_new_service
    ~path:["optsuf"]
    ~get_params:(suffix(opt(string "q" ** (opt (int "i")))))
    (fun sp o () -> 
       Lwt.return
         (html
            (head (title (pcdata "")) [])
            (body [p [pcdata (match o with 
                                 | None -> "<none>"
                                 | Some (s, o) -> 
                                     s^(match o with 
                                          | None -> "<none>"
                                          | Some i -> string_of_int i));
                     ]])))

let optsuf2 =
  register_new_service
    ~path:["optsuf2"]
    ~get_params:(suffix(opt(string "q") ** (opt (int "i"))))
    (fun sp (s, i) () -> 
       Lwt.return
         (html
            (head (title (pcdata "")) [])
            (body [p [pcdata (match s with 
                                 | None -> "<none>"
                                 | Some s -> s);
                      pcdata (match i with 
                                | None -> "<none>"
                                | Some i -> string_of_int i)];
                     ])))

(*******)
let my_nl_params = 
  Eliom_parameters.make_non_localized_parameters
    ~prefix:"tutoeliom"
    ~name:"mynlp"
    (Eliom_parameters.int "a" ** Eliom_parameters.string "s")

let void_with_nlp =
  Eliom_services.add_non_localized_get_parameters
    my_nl_params Eliom_services.void_hidden_coservice'

let nlparams = new_service
    ~path:["voidnl"]
    ~get_params:(suffix_prod (int "year" ** int "month") (int "w" ))
    ()

let nlparams_with_nlp =
  Eliom_services.add_non_localized_get_parameters
    my_nl_params nlparams

let () = register
  nlparams
  (fun sp ((aa, bb), w) () ->
     Lwt.return
       (html
          (head (title (pcdata "")) [])
          (body [p [
                   a void_with_nlp
                     sp [pcdata "void coservice with non loc param"] ((), (11, "aa"));
                   a nlparams_with_nlp
                     sp [pcdata "myself with non loc param"] (((4, 5), 777), (12, "ab"))];
                 p [pcdata "I have my suffix, ";
                    pcdata ("with values year = "^string_of_int aa^
                              " and month = "^string_of_int bb^
                              ". w = "^string_of_int w^".")];
                 (match Eliom_parameters.get_non_localized_get_parameters
                    sp my_nl_params 
                  with
                    | None -> 
                        p [pcdata "I do not have my non localized parameters"]
                    | Some (a, s) -> 
                        p [pcdata "I have my non localized parameters, ";
                           pcdata ("with values a = "^string_of_int a^
                                     " and s = "^s^".")]
                 )]))
    )




(*******)
(* doing requests *)
let extreq = 
  register_new_service
    ~path:["extreq"] 
    ~get_params:unit
    (fun sp () () ->
       Ocsigen_http_client.get "ocsigen.org" "/" () >>= fun frame ->
       (match frame.Ocsigen_http_frame.frame_content with
         | None -> Lwt.return ""
         | Some stream -> Ocsigen_stream.string_of_stream (Ocsigen_stream.get stream)) >>= fun s ->
       (* Here use an XML parser, 
          or send the stream directly using an appropriate Eliom_mkreg module *)
       return
         (html
            (head (title (pcdata "")) [])
            (body [p [pcdata s]])))

let servreq = 
  register_new_service
    ~path:["servreq"] 
    ~get_params:unit
    (fun sp () () ->
       let ri = Eliom_sessions.get_ri sp in
       let ri = Ocsigen_extensions.ri_of_url "tuto/" ri in
       Ocsigen_extensions.serve_request ri >>= fun result ->
       let stream = fst result.Ocsigen_http_frame.res_stream in
       Ocsigen_stream.string_of_stream (Ocsigen_stream.get stream) >>= fun s ->
       (* Here use an XML parser, 
          or send the stream directly using an appropriate Eliom_mkreg module *)
       return
         (html
            (head (title (pcdata "")) [])
            (body [p [pcdata s]])))

let servreqloop = 
  register_new_service
    ~path:["servreqloop"] 
    ~get_params:unit
    (fun sp () () ->
       let ri = Eliom_sessions.get_ri sp in
       Ocsigen_extensions.serve_request ri >>= fun result ->
       let stream = fst result.Ocsigen_http_frame.res_stream in
       Ocsigen_stream.string_of_stream (Ocsigen_stream.get stream) >>= fun s ->
       (* Here use an XML parser, 
          or send the stream directly using an appropriate Eliom_mkreg module *)
       return
         (html
            (head (title (pcdata "")) [])
            (body [p [pcdata s]])))





(* Customizing HTTP headers *)
let headers = 
  register_new_service
    ~code:666
    ~charset:"plopcharset"
(*    ~content_type:"custom/contenttype" *)
    ~cookies:[Eliom_services.Set (Some [], None,
                                  "Customcookie", 
                                  "Value",
                                  true);
              Eliom_services.Set (Some [], None,
                                  "Customcookie2", 
                                  "Value2",
                                  true);
             ]
    ~headers:(Http_headers.add
                (Http_headers.name "XCustom-header")
                "This is an example" 
                Http_headers.empty)
    ~path:["httpheaders"] 
    ~get_params:unit
    (fun sp () () ->
       return
         (html
            (head (title (pcdata "")) [])
            (body [h1 [pcdata "Look at my HTTP headers"]])))


(* form towards a suffix service with constants *)
let create_form (n1, (_, n2)) =
    <:xmllist< <p>
      $string_input ~input_type:`Text ~name:n1 ()$
      $string_input ~input_type:`Text ~name:n2 ()$
      $string_input ~input_type:`Submit ~value:"Click" ()$</p> >>

let constform = register_new_service ["constform"] unit
  (fun sp () () ->
     let f = get_form Tutoeliom.constfix sp create_form in
     return
        (html
          (head (title (pcdata "")) [])
          (body [h1 [pcdata "Hallo"];
                 f ])))


(* Suffix and other service at same URL *)
let su2 =
  register_new_service
    ~path:["fuffix";""]
    ~get_params:(suffix (all_suffix_string "s"))
    (fun _ s () ->
      return
        (html
          (head (title (pcdata "")) [])
          (body [h1
                   [pcdata s];
                 p [pcdata "Try page fuffix/a/b"]])))

let su =
  register_new_service
    ~path:["fuffix";"a";"b"]
    ~get_params:unit
    (fun _ () () ->
      return
        (html
          (head (title (pcdata "")) [])
          (body [h1 [pcdata "Try another suffix"]])))

let su3 =
  register_new_service
    ~path:["fuffix";""]
    ~get_params:unit
    (fun _ () () ->
      return
        (html
          (head (title (pcdata "")) [])
          (body [h1 [pcdata "Try another suffix"]])))

let create_suffixform_su2 s =
    <:xmllist< <p>Write a string:
      $string_input ~input_type:`Text ~name:s ()$ <br/>
      $string_input ~input_type:`Submit ~value:"Click" ()$</p> >>

let suffixform_su2 = register_new_service ["suffixform_su2"] unit
  (fun sp () () ->
     let f = get_form su2 sp create_suffixform_su2 in
     return
       (html
          (head (title (pcdata "")) [])
          (body [h1 [pcdata "Hallo"];
                 f ])))

(* optional parameters *)
let optparam =
  register_new_service
    ~path:["opt"]
    ~get_params:(Eliom_parameters.opt (Eliom_parameters.string "a" **
                                         Eliom_parameters.string "b"))
    (fun sp o () ->
      Lwt.return
        (html
           (head (title (pcdata "")) [])
           (body [h1 [pcdata "Hallo!"];
                  match o with
                    | None -> p [pcdata "no parameters"]
                    | Some (a, b) -> p [pcdata a;
                                        pcdata ", ";
                                        pcdata b]
                 ]))

    )

let optform =
  register_new_service
    ~path:["optform"]
    ~get_params:unit
    (fun sp () () ->
(* testing lwt_get_form *)
       Eliom_predefmod.Xhtml.lwt_get_form
         ~service:optparam ~sp
         (fun (an, bn) -> 
            Lwt.return
              [p [
                 string_input ~input_type:`Text ~name:an ();
                 string_input ~input_type:`Text ~name:bn ();
                 Eliom_predefmod.Xhtml.string_input
                   ~input_type:`Submit
                   ~value:"Click" ()]])
      >>= fun form ->
      let form = 
        (form : Xhtmltypes.form XHTML.M.elt :> [> Xhtmltypes.form ] XHTML.M.elt)
      in  
      return
        (html
           (head (title (pcdata "")) [])
           (body [h1 [pcdata "Hallo!"];
                  form
                 ]))

 )


(* Preapplied service with suffix parameters *)

let presu_service =
  register_new_service
    ~path: ["preappliedsuffix2"]
    ~get_params: (suffix (int "i"))
    (fun _ i () ->
      Lwt.return
        (html
           (head (title (pcdata "")) [])
           (body [p [ pcdata ("You sent: " ^ (string_of_int i))]])))


let creator_handler sp () () =
  let create_form () =
    [fieldset [string_input ~input_type:`Submit ~value:"Click" ()]] in
  let myservice = preapply presu_service 10 in
  let myform = get_form myservice sp create_form in
  Lwt.return
    (html
       (head (title (pcdata "")) [])
       (body   [
        p [pcdata "Form with preapplied parameter:"];
        myform;
        p [a myservice sp [pcdata "Link with preapplied parameter"] ()]
      ]))

let preappliedsuffix =
  register_new_service
    ~path: ["preappliedsuffix"]
    ~get_params: unit
    creator_handler


(* URL with ? or / in data or paths *)

let url_encoding =
  register_new_service
    ~path:["urlencoding&/=?ablah"]
    ~get_params:(suffix_prod (all_suffix "s//\\") any)
    (fun sp (suf, l) () ->
      let ll =
        List.map
          (fun (a,s) -> << <strong>($str:a$, $str:s$) </strong> >>) l
      in
      let sl =
        List.map
          (fun s -> << <strong>$str:s$ </strong> >>) suf
      in
      return
        (html
           (head (title (pcdata "")) [])
           (body [h1 [pcdata "Hallo"];
                  p sl;
                  p ll
                ])))


(* menu with preapplied services *)

let preappl = preapply coucou_params (3,(4,"cinq"))
let preappl2 = preapply uasuffix (1999,01)

let mymenu current sp =
  Eliom_tools.menu ~classe:["menuprincipal"]
    (coucou, <:xmllist< coucou >>)
    [
     (preappl, <:xmllist< params >>);
     (preappl2, <:xmllist< params and suffix >>);
   ] ~service:current ~sp

let preappmenu =
  register_new_service
    ~path:["menu"]
    ~get_params:unit
    (fun sp () () ->
      return
        (html
          (head (title (pcdata "")) [])
          (body [h1 [pcdata "Hallo"];
               mymenu coucou sp ])))




(* GET Non-attached coservice *)
let nonatt = new_coservice' ~get_params:(string "e") ()

(* GET coservice with preapplied fallback *)
(* + Non-attached coservice on a pre-applied coservice *)
(* + Non-attached coservice on a non-attached coservice *)
let f sp s =
  (html
     (head (title (pcdata "")) [])
     (body [h1 [pcdata s];
            p [a nonatt sp [pcdata "clic"] "nonon"];
            get_form nonatt sp
              (fun string_name ->
                [p [pcdata "Non attached coservice: ";
                    string_input ~input_type:`Text ~name:string_name ();
                    string_input ~input_type:`Submit ~value:"Click" ()]])
          ]))

let getco = register_new_coservice
    ~fallback:preappl
    ~get_params:(int "i" ** string "s")
    (fun sp (i,s) () -> return (f sp s))

let _ = register nonatt (fun sp s () -> return (f sp s))

let getcoex =
  register_new_service
    ~path:["getco"]
    ~get_params:unit
    (fun sp () () ->
      return
        (html
          (head (title (pcdata "")) [])
          (body [p [a getco sp [pcdata "clic"] (22,"eee") ];
                 get_form getco sp
                   (fun (number_name,string_name) ->
                     [p [pcdata "Write an int: ";
                         int_input ~input_type:`Text ~name:number_name ();
                         pcdata "Write a string: ";
                         string_input ~input_type:`Text ~name:string_name ();
                         string_input  ~input_type:`Submit ~value:"Click" ()]])
               ])))


(* POST service with preapplied fallback are not possible: *)
(*
let my_service_with_post_params =
  register_new_post_service
    ~fallback:preappl
    ~post_params:(string "value")
    (fun _ () value ->  return
      (html
         (head (title (pcdata "")) [])
         (body [h1 [pcdata value]])))
*)

(* GET coservice with coservice fallback: not possible *)
(*
let preappl3 = preapply getco (777,"ooo")

let getco2 =
  register_new_coservice
    ~fallback:preappl3
    ~get_params:(int "i2" ** string "s2")
    (fun sp (i,s) () ->
      return
        (html
          (head (title (pcdata "")) [])
          (body [h1 [pcdata s]])))

*)


(* POST service with coservice fallback *)
let my_service_with_post_params =
  register_new_post_service
    ~fallback:getco
    ~post_params:(string "value")
    (fun _ (i,s) value ->  return
      (html
         (head (title (pcdata "")) [])
         (body [h1 [pcdata (s^" "^value)]])))

let postcoex = register_new_service ["postco"] unit
  (fun sp () () ->
     let f =
       (post_form my_service_with_post_params sp
          (fun chaine ->
            [p [pcdata "Write a string: ";
                string_input ~input_type:`Text ~name:chaine ()]])
          (222,"ooo")) in
     return
       (html
         (head (title (pcdata "form")) [])
         (body [f])))


(* action on GET attached coservice *)
let v = ref 0

let getact =
  new_service
    ~path:["getact"]
    ~get_params:(int "p")
    ()

let act = Action.register_new_coservice
    ~fallback:(preapply getact 22)
    ~get_params:(int "bip")
    (fun _ g p -> v := g; return ())

(* action on GET non-attached coservice on GET coservice page *)
let naact = Action.register_new_coservice'
    ~get_params:(int "bop")
    (fun _ g p -> v := g; return ())

let naunit = Unit.register_new_coservice'
    ~get_params:(int "bap")
    (fun _ g p -> v := g; return ())

let _ =
  register
    getact
    (fun sp aa () ->
      return
        (html
           (head (title (pcdata "getact")) [])
           (body [h1 [pcdata ("v = "^(string_of_int !v))];
                  p [pcdata ("p = "^(string_of_int aa))];
                  p [a getact sp [pcdata "link to myself"] 0;
                     br ();
                     a act sp [pcdata "an attached action to change v"]
                       (Random.int 100);
                     br ();
                     a naact sp [pcdata "a non attached action to change v"]
                       (100 + Random.int 100);
                     pcdata " (Actually if called after the previous one, v won't change. More precisely, it will change and turn back to the former value because the attached coservice is reloaded after action)";
                     br ();
                     a naunit sp [pcdata "a non attached \"Unit\" page to change v"]
                       (200 + Random.int 100);
                     pcdata " (Reload after clicking here)"
                   ]])))




(* Many cookies *)
let cookiename = "c"

let cookies = new_service ["c";""] (suffix (all_suffix_string "s")) ()

let _ = Cookies.register cookies
    (fun sp s () ->  return
      ((html
        (head (title (pcdata "")) [])
        (body [p
                 (Ocsigen_lib.String_Table.fold
                    (fun n v l ->
                      (pcdata (n^"="^v))::
                      (br ())::l
                    )
                    (get_cookies sp)
                    [a cookies sp [pcdata "send other cookies"] ""; br ();
                     a cookies sp [pcdata "send other cookies and see the url /c/plop"] "plop"]
                    )])),
       let now = Unix.time () in
       let cookies =
         [Eliom_services.Set (Some [], Some (now +. 10.),
                              (cookiename^"6"), 
                              (string_of_int (Random.int 100)),
                              true);
          Eliom_services.Set (Some [], Some (now +. 10.),
                              (cookiename^"7"), 
                              (string_of_int (Random.int 100)),
                              true);
          Eliom_services.Set (Some ["c";"plop"], None,
                              (cookiename^"8"), 
                              (string_of_int (Random.int 100)),
                              false);
          Eliom_services.Set (Some ["c";"plop"], None,
                              (cookiename^"9"),
                              (string_of_int (Random.int 100)),
                              false);
          Eliom_services.Set (Some ["c";"plop"], None,
                              (cookiename^"10"), 
                              (string_of_int (Random.int 100)),
                              true);
          Eliom_services.Set (Some ["c";"plop"], None,
                              (cookiename^"11"), 
                              (string_of_int (Random.int 100)),
                              true);
          Eliom_services.Set (Some ["c";"plop"], None,
                              (cookiename^"12"), 
                              (string_of_int (Random.int 100)),
                              true);
        ]
       in if Ocsigen_lib.String_Table.mem (cookiename^"1") (get_cookies sp)
       then
         (Eliom_services.Unset (None, (cookiename^"1")))::
         (Eliom_services.Unset (None, (cookiename^"2")))::cookies
       else
         (Eliom_services.Set (None, None, (cookiename^"1"),
                              (string_of_int (Random.int 100)), true))::
          (Eliom_services.Set (None, None, (cookiename^"2"),
                               (string_of_int (Random.int 100)), false))::
          (Eliom_services.Set (None, None, (cookiename^"3"),
                               (string_of_int (Random.int 100)), false))
          ::cookies
      ))


(* Cookies or not cookies with Any *)
let sendany =
  Any.register_new_service
    ~path:["sendany2"]
    ~get_params:(suffix (all_suffix_string "type"))
   (fun sp s () ->
     if s = "nocookie"
     then
       Xhtml.send
         ~headers:(Http_headers.add
                     (Http_headers.name "XCustom-header")
                     "This is an example" 
                     Http_headers.empty)
         ~sp
         (html
            (head (title (pcdata "")) [])
            (body [p [pcdata "This page does not set cookies"]]))
     else
       Xhtml.Cookies.send
         sp
         ((html
             (head (title (pcdata "")) [])
             (body [p [pcdata "This page does set a cookie"]])),
          [Eliom_services.Set (None, None, "arf", 
                               (string_of_int (Random.int 100)), false)])
   )


(* Send file *)
let sendfileex =
  register_new_service
    ~path:["files";""]
    ~get_params:unit
    (fun _ () () ->
      return
        (html
          (head (title (pcdata "")) [])
          (body [h1 [pcdata "With a suffix, that page will send a file"]])))

let sendfile2 =
  Files.register_new_service
    ~path:["files";""]
    ~get_params:(suffix (all_suffix "filename"))
    (fun _ s () ->
      return ("/var/www/ocsigen/"^(Ocsigen_lib.string_of_url_path ~encode:false s)))

let sendfileexception =
  register_new_service
    ~path:["files";"exception"]
    ~get_params:unit
    (fun _ () () ->
      return
        (html
          (head (title (pcdata "")) [])
          (body [h1 [pcdata "With another suffix, that page will send a file"]])))


(* Complex suffixes *)
let suffix2 =
  new_service
    ~path:["suffix2";""]
    ~get_params:(suffix (string "suff1" ** int "ii" ** all_suffix "ee"))
    ()

let _ =
  register suffix2
    (fun sp (suf1, (ii, ee)) () ->
      return
        (html
           (head (title (pcdata "")) [])
           (body
              [p [pcdata "The suffix of the url is ";
                  strong [pcdata (suf1^", "^(string_of_int ii)^", "^
                                  (Ocsigen_lib.string_of_url_path ~encode:false ee))]];
              p [a suffix2 sp [pcdata "link to myself"] ("a", (2, []))]])))

let suffix3 =
  register_new_service
    ~path:["suffix3";""]
    ~get_params:(suffix_prod
                   (string "suff1" ** int "ii" ** 
                      all_suffix_user int_of_string string_of_int "ee")
                   (string "a" ** int "b"))
    (fun sp ((suf1, (ii, ee)), (a, b)) () ->
      return
        (html
           (head (title (pcdata "")) [])
           (body
              [p [pcdata "The parameters in the url are ";
                  strong [pcdata (suf1^", "^(string_of_int ii)^", "^
                                  (string_of_int ee)^", "^
                                  a^", "^(string_of_int b))]]])))

let create_suffixform2 (suf1, (ii, ee)) =
    <:xmllist< <p>Write a string:
      $string_input ~input_type:`Text ~name:suf1 ()$ <br/>
      Write an int: $int_input ~input_type:`Text ~name:ii ()$ <br/>
      Write a string: $user_type_input
      (Ocsigen_lib.string_of_url_path ~encode:false)
      ~input_type:`Text ~name:ee ()$ <br/>
      $string_input ~input_type:`Submit ~value:"Click" ()$</p> >>

let suffixform2 = register_new_service ["suffixform2"] unit
  (fun sp () () ->
     let f = get_form suffix2 sp create_suffixform2 in
     return
       (html
          (head (title (pcdata "")) [])
          (body [h1 [pcdata "Hallo"];
                 f ])))

let create_suffixform3 ((suf1, (ii, ee)), (a, b)) =
    <:xmllist< <p>Write a string:
      $string_input ~input_type:`Text ~name:suf1 ()$ <br/>
      Write an int: $int_input ~input_type:`Text ~name:ii ()$ <br/>
      Write an int: $int_input ~input_type:`Text ~name:ee ()$ <br/>
      Write a string: $string_input ~input_type:`Text ~name:a ()$ <br/>
      Write an int: $int_input ~input_type:`Text ~name:b ()$ <br/>
      $string_input ~input_type:`Submit ~value:"Click" ()$</p> >>

let suffixform3 = register_new_service ["suffixform3"] unit
  (fun sp () () ->
     let f = get_form suffix3 sp create_suffixform3 in
     return
        (html
          (head (title (pcdata "")) [])
          (body [h1 [pcdata "Hallo"];
                 f ])))

let suffix5 =
  register_new_service
    ~path:["suffix5"]
    ~get_params:(suffix (all_suffix "s"))
    (fun sp s () ->
      return
        (html
           (head (title (pcdata "")) [])
           (body
              [p [pcdata "This is a page with suffix ";
                  strong [pcdata (Ocsigen_lib.string_of_url_path
                                    ~encode:false s)]]])))

let nosuffix =
  register_new_service
    ~path:["suffix5";"notasuffix"]
    ~get_params:unit
    (fun sp () () ->
      return
        (html
           (head (title (pcdata "")) [])
           (body
              [p [pcdata "This is a page without suffix. Replace ";
                  code [pcdata "notasuffix"];
                  pcdata " in the URL by something else."
                ]])))



(* Send file with regexp *)
let sendfileregexp =
  register_new_service
    ~path:["files2";""]
    ~get_params:unit
    (fun _ () () ->
      return
        (html
          (head (title (pcdata "")) [])
          (body [h1 [pcdata "With a suffix, that page will send a file"]])))

let r = Netstring_pcre.regexp "~([^/]*)(.*)"

let sendfile2 =
  Files.register_new_service
    ~path:["files2";""]
(*    ~get_params:(regexp r "/home/$1/public_html$2" "filename") *)
    ~get_params:(suffix ~redirect_if_not_suffix:false
                   (all_suffix_regexp r "$u($1)/public_html$2" 
                      ~to_string:(fun s -> s) "filename"))
    (fun _ s () -> return s)

(* Here I am using redirect_if_not_suffix:false because 
   otherwise I would need to write a more sophisticated to_string function *)

(*
let sendfile2 =
  Files.register_new_service
    ~path:["files2";""]
    ~get_params:(suffix
                   (all_suffix_regexp r "/home/$1/public_html$2" "filename"))
(*    ~get_params:(suffix (all_suffix_regexp r "$$u($1)$2" "filename")) *)
    (fun _ s () -> return s)
*)

let create_suffixform4 n =
    <:xmllist< <p>Write the name of the file:
      $string_input ~input_type:`Text ~name:n ()$
      $string_input ~input_type:`Submit ~value:"Click" ()$</p> >>

let suffixform4 = register_new_service ["suffixform4"] unit
  (fun sp () () ->
     let f = get_form sendfile2 sp create_suffixform4 in
     return
        (html
          (head (title (pcdata "")) [])
          (body [h1 [pcdata "Hallo"];
                 f ])))


(* Advanced use of any *)
let any2 = register_new_service
    ~path:["any2"]
    ~get_params:(int "i" ** any)
  (fun _ (i,l) () ->
    let ll =
      List.map
        (fun (a,s) -> << <strong>($str:a$, $str:s$)</strong> >>) l
    in
    return
  << <html>
       <head><title></title></head>
       <body>
       <p>
         You sent:
         <span>$list:ll$</span>
         <br/>
         i = $str:(string_of_int i)$
       </p>
       </body>
     </html> >>)

(* the following will not work because s is taken in any. (not checked) *)
let any3 = register_new_service
    ~path:["any3"]
    ~get_params:(int "i" ** any ** string "s")
  (fun _ (i,(l,s)) () ->
    let ll =
      List.map
        (fun (a,s) -> << <strong>($str:a$, $str:s$)</strong> >>) l
    in
    return
  << <html>
       <head><title></title></head>
       <body>
       <p>
         You sent:
         <span>$list:ll$</span>
         <br/>
         i = $str:(string_of_int i)$
         <br/>
         s = $str:s$
       </p>
       </body>
     </html> >>)


(* any cannot be in suffix: (not checked) *)
let any4 = register_new_service
    ~path:["any4"]
    ~get_params:(suffix any)
  (fun _ l () ->
    let ll =
      List.map
        (fun (a,s) -> << <strong>($str:a$, $str:s$)</strong> >>) l
    in
    return
  << <html>
       <head><title></title></head>
       <body>
       <p>
         You sent:
         <span>$list:ll$</span>
       </p>
       </body>
     </html> >>)


let any5 = register_new_service
    ~path:["any5"]
    ~get_params:(suffix_prod (string "s") any)
  (fun _ (s, l) () ->
    let ll =
      List.map
        (fun (a,s) -> << <strong>($str:a$, $str:s$)</strong> >>) l
    in
    return
  << <html>
       <head><title></title></head>
       <body>
       <p>
         You sent <strong>$str:s$</strong> and :
         <span>$list:ll$</span>
       </p>
       </body>
     </html> >>)

(* list in suffix *)
let sufli = new_service
    ~path:["sufli"]
    ~get_params:(suffix (list "l" (string "s" ** int "i")))
    ()

let _ = register sufli
  (fun sp l () ->
    let ll =
      List.map
        (fun (s, i) -> << <strong> $str:(s^string_of_int i)$ </strong> >>) l
    in
    return
  << <html>
       <head><title></title></head>
       <body>
       <p>
         You sent:
         <span>$list:ll$</span>
       </p>
       <p>
         $a sufli sp [pcdata "myself"] [("a", 2)]$, 
         $a sufli sp [pcdata "myself (empty list)"] []$
       </p>
       </body>
     </html> >>)

let create_sufliform f =
  let l =
    f.it (fun (sn, iname) v init ->
            (tr (td [pcdata ("Write a string: ")])
               [td [string_input ~input_type:`Text ~name:sn ()];
                td [pcdata ("Write an integer: ")];
                td [int_input ~input_type:`Text ~name:iname ()];
               ])::init)
      ["one";"two";"three"]
      []
  in
  [table (List.hd l) (List.tl l);
   p [string_input ~input_type:`Submit ~value:"Click" ()]]

let sufliform = register_new_service ["sufliform"] unit
  (fun sp () () ->
     let f = get_form sufli sp create_sufliform in
     return
       (html
          (head (title (pcdata "")) [])
          (body [h1 [pcdata "Hallo"];
                 f ])))

(*
(* mmmh ... disabled dynamically for now *)
let sufli2 = new_service
    ~path:["sufli2"]
    ~get_params:(suffix ((list "l" (int "i")) ** int "j"))
    ()

let _ = register sufli2
  (fun sp (l, j) () ->
    let ll =
      List.map (fun i -> << <strong> $str:(string_of_int i)$ </strong> >>) l
    in
    return
  << <html>
       <head><title></title></head>
       <body>
       <p>
         You sent:
         <span>$list:ll$</span>,
         and
         j=$str:string_of_int j$.
       </p>
       <p>
         $a sufli2 sp [pcdata "myself"] ([1; 2], 3)$, 
         $a sufli2 sp [pcdata "myself (empty list)"] ([], 1)$
       </p>
       </body>
     </html> >>)
*)

let sufliopt = new_service
    ~path:["sufliopt"]
    ~get_params:(suffix (list "l" (opt (string "s"))))
    ()

let _ = register sufliopt
  (fun sp l () ->
    let ll =
      List.map
        (function None -> pcdata "<none>"
           | Some s -> << <strong> $str:s$ </strong> >>) l
    in
    return
  << <html>
       <head><title></title></head>
       <body>
       <p>
         You sent:
         <span>$list:ll$</span>
       </p>
       <p>
         $a sufliopt sp [pcdata "myself"] [Some "a"; None; Some "po"; None; None; Some "k"; None]$, 
         $a sufliopt sp [pcdata "myself (empty list)"] []$
         $a sufliopt sp [pcdata "myself (list [None; None])"] [None; None]$
         $a sufliopt sp [pcdata "myself (list [None])"] [None]$
       </p>
       </body>
     </html> >>)


let sufliopt2 = new_service
    ~path:["sufliopt2"]
    ~get_params:(suffix (list "l" (opt (string "s" ** string "ss"))))
    ()

let _ = register sufliopt2
  (fun sp l () ->
    let ll =
      List.map
        (function None -> pcdata "<none>"
           | Some (s, ss) -> << <strong> ($str:s$, $str:ss$) </strong> >>) l
    in
    return
  << <html>
       <head><title></title></head>
       <body>
       <p>
         You sent:
         <span>$list:ll$</span>
       </p>
       <p>
         $a sufliopt2 sp [pcdata "myself"] [Some ("a", "jj"); None; Some ("po", "jjj"); None; None; Some ("k", "pp"); None]$, 
         $a sufliopt2 sp [pcdata "myself (empty list)"] []$
         $a sufliopt2 sp [pcdata "myself (list [None; None])"] [None; None]$
         $a sufliopt2 sp [pcdata "myself (list [None])"] [None]$
       </p>
       </body>
     </html> >>)


(* set in suffix *)
let sufset = register_new_service
    ~path:["sufset"]
    ~get_params:(suffix (set string "s"))
  (fun _ l () ->
    let ll =
      List.map
        (fun s -> << <strong>$str:s$</strong> >>) l
    in
    return
  << <html>
       <head><title></title></head>
       <body>
       <p>
         You sent:
         <span>$list:ll$</span>
       </p>
       </body>
     </html> >>)



(* form to any2 *)
let any2form = register_new_service
    ~path:["any2form"]
    ~get_params:unit
    (fun sp () () ->
      return
        (html
           (head (title (pcdata "")) [])
           (body [h1 [pcdata "Any Form"];
                  get_form any2 sp
                    (fun (iname,grr) ->
                      [p [pcdata "Form to any2: ";
                          int_input ~input_type:`Text ~name:iname ();
                          raw_input ~input_type:`Text ~name:"plop" ();
                          raw_input ~input_type:`Text ~name:"plip" ();
                          raw_input ~input_type:`Text ~name:"plap" ();
                          string_input ~input_type:`Submit ~value:"Click" ()]])
                ])))


(* bool list *)

let boollist = register_new_service
    ~path:["boollist"]
    ~get_params:(list "a" (bool "b"))
  (fun _ l () ->
    let ll =
      List.map (fun b ->
        (strong [pcdata (if b then "true" else "false")])) l in
    return
      (html
         (head (title (pcdata "")) [])
         (body
            [p ((pcdata "You sent: ")::ll)]
         )))

let create_listform f =
  (* Here, f.it is an iterator like List.map,
     but it must be applied to a function taking 2 arguments
     (and not 1 as in map), the first one being the name of the parameter.
     The last parameter of f.it is the code that must be appended at the
     end of the list created
   *)
  let l =
    f.it (fun boolname v init ->
            (tr (td [pcdata ("Write the value for "^v^": ")])
               [td [bool_checkbox ~name:boolname ()]])::init)
      ["one";"two";"three"]
      []
  in
  [table (List.hd l) (List.tl l);
   p [raw_input ~input_type:`Submit ~value:"Click" ()]]

let boollistform = register_new_service ["boolform"] unit
  (fun sp () () ->
     let f = get_form boollist sp create_listform in return
        (html
          (head (title (pcdata "")) [])
          (body [f])))


(********)


(* any with POST *)
let any = register_new_post_service
    ~fallback:coucou
    ~post_params:any
  (fun _ () l ->
    let ll =
      List.map
        (fun (a,s) -> << <strong>($str:a$, $str:s$)</strong> >>) l
    in
    return
  << <html>
       <head><title></title></head>
       <body>
       <p>
         You sent:
         $list:ll$
       </p>
       </body>
     </html> >>)

(* form to any *)
let anypostform = register_new_service
    ~path:["anypostform"]
    ~get_params:unit
    (fun sp () () ->
      return
        (html
           (head (title (pcdata "")) [])
           (body [h1 [pcdata "Any Form"];
                  post_form any sp
                    (fun () ->
                      [p [pcdata "Empty form to any: ";
                          string_input ~input_type:`Submit ~value:"Click" ()]])
                    ()
                ])))

(**********)
(* upload *)

(* ce qui suit ne doit pas fonctionner. Mais il faudrait l'interdire *)
let get_param_service =
  register_new_service
   ~path:["uploadget"]
   ~get_params:(string "name" ** file "file")
    (fun _ (name,file) () ->
         let to_display =
           let newname = "/tmp/fichier" in
           (try
             Unix.unlink newname;
           with _ -> ());
           Unix.link (get_tmp_filename file) newname;
           let fd_in = open_in newname in
           try
             let line = input_line fd_in in close_in fd_in; line (*end*)
           with End_of_file -> close_in fd_in; "vide"
         in
         return
            (html
                (head (title (pcdata name)) [])
                (body [h1 [pcdata to_display]])))


let uploadgetform = register_new_service ["uploadget"] unit
  (fun sp () () ->
    let f =
(* ARG        (post_form ~a:[(XHTML.M.a_enctype "multipart/form-data")] fichier2 sp *)
     (get_form ~a:[(XHTML.M.a_enctype "multipart/form-data")] ~service:get_param_service ~sp
     (*post_form my_service_with_post_params sp        *)
        (fun (str, file) ->
          [p [pcdata "Write a string: ";
              string_input ~input_type:`Text ~name:str ();
              br ();
              file_input ~name:file ()]])) in  return
         (html
           (head (title (pcdata "form")) [])
           (body [f])))


(*******)
(* Actions that raises an exception *)
let exn_act = Action.register_new_coservice'
    ~get_params:unit
    (fun _ g p -> fail Not_found)

let exn_act_main =
  register_new_service
    ~path:["exnact"]
    ~get_params:unit
    (fun sp () () ->
      return
        (html
           (head (title (pcdata "exnact")) [])
           (body [h1 [pcdata "Hello"];
                  p [a exn_act sp [pcdata "Do the action"] ()
                   ]])))


(* close sessions from outside *)
let close_from_outside =
  register_new_service
    ~path:["close_from_outside"]
    ~get_params:unit
    (fun sp () () ->
      close_all_sessions ~session_name:"persistent_sessions" ~sp () >>= fun () ->
      close_all_sessions ~session_name:"action_example2" ~sp () >>= fun () ->
      return
        (html
           (head (title (pcdata "")) [])
           (body [h1 [pcdata "all sessions called \"persistent_sessions\" and \"action_example2\" closed"];
                  p [a persist_session_example sp [pcdata "try"] ()]])))



(* setting timeouts *)
let set_timeout =
register_new_service
    ~path:["set_timeout"]
    ~get_params:(int "t" ** (bool "recompute" ** bool "overrideconfig"))
    (fun sp (t, (recompute, override_configfile)) () ->
      set_global_persistent_data_session_timeout
        ~override_configfile
        ~session_name:(Some "persistent_sessions")
        ~recompute_expdates:recompute ~sp (Some (float_of_int t));
      set_global_volatile_session_timeout
        ~override_configfile
        ~session_name:(Some "action_example2")
        ~recompute_expdates:recompute ~sp (Some (float_of_int t));
      return
        (html
           (head (title (pcdata "")) [])
           (body [h1 [pcdata "Setting timeout"];
                  p [
                  if recompute
                  then pcdata ("The timeout for sessions called \"persistent_sessions\" and \"action_example2\" has been set to "^(string_of_int t)^" seconds (all expiration dates updated).")
                  else pcdata ("From now, the timeout for sessions called \"persistent_sessions\" and \"action_example2\" will be "^(string_of_int t)^" seconds (expiration dates not updated)."); br ();
                  a persist_session_example sp [pcdata "Try"] ()]])))


let create_form =
  (fun (number_name, (bool1name, bool2name)) ->
    [p [pcdata "New timeout: ";
        Eliom_predefmod.Xhtml.int_input ~input_type:`Text ~name:number_name ();
        br ();
        pcdata "Check the box if you want to recompute all timeouts: ";
        Eliom_predefmod.Xhtml.bool_checkbox ~name:bool1name ();
        br ();
        pcdata "Check the box if you want to override configuration file: ";
        Eliom_predefmod.Xhtml.bool_checkbox ~name:bool2name ();
        Eliom_predefmod.Xhtml.string_input ~input_type:`Submit ~value:"Submit" ()]])

let set_timeout_form =
  register_new_service
    ["set_timeout"]
    unit
    (fun sp () () ->
      let f = Eliom_predefmod.Xhtml.get_form set_timeout sp create_form in
      return
        (html
           (head (title (pcdata "")) [])
           (body [f])))



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

let sraise =
  register_new_service
    ~path:["raise"]
    ~get_params:unit
    (fun _ () () -> failwith "Bad use of exceptions")

let sfail =
  register_new_service
    ~path:["fail"]
    ~get_params:unit
    (fun _ () () -> Lwt.fail (Failure "Service raising an exception"))


(******************************************************************)
let mainpage = register_new_service ["tests"] unit
 (fun sp () () ->
   return
    (html
     (head (title (pcdata "Test"))
        [css_link (make_uri (static_dir sp) sp ["style.css"]) ()])
     (body
       [h1 [img ~alt:"Ocsigen" ~src:(make_uri (static_dir sp) sp ["ocsigen5.png"]) ()];
        h3 [pcdata "Eliom tests"];
        p
        [
         a coucou sp [pcdata "coucou"] (); br ();
         a optform sp [pcdata "Optional parameters"] (); br ();
         a sfail sp [pcdata "Service raising an exception"] (); br ();
         a sraise sp [pcdata "Wrong use of exceptions during service"] (); br ();
         a getcoex sp [pcdata "GET coservice with preapplied fallback, etc"] (); br ();
         a postcoex sp [pcdata "POST service with coservice fallback"] (); br ();
         a su sp [pcdata "Suffix and other service at same URL"] (); br ();
         a suffixform_su2 sp [pcdata "Suffix and other service at same URL: a form towards the suffix service"] (); br ();
         a preappliedsuffix sp [pcdata "Preapplied suffix"] (); br ();
         a constform sp [pcdata "Form towards suffix service with constants"] (); br ();
         a getact sp [pcdata "action on GET attached coservice, etc"] 127; br ();
         a cookies sp [pcdata "Many cookies"] "le suffixe de l'URL"; br ();
         a sendany sp [pcdata "Cookie or not with Any"] "change this suffix to \"nocookie\""; br ();
         a headers sp [pcdata "Customizing HTTP headers"] (); br ();
         a sendfileex sp [pcdata "Send file"] (); br ();
         a sendfile2 sp [pcdata "Send file 2"] "style.css"; br ();
         a sendfileexception sp [pcdata "Do not send file"] (); br ();
         a sendfileregexp sp [pcdata "Send file with regexp"] (); br ();
         a suffixform2 sp [pcdata "Suffix 2"] (); br ();
         a suffixform3 sp [pcdata "Suffix 3"] (); br ();
         a suffixform4 sp [pcdata "Suffix 4"] (); br ();
         a nosuffix sp [pcdata "Page without suffix on the same URL of a page with suffix"] (); br ();
         a anypostform sp [pcdata "POST form to any parameters"] (); br ();
         a any2 sp [pcdata "int + any parameters"]
           (3, [("Ciao","bel"); ("ragazzo","!")]); br ();
         a any3 sp [pcdata "any parameters broken (s after any)"]
           (4, ([("Thierry","Richard");("Sbastien","Stphane")], "s")); br ();
(* broken        a any4 sp [pcdata "Any in suffix"] [("bo","ba");("bi","bu")]; br (); *)
         a any5 sp [pcdata "Suffix + any parameters"]
           ("ee", [("bo","ba");("bi","bu")]); br ();
         a uploadgetform sp [pcdata "Upload with GET"] (); br ();
         a sufli sp [pcdata "List in suffix"] [("bo", 4);("ba", 3);("bi", 2);("bu", 1)]; br ();
         a sufliform sp [pcdata "Form to list in suffix"] (); br ();
         a sufliopt sp [pcdata "List of optional values in suffix"] [None; Some "j"]; br ();
         a sufliopt2 sp [pcdata "List of optional pairs in suffix"] [None; Some ("j", "ee")]; br ();
         a sufset sp [pcdata "Set in suffix"] ["bo";"ba";"bi";"bu"]; br ();
(*         a sufli2 sp [pcdata "List not in the end of in suffix"] ([1; 2; 3], 4); br (); *)
         a boollistform sp [pcdata "Bool list"] (); br ();
         a preappmenu sp [pcdata "Menu with pre-applied services"] (); br ();
         a exn_act_main sp [pcdata "Actions that raises an exception"] (); br ();
         a close_from_outside sp [pcdata "Closing sessions from outside"] (); br ();
         a set_timeout_form sp [pcdata "Setting timeouts from outside sessions"] (); br ();
         a
           ~fragment:"a--   ---++&/@"
           ~service:url_encoding ~sp
           [pcdata "Urls with strange characters inside"]
           (["l/l%l      &l=l+l)l@";"m\\m\"m";"n?n~n"],
            [("po?po&po~po/po+po", "lo?\"l     o#lo'lo lo=lo&l      o/lo+lo");
            ("bo=mo@co:ro", "zo^zo%zo$zo:zo?aaa")]); br ();
         a ~service:(static_dir_with_params ~sp ~get_params:Eliom_parameters.any ())
           ~sp 
           [pcdata "Static file with GET parameters"]
           (["ocsigen5.png"], [("aa", "lmk"); ("bb", "4")]); br ();

         a extreq sp [pcdata "External request"] (); br ();
         a servreq sp [pcdata "Server request"] (); br ();
         a servreqloop sp [pcdata "Looping server request"] (); br ();

         a nlparams sp [pcdata "nl params and suffix, on void coservice"] ((3, 5), 222); br ();
         a optsuf sp [pcdata "optional suffix"] None; br ();
         a optsuf sp [pcdata "optional suffix"] (Some ("<a to/to=3?4=2>", None)); br ();
         a optsuf sp [pcdata "optional suffix"] (Some ("toto", Some 2)); br ();
         a optsuf2 sp [pcdata "optional suffix 2"] (Some "un", Some 2); br ();
         a optsuf2 sp [pcdata "optional suffix 2"] (None, Some 2); br ();
         a optsuf2 sp [pcdata "optional suffix 2"] (Some "un", None); br ();
         a optsuf2 sp [pcdata "optional suffix 2"] (None, None); br ();

         a csrfsafe_get_example sp [pcdata "GET CSRF safe service"] (); br ();
         a csrfsafe_postget_example sp [pcdata "POST CSRF safe service on GET CSRF safe service"] (); br ();
         a csrfsafe_session_example sp [pcdata "POST non attached CSRF safe service in session table"] (); br ();
         a unregister_example sp [pcdata "Unregistering services"] (); br ();


       ]])))