/
soc.go
184 lines (164 loc) · 5.22 KB
/
soc.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
178
179
180
181
182
183
184
// Copyright 2021 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package api
import (
"encoding/hex"
"errors"
"io/ioutil"
"net/http"
"strings"
"github.com/gorilla/mux"
"github.com/newswarm-lab/new-bee/pkg/cac"
"github.com/newswarm-lab/new-bee/pkg/jsonhttp"
"github.com/newswarm-lab/new-bee/pkg/postage"
"github.com/newswarm-lab/new-bee/pkg/soc"
"github.com/newswarm-lab/new-bee/pkg/swarm"
)
var errBadRequestParams = errors.New("owner, id or span is not well formed")
type socPostResponse struct {
Reference swarm.Address `json:"reference"`
}
func (s *server) socUploadHandler(w http.ResponseWriter, r *http.Request) {
owner, err := hex.DecodeString(mux.Vars(r)["owner"])
if err != nil {
s.logger.Debugf("soc upload: bad owner: %v", err)
s.logger.Error("soc upload: %v", errBadRequestParams)
jsonhttp.BadRequest(w, "bad owner")
return
}
id, err := hex.DecodeString(mux.Vars(r)["id"])
if err != nil {
s.logger.Debugf("soc upload: bad id: %v", err)
s.logger.Error("soc upload: %v", errBadRequestParams)
jsonhttp.BadRequest(w, "bad id")
return
}
sigStr := r.URL.Query().Get("sig")
if sigStr == "" {
s.logger.Debugf("soc upload: empty signature")
s.logger.Error("soc upload: empty signature")
jsonhttp.BadRequest(w, "empty signature")
return
}
sig, err := hex.DecodeString(sigStr)
if err != nil {
s.logger.Debugf("soc upload: bad signature: %v", err)
s.logger.Error("soc upload: bad signature")
jsonhttp.BadRequest(w, "bad signature")
return
}
data, err := ioutil.ReadAll(r.Body)
if err != nil {
if jsonhttp.HandleBodyReadError(err, w) {
return
}
s.logger.Debugf("soc upload: read chunk data error: %v", err)
s.logger.Error("soc upload: read chunk data error")
jsonhttp.InternalServerError(w, "cannot read chunk data")
return
}
if len(data) < swarm.SpanSize {
s.logger.Debugf("soc upload: chunk data too short")
s.logger.Error("soc upload: %v", errBadRequestParams)
jsonhttp.BadRequest(w, "short chunk data")
return
}
if len(data) > swarm.ChunkSize+swarm.SpanSize {
s.logger.Debugf("soc upload: chunk data exceeds %d bytes", swarm.ChunkSize+swarm.SpanSize)
s.logger.Error("soc upload: chunk data error")
jsonhttp.RequestEntityTooLarge(w, "payload too large")
return
}
ch, err := cac.NewWithDataSpan(data)
if err != nil {
s.logger.Debugf("soc upload: create content addressed chunk: %v", err)
s.logger.Error("soc upload: chunk data error")
jsonhttp.BadRequest(w, "chunk data error")
return
}
ss, err := soc.NewSigned(id, ch, owner, sig)
if err != nil {
s.logger.Debugf("soc upload: address soc error: %v", err)
s.logger.Error("soc upload: address soc error")
jsonhttp.Unauthorized(w, "invalid address")
return
}
sch, err := ss.Chunk()
if err != nil {
s.logger.Debugf("soc upload: read chunk data error: %v", err)
s.logger.Error("soc upload: read chunk data error")
jsonhttp.InternalServerError(w, "cannot read chunk data")
return
}
if !soc.Valid(sch) {
s.logger.Debugf("soc upload: invalid chunk: %v", err)
s.logger.Error("soc upload: invalid chunk")
jsonhttp.Unauthorized(w, "invalid chunk")
return
}
ctx := r.Context()
has, err := s.storer.Has(ctx, sch.Address())
if err != nil {
s.logger.Debugf("soc upload: store has: %v", err)
s.logger.Error("soc upload: store has")
jsonhttp.InternalServerError(w, "storage error")
return
}
if has {
s.logger.Error("soc upload: chunk already exists")
jsonhttp.Conflict(w, "chunk already exists")
return
}
batch, err := requestPostageBatchId(r)
if err != nil {
s.logger.Debugf("soc upload: postage batch id: %v", err)
s.logger.Error("soc upload: postage batch id")
jsonhttp.BadRequest(w, "invalid postage batch id")
return
}
i, err := s.post.GetStampIssuer(batch)
if err != nil {
s.logger.Debugf("soc upload: postage batch issuer: %v", err)
s.logger.Error("soc upload: postage batch issue")
switch {
case errors.Is(err, postage.ErrNotFound):
jsonhttp.BadRequest(w, "batch not found")
case errors.Is(err, postage.ErrNotUsable):
jsonhttp.BadRequest(w, "batch not usable yet")
default:
jsonhttp.BadRequest(w, "postage stamp issuer")
}
return
}
stamper := postage.NewStamper(i, s.signer)
stamp, err := stamper.Stamp(sch.Address())
if err != nil {
s.logger.Debugf("soc upload: stamp: %v", err)
s.logger.Error("soc upload: stamp error")
switch {
case errors.Is(err, postage.ErrBucketFull):
jsonhttp.PaymentRequired(w, "batch is overissued")
default:
jsonhttp.InternalServerError(w, "stamp error")
}
return
}
sch = sch.WithStamp(stamp)
_, err = s.storer.Put(ctx, requestModePut(r), sch)
if err != nil {
s.logger.Debugf("soc upload: chunk write error: %v", err)
s.logger.Error("soc upload: chunk write error")
jsonhttp.BadRequest(w, "chunk write error")
return
}
if strings.ToLower(r.Header.Get(SwarmPinHeader)) == "true" {
if err := s.pinning.CreatePin(ctx, sch.Address(), false); err != nil {
s.logger.Debugf("soc upload: creation of pin for %q failed: %v", sch.Address(), err)
s.logger.Error("soc upload: creation of pin failed")
jsonhttp.InternalServerError(w, nil)
return
}
}
jsonhttp.Created(w, chunkAddressResponse{Reference: sch.Address()})
}