-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.go
132 lines (123 loc) · 2.66 KB
/
helpers.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"bufio"
"encoding/base32"
"fmt"
"hash/fnv"
"net/http"
"strconv"
"strings"
"time"
)
func (u *URI) Consumes(v *URI) bool {
if u == nil && v == nil {
return true
} else if u == nil && v != nil || u != nil && v == nil {
return false
} else {
if u.Target == v.Target && u.XMLName == v.XMLName {
return true
} else {
return false
}
}
}
func (entry *Entry) UpdatedTime() (t time.Time, err error) {
if entry == nil {
err = fmt.Errorf("nil pointer dereference")
} else {
t = entry.Updated.T
}
return
}
func (d *DateConstruct) Set(t time.Time) (err error) {
if d == nil {
err = fmt.Errorf("nil pointer dereference")
} else {
d.T = t
}
return
}
func (feed *Feed) ETag() (etag string, err error) {
hasher := fnv.New64a()
bw := bufio.NewWriter(hasher)
if feed == nil {
err = fmt.Errorf("nil pointer dereference")
return
} else if e := feed.Id.MarshalTo(bw); e != nil {
err = e
return
} else if e := feed.Updated.MarshalTo(bw); e != nil {
err = e
return
} else if e := bw.Flush(); e != nil {
err = e
return
}
return base32.StdEncoding.EncodeToString(hasher.Sum(nil))[:13], nil
}
func (entry *Entry) ETag() (etag string, err error) {
hasher := fnv.New64a()
bw := bufio.NewWriter(hasher)
if entry == nil {
err = fmt.Errorf("nil pointer dereference")
return
} else if e := entry.Id.MarshalTo(bw); e != nil {
err = e
return
} else if e := entry.Updated.MarshalTo(bw); e != nil {
err = e
return
} else if e := bw.Flush(); e != nil {
err = e
return
}
return base32.StdEncoding.EncodeToString(hasher.Sum(nil))[:13], nil
}
// etag
func matchETag(etag string, header_val string) (isSet bool, match bool, err error) {
if header_val != "" && etag == "" {
return true, false, nil
} else if header_val == "" {
return false, false, nil
} else if header_val == "*" {
return true, true, nil
}
for _, quote_et := range strings.Split(header_val, ", ") {
if unquote_et, e := strconv.Unquote(quote_et); e != nil {
err = e
return
} else if unquote_et == etag {
return true, true, nil
}
}
return true, false, nil
}
func IfMatchIfNoneMatch(etag string, ifmatch string, ifnonematch string) (proceed bool, err error) {
// If-None-Match
if isSet, match, e := matchETag(etag, ifnonematch); e != nil {
err = &HTTPError{
code: http.StatusBadRequest,
message: e.Error(),
}
return
} else if !isSet {
// continue
} else if match {
return
}
// If-Match
if isSet, match, e := matchETag(etag, ifmatch); e != nil {
err = &HTTPError{
code: http.StatusBadRequest,
message: e.Error(),
}
return
} else if !isSet {
// continue
} else if !match {
return
}
proceed = true
return
}