Skip to content

Commit 4601f4d

Browse files
LiberalArtistjeapostrophe
authored andcommitted
support 307 redirects for redirect-to & use see-other for redirect/get
- Adds a `temporarily/same-method` status for `redirect-to` corresponding to `307 Temporary Redirect`. - Changes `redirect/get` and `redirect/get/forget` to use the `see-other` status instead of `temporarily`. This avoids issues with methods other than `GET` and (in practice, on all but very old browsers) `POST`. - Changes the version of `redirect/get` from `#lang web-server` to to accept a `#:headers` argument for consistency with the stateful version. - Expands the documentation for `redirect-to` to discuss specific HTTP status codes and usage considerations.
1 parent 15dce76 commit 4601f4d

6 files changed

Lines changed: 118 additions & 42 deletions

File tree

‎web-server-doc/web-server/scribblings/http.scrbl‎

Lines changed: 88 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -541,28 +541,94 @@ available (@racket[make-secret-salt/file]),
541541

542542
@defmodule[web-server/http/redirect]{
543543

544-
@defproc[(redirect-to [uri non-empty-string?]
545-
[perm/temp redirection-status? temporarily]
546-
[#:headers headers (listof header?) (list)])
547-
response?]{
548-
Generates an HTTP response that redirects the browser to @racket[uri],
549-
while including the @racket[headers] in the response.
550-
551-
Example:
552-
@racket[(redirect-to "http://www.add-three-numbers.com" permanently)]
553-
}
554-
555-
@defproc[(redirection-status? [v any/c])
556-
boolean?]{
557-
Determines if @racket[v] is one of the following values.
558-
}
559-
560-
@defthing[permanently redirection-status?]{A @racket[redirection-status?] for permanent redirections.}
561-
562-
@defthing[temporarily redirection-status?]{A @racket[redirection-status?] for temporary redirections.}
563-
564-
@defthing[see-other redirection-status?]{A @racket[redirection-status?] for "see-other" redirections.}
565-
544+
@deftogether[
545+
(@defproc[(redirect-to [uri non-empty-string?]
546+
[status redirection-status? temporarily]
547+
[#:headers headers (listof header?) '()])
548+
response?]
549+
@defproc[(redirection-status? [v any/c]) boolean?]
550+
@defthing[temporarily redirection-status?]
551+
@defthing[temporarily/same-method redirection-status?]
552+
@defthing[see-other redirection-status?]
553+
@defthing[permanently redirection-status?])]{
554+
The function @racket[redirect-to]
555+
generates an HTTP response that redirects the browser to @racket[uri],
556+
while including the @racket[headers] in the response.
557+
The @racket[status] argument is a @deftech{redirection status}
558+
value, which determines the specific type of HTTP redirect to be used.
559+
560+
The default @tech{redirection status}, @racket[temporarily],
561+
is preserved for backwards compatibility:
562+
new code should usually use either @racket[temporarily/same-method]
563+
or @racket[see-other], instead.
564+
The @racket[temporarily] @tech{redirection status} corresponds to
565+
@hyperlink["https://tools.ietf.org/html/rfc7231#section-6.4.3"]{
566+
@litchar{302 Found}}.
567+
Unfortunately, browsers have not implemented this status consistently
568+
for methods other than @litchar{GET} and (in practice, with all but some
569+
very old browsers) @litchar{POST}.
570+
571+
The @racket[temporarily/same-method] @tech{redirection status}
572+
uses @hyperlink["https://tools.ietf.org/html/rfc7231#section-6.4.7"]{
573+
@litchar{307 Temporary Redirect}}.
574+
This redirects the browser to @racket[uri] using the same HTTP method
575+
as the original request.
576+
577+
The @racket[see-other] @tech{redirection status} corresponds to
578+
@hyperlink["https://tools.ietf.org/html/rfc7231#section-6.4.4"]{
579+
@litchar{303 See Other}}.
580+
It is most often used to implement the @deftech{Post-Redirect-Get}
581+
pattern: as a response to a request using @litchar{POST} or
582+
another HTTP method with side-effects, it causes the browser to
583+
perform a @litchar{GET} or @litchar{HEAD} request for @racket[uri],
584+
which gives a response to the original @litchar{POST} request.
585+
This prevents the @onscreen{Back} and @onscreen{Refresh} buttons
586+
from duplicating effects, such as making a purchase or
587+
adding items to a database.
588+
The web server provides @racket[redirect/get] for added convienience
589+
with @tech{Post-Redirect-Get}.
590+
591+
The @racket[permanently] @tech{redirection status} uses the HTTP status
592+
@hyperlink["https://tools.ietf.org/html/rfc7231#section-6.4.2"]{
593+
@litchar{301 Moved Permanently}}.
594+
It is like @racket[temporarily], except that, as the name suggests,
595+
it signifies that the move is permanent and that search engines,
596+
for example, should use @racket[uri] instead of the URI of the
597+
orriginal request.
598+
Unfortunately, @racket[permanently] is also like @racket[temporarily]
599+
in that browsers have implemented it inconsistently for
600+
methods other than @litchar{GET} and @litchar{HEAD}:
601+
in particular, @hyperlink["https://tools.ietf.org/html/rfc7231#section-6.4.2"]{
602+
RFC 7231} permits that, ``for historical reasons, a user agent @bold{may}
603+
change the request method from @litchar{POST} to @litchar{GET} for the subsequent request.
604+
When it is important to ensure that the request to @racket[uri] use the same method,
605+
there are some possible alternatives:
606+
@itemlist[
607+
@item{RFC 7231 suggests using @litchar{307 Temporary Redirect},
608+
i.e. @racket[temporarily/same-method].
609+
This has the disadvantage that search engines and others won't
610+
update references to the old URI.}
611+
@item{@hyperlink["https://tools.ietf.org/html/rfc7538"]{RFC 7538}
612+
specifies a new HTTP status, @litchar{308 Permanent Redirect},
613+
which fobids changing the request method, analagously to
614+
@litchar{307 Temporary Redirect}.
615+
However, the RFC also highlights some important
616+
@hyperlink["https://tools.ietf.org/html/rfc7538#section-4"]{
617+
deployment considerations} for this status.
618+
In particular, older browsers---including, as of this writing,
619+
some that remain in
620+
@hyperlink["https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/308#Browser_compatibility"]{
621+
relatively common use}---do not understand this status and will
622+
fall back to the semantics of
623+
@hyperlink["https://tools.ietf.org/html/rfc7231#section-6.4.1"]{
624+
@litchar{300 Multiple Choices}}, which is often undesirable.}
625+
@item{The application can note the method of the original request
626+
and use @racket[permanently] for @litchar{GET} and @litchar{HEAD} requests
627+
or one of the other alternatives for other methods.}]
628+
629+
Example:
630+
@racket[(redirect-to "http://www.add-three-numbers.com" permanently)]
631+
}
566632
}
567633

568634
@; ------------------------------------------------------------

‎web-server-doc/web-server/scribblings/lang.scrbl‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,7 @@
4747
Like @racket[send/suspend/url/dispatch] but with a string URL representation.
4848
}
4949

50-
@deftogether[(
51-
@defproc[(redirect/get) request?]
52-
)]{
5350

51+
@defproc[(redirect/get [#:headers hs (listof header?) empty]) request?]{
5452
See @racketmodname[web-server/servlet/web].}
5553
}

‎web-server-doc/web-server/scribblings/web.scrbl‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,15 +152,19 @@ the request is returned from this call to @racket[send/suspend].
152152

153153
@defproc[(redirect/get [#:headers hs (listof header?) empty])
154154
request?]{
155-
Calls @racket[send/suspend] with @racket[redirect-to], passing @racket[hs] as the headers.
155+
Calls @racket[send/suspend] with @racket[redirect-to],
156+
passing @racket[hs] as the headers and
157+
@racket[see-other] as the @tech{redirection status}.
156158

157-
This implements the Post-Redirect-Get pattern.
158-
Use this to prevent the @onscreen["Refresh"] button from duplicating effects, such as adding items to a database.
159+
This implements the @tech{Post-Redirect-Get} pattern.
160+
Use this to prevent the @onscreen["Refresh"] button from duplicating effects,
161+
such as adding items to a database.
159162
}
160163

161164
@defproc[(redirect/get/forget [#:headers hs (listof header?) empty])
162165
request?]{
163-
Calls @racket[send/forward] with @racket[redirect-to], passing @racket[hs] as the headers.
166+
Like @racket[redirect/get], but using @racket[send/forward]
167+
instead of @racket[send/suspend].
164168
}
165169

166170
@defthing[current-servlet-continuation-expiration-handler
Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
#lang racket/base
22
(require racket/contract
33
(only-in racket/string non-empty-string?)
4-
web-server/private/util
54
web-server/http/response-structs
65
web-server/http/request-structs)
76

8-
; redirection-status = (make-redirection-status nat bytes)
9-
(define-struct redirection-status (code message))
7+
; redirection-status = (redirection-status nat bytes)
8+
(struct redirection-status (code message))
109

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

1521
; : str [redirection-status] -> response
1622
(define (redirect-to
@@ -19,7 +25,7 @@
1925
#:headers [headers (list)])
2026
(response (redirection-status-code perm/temp)
2127
(redirection-status-message perm/temp)
22-
(current-seconds) #"text/html"
28+
(current-seconds) #f
2329
(list* (make-header #"Location" (string->bytes/utf-8 uri))
2430
headers)
2531
void))
@@ -31,4 +37,5 @@
3137
[redirection-status? (any/c . -> . boolean?)]
3238
[permanently redirection-status?]
3339
[temporarily redirection-status?]
40+
[temporarily/same-method redirection-status?]
3441
[see-other redirection-status?])

‎web-server-lib/web-server/lang/web.rkt‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
[send/suspend/url ((url? . -> . can-be-response?) . -> . request?)]
4545
[send/suspend/url/dispatch ((((request? . -> . any/c) . -> . url?) . -> . can-be-response?)
4646
. -> . any/c)]
47-
[redirect/get (-> request?)])
47+
[redirect/get (->* () (#:headers (listof header?)) request?)])
4848

4949
;; initial-servlet : (request -> response) -> (request -> can-be-response?)
5050
(define (initialize-servlet start)
@@ -122,5 +122,7 @@
122122
(read (open-input-bytes kont)))]
123123
[_ #f])))
124124

125-
(define (redirect/get)
126-
(send/suspend/url (lambda (k-url) (redirect-to (url->string k-url) temporarily))))
125+
(define (redirect/get #:headers [hs null])
126+
(send/suspend/url
127+
(lambda (k-url)
128+
(redirect-to (url->string k-url) see-other #:headers hs))))

‎web-server-lib/web-server/servlet/web.rkt‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#lang racket/base
22
(require racket/contract
3-
racket/list
43
net/url)
54
(require web-server/managers/manager
65
web-server/private/util
@@ -145,8 +144,8 @@
145144
;; ************************************************************
146145
;; HIGHER-LEVEL EXPORTS
147146

148-
(define ((make-redirect/get send/suspend) #:headers [hs empty])
149-
(send/suspend (lambda (k-url) (redirect-to k-url temporarily #:headers hs))))
147+
(define ((make-redirect/get send/suspend) #:headers [hs null])
148+
(send/suspend (lambda (k-url) (redirect-to k-url see-other #:headers hs))))
150149

151150
; redirect/get : -> request
152151
(define redirect/get (make-redirect/get send/suspend))

0 commit comments

Comments
 (0)