Ticket #1838: redirect.scm

File redirect.scm, 917 bytes (added by Woodrow E Douglass, 19 months ago)
Line 
1(import spiffy intarweb uri-common)
2
3; Generates a handler that can be used in vhost-map that will cause all requests to that URL to be rewritten to the domain specified in 'to'.
4(define (canonicalise-domain to)
5 (let ((to (uri-reference to)))
6 (assert (equal? '(/ "") (uri-path to)))
7 (assert (null? (uri-query to)))
8 ; We don't see fragments on the server and choose not to care about usernames and password fields.
9 (lambda (continue)
10 (let* ((old-u (request-uri (current-request)))
11 (new-u (update-uri old-u
12 scheme: (or (uri-scheme to) (uri-scheme old-u))
13 port: (or (uri-port to) (uri-port old-u))
14 host: (or (uri-host to) (uri-host old-u)))))
15 (with-headers `((location ,new-u))
16 (lambda () (send-status 'moved-permanently)))))))
17
18(vhost-map `(("localhost"
19 . ,(canonicalise-domain "https://posttestserver.dev"))))
20(server-port 8080)
21(start-server)