-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.ss
170 lines (144 loc) · 6.56 KB
/
core.ss
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
#lang scheme/base
(require "base.ss")
(require scheme/list
scheme/string
(mirrors-in)
(unlib-in string))
; Struct types -----------------------------------
(define-struct site
(id rules controllers not-found-proc)
#:property
prop:custom-write
(lambda (site out write?)
((if write? write display)
(vector 'site (site-id site))
out))
#:transparent)
(define-struct controller
(id
[site #:mutable]
[wrapper-proc #:mutable]
[body-proc #:mutable]
[access-proc #:mutable]
[access-denied-proc #:mutable]
[requestless? #:mutable])
#:property
prop:custom-write
(lambda (controller out write?)
((if write? write display)
(vector 'controller (controller-id controller))
out))
#:property
prop:procedure
(lambda (controller . args)
(apply (controller-wrapper-proc controller) controller args))
#:transparent)
; (struct string (string -> any) (any -> string))
(define-struct arg (pattern decoder encoder) #:transparent)
; (struct (-> regexp) (listof arg) (listof (U string arg)))
(define-struct pattern (regexp-maker args elements) #:transparent)
; (struct pattern controller)
(define-struct rule (pattern controller) #:transparent)
; Constructors -----------------------------------
; symbol boolean -> controller
(define (create-controller id requestless?)
(letrec ([controller (make-controller
id
#f
(lambda (controller . args) (apply (default-controller-wrapper) controller args))
(lambda args (apply (default-controller-undefined-responder) controller args))
(lambda args (apply (default-access-procedure) controller args))
(lambda args (apply (default-access-denied-responder) controller args))
requestless?)])
controller))
; (U string arg) ... -> pattern
(define (create-pattern . elements)
(make-pattern (make-regexp-maker elements)
(filter arg? elements)
elements))
; Configuration --------------------------------
(define-enum link-formats (mirrors sexp sexps))
(define-enum link-substitutes (hide span body))
; (parameter link-format)
(define default-link-format
(make-parameter (link-formats mirrors)))
; (parameter link-format)
(define default-link-substitute
(make-parameter (link-substitutes body)))
; controller any ... -> any
(define (plain-controller-wrapper controller . args)
(with-handlers ([exn:fail:no-access?
(lambda (exn)
(apply (controller-access-denied-proc controller) args))])
(dynamic-wind
(lambda ()
(unless (apply (controller-access-proc controller) args)
(raise-exn exn:fail:no-access "Access denied.")))
(lambda ()
(apply (controller-body-proc controller) args))
void)))
; (parameter ((any ... -> any) any ... -> any))
; Initialised in response.ss.
(define default-controller-wrapper
(make-parameter plain-controller-wrapper))
; (parameter (any ... -> boolean))
; Initialised in response.ss.
(define default-access-procedure
(make-parameter (lambda _ #t)))
; (parameter (controller any ... -> response))
; Initialised in response.ss.
(define default-access-denied-responder
(make-parameter (lambda _ (error "not initialised"))))
; (parameter (controller any ... -> response))
; Initialised in response.ss.
(define default-controller-undefined-responder
(make-parameter (lambda _ (error "not initialised"))))
; Helpers ----------------------------------------
; (listof (U string (-> string) arg)) ... -> string
(define (make-regexp-maker elements)
(let ([parts `("^"
,@(for/list ([elem (in-list elements)])
(match elem
[(? string?) (regexp-quote elem)]
[(? arg?) (string-append "(" (arg-pattern elem) ")")]
[(? procedure?) (lambda () (regexp-quote (elem)))]))
"\\/?$")]) ; optional trailing slash
(lambda ()
(pregexp (apply string-append
(for/list ([part (in-list parts)])
(if (procedure? part)
(part)
part)))))))
(define-struct (exn:fail:no-access exn:fail) () #:transparent)
; Provide statements -----------------------------
(provide link-formats
link-substitutes)
(provide/contract
[struct site ([id symbol?]
[rules (listof rule?)]
[controllers (listof controller?)]
[not-found-proc (-> request? response/c)])]
[struct controller ([id symbol?]
[site site?]
[wrapper-proc (or/c procedure? #f)]
[body-proc procedure?]
[access-proc procedure?]
[access-denied-proc procedure?]
[requestless? boolean?])]
[struct arg ([pattern string?]
[decoder procedure?]
[encoder procedure?])]
[struct pattern ([regexp-maker (-> regexp?)]
[args (listof arg?)]
[elements (listof (or/c string? procedure? arg?))])]
[struct rule ([pattern pattern?]
[controller controller?])]
[create-controller (-> symbol? boolean? controller?)]
[create-pattern (->* () () #:rest (listof (or/c string? arg? procedure?)) pattern?)]
[default-link-format (parameter/c (enum-value/c link-formats))]
[default-link-substitute (parameter/c (enum-value/c link-substitutes))]
[plain-controller-wrapper (->* (controller?) () #:rest any/c any)]
[default-controller-wrapper (parameter/c procedure?)]
[default-access-procedure (parameter/c procedure?)]
[default-access-denied-responder (parameter/c procedure?)]
[default-controller-undefined-responder (parameter/c procedure?)])