-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathhttp.go
106 lines (83 loc) · 2.62 KB
/
http.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package gonertia
import (
"net/http"
"strings"
)
const (
headerInertia = "X-Inertia"
headerInertiaLocation = "X-Inertia-Location"
headerInertiaPartialData = "X-Inertia-Partial-Data"
headerInertiaPartialExcept = "X-Inertia-Partial-Except"
headerInertiaPartialComponent = "X-Inertia-Partial-Component"
headerInertiaVersion = "X-Inertia-Version"
headerInertiaReset = "X-Inertia-Reset"
headerVary = "Vary"
headerContentType = "Content-Type"
)
// IsInertiaRequest returns true if the request is an Inertia request.
func IsInertiaRequest(r *http.Request) bool {
return r.Header.Get(headerInertia) != ""
}
func setInertiaInResponse(w http.ResponseWriter) {
w.Header().Set(headerInertia, "true")
}
func deleteInertiaInResponse(w http.ResponseWriter) {
w.Header().Del(headerInertia)
}
func setInertiaVaryInResponse(w http.ResponseWriter) {
w.Header().Set(headerVary, headerInertia)
}
func deleteVaryInResponse(w http.ResponseWriter) {
w.Header().Del(headerVary)
}
func setInertiaLocationInResponse(w http.ResponseWriter, url string) {
w.Header().Set(headerInertiaLocation, url)
}
func setResponseStatus(w http.ResponseWriter, status int) {
w.WriteHeader(status)
}
func onlyFromRequest(r *http.Request) []string {
header := r.Header.Get(headerInertiaPartialData)
if header == "" {
return nil
}
return strings.Split(header, ",")
}
func exceptFromRequest(r *http.Request) []string {
header := r.Header.Get(headerInertiaPartialExcept)
if header == "" {
return nil
}
return strings.Split(header, ",")
}
func resetFromRequest(r *http.Request) []string {
header := r.Header.Get(headerInertiaReset)
if header == "" {
return nil
}
return strings.Split(header, ",")
}
func partialComponentFromRequest(r *http.Request) string {
return r.Header.Get(headerInertiaPartialComponent)
}
func inertiaVersionFromRequest(r *http.Request) string {
return r.Header.Get(headerInertiaVersion)
}
func redirectResponse(w http.ResponseWriter, r *http.Request, url string, status ...int) {
http.Redirect(w, r, url, firstOr[int](status, http.StatusFound))
}
func setJSONResponse(w http.ResponseWriter) {
w.Header().Set(headerContentType, "application/json")
}
func setJSONRequest(r *http.Request) {
r.Header.Set(headerContentType, "application/json")
}
func setHTMLResponse(w http.ResponseWriter) {
w.Header().Set(headerContentType, "text/html")
}
func isSeeOtherRedirectMethod(method string) bool {
return method == http.MethodPut || method == http.MethodPatch || method == http.MethodDelete
}
func refererFromRequest(r *http.Request) string {
return r.Referer()
}