-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
78 lines (63 loc) · 2.4 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
package gonertia
import (
"net/http"
"strings"
)
// IsInertiaRequest returns true if the request is an Inertia request.
func IsInertiaRequest(r *http.Request) bool {
return r.Header.Get("X-Inertia") != ""
}
// setInertiaInResponse sets Inertia header in the response.
func setInertiaInResponse(w http.ResponseWriter) {
w.Header().Set("X-Inertia", "true")
}
// setInertiaVaryInResponse sets Inertia Vary header in the response.
func setInertiaVaryInResponse(w http.ResponseWriter) {
w.Header().Set("Vary", "X-Inertia")
}
// setInertiaLocationInResponse sets Inertia location header in the response.
func setInertiaLocationInResponse(w http.ResponseWriter, url string) {
w.Header().Set("X-Inertia-Location", url)
setResponseStatus(w, http.StatusConflict)
}
// setResponseStatus sets status code in the response.
func setResponseStatus(w http.ResponseWriter, status int) {
w.WriteHeader(status)
}
// partialDataFromRequest returns Inertia partial data from request headers.
func partialDataFromRequest(r *http.Request) []string {
header := r.Header.Get("X-Inertia-Partial-Data")
if header == "" {
return nil
}
return strings.Split(header, ",")
}
// partialComponentFromRequest returns Inertia partial component from request headers.
func partialComponentFromRequest(r *http.Request) string {
return r.Header.Get("X-Inertia-Partial-Component")
}
// setResponseStatus returns Inertia version from request headers.
func inertiaVersionFromRequest(r *http.Request) string {
return r.Header.Get("X-Inertia-Version")
}
// redirectResponse creates redirect response.
func redirectResponse(w http.ResponseWriter, r *http.Request, url string, status ...int) {
http.Redirect(w, r, url, firstOr[int](status, http.StatusFound))
}
// setJSONResponse sets the type of response content in JSON format.
func setJSONResponse(w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json")
}
// setHTMLResponse sets the type of response content in HTML format.
func setHTMLResponse(w http.ResponseWriter) {
w.Header().Set("Content-Type", "text/html")
}
// isSeeOtherRedirectMethod returns HTTP methods that
// must have 303 response status, instead of 302.
func isSeeOtherRedirectMethod(method string) bool {
return method == http.MethodPost || method == http.MethodPatch || method == http.MethodPut
}
// refererFromRequest returns referer header from request.
func refererFromRequest(r *http.Request) string {
return r.Referer()
}