-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
web.tcl
233 lines (222 loc) · 8.86 KB
/
web.tcl
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
lappend auto_path "./vendor"
package require websocket
proc handleConnect {chan addr port} {
fileevent $chan readable [list handleRead $chan $addr $port]
}
proc htmlEscape {s} { string map {& "&" < "<" > ">" "\"" """} $s }
proc readFile {filename contentTypeVar} {
upvar $contentTypeVar contentType
set fd [open $filename r]
fconfigure $fd -encoding binary -translation binary
set response [read $fd]; close $fd; return $response
}
proc getDotAsPdf {dot contentTypeVar} {
upvar $contentTypeVar contentType
set contentType "application/pdf"
set fd [open |[list dot -Tpdf <<$dot] r]
fconfigure $fd -encoding binary -translation binary
set response [read $fd]; close $fd; return $response
}
proc handlePage {path httpStatusVar contentTypeVar} {
upvar $contentTypeVar contentType
switch -exact -- $path {
"/" {
set l [list]
dict for {id stmt} [Statements::all] {
lappend l [subst {
<li>
<details>
<summary style="[expr {
[lsearch -exact [statement clause $stmt] error] != -1
? "color: red"
: ""}]">
$id: [htmlEscape [statement short $stmt]]</summary>
<pre>[htmlEscape [statement clause $stmt]]</pre>
</details>
</li>
}]
}
subst {
<html>
<head>
<link rel="stylesheet" href="/style.css">
<title>Statements</title>
</head>
<nav>
<a href="/new"><button>New program</button></a>
<a href="/programs">Running programs</a>
<a href="/timings">Timings</a>
<a href="/keyboards">Keyboards</a>
<a href="/statementClauseToId.pdf">statementClauseToId graph</a>
<a href="/statements.pdf">statements graph</a>
</nav>
<h1>Statements</h1>
<ul>[join $l "\n"]</ul>
</html>
}
}
"/programs" {
set programs [Statements::findMatches [list /someone/ claims /programName/ has program /program/]]
subst {
<html>
<head>
<link rel="stylesheet" href="/style.css">
<title>Running programs</title>
</head>
<body>
[join [lmap p $programs { dict with p {subst {
<h2>$programName</h2>
<pre><code>[htmlEscape [lindex $program 1]]</code></pre>
}} }] "\n"]
</body>
</html>
}
}
"/timings" {
set runTimes [list]
dict for {lambda runTime} $Evaluator::runTimesMap {
lappend runTimes $lambda $runTime
}
set runTimes [lsort -integer -stride 2 -index 1 $runTimes]
set totalRunTime 0
set l [list]
foreach {lambda runTime} $runTimes {
set runCount [dict get $Evaluator::runCountsMap $lambda]
set totalRunTime [+ $totalRunTime $runTime]
lappend l [subst {
<li>
<pre>[htmlEscape $lambda]</pre> ($runCount runs): $runTime us
</li>
}]
}
subst {
<html>
<head>
<link rel="stylesheet" href="/style.css">
<title>Timings</title>
</head>
<nav>
<a href="/new"><button>New program</button></a>
<a href="/">Statements</a>
<a href="/statementClauseToId.pdf">statementClauseToId graph</a>
<a href="/statements.pdf">statements graph</a>
</nav>
<h1>Timings (total frame run time $totalRunTime us)</h1>
<ul>[join $l "\n"]</ul>
</html>
}
}
"/favicon.ico" {
set contentType "image/x-icon"
readFile "assets/favicon.ico" contentType
}
"/style.css" {
set contentType "text/css"
readFile "assets/style.css" contentType
}
"/statementClauseToId.pdf" {
getDotAsPdf [trie dot [Statements::statementClauseToIdTrie]] contentType
}
"/statements.pdf" {
getDotAsPdf [Statements::dot] contentType
}
"/lib/folk.js" {
set contentType "text/javascript"
readFile "lib/folk.js" contentType
}
"/vendor/gstwebrtc/gstwebrtc-api-2.0.0.min.js" {
set contentType "text/javascript"
readFile "vendor/gstwebrtc/gstwebrtc-api-2.0.0.min.js" contentType
}
default {
upvar $httpStatusVar httpStatus
set httpStatus "HTTP/1.1 404 Not Found"
subst {
<html>
<b>$path</b> Not found.
</html>
}
}
}
}
proc handleRead {chan addr port} {
chan configure $chan -translation crlf
gets $chan line; set firstline $line
# puts "Http: $chan $addr $port: $line"
set headers [list]
while {[gets $chan line] >= 0 && $line ne ""} {
if {[regexp -expanded {^( [^\s:]+ ) \s* : \s* (.+)} $line -> k v]} {
lappend headers $k $v
} else { break }
}
if {[regexp {GET ([^ ]*) HTTP/1.1} $firstline -> path] && $path ne "/ws"} {
set response {}
set matches [Statements::findMatches {/someone/ wishes the web server handles route /route/ with handler /handler/}]
try {
foreach match $matches {
set route [dict get $match route]
set handler [dict get $match handler]
if {[regexp -all $route $path whole_match]} {
fn html {body} {dict create statusAndHeaders "HTTP/1.1 200 OK\nConnection: close\nContent-Type: text/html; charset=utf-8\n\n" body [encoding convertto utf-8 $body]}
fn json {body} {dict create statusAndHeaders "HTTP/1.1 200 OK\nConnection: close\nContent-Type: application/json; charset=utf-8\n\n" body [encoding convertto utf-8 $body]}
set response [apply [list {path ^html ^json} $handler] $path ${^html} ${^json}]
}
}
if {$response eq ""} {
set httpStatus "HTTP/1.1 200 OK"
set contentType "text/html; charset=utf-8"
set body [handlePage $path httpStatus contentType]
if {$contentType eq "text/html; charset=utf-8"} {
set body [encoding convertto utf-8 $body]
}
set response [dict create statusAndHeaders "$httpStatus\nConnection: close\nContent-Type: $contentType\n\n" body $body]
}
if {![dict exists $response statusAndHeaders]} {
error "Response not generated"
}
} on error e {
set contentType "text-html; charset=utf-8"
set body [subst {
<html>
<head>
<title>folk: 500 Internal Server Error</title>
</head>
<body>
<pre>[htmlEscape $e]:
[htmlEscape $::errorInfo]</pre>
</body>
</html>
}]
set response [dict create statusAndHeaders "HTTP/1.1 500 Internal Server Error\nConnection: close\nContent-Type: $contentType\n\n" body [encoding convertto utf-8 $body]]
}
puts -nonewline $chan [dict get $response statusAndHeaders]
if {[dict exists $response body]} {
chan configure $chan -encoding binary -translation binary
puts -nonewline $chan [dict get $response body]
}
close $chan
} elseif {[::websocket::test $::serverSock $chan "/ws" $headers]} {
# puts "WS: $chan $addr $port"
::websocket::upgrade $chan
# from now the handleWS will be called (not anymore handleRead).
} else { puts "Closing: $chan $addr $port $headers"; close $chan }
}
proc handleWS {chan type msg} {
if {$type eq "connect"} {
Assert websocket $chan is connected
} elseif {$type eq "close"} {
Retract websocket $chan is connected
Retract when websocket $chan is connected /...rest/
} elseif {$type eq "text"} {
eval $msg
} elseif {$type eq "ping" || $type eq "pong" || $type eq "disconnect"} {
# puts "Event $type from chan $chan"
} else {
puts "$::thisProcess: Unhandled WS event $type on $chan ($msg)"
}
}
if {[catch {set ::serverSock [socket -server handleConnect 4273]}] == 1} {
error "There's already a Web-capable Folk node running on this machine."
}
::websocket::server $::serverSock
::websocket::live $::serverSock /ws handleWS