Skip to content

Commit

Permalink
fix GET and HEAD
Browse files Browse the repository at this point in the history
Fix filename and Content-type for GET and HEAD APIs.
Fix cookie prefix for bearer token.

Signed-off-by: Tatiana Nesterenko <tatiana@nspcc.io>
  • Loading branch information
tatiana-nspcc committed Jan 15, 2024
1 parent 3f4121d commit b8f60d3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 23 deletions.
9 changes: 4 additions & 5 deletions handlers/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ type ContextKey string

const (
// BearerPrefix is the prefix for authorization token.
BearerPrefix = "Bearer "
BearerPrefix = "Bearer "
BearerCookiePrefix = "Bearer="

// ContextKeyRequestID is the ContextKey for RequestID.
ContextKeyRequestID ContextKey = "requestID"
Expand Down Expand Up @@ -138,18 +139,16 @@ func (a *API) Configure(api *operations.NeofsRestGwAPI) http.Handler {
if s = strings.TrimPrefix(s, BearerPrefix); len(s) == 0 {
return nil, fmt.Errorf("bearer token is empty")
}

return (*models.Principal)(&s), nil
}

api.CookieAuthAuth = func(s string) (*models.Principal, error) {
if !strings.HasPrefix(s, BearerPrefix) {
if !strings.HasPrefix(s, BearerCookiePrefix) {

Check warning on line 146 in handlers/api.go

View check run for this annotation

Codecov / codecov/patch

handlers/api.go#L146

Added line #L146 was not covered by tests
return nil, fmt.Errorf("has not bearer token")
}
if s = strings.TrimPrefix(s, BearerPrefix); len(s) == 0 {
if s = strings.TrimPrefix(s, BearerCookiePrefix); len(s) == 0 {

Check warning on line 149 in handlers/api.go

View check run for this annotation

Codecov / codecov/patch

handlers/api.go#L149

Added line #L149 was not covered by tests
return nil, fmt.Errorf("bearer token is empty")
}

return (*models.Principal)(&s), nil
}

Expand Down
33 changes: 15 additions & 18 deletions handlers/objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,8 @@ func (a *API) GetContainerObject(params operations.GetContainerObjectParams, pri
payloadSize := header.PayloadSize()
res := operations.NewGetContainerObjectOK()

responder, contentType := a.setAttributes(res, payloadSize, params.ContainerID, params.ObjectID, header, params.HTTPRequest.URL.Query().Get("download"))
responder := a.setAttributes(res, payloadSize, params.ContainerID, params.ObjectID, header, params.HTTPRequest.URL.Query().Get("download"))
contentType := res.ContentType

Check warning on line 362 in handlers/objects.go

View check run for this annotation

Codecov / codecov/patch

handlers/objects.go#L361-L362

Added lines #L361 - L362 were not covered by tests
var payload io.ReadCloser = payloadReader
if len(contentType) == 0 {
if payloadSize > 0 {
Expand Down Expand Up @@ -431,7 +432,8 @@ func (a *API) HeadContainerObject(params operations.HeadContainerObjectParams, p
payloadSize := header.PayloadSize()
res := operations.NewHeadContainerObjectOK()

responder, contentType := a.setAttributes(res, payloadSize, params.ContainerID, params.ObjectID, *header, params.HTTPRequest.URL.Query().Get("download"))
responder := a.setAttributes(res, payloadSize, params.ContainerID, params.ObjectID, *header, params.HTTPRequest.URL.Query().Get("download"))
contentType := res.ContentType

Check warning on line 436 in handlers/objects.go

View check run for this annotation

Codecov / codecov/patch

handlers/objects.go#L435-L436

Added lines #L435 - L436 were not covered by tests
if len(contentType) == 0 {
if payloadSize > 0 {
contentType, _, err = readContentType(payloadSize, func(sz uint64) (io.Reader, error) {
Expand Down Expand Up @@ -469,6 +471,7 @@ func isNotFoundError(err error) bool {

type attributeSetter interface {
SetContentLength(contentLength string)
SetContentType(contentType string)
SetXContainerID(xContainerID string)
SetXObjectID(xObjectID string)
SetXOwnerID(xOwnerID string)
Expand All @@ -479,16 +482,14 @@ type attributeSetter interface {
WriteResponse(rw http.ResponseWriter, producer runtime.Producer)
}

func (a *API) setAttributes(res attributeSetter, payloadSize uint64, cid string, oid string, header object.Object, download string) (middleware.Responder, string) {
func (a *API) setAttributes(res attributeSetter, payloadSize uint64, cid string, oid string, header object.Object, download string) middleware.Responder {

Check warning on line 485 in handlers/objects.go

View check run for this annotation

Codecov / codecov/patch

handlers/objects.go#L485

Added line #L485 was not covered by tests
res.SetContentLength(strconv.FormatUint(payloadSize, 10))
res.SetXContainerID(cid)
res.SetXObjectID(oid)
res.SetXOwnerID(header.OwnerID().EncodeToString())

var (
contentType, filename string
responder middleware.Responder
)
var responder middleware.Responder
dis := "inline"

Check warning on line 492 in handlers/objects.go

View check run for this annotation

Codecov / codecov/patch

handlers/objects.go#L491-L492

Added lines #L491 - L492 were not covered by tests
attributes := header.Attributes()
if len(attributes) > 0 {
responder = middleware.ResponderFunc(func(rw http.ResponseWriter, pr runtime.Producer) {
Expand All @@ -500,7 +501,11 @@ func (a *API) setAttributes(res attributeSetter, payloadSize uint64, cid string,
}
switch key {
case object.AttributeFileName:
filename = val
switch download {
case "1", "t", "T", "true", "TRUE", "True", "y", "yes", "Y", "YES", "Yes":
dis = "attachment"

Check warning on line 506 in handlers/objects.go

View check run for this annotation

Codecov / codecov/patch

handlers/objects.go#L504-L506

Added lines #L504 - L506 were not covered by tests
}
res.SetContentDisposition(dis + "; filename=" + path.Base(val))

Check warning on line 508 in handlers/objects.go

View check run for this annotation

Codecov / codecov/patch

handlers/objects.go#L508

Added line #L508 was not covered by tests
res.SetXAttributeFileName(val)
case object.AttributeTimestamp:
attrTimestamp, err := strconv.ParseInt(val, 10, 64)
Expand All @@ -514,7 +519,7 @@ func (a *API) setAttributes(res attributeSetter, payloadSize uint64, cid string,
res.SetXAttributeTimestamp(attrTimestamp)
res.SetLastModified(time.Unix(attrTimestamp, 0).UTC().Format(http.TimeFormat))
case object.AttributeContentType:
contentType = val
res.SetContentType(val)

Check warning on line 522 in handlers/objects.go

View check run for this annotation

Codecov / codecov/patch

handlers/objects.go#L522

Added line #L522 was not covered by tests
default:
if strings.HasPrefix(key, container.SysAttributePrefix) {
key = systemBackwardTranslator(key)
Expand All @@ -525,15 +530,7 @@ func (a *API) setAttributes(res attributeSetter, payloadSize uint64, cid string,
res.WriteResponse(rw, pr)
})
}

var dis = "inline"
switch download {
case "1", "t", "T", "true", "TRUE", "True", "y", "yes", "Y", "YES", "Yes":
dis = "attachment"
}
res.SetContentDisposition(dis + "; filename=" + path.Base(filename))

return responder, contentType
return responder

Check warning on line 533 in handlers/objects.go

View check run for this annotation

Codecov / codecov/patch

handlers/objects.go#L533

Added line #L533 was not covered by tests
}

// initializes io.Reader with the limited size and detects Content-Type from it.
Expand Down

0 comments on commit b8f60d3

Please sign in to comment.