File: redirect.rkt

package info (click to toggle)
racket 7.9%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 178,684 kB
  • sloc: ansic: 282,112; lisp: 234,887; pascal: 70,954; sh: 27,112; asm: 16,268; makefile: 4,613; cpp: 2,715; ada: 1,681; javascript: 1,244; cs: 879; exp: 499; csh: 422; python: 274; xml: 106; perl: 104
file content (41 lines) | stat: -rw-r--r-- 1,534 bytes parent folder | download | duplicates (5)
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
#lang racket/base
(require racket/contract
         (only-in racket/string non-empty-string?)
         web-server/http/response-structs
         web-server/http/request-structs)

; redirection-status = (redirection-status nat bytes)
(struct redirection-status (code message))

(define permanently
  ;; NOTE: 308 permanent redirect is not supported by
  ;; Internet Explorer on Windows 7 or 8.1 as of 2019-02-26.
  ;; (IE on Windows 10 does support it.)
  ;; https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/308
  ;; https://tools.ietf.org/html/rfc7538#section-4
  (redirection-status 301 #"Moved Permanently"))
(define temporarily (redirection-status 302 #"Found"))
(define temporarily/same-method (redirection-status 307 #"Temporary Redirect"))
(define see-other (redirection-status 303 #"See Other"))

; : str [redirection-status] -> response
(define (redirect-to 
         uri
         [perm/temp temporarily]
         #:headers [headers (list)])
  (response (redirection-status-code perm/temp)
            (redirection-status-message perm/temp)
            (current-seconds) #f
            (list* (make-header #"Location" (string->bytes/utf-8 uri))
                   headers)
            void))

(provide/contract
 [redirect-to
  (->* (non-empty-string?) (redirection-status? #:headers (listof header?))
       response?)]
 [redirection-status? (any/c . -> . boolean?)]
 [permanently redirection-status?]
 [temporarily redirection-status?]
 [temporarily/same-method redirection-status?]
 [see-other redirection-status?])