-
Notifications
You must be signed in to change notification settings - Fork 0
/
post.go
153 lines (123 loc) · 3.09 KB
/
post.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
package streisand
import (
"encoding/hex"
"io"
"log"
"net/http"
"github.com/Jille/convreq"
"github.com/Jille/convreq/respond"
)
func (s *server) handlePostBlob(r *http.Request) convreq.HttpResponse {
if r.Method != "POST" {
return respond.MethodNotAllowed("Method Not Allowed")
}
// TODO: Simultaneously sync it to peers.
hash, err := s.Post(r.Body)
if err != nil {
return respond.Error(err)
}
return respond.String(hex.EncodeToString(hash))
}
func (s *server) handleInternalPostBlob(r *http.Request) convreq.HttpResponse {
if r.Method != "POST" {
return respond.MethodNotAllowed("Method Not Allowed")
}
var h Hash
n, err := hex.Decode(h[:], []byte(r.Header.Get("X-StreiSANd-Hash")))
if err != nil {
return respond.BadRequest("couldn't decode hash in X-StreiSANd-Hash")
}
if len(h) != n {
return respond.BadRequest("wrong hash length in X-StreiSANd-Hash")
}
has, err := s.store.Has(h[:])
if err != nil {
return respond.Error(err)
}
if has {
go func() {
warnOnErr(s.checkXorsumOf(&h),
"checking leaf xorsum of %s", &h)
}()
// TODO: See if there's a better code than HTTP 409 Conflict.
return respond.OverrideResponseCode(respond.String("already exists"), 409)
}
// TODO: abort if hash is not equal to h (X-StreiSANd-Hash)
hash, err := s.Post(r.Body)
if err != nil {
return respond.Error(err)
}
return respond.String(hex.EncodeToString(hash))
}
func (s *server) Post(blob io.ReadCloser) (hash []byte, err error) {
w, err := s.store.NewWriter()
if err != nil {
return
}
defer w.Abort()
if _, err = io.Copy(w, blob); err != nil {
return
}
s.mutex.Lock()
defer s.mutex.Unlock()
if err = blob.Close(); err != nil {
return
}
if err = w.Close(); err != nil {
return
}
hash = w.Hash()
if w.IsNew() {
s.xors.Add((*Hash)(hash))
}
return
}
func (s *server) handleDebugAddXor(r *http.Request) convreq.HttpResponse {
if r.Method != "POST" {
return respond.MethodNotAllowed("Method Not Allowed")
}
var h Hash
n, err := hex.Decode(h[:], []byte(r.Header.Get("X-StreiSANd-Hash")))
if err != nil {
return respond.BadRequest("couldn't decode hash in X-StreiSANd-Hash")
}
if len(h) != n {
return respond.BadRequest("wrong hash length in X-StreiSANd-Hash")
}
s.mutex.Lock()
defer s.mutex.Unlock()
s.xors.Add(&h)
xorsum := s.xors.GetLeaf(&h)
return respond.String(xorsum.String())
}
func (s *server) checkXorsumOf(h *Hash) (err error) {
if s.conf.Debug {
log.Printf("checking xorsum of %s", h)
}
s.mutex.Lock()
defer s.mutex.Unlock()
// compute the difference between xorsum stored
// and the xorsum computed from the disk store
storedXorsum := s.xors.GetLeaf(h)
var computedXorsum Hash
if err := s.store.Scan(h[:], uint8(s.xors.Depth()),
func(hash []byte) {
(*Hash)(hash).XorInto(computedXorsum[:])
}); err != nil {
return err
}
diff := storedXorsum.Xor(&computedXorsum)
if diff.IsZero() {
// all ok
return
}
if diff.Equals(h) {
log.Printf("warning: adding missing hash %s to xorsum", h)
s.xors.Add(h)
return
}
// something serious is wrong;
panic("corrupted xorsum table")
// TODO: start repairing instead
return
}