-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsrv.html
1212 lines (1202 loc) · 49.8 KB
/
srv.html
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
<html dir="ltr">
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type"/>
<title>Arc: Web server</title>
<link rel="shortcut icon" href="/assets/favicon.png">
<link rel="stylesheet" type="text/css" href="code.css">
<link href="/assets/bootstrap.css" rel="stylesheet">
</head>
<body style='margin:12px 50px 0'>
<div class="navbar navbar-inverse">
<div class="navbar-inner">
<ul class="nav navbar-nav">
<li><a href="/ref/">Arc 3.1</a>
<li><a href="http://tryarc.org">Try it</a></li>
<li><a href="http://github.com/arclanguage/anarki">Get it</a></li>
<li><a href="http://ycombinator.com/arc/tut.txt">Tutorial</a></li>
<li><a href="http://arclanguage.org/forum">Forum</a></li>
</ul>
</div>
</div> <!-- end of navbar -->
<div class="links">Previous: <a href="html.html">HTML generation</a>
Up: <a href="index.html">Contents</a>
Next: <a href="app.html">The Arc application server</a>
</div>
<h1 class="links">Web server</h1>
Arc includes a web server with several interesting features, including a continuation-based design. The web server includes many different operations to create forms and links with associated continuations.
<p>
The Arc distribution comes with sample web applications including a new site (<code>news.arc</code>) and a blog (<code>blog.arc</code>).
<h2>Running the server</h2>
The server can be started simply with
<pre class="repl">
arc>(serve 8080)
</pre>
<p>
However, it is generally better to start the server in a separate thread, so the Arc REPL can be used. This allows the web server to be modified while it is running.
<pre class="repl">
arc> (thread (serve 8080))
</pre>
A handler can be associated with a URL using the <code>defop</code> macro; this defines a handler function, taking the <code>req</code> parameter:
<pre class="code">
(defop hello req (prn "Hello world!"))
</pre>
The resulting page can be accessed at <a href="http://localhost:8080/hello">http://localhost:8080/hello</a>.
<p>
The Arc web server is rather fragile and platform-dependent. If you encounter problems, the easiest solution is to use the unofficial <a href="http://arcfn.com/2008/02/git-and-anarki-arc-repository-brief.html">Anarki version</a>, which has multiple patches.
<p>
To define a top-level page (<a href="http://localhost:8080">http://localhost:8080</a>), use the page name <code>||</code> (the MzScheme representation of the empty symbol, between <a href="http://download.plt-scheme.org/doc/372/html/mzscheme/mzscheme-Z-H-11.html#node_sec_11.2.4">quoting vertical bars</a>).
<pre class="code">
(defop || req (pr "This is the home page."))
</pre>
<p>
To redirect a page to a different page, the <code>defopr</code> macro is used, with a function that outputs the name of the target page. For example, to redirect <a href="http://localhost:8080/index.html">http://localhost:8080/index.html</a> to the earlier "hello" page:
<pre class="code">
(defopr index.html req (prn "hello"))
</pre>
<p>
The <code>req</code> parameter receives a table that has the field <code>ip</code> holding the IP address of the client, and potentially <code>cooks</code> holding the cookies, and <code>args</code> holding a list of key value pairs from the URL's query string.
<p>
The <code>defop-raw</code> and <code>defopr-raw</code> macros let the handler function add HTTP headers. The handler must then output a blank line followed by the HTML content or redirect path. One use of this is to add cookies to the headers.
<pre class="code">
(defop-raw bar (str req) (w/stdout str
(prn "Set-Cookie: mycookie=42")
(prn)
(prn (req 'ip)) (br) (prn (req 'cooks)) (br) (prn (req 'args))))
</pre>
On the second reload (after the cookie gets assigned), <a href="http://localhost:8080/bar?x=1&y=2&z">http://localhost:8080/bar?x=1&y=2&z</a> will display:
<pre class="code">
127.0.0.1
((mycookie 42))
((x 1) (y 2) (z ))
</pre>
This illustrates how the handler can access the client's IP address, the cookies, and the URL query parameters. The handle does not have access to other HTTP headers.
<p>
This functionality can be used to implement a simple form and form handler; the form is at <a href="http://localhost:8080/myform">http://localhost:8080/myform</a>. This example uses some of the <a href="html.html">HTML functions</a>.
<pre class="code">
(defop myform req (form "myhandler" (single-input "Enter:" 'foo 10 "Submit")))
(defop myhandler req (prn "You entered") (prbold (alref (req 'args) "foo")))
</pre>
<p>
Several types of output functions are supported by Arc. The simplest is a function that outputs HTML. A function can also optionally output additional HTTP headers. Arc also supports redirect functions that can perform server-side operations and then redirect the browser to a new page; these functions can optionally output additional headers. Finally, Arc has partial support for asynchronous functions, which don't return any response to the request.
<p>
The following table shows the macros for generating arbitrary server-side handlers of the given types.
<table width=100% class="info">
<tr><th width=16%> </th><th width=16%>HTML</th><th width=16%>Headers + HTML</th><th width=16%>Redirect</th><th width=16%>Headers + Redirect</th></tr>
<tr>
<th>Macro</th>
<td><code>defop</code></td>
<td><code>defop-raw</code></td>
<td><code>defopr</code></td>
<td><code>defopr-raw</code></td>
</tr></table>
<h2>Continuations</h2>
The previous section showed how to implement basic HTML and redirect handlers in Arc.
Arc's web server also provides continuation support, which is typically used for handlers. That is, links and forms can be assigned a function that will be executed on the server if the link or form is clicked.
(The continuation would be called a callback in some languages.)
<p>
The continuations used by the web server are explicit function. (The web server does not use <code>ccc</code> first-class continuations.) The functions take a request object as argument and print the appropriate response. By using closures, the functions can maintain state across requests; the state will be included in the closure when the function is defined, and the function can access the state when it is later invoked.
<p>
The web server includes many operations to associate continuation functions and fnids with links and forms.
The web server contains operations to generate links and forms of the various types, as shown in the following table.
<p>
The previous example can be implemented with continuations as follows:
<pre class="code">
(defop myform2 req (aform myfunc (single-input "Enter:" 'foo 10 "Submit")))
(def myfunc (req) (prn "You entered") (prbold (alref (req 'args) "foo")))
</pre>
Generally, the continuation function is defined within the original form definition, so a closure can be created. (In this case, the closure is unnecessary, as no state from the first operation is preserved.)
<pre class="code">
(defop myform2 req (aform
[do (prn "You entered") (prbold (alref (_ 'args) "foo"))]
(single-input "Enter:" 'foo 10 "Submit")))
</pre>
<p>
Internally, the web server associates a token called the <code>fnid</code> with each instance of a link or form, and records the continuation function for each token. Periodically, old fnids are deleted.
<p>
The following table illustrates the operations to generate links, URLs, or forms, for the four different types of output functions. (In practice, both <code>arform</code> and <code>arformh</code> give access to the headers.)
<table width=100% class="info">
<tr><th width=16%> </th><th width=16%>HTML</th><th width=16%>Headers + HTML</th><th width=16%>Redirect</th><th width=16%>Headers + Redirect</th></tr>
<tr>
<th>Link generation</th>
<td><code>w/link, w/link-if, onlink, linkf</code></td>
<td><code></code></td>
<td><code>w/rlink, rlinkf</code></td>
<td><code></code></td>
</tr>
<tr>
<th>URL generation</th>
<td><code>flink, url-for</code></td>
<td><code></code></td>
<td><code>rflink</code></td>
<td><code></code></td>
</tr>
<tr>
<th>Form generation</th>
<td><code>aform, timed-aform</code></td>
<td><code>aformh</code></td>
<td><code>arform</code></td>
<td><code>arformh</code></td>
</tr>
</table>
<p>
Different operations use one of five different mechanisms to specify the continuation function. The function may be specified as an expression, a function of one variable (the request object), a request parameter and body, output stream and request parameters and a body, or a body alone. The documentation should be consulted to see which mechanism goes with which function.
<h2>Explanation of the "Arc Challenge" solution</h2>
The <a href="http://www.paulgraham.com/arcchallenge.html">Arc Challenge</a> posed the problem of implementing a simple web function. Under the URL "/said", it provides a form with input field. When submitted, the form goes to a page with a link "click here". The link leads to a page with the text "You said: " followed by the original input.
<p>
The solution in Arc is as follows:
<pre class="code">
(defop said req
(aform [w/link (pr "you said: " (arg _ "foo"))
(pr "click here")]
(input "foo")
(submit)))
</pre>
The solution may be easier to understand if the continuation functions are made explicit:
<pre class="code">
(defop said req
(aform form-continuation
(input "foo")
(submit)))
(def form-continuation (req)
(w/link
(pr "you said: " (arg req "foo")) ;link continuation expression
(pr "click here")))
</pre>
The <code>said</code> operation creates a form that when submitted
executes the <code>form-continuation</code> function. The continuation function
creates a link that will execute the link continuation expression.
The important thing to notice is the link continuation expression is defined
inside <code>form-continuation</code>, which results in a closure with the
<code>req</code> object from <code>form-continuation</code>. That is,
when the link continuation executes, potentially a long time after
<code>form-continuation</code> completes, it will still have the state.
This illustrates how closures can be used to carry state from one request
to another.
The form can be executed multiple times, and each execution will have a unique state in the link continuation closure.
<h2>Web server</h2>
<p><table class='arc'>
<tr>
<td class='arc'><a name='defop'></a>
<img src='macro.gif' title='Macro'/>
<span class='op'>defop</span> <span class='args'>name req-param [body ...]</span>
<div class='desc'>Defines a HTML response handler for the URL path <code>name</code>. The handler body takes one parameter, the <code>req</code> object and should output to <code>stdout</code>.</div>
</td>
<td class='arc'><pre>
>(defop foo req (prn "Hello"))
<span class="return">#<procedure: gs2265>
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='defop-raw'></a>
<img src='macro.gif' title='Macro'/>
<span class='op'>defop-raw</span> <span class='args'>name (outstream-param req-param) [body ...]</span>
<div class='desc'>Defines a HTML response handler for the URL path <code>name</code>. The handler body takes two parameters: the output stream and the <code>req</code> object. The handler can output additional HTTP headers.</div>
</td>
<td class='arc'><pre>
>(defop-raw fooraw (str req)
(w/stdout str (prn "X-Arc: foo") (prn) (prn "Hello")))
<span class="return">#<procedure: gs2271>
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='defopr'></a>
<img src='macro.gif' title='Macro'/>
<span class='op'>defopr</span> <span class='args'>name req-param [body ...]</span>
<div class='desc'>Defines a redirect response handler for the URL path <code>name</code>. The handler body takes one parameter, the <code>req</code> object and should output to <code>stdout</code>.</div>
</td>
<td class='arc'><pre>
>(defopr bar req (prn "foo"))
<span class="return">#<procedure: gs2282>
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='defopr-raw'></a>
<img src='macro.gif' title='Macro'/>
<span class='op'>defopr-raw</span> <span class='args'>name (outstream-param req-param) [body ...]</span>
<div class='desc'>Defines a redirect response handler for the URL path <code>name</code>. The handler body takes two parameters: the output stream and the <code>req</code> object. The handler can output additional HTTP headers.</div>
</td>
<td class='arc'><pre>
>(defopr-raw baz (str req)
(disp "Set-Cookie: this=that\n" str) "foo")
<span class="return">#<procedure: gs2291>
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='aform'></a>
<img src='macro.gif' title='Macro'/>
<span class='op'>aform</span> <span class='args'>f [body ...]</span>
<div class='desc'>Generates a form from <code>body</code>. <code>f</code> is a continuation function to execute when the form is submitted.</div>
</td>
<td class='arc'><pre>
>(defop af rq (aform (fn (req) (prn req)) (submit)))
<span class="return">#<procedure: gs2301>
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='fnform'></a>
<img src='macro.gif' title='Macro'/>
<span class='op'>fnform</span> <span class='args'>f bodyfn [redir]</span>
<div class='desc'>Generates a form from a body function. <code>f</code> is a continuation function to execute when the form is submitted.</div>
</td>
<td class='arc'><pre>
>(defop af rq (fnform (fn (req) (fn (_) (prn req))) (submit)))
<span class="return">#<procedure: gs2313>
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='timed-aform'></a>
<img src='macro.gif' title='Macro'/>
<span class='op'>timed-aform</span> <span class='args'>lasts f [body ...]</span>
<div class='desc'>Generates a form. The <code>fnid</code> has a lifetime of <code>lasts</code> seconds.</div>
</td>
<td class='arc'><pre>
>(defop taf rq (timed-aform 10 (fn (req) (prn req)) (submit)))
<span class="return">#<procedure: gs2324>
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='timed-arform'></a>
<img src='macro.gif' title='Macro'/>
<span class='op'>timed-arform</span> <span class='args'>lasts f [body ...]</span>
<div class='desc'>Generates a form. Like timed-aform except with a redirect. New in arc3.</div>
</td>
<td class='arc'><pre>
>(defop taf rq (timed-arform 10 (fn (req) (prn req)) (submit)))
<span class="return">#<procedure: gs2335>
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='arform'></a>
<img src='macro.gif' title='Macro'/>
<span class='op'>arform</span> <span class='args'>f [body ...]</span>
<div class='desc'>Generates a form that goes to a redirect. The callback function <code>f</code> must return the redirect target.</div>
</td>
<td class='arc'><pre>
>(defop arf rq (arform (fn (req) "foo") (prn "Click:") (submit)))
<span class="return">#<procedure: gs2346>
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='aformh'></a>
<img src='macro.gif' title='Macro'/>
<span class='op'>aformh</span> <span class='args'>f [body ...]</span>
<div class='desc'>Generates a form with additional headers.</div>
</td>
<td class='arc'><pre>
>(defop afh rq (aformh
(fn (req) (prn "Set-Cookie: a=b\n") (prn req))
(prn "Click:") (submit)))
<span class="return">#<procedure: gs2357>
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='arformh'></a>
<img src='macro.gif' title='Macro'/>
<span class='op'>arformh</span> <span class='args'>f [body ...]</span>
<div class='desc'>Generates a form that goes to a redirect with additional headers.</div>
</td>
<td class='arc'><pre>
>(defop arfh rq (arform (fn (req) (prn "Set-Cookie: a=b") "foo")
(prn "Click:") (submit)))
<span class="return">#<procedure: gs2368>
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='url-for'></a>
<img src='proc.gif' title='Procedure'/>
<span class='op'>url-for</span> <span class='args'>fnid</span>
<div class='desc'>Generates a URL linking to the given <code>fnid</code>, using <code>fnurl*</code>.</div>
</td>
<td class='arc'><pre>
>(url-for '1234)
<span class="return">"/x?fnid=1234"
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='afnid'></a>
<img src='macro.gif' title='Macro'/>
<span class='op'>afnid</span> <span class='args'>f</span>
<div class='desc'>Generate anaphoric <code>fnid</code>. The continuation function <code>f</code> can access the <code>fnid</code> as the variable <code>it</code>. <code>f</code> must print a blank line before the HTML content.</div>
</td>
<td class='arc'><pre>
>(afnid (fn (req) (prn "\nFnid is " it)))
<span class="return">ApzY33zZEd
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='flink'></a>
<img src='proc.gif' title='Procedure'/>
<span class='op'>flink</span> <span class='args'>f</span>
<div class='desc'>Generates a URL that will run the continuation function <code>f</code>. It creates a <code>fnid</code> to do this.</div>
</td>
<td class='arc'><pre>
>(flink (fn (req) (prn "hi")))
<span class="return">"/x?fnid=DltGUEIJsK"
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='rflink'></a>
<img src='proc.gif' title='Procedure'/>
<span class='op'>rflink</span> <span class='args'>f</span>
<div class='desc'>Generates a URL that will run the redirect continuation function <code>f</code>. It creates a <code>fnid</code> to do this.</div>
</td>
<td class='arc'><pre>
>(rflink (fn (req) (prn "Header: x") "foo"))
<span class="return">"/r?fnid=wzWRwQ3LcI"
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='w/link'></a>
<img src='macro.gif' title='Macro'/>
<span class='op'>w/link</span> <span class='args'>expr [body ...]</span>
<div class='desc'>Wraps <code>body</code> in a link to continuation expression <code>expr</code>.</div>
</td>
<td class='arc'><pre>
>(defop wl req (w/link (prn "You clicked.") (prn "Click here")))
<span class="return">#<procedure: gs2386>
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='w/link-if'></a>
<img src='macro.gif' title='Macro'/>
<span class='op'>w/link-if</span> <span class='args'>test expr [body ...]</span>
<div class='desc'>If <code>test</code> is true, wraps <code>body</code> in a link to continuation expression <code>expr</code>. Otherwise, just displays <code>body</code> without the link.</div>
</td>
<td class='arc'><pre>
>(defop wli req (w/link-if t (prn "You clicked.")
(prn "Click here")))
<span class="return">#<procedure: gs2398>
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='w/rlink'></a>
<img src='macro.gif' title='Macro'/>
<span class='op'>w/rlink</span> <span class='args'>expr [body ...]</span>
<div class='desc'>Wraps <code>body</code> in a link to redirect continuation expression <code>expr</code>.</div>
</td>
<td class='arc'><pre>
>(defop wrl req (w/rlink "foo" (prn "Click here")))
<span class="return">#<procedure: gs2410>
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='onlink'></a>
<img src='macro.gif' title='Macro'/>
<span class='op'>onlink</span> <span class='args'>text [body ...]</span>
<div class='desc'>Creates a link with contents <code>text</code> that will execute continuation expression <code>body</code>. Note that <code>onlink</code> reverses the roles of the arguments compared to <code>w/link</code>.</div>
</td>
<td class='arc'><pre>
>(defop ol req (onlink "Click here" (prn "You clicked.")))
<span class="return">#<procedure: gs2422>
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='onrlink'></a>
<img src='macro.gif' title='Macro'/>
<span class='op'>onrlink</span> <span class='args'>text [body ...]</span>
<div class='desc'>Creates a rlink with contents <code>text</code> that will execute continuation expression <code>body</code>. New in arc3.</div>
</td>
<td class='arc'><pre>
>(defop ol req (onrlink "Click here" (prn "You clicked.")))
<span class="return">#<procedure: gs2434>
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='linkf'></a>
<img src='macro.gif' title='Macro'/>
<span class='op'>linkf</span> <span class='args'>text parms [body ...]</span>
<div class='desc'>Creates a link with contents <code>text</code> to run the continuation function specified by <code>params</code> and <code>body</code>. Params should be a single argument such as <code>(req)</code>.</div>
</td>
<td class='arc'><pre>
>(defop lf rq (linkf "Click here" (req) (prn "Your request: " req)))
<span class="return">#<procedure: gs2446>
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='rlinkf'></a>
<img src='macro.gif' title='Macro'/>
<span class='op'>rlinkf</span> <span class='args'>text parms [body ...]</span>
<div class='desc'>Creates a link with contents <code>text</code> to run the redirection continuation function specified by <code>params</code> and <code>body</code>. Params should be a single argument such as <code>(req)</code>.</div>
</td>
<td class='arc'><pre>
>(defop rlf rq (rlinkf "Click here" (req) "foo"))
<span class="return">#<procedure: gs2457>
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='arg'></a>
<img src='proc.gif' title='Procedure'/>
<span class='op'>arg</span> <span class='args'>req key</span>
<div class='desc'>Looks up the URL query value for <code>key</code> in <code>req</code>.</div>
</td>
<td class='arc'><pre>
>(let req (obj args '((foo 1)))
(arg req 'foo))
<span class="return">1
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='serve'></a>
<img src='proc.gif' title='Procedure'/>
<span class='op'>serve</span> <span class='args'>[port]</span>
<div class='desc'>Start server, by default on port 8080.</div>
</td>
<td class='arc'><pre>
>(serve 8080)
</pre>
</td></tr>
<tr>
<td class='arc'><a name='srvlog'></a>
<img src='proc.gif' title='Procedure'/>
<span class='op'>srvlog</span> <span class='args'>type [args ...]</span>
<div class='desc'>Records timestamp and <code>args</code> in the server log <code>type</code>.</div>
</td>
<td class='arc'><pre>
>(srvlog 'blog "Stuff to log")
<span class="stdout">Error: open-output-file: cannot open output file
path: /pa
th/to/doc/arc/logs/blog-2018-08-26
system error: No such f
ile or directory; errno=2
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='defbg'></a>
<img src='proc.gif' title='Procedure'/>
<span class='op'>defbg</span> <span class='args'>id sec [body ...]</span>
<div class='desc'>Creates a background thread with the given id tha will run the body every sec seconds. Any existing thread with the same id is terminated. New in arc3.</div>
</td>
<td class='arc'> </td></tr>
</table>
<h2>Web server configuration variables</h2>
<p><table class='arc'>
<tr>
<td class='arc'><a name='arcdir*'></a>
<img src='var.gif' title='Variable'/>
<span class='op'>arcdir*</span> <span class='args'></span>
<div class='desc'>Parent directory for server data.</div>
</td>
<td class='arc'><pre>
>arcdir*
<span class="return">"arc/"
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='logdir*'></a>
<img src='var.gif' title='Variable'/>
<span class='op'>logdir*</span> <span class='args'></span>
<div class='desc'>Directory to hold server logs.</div>
</td>
<td class='arc'><pre>
>logdir*
<span class="return">"arc/logs/"
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='staticdir*'></a>
<img src='var.gif' title='Variable'/>
<span class='op'>staticdir*</span> <span class='args'></span>
<div class='desc'>Directory to hold static files. New in arc3.</div>
</td>
<td class='arc'><pre>
>staticdir*
<span class="return">"static/"
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='static-max-age*'></a>
<img src='var.gif' title='Variable'/>
<span class='op'>static-max-age*</span> <span class='args'></span>
<div class='desc'>Maximum age for static files; used in Cache-Control header. New in arc3.</div>
</td>
<td class='arc'><pre>
>static-max-age*
<span class="return">nil
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='max-age*'></a>
<img src='var.gif' title='Variable'/>
<span class='op'>max-age*</span> <span class='args'></span>
<div class='desc'>Table of maximum age by operation; used in Cache-Control header. New in arc3.</div>
</td>
<td class='arc'><pre>
>max-age*
<span class="return">#hash()
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='quitsrv*'></a>
<img src='var.gif' title='Variable'/>
<span class='op'>quitsrv*</span> <span class='args'></span>
<div class='desc'>Flag to shut down server if set to true.</div>
</td>
<td class='arc'><pre>
>quitsrv*
<span class="return">nil
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='breaksrv*'></a>
<img src='var.gif' title='Variable'/>
<span class='op'>breaksrv*</span> <span class='args'></span>
<div class='desc'>Flag to enable stopping server with control-C.</div>
</td>
<td class='arc'><pre>
>breaksrv*
<span class="return">nil
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='srv-noisy*'></a>
<img src='var.gif' title='Variable'/>
<span class='op'>srv-noisy*</span> <span class='args'></span>
<div class='desc'>Flag to enable debug logging in the server.</div>
</td>
<td class='arc'><pre>
>srv-noisy*
<span class="return">nil
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='threadlife*'></a>
<img src='var.gif' title='Variable'/>
<span class='op'>threadlife*</span> <span class='args'></span>
<div class='desc'>Maximum time (in seconds) for a thread to handle a request before being killed.</div>
</td>
<td class='arc'><pre>
>threadlife*
<span class="return">30
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='throttle-ips*'></a>
<img src='var.gif' title='Variable'/>
<span class='op'>throttle-ips*</span> <span class='args'></span>
<div class='desc'>Table of IP addresses that should be throttled; they will be delayed by up to <code>throttle-time*</code> seconds.</div>
</td>
<td class='arc'><pre>
>throttle-ips*
<span class="return">#hash()
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='throttle-time*'></a>
<img src='var.gif' title='Variable'/>
<span class='op'>throttle-time*</span> <span class='args'></span>
<div class='desc'>Maximum number of seconds to delay requests from IP addresses in <code>throttle-ips*</code></div>
</td>
<td class='arc'><pre>
>throttle-time*
<span class="stdout">Error: _throttle-time*: undefined;
cannot reference an iden
tifier before its definition
in module: top-level
intern
al name: _throttle-time*
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='ignore-ips*'></a>
<img src='var.gif' title='Variable'/>
<span class='op'>ignore-ips*</span> <span class='args'></span>
<div class='desc'>IP addresses to ignore. New in arc3.</div>
</td>
<td class='arc'><pre>
>ignore-ips*
<span class="return">#hash()
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='spurned*'></a>
<img src='var.gif' title='Variable'/>
<span class='op'>spurned*</span> <span class='args'></span>
<div class='desc'>Count of ignored IP addresses by address. New in arc3.</div>
</td>
<td class='arc'><pre>
>spurned*
<span class="return">#hash()
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='req-limit*'></a>
<img src='var.gif' title='Variable'/>
<span class='op'>req-limit*</span> <span class='args'></span>
<div class='desc'>Maximum number of requests in req-window* seconds before triggering DoS attack detection. New in arc3.</div>
</td>
<td class='arc'><pre>
>req-limit*
<span class="return">30
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='req-window*'></a>
<img src='var.gif' title='Variable'/>
<span class='op'>req-window*</span> <span class='args'></span>
<div class='desc'>Time window in seconds for triggering throttling. New in arc3.</div>
</td>
<td class='arc'><pre>
>req-window*
<span class="return">10
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='dos-window*'></a>
<img src='var.gif' title='Variable'/>
<span class='op'>dos-window*</span> <span class='args'></span>
<div class='desc'>Time window in seconds for triggering DoS attack detection. New in arc3.</div>
</td>
<td class='arc'><pre>
>dos-window*
<span class="return">2
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='opcounts*'></a>
<img src='var.gif' title='Variable'/>
<span class='op'>opcounts*</span> <span class='args'></span>
<div class='desc'>Table counting number of times each operation executed. New in arc3.</div>
</td>
<td class='arc'><pre>
>opcounts*
<span class="return">#hash()
</span></pre>
</td></tr>
</table>
<h2>Web server internals</h2>
The key stages in the life cycle of a request are:
<ul>
<li> <code>handle-request</code> handles one request. It accepts a connection on socket <code>s</code>, starts a thread running <code>handle-request-thread</code>, and lets the thread run for <code>life</code> seconds.
</li> handle-request-thread is the thread to handle a request. It reads the header, calls <code>parseheader</code>, <code>srvlog</code>, <code>respond</code> (get) or <code>handle-post</code> (post) or <code>respond-err</code>. Finally, it executes <code>harvest-fnids</code> when done.
<li>
One key component of the web server is <code>(respond str op args cooks ip)</code>, which generates the response to a GET or POST request.
<code>str</code> is the output stream, rest from <code>parseheader</code>
It performs the following actions:
<ul>
<li>
If <code>op</code> is in <code>redirector*</code>, it does a redirect.
<li> Otherwise, if <code>op</code> is in <code>srvops*</code>, it prints <code>header*</code> and executes the associated function.
<li> Otherwise, a <code>static-filetype</code> is copied with <code>srv-header*</code>.
<li> Otherwise, it returns <code>respond-err</code>.
</ul>
<li>A fnid-based request will go to a handler such as <code>x</code>, which executes the continuation associated with the fnid, which it gets from <code>fns*</code>. Earlier, the continuation function was registered by <code>fnid</code>.
</ul>
<p>
The URL "/deadlink" displays a "dead link" message. The URL "/" displays a welcome message. The URL "/topips" displays the user IP addresses with the heaviest server use.
<p>
The table below provides some internal implementation details. The table shows which URL is assigned to each request type, and which macro is used to implement it. Somewhat surprisingly, that the HTML functions with and without headers both use the same underlying URL and implementation; the higher-level macros simply don't provide headers if no headers are desired.
<table width=100% class="info">
<tr><th width=16%> </th><th width=16%>HTML</th><th width=16%>Headers + HTML</th><th width=16%>Redirect</th><th width=16%>Headers + Redirect</th><th width=16%>Asynchronous</th></tr>
<tr>
<th>Path variable</th>
<td><code>fnurl*</code></td>
<td><code>fnurl*</code></td>
<td><code>rfnurl*</code></td>
<td><code>rfnurl2*</code></td>
<td><code>jfnurl*</code></td>
</tr>
<th>URL Path</th>
<td><code>/x</code></td>
<td><code>/x</code></td>
<td><code>/r</code></td>
<td><code>/y</code></td>
<td><code>/a</code></td>
</tr>
<th>Implementation macro</th>
<td><code>defop-raw x</code></td>
<td><code>defop-raw x</code></td>
<td><code>defopr r</code></td>
<td><code>defopr-raw y</code></td>
<td><code>defop-raw a</code></td>
</tr>
</table>
The following table lists the operations and variables in <code>srv.arc</code> that are generally for internal use.
<p><table class='arc'>
<tr>
<td class='arc'><a name='serve1'></a>
<img src='proc.gif' title='Procedure'/>
<span class='op'>serve1</span> <span class='args'>[port]</span>
<div class='desc'>Start server to handle a single request.</div>
</td>
<td class='arc'><pre>
>(serve1 8080)
</pre>
</td></tr>
<tr>
<td class='arc'><a name='ensure-srvdirs'></a>
<img src='proc.gif' title='Procedure'/>
<span class='op'>ensure-srvdirs</span> <span class='args'></span>
<div class='desc'>Create directories for <code>arcdir*</code> and <code>logdir*</code> if necessary.</div>
</td>
<td class='arc'><pre>
>(ensure-srvdirs)
</pre>
</td></tr>
<tr>
<td class='arc'><a name='handle-request'></a>
<img src='proc.gif' title='Procedure'/>
<span class='op'>handle-request</span> <span class='args'>s [breaksrv]</span>
<div class='desc'>Handles a request. It accepts a connection on socket <code>s</code> and starts a thread running <code>handle-request-thread</code>. If breaksrv is true, ^C will break out of the server.</div>
</td>
<td class='arc'> </td></tr>
<tr>
<td class='arc'><a name='handle-request-thread'></a>
<img src='proc.gif' title='Procedure'/>
<span class='op'>handle-request-thread</span> <span class='args'>i o ip</span>
<div class='desc'>The core code to handle a request. The arguments are the input port, the output port, and the user's IP address. It reads the header, calls <code>parseheader</code>, <code>srvlog</code>, <code>respond</code> (get) or <code>handle-post</code> (post) or <code>respond-err</code>. Then executes <code>harvest-fnids</code> when done.</div>
</td>
<td class='arc'> </td></tr>
<tr>
<td class='arc'><a name='handle-post'></a>
<img src='proc.gif' title='Procedure'/>
<span class='op'>handle-post</span> <span class='args'>i o op args n cookies ip</span>
<div class='desc'>Handles a POST action. The arguments are the input port, the output port, the path, the arguments, the content-length, the cookies, and the user's IP address. Collects POST data and calls <code>respond</code>.</div>
</td>
<td class='arc'> </td></tr>
<tr>
<td class='arc'><a name='respond'></a>
<img src='proc.gif' title='Procedure'/>
<span class='op'>respond</span> <span class='args'>str op args cooks ip</span>
<div class='desc'>Responds to a GET or POST request. <code>str</code> is the output stream, rest from <code>parseheader</code>. If <code>op</code> is in <code>redirector*</code>, it does a redirect. If <code>op</code> is in <code>srvops*</code> otherwise, it prints <code>header*</code> and executes the associated function. Otherwise, a <code>static-filetype</code> is copied with <code>type-header*</code>. Otherwise, <code>respond-err</code>.</div>
</td>
<td class='arc'> </td></tr>
<tr>
<td class='arc'><a name='save-optime'></a>
<img src='proc.gif' title='Procedure'/>
<span class='op'>save-optime</span> <span class='args'>name elapsed</span>
<div class='desc'>Updates <code>optimes*</code> with the time taken to run <code>name</code>.</div>
</td>
<td class='arc'><pre>
>(save-optime "foo" 10)
<span class="return">(10)
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='static-filetype'></a>
<img src='proc.gif' title='Procedure'/>
<span class='op'>static-filetype</span> <span class='args'>sym</span>
<div class='desc'>Returns a filetype symbol for a static file.</div>
</td>
<td class='arc'><pre>
>(static-filetype "foo.gif")
<span class="return">gif
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='respond-err'></a>
<img src='proc.gif' title='Procedure'/>
<span class='op'>respond-err</span> <span class='args'>str msg [args ...]</span>
<div class='desc'>Generates an error response page containing <code>msg</code> and <code>args</code>.</div>
</td>
<td class='arc'><pre>
>(respond-err (stdout) "Bad news")
</pre>
</td></tr>
<tr>
<td class='arc'><a name='parseurl'></a>
<img src='proc.gif' title='Procedure'/>
<span class='op'>parseurl</span> <span class='args'>s</span>
<div class='desc'>Parses an action and URL. Returns a list of <code>type</code> (<code>get</code> or <code>post</code>), <code>op</code> (the path), and an association list of <code>args</code>.</div>
</td>
<td class='arc'><pre>
>(parseurl "GET /x?foo=1&bar")
<span class="return">(get x (("foo" "1") ("bar" "")))
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='parseheader'></a>
<img src='proc.gif' title='Procedure'/>
<span class='op'>parseheader</span> <span class='args'>lines</span>
<div class='desc'>Parses a request header. Returns a list of <code>type</code>, <code>op</code>, <code>args</code>, <code>content-length</code> (for <code>post</code>), and <code>cookies</code>.</div>
</td>
<td class='arc'><pre>
>(parseheader '("POST /foo?a=b" "Cookie: bar=baz"
"Content-Length: 42"))
<span class="return">(post foo (("a" "b")) 42 (("bar" "baz")))
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='parsecookies'></a>
<img src='proc.gif' title='Procedure'/>
<span class='op'>parsecookies</span> <span class='args'>s</span>
<div class='desc'>Parse a HTTP cookies header. Returns an association list of name/value pairs.</div>
</td>
<td class='arc'><pre>
>(parsecookies "Cookie: name1=val1;name2=val2")
<span class="return">(("name1" "val1") ("name2" "val2"))
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='parseargs'></a>
<img src='proc.gif' title='Procedure'/>
<span class='op'>parseargs</span> <span class='args'>s</span>
<div class='desc'>Parses args part of URL. Returns an association list of key/value pairs.</div>
</td>
<td class='arc'><pre>
>(parseargs "x=a+b&y=%c3%e9&z")
<span class="return">(("x" "a b") ("y" "��") ("z" ""))
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='reassemble-args'></a>
<img src='proc.gif' title='Procedure'/>
<span class='op'>reassemble-args</span> <span class='args'>req</span>
<div class='desc'>Creates the URL query string from the <code>args</code> in <code>req</code>. The arguments are not URL-encoded, so they must not contain any special characters.</div>
</td>
<td class='arc'><pre>
>(let req (obj args '(("x" "foo") ("y" "bar")))
(reassemble-args req))
<span class="return">"?x=foo&y=bar"
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='new-fnid'></a>
<img src='proc.gif' title='Procedure'/>
<span class='op'>new-fnid</span> <span class='args'></span>
<div class='desc'>Generates a random unused fnid symbol.</code>.</div>
</td>
<td class='arc'><pre>
>(new-fnid)
<span class="return">OZ3W6XFovO
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='fnid'></a>
<img src='proc.gif' title='Procedure'/>
<span class='op'>fnid</span> <span class='args'>f</span>
<div class='desc'>Generates a <code>fnid</code> with continuation function <code>f</code>.</div>
</td>
<td class='arc'><pre>
>(fnid (fn (req) (prn "hi")))
<span class="return">h74MwrIYrF
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='timed-fnid'></a>
<img src='proc.gif' title='Procedure'/>
<span class='op'>timed-fnid</span> <span class='args'>lasts f</span>
<div class='desc'>Generates a <code>fnid</code> that will expire after <code>lasts</code> seconds.</div>
</td>
<td class='arc'><pre>
>(timed-fnid 100 (fn (req) (prn "hi")))
<span class="return">R6cceNyVpi
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='harvest-fnids'></a>
<img src='proc.gif' title='Procedure'/>
<span class='op'>harvest-fnids</span> <span class='args'>[max]</span>
<div class='desc'>If <code>fns*</code> has more than <code>max</code> fnids, purges any expired <code>timed-fnids*</code> and purges the oldest <code>fnids*</code> (10% of <code>max</code> are purged).</div>
</td>
<td class='arc'><pre>
>(harvest-fnids 1000)
<span class="return">nil
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='fnid-field'></a>
<img src='proc.gif' title='Procedure'/>
<span class='op'>fnid-field</span> <span class='args'>id</span>
<div class='desc'>Generates a hidden field assigning <code>id</code> to <code>fnid</code>.</div>
</td>
<td class='arc'><pre>
>(fnid-field 'abc123)
<span class="stdout"><input type=hidden name="fnid" value="abc123">
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='unique-id'></a>
<img src='proc.gif' title='Procedure'/>
<span class='op'>unique-id</span> <span class='args'>[length]</span>
<div class='desc'>Generates a unique random alphanumeric id. The minimum length is 5, and the default length is 8. The table <code>unique-ids*</code> holds all generated ids to ensure uniqueness.</div>
</td>
<td class='arc'><pre>
>(unique-id)
<span class="return">32o5uGIQ
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='memodate'></a>
<img src='proc.gif' title='Procedure'/>
<span class='op'>memodate</span> <span class='args'></span>
<div class='desc'>Returns current date. Uses memoization for efficiency; dates are cached for 60 seconds. Only works on some platforms.</div>
</td>
<td class='arc'> </td></tr>
<tr>
<td class='arc'><a name='fns*'></a>
<img src='var.gif' title='Variable'/>
<span class='op'>fns*</span> <span class='args'></span>
<div class='desc'>Table mapping from <code>fnid</code> to continuation function.</div>
</td>
<td class='arc'> </td></tr>
<tr>
<td class='arc'><a name='fnids*'></a>
<img src='var.gif' title='Variable'/>
<span class='op'>fnids*</span> <span class='args'></span>
<div class='desc'>List of fnids without an explicit lifetime.</div>
</td>
<td class='arc'> </td></tr>
<tr>
<td class='arc'><a name='timed-fnids*'></a>
<img src='var.gif' title='Variable'/>
<span class='op'>timed-fnids*</span> <span class='args'></span>
<div class='desc'>List of entries for fnids with an explicit lifetime. Each entry is a list of <code>fnid, time, lasts</code> for <code>fnids</code> with an expiration time.</div>
</td>
<td class='arc'> </td></tr>
<tr>
<td class='arc'><a name='fnurl*'></a>
<img src='var.gif' title='Variable'/>
<span class='op'>fnurl*</span> <span class='args'></span>
<div class='desc'>URL path for a fnid continuation.</div>
</td>
<td class='arc'><pre>
>fnurl*
<span class="return">"/x"
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='rfnurl*'></a>
<img src='var.gif' title='Variable'/>
<span class='op'>rfnurl*</span> <span class='args'></span>
<div class='desc'>URL path for a fnid continuation redirect.</div>
</td>
<td class='arc'><pre>
>rfnurl*
<span class="return">"/r"
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='rfnurl2*'></a>
<img src='var.gif' title='Variable'/>
<span class='op'>rfnurl2*</span> <span class='args'></span>
<div class='desc'>URL path for a fnid continuation raw redirect.</div>
</td>
<td class='arc'><pre>
>rfnurl2*
<span class="return">"/y"
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='jfnurl*'></a>
<img src='var.gif' title='Variable'/>
<span class='op'>jfnurl*</span> <span class='args'></span>
<div class='desc'>URL path for an asynchronous fnid continuation.</div>
</td>
<td class='arc'><pre>
>jfnurl*
<span class="return">"/a"
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='unique-ids*'></a>