-
Notifications
You must be signed in to change notification settings - Fork 3
/
fileserver.ink
318 lines (295 loc) · 6.99 KB
/
fileserver.ink
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
#!/usr/bin/env ink
` an http static file server
with support for directory indexes `
std := load('std')
log := std.log
f := std.format
slice := std.slice
cat := std.cat
map := std.map
each := std.each
readFile := std.readFile
DIR := './public'
PORT := 7800
ALLOWINDEX := false
` short non-comprehensive list of MIME types `
TYPES := {
` text formats `
html: 'text/html; charset=utf-8'
js: 'text/javascript; charset=utf-8'
css: 'text/css; charset=utf-8'
txt: 'text/plain; charset=utf-8'
md: 'text/plain; charset=utf-8'
` serve go & ink source code as plain text`
ink: 'text/plain; charset=utf-8'
go: 'text/plain; charset=utf-8'
` image formats `
jpg: 'image/jpeg'
jpeg: 'image/jpeg'
png: 'image/png'
gif: 'image/gif'
svg: 'image/svg+xml'
` other misc `
pdf: 'application/pdf'
zip: 'application/zip'
json: 'application/json'
}
` prepare standard header `
hdr := attrs => (
base := {
'X-Served-By': 'ink-serve'
'Content-Type': 'text/plain'
}
each(keys(attrs), k => base.(k) := attrs.(k))
base
)
` is this path a path to a directory? `
dirPath? := path => path.(len(path) - 1) :: {
'/' -> true
_ -> false
}
` main server handler `
close := listen('0.0.0.0:' + string(PORT), evt => evt.type :: {
'error' -> log('server error: ' + evt.message)
'req' -> (
log(f('{{ method }}: {{ url }}', evt.data))
` set up timer `
start := time()
` trim the elapsed-time millisecond count at 2-3 decimal digits `
getElapsed := () => slice(string(floor((time() - start) * 1000000) / 1000), 0, 5)
` normalize path `
url := trimQP(evt.data.url)
` respond to file request `
evt.data.method :: {
'GET' -> handlePath(url, DIR + url, evt.end, getElapsed)
_ -> (
` if other methods, just drop the request `
log(' -> ' + evt.data.url + ' dropped')
(evt.end)({
status: 405
headers: hdr({})
body: 'method not allowed'
})
)
}
)
})
` handles requests to path with given parameters `
handlePath := (url, path, end, getElapsed) => stat(path, evt => evt.type :: {
'error' -> (
log(f(' -> {{ url }} led to error in {{ ms }}ms: {{ error }}', {
url: url
ms: getElapsed()
error: evt.message
}))
end({
status: 500
headers: hdr({})
body: 'server error'
})
)
'data' -> handleStat(url, path, evt.data, end, getElapsed)
})
` handles requests to validated paths `
handleStat := (url, path, data, end, getElapsed) => data :: {
` means file didn't exist `
() -> (
` what if the path omits the .html extension? `
hpath := path + '.html'
stat(hpath, evt => evt.type :: {
'error' -> (
log(f(' -> {{ url }} (.html) led to error in {{ ms }}ms: {{ error }}', {
url: url
ms: getElapsed()
error: evt.message
}))
end({
status: 500
headers: hdr({})
body: 'server error'
})
)
'data' -> evt.data :: {
{dir: false, name: _, len: _} -> handlePath(url, hpath, end, getElapsed)
_ -> (
log(f(' -> {{ url }} not found in {{ ms }}ms', {
url: url
ms: getElapsed()
}))
end({
status: 404
headers: hdr({})
body: 'not found'
})
)
}
})
)
{dir: true, name: _, len: _, mod: _} -> dirPath?(path) :: {
true -> handleDir(url, path, data, end, getElapsed)
false -> (
log(f(' -> {{ url }} returned redirect to {{ url }}/ in {{ ms }}ms', {
url: url
ms: getElapsed()
}))
end({
status: 301
headers: hdr({
'Location': url + '/'
})
body: ''
})
)
}
{dir: false, name: _, len: _, mod: _} -> readFile(path, data => handleFileRead(url, path, data, end, getElapsed))
_ -> end({
status: 500
headers: hdr({})
body: 'server invariant violation'
})
}
` handles requests to readFile() `
handleFileRead := (url, path, data, end, getElapsed) => data :: {
() -> (
log(f(' -> {{ url }} failed read in {{ ms }}ms', {
url: url
ms: getElapsed()
}))
end({
status: 500
headers: hdr({})
body: 'server error'
})
)
_ -> (
fileType := getType(path)
log(f(' -> {{ url }} ({{ type }}) served in {{ ms }}ms', {
url: url
type: fileType
ms: getElapsed()
}))
end({
status: 200
headers: hdr({
'Content-Type': getType(path)
})
body: data
})
)
}
` handles requests to directories '/' `
handleDir := (url, path, data, end, getElapsed) => (
ipath := path + 'index.html'
stat(ipath, evt => evt.type :: {
'error' -> (
log(f(' -> {{ url }} (index) led to error in {{ ms }}ms: {{ error }}', {
url: url
ms: getElapsed()
error: evt.message
}))
end({
status: 500
headers: hdr({})
body: 'server error'
})
)
'data' -> evt.data :: {
() -> handleExistingDir(url, path, end, getElapsed)
` in the off chance that /index.html is a dir, just render index `
{dir: true, name: _, len: _, mod: _} -> handleExistingDir(url, path, end, getElapsed)
{dir: false, name: _, len: _, mod: _} -> handlePath(url, ipath, end, getElapsed)
_ -> end({
status: 500
headers: hdr({})
body: 'server invariant violation'
})
}
})
)
` handle a directory we stat() confirmed to exist `
handleExistingDir := (url, path, end, getElapsed) => ALLOWINDEX :: {
true -> handleNoIndexDir(url, path, end, getElapsed)
false -> (
log(f(' -> {{ url }} not allowed in {{ ms }}ms', {
url: url
ms: getElapsed()
}))
end({
status: 403
headers: hdr({})
body: 'permission denied'
})
)
}
` helpers for rendering the directory index page `
makeIndex := (path, items) => '<title>' + path +
'</title><style>body{font-family: system-ui,sans-serif}</style><h1>index of <code>' +
path + '</code></h1><ul>' + items + '</ul>'
makeIndexLi := (fileStat, separator) => '<li><a href="' + fileStat.name + '" title="' + fileStat.name + '">' +
fileStat.name + separator + ' (' + string(fileStat.len) + ' B)</a></li>'
` handles requests to dir() without /index.html `
handleNoIndexDir := (url, path, end, getElapsed) => dir(path, evt => evt.type :: {
'error' -> (
log(f(' -> {{ url }} dir() led to error in {{ ms }}ms: {{ error }}', {
url: url
ms: getElapsed()
error: evt.message
}))
end({
status: 500
headers: hdr({})
body: 'server error'
})
)
'data' -> (
log(f(' -> {{ url }} (index) served in {{ ms }}ms', {
url: url
ms: getElapsed()
}))
end({
status: 200
headers: hdr({
'Content-Type': 'text/html'
})
body: makeIndex(
slice(path, 2, len(path))
cat(map(evt.data, fileStat => makeIndexLi(
fileStat
fileStat.dir :: {
true -> '/'
false -> ''
}
)), '')
)
})
)
})
` trim query parameters `
trimQP := path => (
max := len(path)
(sub := (idx, acc) => idx :: {
max -> path
_ -> path.(idx) :: {
'?' -> acc
_ -> sub(idx + 1, acc + path.(idx))
}
})(0, '')
)
` given a path, get the MIME type `
getType := path => (
guess := TYPES.(getPathEnding(path))
guess :: {
() -> 'application/octet-stream'
_ -> guess
}
)
` given a path, get the file extension `
getPathEnding := path => (
(sub := (idx, acc) => idx :: {
0 -> path
_ -> path.(idx) :: {
'.' -> acc
_ -> sub(idx - 1, path.(idx) + acc)
}
})(len(path) - 1, '')
)