blob: 752610eb6a04cf5bdaafc600efc22b0ee343d57e (
plain)
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
|
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 totalTimes [list]
dict for {body totalTime} $Evaluator::totalTimesMap {
dict with totalTime {
lappend totalTimes $body [expr {$loadTime + $runTime + $unloadTime}]
}
}
set totalTimes [lsort -integer -stride 2 -index 1 $totalTimes]
set totalFrameTime 0
set l [list]
foreach {body totalTime} $totalTimes {
set runs [dict get $Evaluator::runsMap $body]
set totalFrameTime [expr {$totalFrameTime + $totalTime/$::stepCount}]
lappend l [subst {
<li>
<pre>[htmlEscape $body]</pre> ($runs runs): [dict get $Evaluator::totalTimesMap $body]: $totalTime microseconds total ([expr {$totalTime/$::stepCount}] us per frame), $runs runs ([expr {$totalTime/$runs}] us per run; [expr {$runs/$::stepCount}] runs per frame)
</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 (sum per-frame time $totalFrameTime 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
}
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 $body}
fn json {body} {dict create statusAndHeaders "HTTP/1.1 200 OK\nConnection: close\nContent-Type: application/json; charset=utf-8\n\n" body $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]
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 $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" || $type eq "ping" || $type eq "pong"} {
# puts "Event $type from chan $chan"
} elseif {$type eq "text"} {
eval $msg
} elseif {$type eq "disconnect"} {
Commit $chan statements {}
foreach peerNs [namespace children ::Peers] {
apply [list {disconnectedChan} {
variable chan
if {$chan eq $disconnectedChan} {
namespace delete [namespace current]
}
} $peerNs] $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
|