/
redirect.go
71 lines (59 loc) · 1.84 KB
/
redirect.go
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package webutil
import (
"encoding/json"
"errors"
"fmt"
"html"
"net/http"
"net/url"
)
type Redirect struct {
URL *url.URL
Code int
}
// SendRedirect is deprecated. http.Redirect() should be used instead
func SendRedirect(w http.ResponseWriter, target string, code int) {
w.Header().Set("Location", target)
w.Header().Set("Content-Type", "text/html")
w.Header().Set("X-Content-Type-Options", "nosniff")
w.WriteHeader(code) // http.StatusFound
fmt.Fprintf(w, "You are being redirected to <a href=\"%s\">%s</a>. If you see this message, please manually follow the link.", html.EscapeString(target), html.EscapeString(target))
// try various stuff to cause the redirect to happen in case header failed to happen
if js, err := json.Marshal(target); err == nil {
fmt.Fprintf(w, "<script language=\"javascript\">window.location = %s;</script>", js)
}
fmt.Fprintf(w, "<meta http-equiv=\"Refresh\" content=\"0; url=%s\"/>", html.EscapeString(target))
fmt.Fprintf(w, "<meta http-equiv=\"Location\" content=\"%s\"/>", html.EscapeString(target))
}
// code can be one of http.StatusMovedPermanently or http.StatusFound or
// any 3xx http status code
func RedirectErrorCode(u *url.URL, code int) error {
// generate a redirect error
n := &Redirect{URL: new(url.URL), Code: code}
// copy url
*n.URL = *u
return n
}
func RedirectError(u *url.URL) error {
// generate a redirect error
n := &Redirect{URL: new(url.URL), Code: http.StatusFound}
// copy url
*n.URL = *u
return n
}
func (e *Redirect) Error() string {
return fmt.Sprintf("Redirect required to %s", e.URL)
}
func (e *Redirect) ServeHTTP(w http.ResponseWriter, req *http.Request) {
http.Redirect(w, req, e.URL.String(), e.Code)
}
func (e *Redirect) HTTPStatus() int {
return e.Code
}
func IsRedirect(e error) http.Handler {
var r *Redirect
if errors.As(e, &r) {
return r
}
return nil
}