blob: 1f8c79e68190f25d452797885726776c185dea7c (
plain) (
blame)
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
|
import std/[os, strutils]
import prologue, regex
const roll = readFile("src/rickroll.html")
# note: prologue appears to deal with directory traversal for us, nice
proc serve(ctx: Context, regex: Regex) {.async.} =
if regex in $ctx.request.headers["User-Agent"]:
resp roll
else:
try:
if ($ctx.request.path == "/"):
resp readFile(getCurrentDir() & "/index.html")
else:
resp readFile(getCurrentDir() & $ctx.request.path)
except IOError:
resp "<html><body><h1>404 Not Found</h1></body></html>"
try:
let port = Port(paramStr(1).parseInt())
let regex = re(paramStr(2))
let settings = newSettings(port = port)
let app = newApp(settings)
# gcsafe for passing a global to a callback
proc thunk(ctx: Context) {.async, gcsafe.} =
yield serve(ctx, regex)
# match all requests
app.get("*$", thunk)
app.run()
except ValueError, IndexDefect:
echo "usage: cattrap <port> <regex>"
except OSError:
echo "cattrap: insufficient permissions, try a different port"
|