Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkg/headers: Move all explicit header keys into const #236

Merged
merged 1 commit into from
Mar 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions pkg/headers/headers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package headers

// We treat all headers as HTTP/2 headers
const (
// ContentLength entity-header field indicates the size of the
// entity-body, in decimal number of OCTETs, sent to the recipient or,
// in the case of the HEAD method, the size of the entity-body that
// would have been sent had the request been a GET.
ContentLength = "content-length"
// ContentType entity-header field indicates the media type of the
// entity-body sent to the recipient or, in the case of the HEAD method,
// the media type that would have been sent had the request been a GET.
ContentType = "content-type"
// ETag response-header field provides the current value of the
// entity tag for the requested variant. The entity tag
// MAY be used for comparison with other entities from the same resource
ETag = "etag"
// LastModified entity-header field indicates the date and time at
// which the origin server believes the variant was last modified.
LastModified = "last-modified"
// Location response-header field is used to redirect the recipient
// to a location other than the Request-URI for completion of the
// request or identification of a new resource. For 201 (Created)
// responses, the Location is that of the new resource which was created
// by the request. For 3xx responses, the location SHOULD indicate the
// server's preferred URI for automatic redirection to the resource. The
// field value consists of a single absolute URI.
Location = "location"
)
7 changes: 4 additions & 3 deletions services/cos/storager.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/tencentyun/cos-go-sdk-v5"

"github.com/Xuanwo/storage/pkg/headers"
"github.com/Xuanwo/storage/pkg/iowrap"
"github.com/Xuanwo/storage/services"
"github.com/Xuanwo/storage/types"
Expand Down Expand Up @@ -241,19 +242,19 @@ func (s *Storage) Stat(path string, pairs ...*types.Pair) (o *types.Object, err
// > Last-Modified: Fri, 09 Aug 2019 10:20:56 GMT
//
// ref: https://cloud.tencent.com/document/product/436/7745
if v := output.Header.Get("Last-Modified"); v != "" {
if v := output.Header.Get(headers.LastModified); v != "" {
lastModified, err := time.Parse(time.RFC1123, v)
if err != nil {
return nil, err
}
o.UpdatedAt = lastModified
}

if v := output.Header.Get("Content-Type"); v != "" {
if v := output.Header.Get(headers.ContentType); v != "" {
o.SetContentType(v)
}

if v := output.Header.Get("ETag"); v != "" {
if v := output.Header.Get(headers.ETag); v != "" {
o.SetETag(output.Header.Get(v))
}

Expand Down
13 changes: 7 additions & 6 deletions services/oss/storager.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import (
"strings"
"time"

"github.com/Xuanwo/storage/pkg/iowrap"
"github.com/Xuanwo/storage/services"
"github.com/aliyun/aliyun-oss-go-sdk/oss"

"github.com/Xuanwo/storage/pkg/headers"
"github.com/Xuanwo/storage/pkg/iowrap"
"github.com/Xuanwo/storage/services"
"github.com/Xuanwo/storage/types"
"github.com/Xuanwo/storage/types/metadata"
)
Expand Down Expand Up @@ -214,15 +215,15 @@ func (s *Storage) Stat(path string, pairs ...*types.Pair) (o *types.Object, err
ObjectMeta: metadata.NewObjectMeta(),
}

if v := output.Get("Content-Length"); v != "" {
if v := output.Get(headers.ContentLength); v != "" {
size, err := strconv.ParseInt(v, 10, 64)
if err != nil {
return nil, err
}
o.Size = size
}

if v := output.Get("Last-Modified"); v != "" {
if v := output.Get(headers.LastModified); v != "" {
lastModified, err := time.Parse(time.RFC822, v)
if err != nil {
return nil, err
Expand All @@ -233,11 +234,11 @@ func (s *Storage) Stat(path string, pairs ...*types.Pair) (o *types.Object, err
// OSS advise us don't use Etag as Content-MD5.
//
// ref: https://help.aliyun.com/document_detail/31965.html
if v := output.Get("ETag"); v != "" {
if v := output.Get(headers.ETag); v != "" {
o.SetETag(v)
}

if v := output.Get("Content-Type"); v != "" {
if v := output.Get(headers.ContentType); v != "" {
o.SetContentType(v)
}

Expand Down
3 changes: 2 additions & 1 deletion services/qingstor/servicer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/yunify/qingstor-sdk-go/v3/service"

"github.com/Xuanwo/storage"
"github.com/Xuanwo/storage/pkg/headers"
"github.com/Xuanwo/storage/services"
"github.com/Xuanwo/storage/types"
ps "github.com/Xuanwo/storage/types/pairs"
Expand Down Expand Up @@ -216,7 +217,7 @@ func (s *Service) detectLocation(name string) (location string, err error) {
}

// Example URL: https://bucket.zone.qingstor.com
location = strings.Split(r.Header.Get("Location"), ".")[1]
location = strings.Split(r.Header.Get(headers.Location), ".")[1]
return
}

Expand Down