-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathconnection.go
More file actions
308 lines (273 loc) · 8.99 KB
/
connection.go
File metadata and controls
308 lines (273 loc) · 8.99 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package retrieval
import (
"context"
"crypto/sha256"
"errors"
"fmt"
"hash"
"io"
"net/http"
"net/url"
"github.com/storacha/go-ucanto/client"
"github.com/storacha/go-ucanto/core/dag/blockstore"
"github.com/storacha/go-ucanto/core/delegation"
"github.com/storacha/go-ucanto/core/invocation"
"github.com/storacha/go-ucanto/core/ipld"
"github.com/storacha/go-ucanto/core/ipld/block"
"github.com/storacha/go-ucanto/core/ipld/codec/cbor"
"github.com/storacha/go-ucanto/core/ipld/codec/json"
ucansha256 "github.com/storacha/go-ucanto/core/ipld/hash/sha256"
"github.com/storacha/go-ucanto/core/message"
mdm "github.com/storacha/go-ucanto/core/message/datamodel"
rdm "github.com/storacha/go-ucanto/server/retrieval/datamodel"
"github.com/storacha/go-ucanto/transport"
"github.com/storacha/go-ucanto/transport/headercar"
hcmsg "github.com/storacha/go-ucanto/transport/headercar/message"
thttp "github.com/storacha/go-ucanto/transport/http"
"github.com/storacha/go-ucanto/ucan"
)
// Option is an option configuring a retrieval connection.
type Option func(cfg *config)
type config struct {
client *http.Client
headers http.Header
}
// WithClient configures the HTTP client the connection should use to make
// requests.
func WithClient(c *http.Client) Option {
return func(cfg *config) {
cfg.client = c
}
}
// WithHeaders configures additional HTTP headers to send with requests.
func WithHeaders(h http.Header) Option {
return func(cfg *config) {
cfg.headers = h
}
}
// NewConnection creates a new connection to a retrieval server that uses the
// headercar transport.
func NewConnection(id ucan.Principal, url *url.URL, opts ...Option) (*Connection, error) {
cfg := config{}
for _, o := range opts {
o(&cfg)
}
hasher := sha256.New
channel := thttp.NewChannel(
url,
thttp.WithMethod("GET"),
thttp.WithSuccessStatusCode(
http.StatusOK,
http.StatusPartialContent,
http.StatusNotExtended, // indicates further proof must be supplied
http.StatusRequestHeaderFieldsTooLarge, // indicates invocation is too large to fit in headers
),
thttp.WithClient(cfg.client),
thttp.WithHeaders(cfg.headers),
)
codec := headercar.NewOutboundCodec()
return &Connection{id, channel, codec, hasher}, nil
}
type Connection struct {
id ucan.Principal
channel transport.Channel
codec transport.OutboundCodec
hasher func() hash.Hash
}
var _ client.Connection = (*Connection)(nil)
func (c *Connection) ID() ucan.Principal {
return c.id
}
func (c *Connection) Codec() transport.OutboundCodec {
return c.codec
}
func (c *Connection) Channel() transport.Channel {
return c.channel
}
func (c *Connection) Hasher() hash.Hash {
return c.hasher()
}
// Execute performs a UCAN invocation using the headercar transport,
// implementing a "probe and retry" pattern to handle HTTP header size
// limitations when the invocation is too large to fit.
//
// The method first attempts to send the complete invocation (including all
// proofs) in HTTP headers. If this fails due to size constraints (typically 4KB
// header limit), it falls back to a multipart negotiation protocol:
//
// 1. Send invocation with ALL proofs omitted
// 2. Server responds with 510 (Not Extended) listing missing proof CID(s)
// 3. Send partial invocations with each missing proof attached one by one
// 4. Repeat until server has all required proofs (200/206 response)
//
// This approach optimizes for the common case (shallow delegation chains that
// fit in headers) while also handling deep proof chains that require
// multiple round trips. The server caches proofs between requests, so each
// proof only needs to be sent once per session.
//
// Note: The current implementation processes missing proofs sequentially rather
// than in batches, which means deep delegation chains will result in multiple
// HTTP round trips. This trade-off prioritizes implementation simplicity over
// network efficiency, which is acceptable given current delegation chain depths
// but may need optimization as authorization hierarchies grow deeper.
//
// Returns the execution response, the final HTTP response, and any error
// encountered.
func Execute(ctx context.Context, inv invocation.Invocation, conn client.Connection) (client.ExecutionResponse, transport.HTTPResponse, error) {
input, err := message.Build([]invocation.Invocation{inv}, nil)
if err != nil {
return nil, nil, fmt.Errorf("building message: %w", err)
}
var response transport.HTTPResponse
multi := false
req, err := conn.Codec().Encode(input)
if err != nil {
if errors.Is(err, hcmsg.ErrHeaderTooLarge) {
multi = true
} else {
return nil, nil, fmt.Errorf("encoding message: %w", err)
}
} else {
response, err = conn.Channel().Request(ctx, req)
if err != nil {
return nil, nil, fmt.Errorf("sending message: %w", err)
}
if response.Status() == http.StatusRequestHeaderFieldsTooLarge {
multi = true
err := response.Body().Close() // we don't need this anymore
if err != nil {
return nil, nil, fmt.Errorf("closing response body: %w", err)
}
}
}
// if the header fields are too big, we need to split the delegation into
// multiple requests...
if multi {
response, err = sendPartialInvocations(ctx, inv, conn)
if err != nil {
return nil, nil, fmt.Errorf("sending partial invocations: %w", err)
}
}
output, err := conn.Codec().Decode(response)
if err != nil {
return nil, response, fmt.Errorf("decoding message: %w", err)
}
return client.ExecutionResponse(output), response, nil
}
func sendPartialInvocations(ctx context.Context, inv invocation.Invocation, conn client.Connection) (transport.HTTPResponse, error) {
br, err := blockstore.NewBlockReader(blockstore.WithBlocksIterator(inv.Export()))
if err != nil {
return nil, fmt.Errorf("reading invocation blocks: %w", err)
}
part, err := omitProofs(inv)
if err != nil {
return nil, fmt.Errorf("creating invocation %s with omitted proofs: %w", inv.Link().String(), err)
}
parts := map[string]delegation.Delegation{}
prfs := inv.Proofs()
for len(prfs) > 0 {
root := prfs[0]
prfs = prfs[1:]
prf, err := delegation.NewDelegationView(root, br)
if err != nil {
return nil, fmt.Errorf("creating delegation: %w", err)
}
prfs = append(prfs, prf.Proofs()...)
// now export without proofs
prf, err = omitProofs(prf)
if err != nil {
return nil, fmt.Errorf("creating delegation %s with omitted proofs: %w", prf.Link().String(), err)
}
parts[prf.Link().String()] = prf
}
// we already tried this
if len(parts) == 0 {
return nil, errors.New("invocation is too big to send in HTTP headers")
}
// now send the parts
for {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}
input, err := newPartialInvocationMessage(inv.Link(), part)
if err != nil {
return nil, fmt.Errorf("building message: %w", err)
}
req, err := conn.Codec().Encode(input)
if err != nil {
return nil, fmt.Errorf("encoding message: %w", err)
}
res, err := conn.Channel().Request(ctx, req)
if err != nil {
return nil, fmt.Errorf("sending message: %w", err)
}
if res.Status() == http.StatusPartialContent || res.Status() == http.StatusOK {
return res, nil
}
// if still too big, then fail
if res.Status() == http.StatusRequestHeaderFieldsTooLarge {
return nil, errors.New("invocation is too big to send in HTTP headers")
}
if res.Status() != http.StatusNotExtended {
return nil, fmt.Errorf("unexpected status code: %d", res.Status())
}
bodyReader := res.Body()
body, err := io.ReadAll(bodyReader)
if err != nil {
bodyReader.Close()
return nil, fmt.Errorf("reading not extended body: %w", err)
}
if err = bodyReader.Close(); err != nil {
return nil, fmt.Errorf("closing response body: %w", err)
}
var model rdm.MissingProofsModel
err = json.Decode(body, &model, rdm.MissingProofsType())
if err != nil {
return nil, fmt.Errorf("decoding body: %w", err)
}
if len(model.Proofs) == 0 {
return nil, errors.New("server did not include missing proofs in response")
}
p, ok := parts[model.Proofs[0].String()]
if !ok {
return nil, fmt.Errorf("missing proof not found or was already sent: %s", model.Proofs[0].String())
}
part = p
delete(parts, p.Link().String())
}
}
func omitProofs(dlg delegation.Delegation) (delegation.Delegation, error) {
blocks := dlg.Export(delegation.WithOmitProof(dlg.Proofs()...))
bs, err := blockstore.NewBlockReader(blockstore.WithBlocksIterator(blocks))
if err != nil {
return nil, err
}
return delegation.NewDelegation(dlg.Root(), bs)
}
func newPartialInvocationMessage(invocation ipld.Link, part delegation.Delegation) (message.AgentMessage, error) {
bs, err := blockstore.NewBlockStore(blockstore.WithBlocksIterator(part.Export()))
if err != nil {
return nil, err
}
msg := mdm.AgentMessageModel{
UcantoMessage7: &mdm.DataModel{
Execute: []ipld.Link{invocation},
},
}
rt, err := block.Encode(
&msg,
mdm.Type(),
cbor.Codec,
ucansha256.Hasher,
)
if err != nil {
return nil, err
}
err = bs.Put(rt)
if err != nil {
return nil, err
}
return message.NewMessage(rt.Link(), bs)
}