-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest.go
138 lines (115 loc) · 2.91 KB
/
rest.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
package streisand
import (
"bytes"
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"log"
"net/http"
"os"
"strings"
"github.com/Jille/convreq"
"github.com/Jille/convreq/respond"
)
const BytesPerHash = 32
type Hash [BytesPerHash]byte
func (h *Hash) UnmarshalText(text []byte) error {
// TODO: Implement
return errors.New("UnmarshalText is not yet implemented")
}
func (h *Hash) MarshalText() ([]byte, error) {
return []byte(hex.EncodeToString(h[:])), nil
}
func (h *Hash) String() string {
return hex.EncodeToString(h[:])
}
func (h *Hash) XorInto(s []byte) {
if len(s) != BytesPerHash {
panic("xoring hash into slice of incorrect size")
}
// TODO: optimize?
for i := 0; i < BytesPerHash; i++ {
s[i] ^= h[i]
}
}
func (h *Hash) Xor(other *Hash) Hash {
result := *other
h.XorInto(result[:])
return result
}
func (h *Hash) Equals(other *Hash) bool {
return bytes.Compare(h[:], other[:]) == 0
}
var zeroHash Hash
func (h *Hash) IsZero() bool {
return h.Equals(&zeroHash)
}
func (h *Hash) PrefixToNumber(prefixLength uint) uint32 {
p32 := binary.BigEndian.Uint32(h[0:4])
return p32 >> (32 - prefixLength)
}
type Prefix struct {
Hash Hash
Length int
}
func httpPathToHash(path string) (Hash, bool) {
var ret Hash
n, err := hex.Decode(ret[:], []byte(strings.TrimPrefix(strings.TrimPrefix(path, "/internal"), "/blob/")))
if err != nil {
return Hash{}, false
}
if len(ret) != n {
return Hash{}, false
}
return ret, true
}
func (s *server) handleGetBlob(
r *http.Request, allowForward bool) convreq.HttpResponse {
if r.Method != "GET" {
return respond.MethodNotAllowed("Method Not Allowed")
}
hash, ok := httpPathToHash(r.URL.Path)
if !ok {
return respond.BadRequest("invalid hash")
}
s.mutex.RLock()
defer s.mutex.RUnlock()
fh, err := s.store.Get(hash[:])
if os.IsNotExist(err) {
if allowForward {
// TODO try with another
}
return respond.NotFound("blob not found")
}
st, err := fh.Stat()
if err != nil {
return respond.Error(err)
}
hdrs := http.Header{}
hdrs.Set("Content-Length", fmt.Sprint(st.Size()))
hdrs.Set("Last-Modified", st.ModTime().UTC().Format(http.TimeFormat))
hdrs.Set("Etag", fmt.Sprintf(`"%s"`, hex.EncodeToString(hash[:])))
hdrs.Set("Cache-Control", "max-age=604800, immutable, stale-if-error=604800")
return respond.WithHeaders(respond.Reader(fh), hdrs)
}
func (s *server) handleGetList(r *http.Request) convreq.HttpResponse {
if r.Method != "GET" {
return respond.MethodNotAllowed("Method Not Allowed")
}
s.mutex.RLock()
defer s.mutex.RUnlock()
var ret []string
if err := s.store.Scan([]byte("1d"), 0, func(h []byte) {
ret = append(ret, hex.EncodeToString(h))
}); err != nil {
return respond.Error(err)
}
return respond.String(fmt.Sprintf("%d entries\n", len(ret)) + strings.Join(ret, "\n"))
}
func warnOnErr(err error, message string, v ...interface{}) {
if err == nil {
return
}
log.Printf("warning: %s: %v", fmt.Sprintf(message, v...), err)
}