controllers_web_site.anubis
42.4 KB
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
/*
* Created by PyramIDE.
* User: フランスのトトロ aka (David RENÉ)
* Date: 07/10/2019
* Time: 10:49
* © David RENÉ
*/
website with:
- controllers
- web session
- plugin
- web page renderer
transmit CXM_web_session.anubis
transmit types/controllers_web_site.anubis
read calexium_lib/web/plugin/plugin.anubis
read config/logger_config.anubis
public type WEB_Action:
web_action(
WEB_Action_Name name, // name of action
WEB_Action_Allowed_Protocol allowed_proto,
(WEB_Session) -> Bool allow, // true if action allowed
(WEB_Session) -> WEB_Controller_Result do_it
)
.
public define WEB_Controller_Result
ajax
(
HTTP_Answer http_answer
)=
ajax(failure, http_answer)
.
public define WEB_Controller_Result
ajax
(
WEB_Session session, //modified session
HTTP_Answer http_answer
)=
ajax(success(session), http_answer)
.
public define WEB_Controller_Result
ajax
(
HTML_Partial_Content content
)=
ajax(failure, content, "").
public define WEB_Controller_Result
ajax
(
HTML_Partial_Content content,
String additional_script
)=
ajax(failure, content, additional_script).
public define WEB_Controller_Result
ajax
(
WEB_Session session, //modified session
HTML_Partial_Content content
)=
ajax(success(session), content, "").
public define WEB_Controller_Result
ajax
(
WEB_Session session, //modified session
HTML_Partial_Content content,
String additional_script
)=
ajax(success(session), content, additional_script).
public define WEB_Controller_Result
ajax
(
Printable_tree content
)=
ajax(failure, content, "").
public define WEB_Controller_Result
ajax
(
Printable_tree content,
String additional_script
)=
ajax(failure, content, additional_script).
public define WEB_Controller_Result
ajax
(
WEB_Session session, //modified session
Printable_tree content
)=
ajax(success(session), content, "").
public define WEB_Controller_Result
ajax
(
WEB_Session session, //modified session
Printable_tree content,
String additional_script
)=
ajax(success(session), content, additional_script).
public define WEB_Controller_Result
renderer_content(
HTML_Partial_Content content,
)=
renderer_content(failure, content)
.
public define WEB_Controller_Result
renderer_content(
WEB_Session session, //modified session if success else failure
HTML_Partial_Content content,
)=
renderer_content(success(session), content)
.
public define WEB_Controller_Result
redraw(
HTML_Partial_Content content,
)=
redraw(failure, content)
.
public type WEB_Page_Renderer:
web_page_renderer(
String app, //application
String name, //renderer name
(WEB_Session _session, List(WEB_Plugin), HTML_Partial_Content _content) -> HTTP_Answer page_layout //call back for rendering page
)
.
define WEB_Page_Renderer
default_page_renderer =
web_page_renderer("AWS", "AWS_DEFAULT_PAGE",
( WEB_Session _session,
List(WEB_Plugin) _plugins,
HTML_Partial_Content _content
) |->
with _title = get_page_title(_session),
html_page
(
http_ok,
[ title(_title), ]// title of web site
,
body // body of page
(
[], //body options empty
(HTML_Off_Form)partial(_content)
)
)
)
.
define WEB_Controller_Result
apply_action
(
WEB_Session _session,
String requested_action_name,
List(WEB_Action) actions_list
) =
//println("apply_action ");
if actions_list is
{
[] then http_answer(error_page(http_not_found, "["+requested_action_name+"] Action not found in all controllers", _session)),
[h . t] then
if h.name = no_action then
http_answer( _session, html_content(http_no_content, empty))
else
with action_name = if h.name is
{
no_action then "",
controller_action(_, name) then name,
action_name(name) then name,
url(url) then url,
},
if requested_action_name = action_name then
with step = get_Int(_session.fields, "AWS_SESSION_STEP", 0) + 1,
replace_Int(_session.fields, "AWS_SESSION_STEP", step);
if h.allowed_proto is
{
//Action only in HTTP
http then
if _session.web_request.is_https then
http_answer(error_page(http_forbidden, "Only HTTP authorized", _session))
else
//ask to the server the authorization to execute that action
if h.allow(_session) then
h.do_it(_session)
else
http_answer(error_page(http_unauthorized, "you are not authorized for this action", _session))
//Action only in HTTPS
https then
if _session.web_request.is_https then
//ask to the server the authorization to execute that action
if h.allow(_session) then
h.do_it(_session)
else
http_answer(error_page(http_unauthorized, "you are not authorized for this action", _session))
else
http_answer(error_page(http_forbidden, "Only HTTPS authorized", _session))
//Action in both HTTP / HTTPS
http_https then
//ask to the server the authorization to execute that action
if h.allow(_session) then
//println("do_it "+action_name);
h.do_it(_session)
else
http_answer(error_page(http_unauthorized, "you are not authorized for this action", _session))
}
else
apply_action(_session, requested_action_name, t)
}
.
define Maybe(WEB_Controller)
get_controller
(
String controller_name,
List(WEB_Controller) controllers,
(LogLevel, String) -> One logger //logger
)=
if controllers is
{
[] then
logger(logWarning, "Can't find controller ["+controller_name+"]");
failure,
[h . t] then
if h.name = controller_name then
//println("Controller ["+controller_name+"] found");
success(h)
else
get_controller(controller_name, t, logger)
}
.
define WEB_Page_Renderer
get_page_renderer
(
String renderer_name,
List(WEB_Page_Renderer) renderers,
(LogLevel, String) -> One logger //logger
)=
if renderers is
{
[] then
logger(logWarning, "Can't find page_renderer ["+renderer_name+"], use the default");
default_page_renderer,
[h . t] then
if h.name = renderer_name then
//println("renderer_name ["+renderer_name+"] found");
h
else
//println("renderer_name "+renderer_name+" not matching");
get_page_renderer(renderer_name, t, logger)
}
.
define WEB_Page_Renderer
get_page_renderer
(
WEB_Session _session,
WEB_Page_Renderer _current_page_renderer,
List(WEB_Page_Renderer) renderers,
(LogLevel, String) -> One logger //logger
)=
//get the renderer page name to use from the session "AWS_PAGE_RENDERER"
if get_String(_session.fields, "AWS_CURRENT_PAGE_RENDERER") is
{
failure then logger(logWarning, "No renderer defined, use the default"); default_page_renderer,
success(renderer_name) then
//check if the renderer_page already in cache
if renderer_name = _current_page_renderer.name then
_current_page_renderer
else
//if not in cache, use the selector function
get_page_renderer(renderer_name, renderers, logger)
}
.
public define JsonValue to_html_ajax_content(Printable_tree content_HTML, String additional_script).
public define JsonValue to_html_ajax_content(HTML_Partial_Content content_HTML, CommonInfo cinfo, String additional_script).
define (Maybe(WEB_Session), HTTP_Answer)
apply_controller_action
(
WEB_Controller controller,
List(WEB_Plugin) plugins,
List(WEB_Controller) controllers,
WEB_Page_Renderer _current_page_renderer,
List(WEB_Page_Renderer) page_renderers,
CommonInfo cinfo,
WEB_Session _session,
Maybe(String) _mb_action_name,
(HTTP_Info,
Var(List(Web_arg)),
Bool is_https) -> WEB_Session initial_session,
(LogLevel, String) -> One logger //logger
)=
//get the action name
with mb_action_name =
if _mb_action_name is
{
failure then get_String(*_session.web_request.lwa, "aws_action"),
success(_an) then success(_an)
},
if mb_action_name is
{
failure then
//(failure, error_page(http_not_found, "Action name failure", _session)),
with new_session = initial_session(_session.web_request.http_info, _session.web_request.lwa, _session.web_request.is_https),
if get_controller(get_String(*new_session.web_request.lwa, "aws_controller", "root"), controllers, logger) is
{
failure then (failure, error_page(http_not_found, "Initial Session Controller not found", new_session)),
success(new_controller) then apply_controller_action(new_controller, plugins, controllers, _current_page_renderer, page_renderers, cinfo, new_session, failure, initial_session, logger),
},
success(action_name) then
//logger(logInfo, "ACTION_NAME ["+action_name+"] from IP "+get_String(_session.fields, "IP", ""));
if apply_action(_session, action_name, *controller.controller_actions) is
{
http_answer(session, answer) then
since session is web_session(id, lang, entries, current, previous, _),
with new_web_session = web_session(id, lang, entries, current, previous, current),
// println("***** http_answer(session, answer) record new draw point to *****");
// println("web arguments:"+
// dump_web_arg_values(*current.lwa));
// println("************************************");
(success(new_web_session), answer),
http_answer(answer) then
since _session is web_session(id, lang, entries, current, previous, _),
with new_web_session = web_session(id, lang, entries, current, previous, current),
// println("***** http_answer(answer) record new draw point to *****");
// println("web arguments:"+
// dump_web_arg_values(*current.lwa));
// println("************************************");
(success(new_web_session), answer),
redirect(new_session) then
if get_controller(get_String(*new_session.web_request.lwa, "aws_controller", "root"), controllers, logger) is
{
failure then (failure, error_page(http_not_found, "Redirect Controller not found", new_session)),
success(new_controller) then apply_controller_action(new_controller, plugins, controllers, _current_page_renderer, page_renderers, cinfo, new_session, failure, initial_session, logger),
},
redirect(new_session, r_controller_name, r_action_name) then
if get_controller(r_controller_name, controllers, logger) is
{
failure then (failure, error_page(http_not_found, "Redirect Controller "+r_controller_name+" not found", new_session)),
success(new_controller) then
apply_controller_action(new_controller, plugins, controllers, _current_page_renderer, page_renderers, cinfo, new_session, success(r_action_name), initial_session, logger),
},
redirect_to_previous then
if get_controller(get_String(*_session.previous_web_request.lwa, "aws_controller", "root"), controllers, logger) is
{
failure then (failure, error_page(http_not_found, "Redirect to previous Controller not found", _session)),
success(new_controller) then
since _session is web_session(id, lang, entries, _, previous, draw),
apply_controller_action(new_controller, plugins, controllers, _current_page_renderer, page_renderers, cinfo, web_session(id, lang, entries, previous, previous, draw), failure, initial_session, logger),
},
redirect_to_previous(entries) then
if get_controller(get_String(*_session.previous_web_request.lwa, "aws_controller", "root"), controllers, logger) is
{
failure then (failure, error_page(http_not_found, "Redirect to previous Controller not found", _session)),
success(new_controller) then
since _session is web_session(id, lang, _, _, previous, draw),
apply_controller_action(new_controller, plugins, controllers, _current_page_renderer, page_renderers, cinfo, web_session(id, lang, entries, previous, previous, draw), failure, initial_session, logger),
},
//ajax(answer) then (failure, answer),
//ajax with modified session that must be saved
ajax(session, answer) then (session, answer),
//ajax(content, additional_script) then (failure, json(http_ok, to_html_ajax_content(content, cinfo, additional_script))),
ajax(session, content, additional_script) then (session, json(http_ok, to_html_ajax_content(content, cinfo, additional_script))),
//ajax(content, additional_script) then (failure, json(http_ok, to_html_ajax_content(content, additional_script))),
ajax(session, content, additional_script) then (session, json(http_ok, to_html_ajax_content(content, additional_script))),
send_file(file_path, c_disposition) then (failure, send_file(file_path, c_disposition)),
renderer_content(session, content) then
with the_session = if session is {failure then _session, success(__session) then __session},
since the_session is web_session(id, lang, entries, current, previous, _),
with new_web_session = web_session(id, lang, entries, current, previous, current),
// println("***** renderer_content(session, content) record new draw point to *****");
// println("web arguments:"+
// dump_web_arg_values(*current.lwa));
// println("************************************");
with page_renderer = get_page_renderer(new_web_session, _current_page_renderer, page_renderers, logger),
(success(new_web_session), page_renderer.page_layout(new_web_session, plugins, content))
}
}
.
public define Web_Site
make_web_site_controller_description
(
String website_name, // site_UID
List(String) common_names, // for example: ["www.our-business.com"]
String site_directory,
String state_directory,
One -> One init,
(HTTP_Info,
Var(List(Web_arg)),
Bool is_https) -> WEB_Session initial_session,
(WEB_Session,
HTTP_Info,
Var(List(Web_arg)),
Bool is_https) -> WEB_Session expired_session,
Var(List(WEB_Controller)) web_controllers,
Var(List(WEB_Page_Renderer)) web_page_renderers,
Var(List(WEB_Plugin)) web_plugins,
Maybe(WEB_Session) -> List(HTTP_header) additional_headers,
List(HTTP_header) constant_additional_headers,
Int timeout,
Redirections redirections,
String charset,
List(String) journal_extensions,
List(String) journal_headers,
(LogLevel, String) -> One logger, //logger
String secret,
List(MIME) known_mime_types,
(String action_name,
List(Web_arg) args) -> One before_send_file
)=
//call the initialization function
init(unique);
//
// make required directories (if needed)
//
with //web_sites_directory = (String) make_directory(my_anubis_directory+"/web_sites"),
base_directory = (String) make_directory(site_directory),
// state_directory = make_directory(site_directory+"/states"),
forget((String)make_directory(site_directory+"/public"));
//
// construct tool functions
// TODO 'make_separate_web_args_function' must retrieve state_cookies before parsing web_args if using_state_cookies is true
with save_session = make_save_session_function(timeout, state_directory, logger),
with current_page_renderer = var((WEB_Page_Renderer)default_page_renderer),
// with left_menu_list = get_left_menu_from_plugins(*web_plugins),
// retrieve_session = make_retrieve_session_function(state_directory, website_name),
//separate_web_args = make_separate_web_args_function(state_directory, retrieve_state),
//apply_action = make_apply_action_function(actions),
//
// construct the site handler
//
site_handler = (Word32 http_port, Word32 https_port) |->
((String host_name,
HTTP_Info http_info,
List(Web_arg) _lwa,
Bool is_https) |->
//(Printable_tree)
logger(logTrace,"host_name "+host_name);
logger(logTrace, dump_http_info(http_info));
logger(logTrace, dump_web_arg_values(_lwa));
with cinfo = info(host_name, http_port, https_port, site_directory, secret),
//retrieve the previous session and determine the new one
with current_session = if retrieve_session(http_info, state_directory, website_name) is
{
not_found then
logger(logInfo, "previous state not found");
initial_session(http_info, var(_lwa), is_https),
out_of_date(previous_session) then
logger(logInfo, "previous out_of_date");
//expired_session(previous_session, http_info, var(_lwa), is_https),
since previous_session is web_session(id, lang, entries, previous, _, draw),
replace_String(entries, "IP", http_header_value(http_info.http_headers, "x-forwarded-for", ip_addr_to_string(http_info.ip_address)));
web_session(id, lang, entries, web_request(http_info, var(_lwa), is_https), previous, draw)
still_valid(previous_session) then
since previous_session is web_session(id, lang, entries, previous, _, draw),
replace_String(entries, "IP", http_header_value(http_info.http_headers, "x-forwarded-for", ip_addr_to_string(http_info.ip_address)));
web_session(id, lang, entries, web_request(http_info, var(_lwa), is_https), previous, draw)
},
//apply the action according to current session
//since apply_controller_action(get_controller(get_String(lwa, "aws_controller", "root"), *web_controllers), *web_controllers, current_session)
since if get_controller(get_String(*current_session.web_request.lwa, "aws_controller", "root"), *web_controllers, logger) is
{
failure then
(failure, error_page(http_not_found, "Controller not found", current_session)),
success(new_controller) then
apply_controller_action(new_controller, *web_plugins, *web_controllers, *current_page_renderer, *web_page_renderers, cinfo, current_session, failure, initial_session, logger),
}
is (mb_new_session, http_answer),
//save the session if need and construct the according cookie
with cookie_headers =
if mb_new_session is
{
failure then [],
success(new_session) then
with session_name = save_session(new_session),
make_session_cookie_headers(website_name, session_name)
},
//formatting and send http answer because this is the last function
format(cinfo,
//state_name,
additional_headers(mb_new_session) + cookie_headers,
http_answer,
is_https,
charset)
),
//
// make the delete_out_of_date function
//
delete_out_of_date =
make_delete_out_of_date_sessions_function(site_directory+"/states", logger),
//
// construct the web site description
//
web_site((Word32 http_port, Word32 https_port) |->
web_site_description(common_names,
site_directory,
redirections,
charset,
journal_extensions,
journal_headers,
logger,
secret,
known_mime_types,
site_handler(http_port,https_port),
constant_additional_headers,
(HTTP_Info http_info, List(Web_arg) lwa) |-> unique
// if separate_web_args(lwa, http_info ) is
// swa(mb_previous_state,mb_action_name,operands) then
// if mb_action_name is
// {
// failure then unique
// success(an) then before_send_file(an,operands)
// }
//using_state_cookies
),
delete_out_of_date).
public type WEB_Controller:
web_controller(
String name, //controller name
Var(List(WEB_Action)) controller_actions, //list of all actions of that controller
(WEB_Session)-> WEB_Controller_Result error //Error renderer
)
.
define (WEB_Session)-> WEB_Controller_Result
web_controller_error
(
String name
)=
(
WEB_Session session
) |->
http_answer(session, web_controller_error_page(name)).
public define WEB_Controller
web_controller
(
String name, //controller name
Var(List(WEB_Action)) controller_actions //list of all actions of that controller
//(WEB_Session)-> WEB_Controller_Result error //Error renderer
)=
web_controller(name, controller_actions, web_controller_error(name))
.
public type WEB_Page_Renderer:
web_page_renderer(
String app, //application
String name, //renderer name
(WEB_Session _session, List(WEB_Plugin), HTML_Partial_Content _content) -> HTTP_Answer page_layout //call back for rendering page
)
.
define WEB_Page_Renderer
default_page_renderer =
web_page_renderer("AWS", "AWS_DEFAULT_PAGE",
( WEB_Session _session,
List(WEB_Plugin) _plugins,
HTML_Partial_Content _content
) |->
with _title = get_page_title(_session),
html_page
(
http_ok,
[ title(_title), ]// title of web site
,
body // body of page
(
[], //body options empty
(HTML_Off_Form)partial(_content)
)
)
)
.
define WEB_Controller_Result
apply_action
(
WEB_Session _session,
String requested_action_name,
List(WEB_Action) actions_list
) =
//println("apply_action ");
if actions_list is
{
[] then http_answer(error_page(http_not_found, "["+requested_action_name+"] Action not found in all controllers", _session)),
[h . t] then
if h.name = no_action then
http_answer( _session, html_content(http_no_content, empty))
else
with action_name = if h.name is
{
no_action then "",
controller_action(_, name) then name,
action_name(name) then name,
url(url) then url,
},
if requested_action_name = action_name then
with step = get_Int(_session.fields, "AWS_SESSION_STEP", 0) + 1,
replace_Int(_session.fields, "AWS_SESSION_STEP", step);
if h.allowed_proto is
{
//Action only in HTTP
http then
if _session.web_request.is_https then
http_answer(error_page(http_forbidden, "Only HTTP authorized", _session))
else
//ask to the server the authorization to execute that action
if h.allow(_session) then
h.do_it(_session)
else
http_answer(error_page(http_unauthorized, "you are not authorized for this action", _session))
//Action only in HTTPS
https then
if _session.web_request.is_https then
//ask to the server the authorization to execute that action
if h.allow(_session) then
h.do_it(_session)
else
http_answer(error_page(http_unauthorized, "you are not authorized for this action", _session))
else
http_answer(error_page(http_forbidden, "Only HTTPS authorized", _session))
//Action in both HTTP / HTTPS
http_https then
//ask to the server the authorization to execute that action
if h.allow(_session) then
//println("do_it "+action_name);
h.do_it(_session)
else
http_answer(error_page(http_unauthorized, "you are not authorized for this action", _session))
}
else
apply_action(_session, requested_action_name, t)
}
.
define Maybe(WEB_Controller)
get_controller
(
String controller_name,
List(WEB_Controller) controllers,
(LogLevel, String) -> One logger //logger
)=
if controllers is
{
[] then
logger(logWarning, "Can't find controller ["+controller_name+"]");
failure,
[h . t] then
if h.name = controller_name then
//println("Controller ["+controller_name+"] found");
success(h)
else
get_controller(controller_name, t, logger)
}
.
define WEB_Page_Renderer
get_page_renderer
(
String renderer_name,
List(WEB_Page_Renderer) renderers,
(LogLevel, String) -> One logger //logger
)=
if renderers is
{
[] then
logger(logWarning, "Can't find page_renderer ["+renderer_name+"], use the default");
default_page_renderer,
[h . t] then
if h.name = renderer_name then
//println("renderer_name ["+renderer_name+"] found");
h
else
//println("renderer_name "+renderer_name+" not matching");
get_page_renderer(renderer_name, t, logger)
}
.
define WEB_Page_Renderer
get_page_renderer
(
WEB_Session _session,
WEB_Page_Renderer _current_page_renderer,
List(WEB_Page_Renderer) renderers,
(LogLevel, String) -> One logger //logger
)=
//get the renderer page name to use from the session "AWS_PAGE_RENDERER"
if get_String(_session.fields, "AWS_CURRENT_PAGE_RENDERER") is
{
failure then logger(logWarning, "No renderer defined, use the default"); default_page_renderer,
success(renderer_name) then
//check if the renderer_page already in cache
if renderer_name = _current_page_renderer.name then
_current_page_renderer
else
//if not in cache, use the selector function
get_page_renderer(renderer_name, renderers, logger)
}
.
define (Maybe(WEB_Session), HTTP_Answer)
apply_controller_action
(
WEB_Controller controller,
List(WEB_Plugin) plugins,
List(WEB_Controller) controllers,
WEB_Page_Renderer _current_page_renderer,
List(WEB_Page_Renderer) page_renderers,
CommonInfo cinfo,
WEB_Session _session,
Maybe(String) _mb_action_name,
(HTTP_Info,
Var(List(Web_arg)),
Bool is_https) -> WEB_Session initial_session,
(LogLevel, String) -> One logger //logger
)=
//get the action name
with mb_action_name =
if _mb_action_name is
{
failure then get_String(*_session.web_request.lwa, "aws_action"),
success(_an) then success(_an)
},
if mb_action_name is
{
failure then
//(failure, error_page(http_not_found, "Action name failure", _session)),
with new_session = initial_session(_session.web_request.http_info, _session.web_request.lwa, _session.web_request.is_https),
if get_controller(get_String(*new_session.web_request.lwa, "aws_controller", "root"), controllers, logger) is
{
failure then (failure, error_page(http_not_found, "Initial Session Controller not found", new_session)),
success(new_controller) then apply_controller_action(new_controller, plugins, controllers, _current_page_renderer, page_renderers, cinfo, new_session, failure, initial_session, logger),
},
success(action_name) then
//logger(logInfo, "ACTION_NAME ["+action_name+"] from IP "+get_String(_session.fields, "IP", ""));
if apply_action(_session, action_name, *controller.controller_actions) is
{
http_answer(session, answer) then
since session is web_session(id, lang, entries, current, previous, _),
with new_web_session = web_session(id, lang, entries, current, previous, current),
// println("***** http_answer(session, answer) record new draw point to *****");
// println("web arguments:"+
// dump_web_arg_values(*current.lwa));
// println("************************************");
(success(new_web_session), answer),
http_answer(answer) then
since _session is web_session(id, lang, entries, current, previous, _),
with new_web_session = web_session(id, lang, entries, current, previous, current),
// println("***** http_answer(answer) record new draw point to *****");
// println("web arguments:"+
// dump_web_arg_values(*current.lwa));
// println("************************************");
(success(new_web_session), answer),
redirect(new_session) then
if get_controller(get_String(*new_session.web_request.lwa, "aws_controller", "root"), controllers, logger) is
{
failure then (failure, error_page(http_not_found, "Redirect Controller not found", new_session)),
success(new_controller) then apply_controller_action(new_controller, plugins, controllers, _current_page_renderer, page_renderers, cinfo, new_session, failure, initial_session, logger),
},
redirect(new_session, r_controller_name, r_action_name) then
if get_controller(r_controller_name, controllers, logger) is
{
failure then (failure, error_page(http_not_found, "Redirect Controller "+r_controller_name+" not found", new_session)),
success(new_controller) then
apply_controller_action(new_controller, plugins, controllers, _current_page_renderer, page_renderers, cinfo, new_session, success(r_action_name), initial_session, logger),
},
redirect_to_previous then
if get_controller(get_String(*_session.previous_web_request.lwa, "aws_controller", "root"), controllers, logger) is
{
failure then (failure, error_page(http_not_found, "Redirect to previous Controller not found", _session)),
success(new_controller) then
since _session is web_session(id, lang, entries, _, previous, draw),
apply_controller_action(new_controller, plugins, controllers, _current_page_renderer, page_renderers, cinfo, web_session(id, lang, entries, previous, previous, draw), failure, initial_session, logger),
},
redirect_to_previous(entries) then
if get_controller(get_String(*_session.previous_web_request.lwa, "aws_controller", "root"), controllers, logger) is
{
failure then (failure, error_page(http_not_found, "Redirect to previous Controller not found", _session)),
success(new_controller) then
since _session is web_session(id, lang, _, _, previous, draw),
apply_controller_action(new_controller, plugins, controllers, _current_page_renderer, page_renderers, cinfo, web_session(id, lang, entries, previous, previous, draw), failure, initial_session, logger),
},
//ajax(answer) then (failure, answer),
//ajax with modified session that must be saved
ajax(session, answer) then (session, answer),
//ajax(content, additional_script) then (failure, json(http_ok, to_html_ajax_content(content, cinfo, additional_script))),
ajax(session, content, additional_script) then (session, json(http_ok, to_html_ajax_content(content, cinfo, additional_script))),
//ajax(content, additional_script) then (failure, json(http_ok, to_html_ajax_content(content, additional_script))),
ajax(session, content, additional_script) then (session, json(http_ok, to_html_ajax_content(content, additional_script))),
send_file(file_path, c_disposition) then (failure, send_file(file_path, c_disposition)),
renderer_content(session, content) then
with the_session = if session is {failure then _session, success(__session) then __session},
since the_session is web_session(id, lang, entries, current, previous, _),
with new_web_session = web_session(id, lang, entries, current, previous, current),
// println("***** renderer_content(session, content) record new draw point to *****");
// println("web arguments:"+
// dump_web_arg_values(*current.lwa));
// println("************************************");
with page_renderer = get_page_renderer(new_web_session, _current_page_renderer, page_renderers, logger),
(success(new_web_session), page_renderer.page_layout(new_web_session, plugins, content))
}
}
.
public define Web_Site
make_web_site_controller_description
(
String website_name, // site_UID
List(String) common_names, // for example: ["www.our-business.com"]
String site_directory,
String state_directory,
One -> One init,
(HTTP_Info,
Var(List(Web_arg)),
Bool is_https) -> WEB_Session initial_session,
(WEB_Session,
HTTP_Info,
Var(List(Web_arg)),
Bool is_https) -> WEB_Session expired_session,
Var(List(WEB_Controller)) web_controllers,
Var(List(WEB_Page_Renderer)) web_page_renderers,
Var(List(WEB_Plugin)) web_plugins,
Maybe(WEB_Session) -> List(HTTP_header) additional_headers,
List(HTTP_header) constant_additional_headers,
Int timeout,
Redirections redirections,
String charset,
List(String) journal_extensions,
List(String) journal_headers,
(LogLevel, String) -> One logger, //logger
String secret,
List(MIME) known_mime_types,
(String action_name,
List(Web_arg) args) -> One before_send_file
)=
//call the initialization function
init(unique);
//
// make required directories (if needed)
//
with //web_sites_directory = (String) make_directory(my_anubis_directory+"/web_sites"),
base_directory = (String) make_directory(site_directory),
// state_directory = make_directory(site_directory+"/states"),
forget((String)make_directory(site_directory+"/public"));
//
// construct tool functions
// TODO 'make_separate_web_args_function' must retrieve state_cookies before parsing web_args if using_state_cookies is true
with save_session = make_save_session_function(timeout, state_directory, logger),
with current_page_renderer = var((WEB_Page_Renderer)default_page_renderer),
// with left_menu_list = get_left_menu_from_plugins(*web_plugins),
// retrieve_session = make_retrieve_session_function(state_directory, website_name),
//separate_web_args = make_separate_web_args_function(state_directory, retrieve_state),
//apply_action = make_apply_action_function(actions),
//
// construct the site handler
//
site_handler = (Word32 http_port, Word32 https_port) |->
((String host_name,
HTTP_Info http_info,
List(Web_arg) _lwa,
Bool is_https) |->
//(Printable_tree)
logger(logTrace,"host_name "+host_name);
logger(logTrace, dump_http_info(http_info));
logger(logTrace, dump_web_arg_values(_lwa));
with cinfo = info(host_name, http_port, https_port, site_directory, secret),
//retrieve the previous session and determine the new one
with current_session = if retrieve_session(http_info, state_directory, website_name) is
{
not_found then
logger(logInfo, "previous state not found");
initial_session(http_info, var(_lwa), is_https),
out_of_date(previous_session) then
logger(logInfo, "previous out_of_date");
//expired_session(previous_session, http_info, var(_lwa), is_https),
since previous_session is web_session(id, lang, entries, previous, _, draw),
replace_String(entries, "IP", http_header_value(http_info.http_headers, "x-forwarded-for", ip_addr_to_string(http_info.ip_address)));
web_session(id, lang, entries, web_request(http_info, var(_lwa), is_https), previous, draw)
still_valid(previous_session) then
since previous_session is web_session(id, lang, entries, previous, _, draw),
replace_String(entries, "IP", http_header_value(http_info.http_headers, "x-forwarded-for", ip_addr_to_string(http_info.ip_address)));
web_session(id, lang, entries, web_request(http_info, var(_lwa), is_https), previous, draw)
},
//apply the action according to current session
//since apply_controller_action(get_controller(get_String(lwa, "aws_controller", "root"), *web_controllers), *web_controllers, current_session)
since if get_controller(get_String(*current_session.web_request.lwa, "aws_controller", "root"), *web_controllers, logger) is
{
failure then
(failure, error_page(http_not_found, "Controller not found", current_session)),
success(new_controller) then
apply_controller_action(new_controller, *web_plugins, *web_controllers, *current_page_renderer, *web_page_renderers, cinfo, current_session, failure, initial_session, logger),
}
is (mb_new_session, http_answer),
//save the session if need and construct the according cookie
with cookie_headers =
if mb_new_session is
{
failure then [],
success(new_session) then
with session_name = save_session(new_session),
make_session_cookie_headers(website_name, session_name)
},
//formatting and send http answer because this is the last function
format(cinfo,
//state_name,
additional_headers(mb_new_session) + cookie_headers,
http_answer,
is_https,
charset)
),
//
// make the delete_out_of_date function
//
delete_out_of_date =
make_delete_out_of_date_sessions_function(site_directory+"/states", logger),
//
// construct the web site description
//
web_site((Word32 http_port, Word32 https_port) |->
web_site_description(common_names,
site_directory,
redirections,
charset,
journal_extensions,
journal_headers,
logger,
secret,
known_mime_types,
site_handler(http_port,https_port),
constant_additional_headers,
(HTTP_Info http_info, List(Web_arg) lwa) |-> unique
// if separate_web_args(lwa, http_info ) is
// swa(mb_previous_state,mb_action_name,operands) then
// if mb_action_name is
// {
// failure then unique
// success(an) then before_send_file(an,operands)
// }
//using_state_cookies
),
delete_out_of_date).