-
Notifications
You must be signed in to change notification settings - Fork 342
/
headobject.go
75 lines (67 loc) · 2.5 KB
/
headobject.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
package operations
import (
"errors"
"fmt"
"net/http"
"github.com/treeverse/lakefs/pkg/catalog"
gatewayerrors "github.com/treeverse/lakefs/pkg/gateway/errors"
"github.com/treeverse/lakefs/pkg/graveler"
"github.com/treeverse/lakefs/pkg/httputil"
"github.com/treeverse/lakefs/pkg/permissions"
)
type HeadObject struct{}
func (controller *HeadObject) RequiredPermissions(_ *http.Request, repoID, _, path string) (permissions.Node, error) {
return permissions.Node{
Permission: permissions.Permission{
Action: permissions.ReadObjectAction,
Resource: permissions.ObjectArn(repoID, path),
},
}, nil
}
func (controller *HeadObject) Handle(w http.ResponseWriter, req *http.Request, o *PathOperation) {
o.Incr("stat_object", o.Principal, o.Repository.Name, o.Reference)
entry, err := o.Catalog.GetEntry(req.Context(), o.Repository.Name, o.Reference, o.Path, catalog.GetEntryParams{})
if errors.Is(err, graveler.ErrNotFound) {
// TODO: create distinction between missing repo & missing key
o.Log(req).Debug("path not found")
_ = o.EncodeError(w, req, gatewayerrors.Codes.ToAPIErr(gatewayerrors.ErrNoSuchKey))
return
}
if err != nil {
o.Log(req).WithError(err).Error("failed querying path")
_ = o.EncodeError(w, req, gatewayerrors.Codes.ToAPIErr(gatewayerrors.ErrInternalError))
return
}
if entry.Expired {
o.Log(req).WithError(err).Info("querying expired object")
_ = o.EncodeError(w, req, gatewayerrors.Codes.ToAPIErr(gatewayerrors.ErrNoSuchVersion))
return
}
// range query
var rng httputil.Range
var rngErr error
// range query
rangeSpec := req.Header.Get("Range")
if len(rangeSpec) > 0 {
rng, rngErr = httputil.ParseRange(rangeSpec, entry.Size)
if rngErr != nil {
o.Log(req).WithError(err).WithField("range", rangeSpec).Debug("invalid range spec")
if errors.Is(rngErr, httputil.ErrUnsatisfiableRange) {
w.WriteHeader(http.StatusRequestedRangeNotSatisfiable)
return
}
}
}
o.SetHeader(w, "Accept-Ranges", "bytes")
o.SetHeader(w, "Last-Modified", httputil.HeaderTimestamp(entry.CreationDate))
o.SetHeader(w, "ETag", httputil.ETag(entry.Checksum))
o.SetHeader(w, "Content-Type", entry.ContentType)
amzMetaWriteHeaders(w, entry.Metadata)
if rangeSpec != "" && rngErr == nil {
o.SetHeader(w, "Content-Length", fmt.Sprintf("%d", rng.Size()))
o.SetHeader(w, "Content-Range", fmt.Sprintf("bytes %d-%d/%d", rng.StartOffset, rng.EndOffset, entry.Size))
w.WriteHeader(http.StatusPartialContent)
} else {
o.SetHeader(w, "Content-Length", fmt.Sprintf("%d", entry.Size))
}
}