server: the map-tile gate and the internal X-Accel path (#24)

This commit is contained in:
2026-11-15 12:30:48 +01:00
parent 5ff15c4b36
commit 669e47e1ba
3 changed files with 294 additions and 1 deletions
+42 -1
View File
@@ -23,6 +23,10 @@
## list (`encountered.remove`, recorded on every sighting), character props through `prop.*`, and the
## players/keys half that names a player: `player.setname`, `player.rename`, `key.show` and the
## `roster` frame. The token link (`token.create`/`token.update` `character_id`) lands here too.
## M3 slice 9 (#24): map tiles - `GET /tiles/<scene>/v<art_version>/<x>_<y>.png` in
## server/tiles.nim: a strict canonical URL, the player's explored rule (the GM always), `404` for a
## tile that does not exist and a bodyless `200` with `X-Accel-Redirect` to the internal `/_tiles/`
## location, which nginx alone can serve (DESIGN 3.3, 6.6, 11.1).
## Close reasons (FIXED, owner decision #18): mummy 0.4.8 cannot put a code in the close frame, so
## every server-initiated close is preceded by a `closing {reason, code}` message and the client
## reads 1005 + that message (DESIGN 6.5).
@@ -32,7 +36,7 @@ import std/typedthreads
import db_connector/db_sqlite
import mummy, mummy/routers
import config, db, eventlog, crypto, auth, conns, instancelock, protocol, world
import characters, geometry, props, text, tokens, vision, views, explored, worldmap, faults, moverules
import characters, geometry, props, text, tiles, tokens, vision, views, explored, worldmap, faults, moverules
const
CookieName* = "fff_session"
@@ -562,6 +566,42 @@ proc join(request: Request) {.gcsafe.} =
headers["Referrer-Policy"] = "no-referrer"
request.respond(303, headers)
proc tileGate(request: Request) {.gcsafe.} =
## GET /tiles/<scene>/v<art_version>/<x>_<y>.png (DESIGN 3.3, 6.6; slice 9 / #24). Order: parse
## the URL (malformed, out of range, another scene, stale art version -> 404 whatever the session),
## then the session (missing or unknown -> 403), then the explored grid (unexplored -> 403).
## The GM is always allowed. Nim never opens a tile file: a `200` carries `X-Accel-Redirect` to
## the internal `/_tiles/` location and `Cache-Control: private` (Q4), and nginx serves the bytes.
let parsed = parseTileRequest(request.pathParams["scene"], request.pathParams["version"],
request.pathParams["file"], app.scene)
if parsed.outcome != toTileOk:
request.respond(404, body = "")
return
let token = sessionCookie(request)
var player: Option[Player]
if token.len > 0:
withLock app.dbLock:
player = sessionPlayer(app.db, sha256Hex(token))
if player.isNone:
request.respond(403, body = "")
return
var allowed = player.get.role == prGm
if not allowed:
withLock app.dbLock:
if app.visions.hasKey(player.get.id):
allowed = tileCoveredByExplored(app.scene, app.visions[player.get.id].explored,
parsed.req.x, parsed.req.y)
if not allowed:
request.respond(403, body = "")
return
var headers: HttpHeaders
headers["X-Accel-Redirect"] = tilePath(app.scene, parsed.req.x, parsed.req.y)
headers["Cache-Control"] = "private"
request.respond(200, headers)
proc wsUpgrade(request: Request) {.gcsafe.} =
## GET /ws: Origin, session, proto, total cap, then the upgrade (DESIGN 6.5 rows 1-5).
if request.headers["Origin"] != app.cfg.publicOrigin:
@@ -1952,6 +1992,7 @@ proc heartbeat() {.thread.} =
proc buildRouter(): Router =
result.get("/healthz", healthz)
result.post("/join", join)
result.get("/tiles/@scene/@version/@file", tileGate)
result.get("/ws", wsUpgrade)
proc cliUsage(): string =