forked from per-gron/sack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testapp.scm
102 lines (97 loc) · 4.17 KB
/
testapp.scm
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
(import src/server)
(sack-start!
(lambda (env)
(let ((ret #t))
(values 200
'(("Content-Type" . "text/plain"))
(lambda ()
(and
ret
(begin
(set! ret #f)
(with-output-to-u8vector
(list char-encoding: 'UTF-8)
(lambda ()
(write (env 'sack:uri)) (print "Hello, world!")))))))))
port-number: 3333)
(import src/server)
(sack-start!
(lambda (env)
(let ((ret #t))
(values 200
'(("Content-Type" . "text/html"))
(lambda ()
(and
ret
(begin
(set! ret #f)
(with-output-to-u8vector
(list char-encoding: 'UTF-8)
(lambda ()
(print "<pre>\n") (write (##vector->list (env 'sack:uri))) (print "</pre>\n")
(print "<form action=. method=get>"
"<textarea name=c></textarea><input type=submit />"
"</form>")))))))))
port-number: 3334)
; Test of reading the HTTP request body. This is done with the 'sack:body environment variable, as follows:
(import src/server)
(define console-output-port (current-output-port))
(sack-start!
(lambda (env)
(let ((ret #t))
(with-output-to-port console-output-port (lambda () (print "Testapp got from HTTP request URI:") (write (env 'sack:uri)) (print "\n")))
((env 'sack:body) (lambda (u8v)
(with-output-to-port console-output-port (lambda () (print "Testapp got from HTTP request u8v: ") (write u8v) (print "\n")))
; [Returning #f here would make sack:body cancel.]
))
; Another way of invoking sack:body would be this:
; ((env 'sack:body) (lambda (u8v len)
; ; [We now have access to the bytes 0 to len - 1 in u8v, up to the point that this procedure returns.
; ; unlike in the sack:body use example above, Sack did not make a special u8vector for our procedure
; ; here, but we just got a copy of Sack's internal read buffer as to read out all we want from it,
; ; up to and only up to that we return back from this procedure.]
; )
; copying?: #f)
(values 200
'(("Content-Type" . "text/html"))
(lambda ()
(and
ret
(begin
(set! ret #f)
(with-output-to-u8vector
(list char-encoding: 'UTF-8)
(lambda ()
; (write (env 'sack:uri))
(print "<form action=. method=post>"
"<textarea name=c></textarea><input type=submit />"
"</form>")))))))))
port-number: 3335)
; Test file upload using the form decoding mechanism.
; Is form decoding mature for production use?
(import src/server src/mime2 src/form (std string/util misc/u8v srfi/13))
(define console-output-port (current-output-port))
(define (form-post-decode* thunk)
(lambda (env)
(form-post-decode env thunk)))
(sack-start!
(form-post-decode*
(lambda (env)
(let ((ret #t))
(values 200
'(("Content-Type" . "text/html"))
(lambda ()
(and
ret
(begin
(set! ret #f)
(with-output-to-u8vector
(list char-encoding: 'UTF-8)
(lambda ()
(print port: console-output-port "Successfully reached \n")
; (write (env 'sack:uri))
(print "Form data: ") (write (env 'form:data))
(print "<hr/><form action=\".\" method=\"post\" enctype=\"multipart/form-data\">"
"<textarea name=c></textarea><input type=file name=filen /><input type=submit />"
"</form>"))))))))))
port-number: 3336)