forked from tomnomnom/meg
-
Notifications
You must be signed in to change notification settings - Fork 3
/
request.go
52 lines (44 loc) · 968 Bytes
/
request.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
package main
import (
"net/url"
"strings"
"time"
)
// a request is a wrapper for a URL that we want to request
type request struct {
method string
path string
host string
headers []string
body string
followLocation bool
timeout time.Duration
}
// Hostname returns the hostname part of the request
func (r request) Hostname() string {
u, err := url.Parse(r.host)
// the hostname part is used only for the rate
// limiting and the
if err != nil {
return "unknown"
}
return u.Hostname()
}
// URL returns the full URL to request
func (r request) URL() string {
return r.host + r.path
}
// hasHeader returns true if the request
// has the provided header
func (r request) HasHeader(h string) bool {
norm := func(s string) string {
return strings.ToLower(strings.TrimSpace(s))
}
for _, candidate := range r.headers {
p := strings.SplitN(candidate, ":", 2)
if norm(p[0]) == norm(h) {
return true
}
}
return false
}