-
Notifications
You must be signed in to change notification settings - Fork 35
/
ldp.go
177 lines (159 loc) · 3.83 KB
/
ldp.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package gold
import (
"bytes"
"crypto/md5"
"crypto/rand"
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"os"
"strings"
)
type linkheader struct {
uri string
rel string
}
// Linkheaders holds the list of Link headers
type Linkheaders struct {
headers []*linkheader
}
type preferheader struct {
omit []string
include []string
}
// Preferheaders holds the list of Prefer headers
type Preferheaders struct {
headers []*preferheader
}
// ParsePreferHeader parses the LDP specific Prefer header
func ParsePreferHeader(header string) *Preferheaders {
ret := new(Preferheaders)
for _, v := range strings.Split(header, ",") {
item := new(preferheader)
v = strings.TrimSpace(v)
if strings.HasPrefix(v, "return=representation") {
for _, s := range strings.Split(v, ";") {
s = strings.TrimSpace(s)
if strings.HasPrefix(s, "omit") {
s = strings.TrimLeft(s, "omit=")
s = strings.TrimLeft(s, "\"")
s = strings.TrimRight(s, "\"")
for _, u := range strings.Split(s, " ") {
item.omit = append(item.omit, u)
}
}
if strings.HasPrefix(s, "include") {
s = strings.TrimLeft(s, "include=")
s = strings.TrimLeft(s, "\"")
s = strings.TrimRight(s, "\"")
for _, u := range strings.Split(s, " ") {
item.include = append(item.include, u)
}
}
}
ret.headers = append(ret.headers, item)
}
}
return ret
}
// Omits returns the types of resources to omit when listing an LDPC
func (p *Preferheaders) Omits() []string {
var ret []string
for _, v := range p.headers {
for _, u := range v.omit {
ret = append(ret, u)
}
}
return ret
}
// Includes returns the types of resources to include when listing an LDPC
func (p *Preferheaders) Includes() []string {
var ret []string
for _, v := range p.headers {
for _, u := range v.include {
ret = append(ret, u)
}
}
return ret
}
// ParseLinkHeader is a generic Link header parser
func ParseLinkHeader(header string) *Linkheaders {
ret := new(Linkheaders)
for _, v := range strings.Split(header, ", ") {
item := new(linkheader)
for _, s := range strings.Split(v, ";") {
s = strings.TrimSpace(s)
if strings.HasPrefix(s, "<") && strings.HasSuffix(s, ">") {
s = strings.TrimLeft(s, "<")
s = strings.TrimRight(s, ">")
item.uri = s
} else if strings.Index(s, "rel=") >= 0 {
s = strings.TrimLeft(s, "rel=")
if strings.HasPrefix(s, "\"") || strings.HasPrefix(s, "'") {
s = s[1:]
}
if strings.HasSuffix(s, "\"") || strings.HasSuffix(s, "'") {
s = s[:len(s)-1]
}
item.rel = s
}
}
ret.headers = append(ret.headers, item)
}
return ret
}
// MatchRel attempts to match a Link header based on the rel value
func (l *Linkheaders) MatchRel(rel string) string {
for _, v := range l.headers {
if v.rel == rel {
return v.uri
}
}
return ""
}
// MatchURI attempts to match a Link header based on the href value
func (l *Linkheaders) MatchURI(uri string) bool {
for _, v := range l.headers {
if v.uri == uri {
return true
}
}
return false
}
// NewUUID generates a new UUID string
func NewUUID() string {
uuid := make([]byte, 16)
io.ReadFull(rand.Reader, uuid)
uuid[8] = uuid[8]&^0xc0 | 0x80
uuid[6] = uuid[6]&^0xf0 | 0x40
return hex.EncodeToString(uuid)
}
// NewETag generates ETag
func NewETag(path string) (string, error) {
var (
hash []byte
md5s string
err error
)
stat, err := os.Stat(path)
if err != nil {
return "", err
}
if stat.IsDir() {
if files, err := ioutil.ReadDir(path); err == nil {
if len(files) == 0 {
md5s += stat.ModTime().String()
}
for _, file := range files {
md5s += file.ModTime().String() + fmt.Sprintf("%d", file.Size())
}
}
} else {
md5s += stat.ModTime().String() + fmt.Sprintf("%d", stat.Size())
}
h := md5.New()
io.Copy(h, bytes.NewBufferString(md5s))
hash = h.Sum([]byte(""))
return hex.EncodeToString(hash), err
}