2022-08-30 12:45:37 +00:00
|
|
|
#lang racket/base
|
|
|
|
(require racket/path
|
|
|
|
racket/runtime-path
|
|
|
|
net/url
|
|
|
|
web-server/http/request-structs
|
|
|
|
web-server/servlet-dispatch
|
|
|
|
web-server/dispatchers/filesystem-map
|
|
|
|
(only-in web-server/dispatchers/dispatch next-dispatcher)
|
|
|
|
(prefix-in files: web-server/dispatchers/dispatch-files)
|
2023-02-05 04:56:15 +00:00
|
|
|
"../lib/mime-types.rkt"
|
2023-03-08 09:56:04 +00:00
|
|
|
"../lib/syntax.rkt"
|
2022-08-30 12:45:37 +00:00
|
|
|
"config.rkt")
|
|
|
|
|
|
|
|
(provide
|
|
|
|
static-dispatcher)
|
|
|
|
|
|
|
|
(module+ test
|
|
|
|
(require rackunit))
|
|
|
|
|
|
|
|
(define-runtime-path path-static "../static")
|
2023-03-08 09:56:04 +00:00
|
|
|
(define path-archive (anytime-path ".." "storage/archive"))
|
2022-08-30 12:45:37 +00:00
|
|
|
|
|
|
|
(define hash-ext-mime-type
|
|
|
|
(hash #".css" #"text/css"
|
2022-10-22 11:26:06 +00:00
|
|
|
#".js" #"text/javascript"
|
2022-08-30 12:45:37 +00:00
|
|
|
#".png" #"image/png"
|
|
|
|
#".svg" #"image/svg+xml"
|
2022-11-10 10:55:31 +00:00
|
|
|
#".woff2" #"font/woff2"
|
2022-08-30 12:45:37 +00:00
|
|
|
#".txt" #"text/plain"))
|
|
|
|
|
|
|
|
(define (make-path segments)
|
|
|
|
(map (λ (seg) (path/param seg '())) segments))
|
|
|
|
(module+ test
|
|
|
|
(check-equal? (make-path '("static" "main.css"))
|
|
|
|
(list (path/param "static" '()) (path/param "main.css" '()))))
|
|
|
|
|
2023-02-05 04:56:15 +00:00
|
|
|
;; given a request path, return a rewritten request path and the source directory on the filesystem to serve based on
|
2022-08-30 12:45:37 +00:00
|
|
|
(define (path-rewriter p)
|
|
|
|
(cond
|
|
|
|
; url is ^/static/... ?
|
|
|
|
[(equal? (path/param-path (car p)) "static")
|
|
|
|
; rewrite to ^/... which will be treated as relative to static/ on the filesystem
|
2023-02-05 04:56:15 +00:00
|
|
|
(values (cdr p) path-static)]
|
|
|
|
; url is ^/archive/... ?
|
|
|
|
[(equal? (path/param-path (car p)) "archive")
|
|
|
|
; rewrite req to ^/<wikiname> and dir to /storage/archive
|
|
|
|
(values (cdr p) path-archive)]
|
2022-08-30 12:45:37 +00:00
|
|
|
; url is literally ^/robots.txt
|
|
|
|
[(equal? p (make-path '("robots.txt")))
|
|
|
|
; rewrite to ^/... -- it already is!
|
2023-02-05 04:56:15 +00:00
|
|
|
(values p path-static)]
|
2022-08-30 12:45:37 +00:00
|
|
|
; not going to use the static file dispatcher
|
|
|
|
[#t (next-dispatcher)]))
|
|
|
|
(module+ test
|
2023-02-05 04:56:15 +00:00
|
|
|
(check-equal? (call-with-values (λ () (path-rewriter (make-path '("static" "main.css")))) cons)
|
|
|
|
(cons (make-path '("main.css")) path-static))
|
|
|
|
(check-equal? (call-with-values (λ () (path-rewriter (make-path '("static" "robots.txt")))) cons)
|
|
|
|
(cons (make-path '("robots.txt")) path-static))
|
|
|
|
(check-equal? (call-with-values (λ () (path-rewriter (make-path '("robots.txt")))) cons)
|
|
|
|
(cons (make-path '("robots.txt")) path-static))
|
|
|
|
(check-equal? (call-with-values (λ () (path-rewriter (make-path '("archive" "minecraft" "styles" "main.css")))) cons)
|
|
|
|
(cons (make-path '("minecraft" "styles" "main.css")) path-archive)))
|
2022-08-30 12:45:37 +00:00
|
|
|
|
|
|
|
(define (static-dispatcher conn old-req)
|
|
|
|
(define old-uri (request-uri old-req))
|
|
|
|
(define old-path (url-path old-uri))
|
2023-02-05 04:56:15 +00:00
|
|
|
(define-values (new-path source-dir) (path-rewriter old-path))
|
2022-08-30 12:45:37 +00:00
|
|
|
(define new-uri (struct-copy url old-uri [path new-path]))
|
|
|
|
(define new-req (struct-copy request old-req [uri new-uri]))
|
|
|
|
((files:make
|
2023-02-05 04:56:15 +00:00
|
|
|
#:url->path (lambda (u) ((make-url->path source-dir) u))
|
|
|
|
#:path->headers (lambda (p) (list (header #"Access-Control-Allow-Origin" #"*")
|
|
|
|
(header #"Referrer-Policy" #"same-origin")))
|
2022-08-30 12:45:37 +00:00
|
|
|
#:path->mime-type (lambda (u) (ext->mime-type (path-get-extension u)))
|
2022-10-30 10:51:15 +00:00
|
|
|
#:cache-no-cache (config-true? 'debug)
|
|
|
|
#:cache-immutable (not (config-true? 'debug))
|
|
|
|
#:cache-max-age (if (config-true? 'debug) #f 604800))
|
2022-08-30 12:45:37 +00:00
|
|
|
conn new-req))
|