-
Notifications
You must be signed in to change notification settings - Fork 2
/
httpipfs.go
209 lines (183 loc) · 5.71 KB
/
httpipfs.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
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
package frisbii
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"sync"
"time"
lassiehttp "github.com/filecoin-project/lassie/pkg/server/http"
lassietypes "github.com/filecoin-project/lassie/pkg/types"
"github.com/ipfs/go-cid"
"github.com/ipfs/go-unixfsnode"
"github.com/ipld/go-ipld-prime/datamodel"
"github.com/ipld/go-ipld-prime/linking"
)
var _ http.Handler = (*HttpIpfs)(nil)
type ErrorLogger interface {
LogError(status int, err error)
}
// HttpIpfs is an http.Handler that serves IPLD data via HTTP according to the
// Trustless Gateway specification.
type HttpIpfs struct {
ctx context.Context
logWriter io.Writer
lsys linking.LinkSystem
maxResponseDuration time.Duration
maxResponseBytes int64
}
func NewHttpIpfs(
ctx context.Context,
logWriter io.Writer,
lsys linking.LinkSystem,
maxResponseDuration time.Duration,
maxResponseBytes int64,
) *HttpIpfs {
return &HttpIpfs{
ctx: ctx,
logWriter: logWriter,
lsys: lsys,
maxResponseDuration: maxResponseDuration,
maxResponseBytes: maxResponseBytes,
}
}
func (hi *HttpIpfs) ServeHTTP(res http.ResponseWriter, req *http.Request) {
ctx := hi.ctx
if hi.maxResponseDuration > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, hi.maxResponseDuration)
defer cancel()
}
logError := func(status int, err error) {
if lrw, ok := res.(ErrorLogger); ok {
lrw.LogError(status, err)
} else {
logger.Debug("Error handling request from [%s] for [%s] status=%d, msg=%s", req.RemoteAddr, req.URL, status, err.Error())
}
}
path := datamodel.ParsePath(req.URL.Path)
_, path = path.Shift() // remove /ipfs
// filter out everything but GET requests
switch req.Method {
case http.MethodGet:
break
default:
res.Header().Add("Allow", http.MethodGet)
logError(http.StatusMethodNotAllowed, errors.New("method not allowed"))
return
}
// check if CID path param is missing
if path.Len() == 0 {
// not a valid path to hit
logError(http.StatusNotFound, errors.New("not found"))
return
}
includeDupes, err := lassiehttp.CheckFormat(req)
if err != nil {
logError(http.StatusBadRequest, err)
return
}
fileName, err := lassiehttp.ParseFilename(req)
if err != nil {
logError(http.StatusBadRequest, err)
return
}
// validate CID path parameter
var cidSeg datamodel.PathSegment
cidSeg, path = path.Shift()
rootCid, err := cid.Parse(cidSeg.String())
if err != nil {
logError(http.StatusInternalServerError, errors.New("failed to parse CID path parameter"))
return
}
dagScope, err := lassiehttp.ParseScope(req)
if err != nil {
logError(http.StatusBadRequest, err)
return
}
if fileName == "" {
fileName = fmt.Sprintf("%s%s", rootCid.String(), lassiehttp.FilenameExtCar)
}
selNode := unixfsnode.UnixFSPathSelectorBuilder(path.String(), dagScope.TerminalSelectorSpec(), false)
bytesWrittenCh := make(chan struct{})
writer := newIpfsResponseWriter(res, hi.maxResponseBytes, func() {
// called once we start writing blocks into the CAR (on the first Put())
res.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%q", fileName))
res.Header().Set("Accept-Ranges", lassiehttp.ResponseAcceptRangesHeader)
res.Header().Set("Cache-Control", lassiehttp.ResponseCacheControlHeader)
res.Header().Set("Content-Type", lassiehttp.ResponseContentTypeHeader)
res.Header().Set("Etag", etag(rootCid, path.String(), dagScope, includeDupes))
res.Header().Set("X-Content-Type-Options", "nosniff")
res.Header().Set("X-Ipfs-Path", "/"+datamodel.ParsePath(req.URL.Path).String())
close(bytesWrittenCh)
})
if err := StreamCar(ctx, hi.lsys, rootCid, selNode, writer, includeDupes); err != nil {
logError(http.StatusInternalServerError, err)
select {
case <-bytesWrittenCh:
logger.Debugw("unclean close", "cid", rootCid, "err", err)
if err := closeWithUnterminatedChunk(res); err != nil {
logger.Infow("unable to send early termination", "err", err)
}
return
default:
}
logger.Debugw("error streaming CAR", "cid", rootCid, "err", err)
}
}
var _ io.Writer = (*ipfsResponseWriter)(nil)
type ipfsResponseWriter struct {
w io.Writer
fn func()
byteCount int
once sync.Once
maxBytes int64
}
func newIpfsResponseWriter(w io.Writer, maxBytes int64, fn func()) *ipfsResponseWriter {
return &ipfsResponseWriter{
w: w,
maxBytes: maxBytes,
fn: fn,
}
}
func (w *ipfsResponseWriter) Write(p []byte) (int, error) {
w.once.Do(w.fn)
w.byteCount += len(p)
if w.maxBytes > 0 && int64(w.byteCount) > w.maxBytes {
return 0, fmt.Errorf("response too large: %d bytes", w.byteCount)
}
return w.w.Write(p)
}
func etag(root cid.Cid, path string, scope lassietypes.DagScope, duplicates bool) string {
return lassietypes.RetrievalRequest{
Cid: root,
Path: path,
Scope: scope,
Duplicates: duplicates,
}.Etag()
}
// closeWithUnterminatedChunk attempts to take control of the the http conn and terminate the stream early
//
// (copied from github.com/filecoin-project/lassie/pkg/server/http/ipfs.go)
func closeWithUnterminatedChunk(res http.ResponseWriter) error {
hijacker, ok := res.(http.Hijacker)
if !ok {
return errors.New("unable to access hijack interface")
}
conn, buf, err := hijacker.Hijack()
if err != nil {
return fmt.Errorf("unable to access conn through hijack interface: %w", err)
}
if _, err := buf.Write(lassiehttp.ResponseChunkDelimeter); err != nil {
return fmt.Errorf("writing response chunk delimiter: %w", err)
}
if err := buf.Flush(); err != nil {
return fmt.Errorf("flushing buff: %w", err)
}
// attempt to close just the write side
if err := conn.Close(); err != nil {
return fmt.Errorf("closing write conn: %w", err)
}
return nil
}