-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest.go
More file actions
93 lines (77 loc) · 1.76 KB
/
Copy pathrequest.go
File metadata and controls
93 lines (77 loc) · 1.76 KB
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
package alpha
import (
"mime"
"strings"
"net/url"
"net/http"
)
type Request struct {
In *http.Request
Res *Response
Headers http.Header
Query url.Values
}
func (req *Request) get(field string) string {
var val string
field = strings.Title(strings.ToLower(field))
if field == "Referer" || field == "Referrer" {
val = req.Headers.Get("Referrer")
if val == "" {
val = req.Headers.Get("Referer")
}
} else {
val = req.Headers.Get(field)
}
return val
}
func (req *Request) Get(field string) string {
return req.get(field)
}
func (req *Request) Header(field string) string {
return req.get(field)
}
//
// Check if the incoming request contains the "Content-Type"
// header field, and it contains the give mime `type`.
//
// Examples:
//
// // With Content-Type: text/html; charset=utf-8
// req.Is("html")
// req.Is("text/html")
// req.Is("text/*")
// // => true
//
func (req *Request) Is(mtype string) bool {
ct := req.get("Content-Type")
if ct == "" {
return false
}
ct = strings.Split(ct, ";")[0]
if ^strings.Index(mtype, "/") == 0 {
if !strings.HasPrefix(mtype, ".") {
mtype = "." + mtype
}
mtype = mime.TypeByExtension(mtype)
mtype = strings.Split(mtype, ";")[0]
}
if ^strings.Index(mtype, "*") != 0 {
ts := strings.Split(mtype, "/")
cts := strings.Split(ct, "/")
if "*" == ts[0] && ts[1] == cts[1] {
return true
}
if "*" == ts[1] && ts[0] == cts[0] {
return true
}
return false
}
return ^strings.Index(ct, mtype) != 0
}
func (req *Request) Xhr() bool {
val := req.get("X-Requested-With")
return val != "" && "xmlhttprequest" == strings.ToLower(val)
}
func (req *Request) Path() string {
return req.In.URL.Path
}