Skip to content

Commit

Permalink
feat: expose closer for GetResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
aschmahmann committed Mar 30, 2023
1 parent 6f324be commit 952f135
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 0 deletions.
12 changes: 12 additions & 0 deletions gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@ type GetRange struct {
type GetResponse struct {
bytes files.File
directoryMetadata *directoryMetadata
closeFn func() error
}

func (g *GetResponse) Close() error {
if g.closeFn == nil {
return nil
}
return g.closeFn()
}

func (g *GetResponse) SetCloser(closeFn func() error) {
g.closeFn = closeFn
}

type directoryMetadata struct {
Expand Down
1 change: 1 addition & 0 deletions gateway/handler_defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func (i *handler) serveDefaults(ctx context.Context, w http.ResponseWriter, r *h
}
}
}
defer getResp.Close()
if getResp.bytes != nil {
bytesResponse = getResp.bytes
defer bytesResponse.Close()
Expand Down
3 changes: 3 additions & 0 deletions gateway/handler_unixfs__redirects.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ func (i *handler) getRedirectRules(r *http.Request, redirectsPath ImmutablePath)
}
return false, nil, err
}
defer redirectsFileGetResp.Close()

if redirectsFileGetResp.bytes == nil {
return false, nil, fmt.Errorf(" _redirects is not a file")
Expand All @@ -186,6 +187,7 @@ func (i *handler) serve4xx(w http.ResponseWriter, r *http.Request, content4xxPat
if err != nil {
return err
}
defer getresp.Close()

if getresp.bytes == nil {
return fmt.Errorf("could not convert node for %d page to file", status)
Expand Down Expand Up @@ -266,6 +268,7 @@ func (i *handler) searchUpTreeFor404(r *http.Request, imPath ImmutablePath) (fil
if err != nil {
continue
}
defer getResp.Close()
if getResp.bytes == nil {
return nil, "", fmt.Errorf("found a pretty 404 but it was not a file")
}
Expand Down
1 change: 1 addition & 0 deletions gateway/handler_unixfs_dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ func (i *handler) serveDirectory(ctx context.Context, w http.ResponseWriter, r *
var getResp *GetResponse
_, getResp, err = i.api.Get(ctx, imIndexPath)
if err == nil {
defer getResp.Close()
if getResp.bytes == nil {
webError(w, fmt.Errorf("%q could not be read: %w", imIndexPath, files.ErrNotReader), http.StatusUnprocessableEntity)
return false
Expand Down

0 comments on commit 952f135

Please sign in to comment.