2021-04-18 12:41:13 -07:00
|
|
|
// Copyright (c) 2015-2021 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2015-02-15 00:48:15 -08:00
|
|
|
|
2016-08-18 16:23:42 -07:00
|
|
|
package cmd
|
2015-02-11 03:23:15 -08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2024-05-15 11:04:16 -07:00
|
|
|
"context"
|
2018-01-22 14:54:55 -08:00
|
|
|
"encoding/json"
|
2023-01-17 05:08:33 +05:30
|
|
|
"encoding/xml"
|
2017-01-10 16:43:48 -08:00
|
|
|
"fmt"
|
2015-02-11 03:23:15 -08:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
2020-03-12 12:37:27 -07:00
|
|
|
"strings"
|
2017-01-10 16:43:48 -08:00
|
|
|
"time"
|
2018-09-20 19:22:09 -07:00
|
|
|
|
2023-07-22 07:19:43 -07:00
|
|
|
"github.com/minio/minio-go/v7/pkg/tags"
|
2021-06-01 14:59:40 -07:00
|
|
|
"github.com/minio/minio/internal/crypto"
|
|
|
|
xhttp "github.com/minio/minio/internal/http"
|
2022-08-23 17:04:11 -07:00
|
|
|
xxml "github.com/minio/xxml"
|
2015-02-11 03:23:15 -08:00
|
|
|
)
|
|
|
|
|
2017-01-10 16:43:48 -08:00
|
|
|
// Returns a hexadecimal representation of time at the
|
|
|
|
// time response is sent to the client.
|
|
|
|
func mustGetRequestID(t time.Time) string {
|
|
|
|
return fmt.Sprintf("%X", t.UnixNano())
|
2015-07-14 14:29:56 -07:00
|
|
|
}
|
|
|
|
|
2020-07-30 19:45:12 -07:00
|
|
|
// setEventStreamHeaders to allow proxies to avoid buffering proxy responses
|
|
|
|
func setEventStreamHeaders(w http.ResponseWriter) {
|
|
|
|
w.Header().Set(xhttp.ContentType, "text/event-stream")
|
|
|
|
w.Header().Set(xhttp.CacheControl, "no-cache") // nginx to turn off buffering
|
|
|
|
w.Header().Set("X-Accel-Buffering", "no") // nginx to turn off buffering
|
|
|
|
}
|
|
|
|
|
2015-02-23 16:46:48 -08:00
|
|
|
// Write http common headers
|
2016-01-08 00:40:06 -08:00
|
|
|
func setCommonHeaders(w http.ResponseWriter) {
|
2020-11-19 09:16:02 -08:00
|
|
|
// Set the "Server" http header.
|
2024-03-08 19:07:08 -08:00
|
|
|
w.Header().Set(xhttp.ServerInfo, MinioStoreName)
|
2020-11-19 09:16:02 -08:00
|
|
|
|
2017-06-23 16:05:40 -07:00
|
|
|
// Set `x-amz-bucket-region` only if region is set on the server
|
|
|
|
// by default minio uses an empty region.
|
2024-05-16 16:13:47 -07:00
|
|
|
if region := globalSite.Region(); region != "" {
|
2019-07-02 22:34:32 -07:00
|
|
|
w.Header().Set(xhttp.AmzBucketRegion, region)
|
2017-06-23 16:05:40 -07:00
|
|
|
}
|
2019-07-02 22:34:32 -07:00
|
|
|
w.Header().Set(xhttp.AcceptRanges, "bytes")
|
2018-09-25 20:39:46 +01:00
|
|
|
|
|
|
|
// Remove sensitive information
|
|
|
|
crypto.RemoveSensitiveHeaders(w.Header())
|
2015-02-11 03:23:15 -08:00
|
|
|
}
|
|
|
|
|
2016-03-06 12:16:22 -08:00
|
|
|
// Encodes the response headers into XML format.
|
|
|
|
func encodeResponse(response interface{}) []byte {
|
2023-01-17 05:08:33 +05:30
|
|
|
var buf bytes.Buffer
|
|
|
|
buf.WriteString(xml.Header)
|
|
|
|
if err := xml.NewEncoder(&buf).Encode(response); err != nil {
|
2024-04-04 13:04:40 +01:00
|
|
|
bugLogIf(GlobalContext, err)
|
2022-08-23 17:04:11 -07:00
|
|
|
return nil
|
|
|
|
}
|
2023-01-17 05:08:33 +05:30
|
|
|
return buf.Bytes()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use this encodeResponseList() to support control characters
|
|
|
|
// this function must be used by only ListObjects() for objects
|
|
|
|
// with control characters, this is a specialized extension
|
|
|
|
// to support AWS S3 compatible behavior.
|
|
|
|
//
|
|
|
|
// Do not use this function for anything other than ListObjects()
|
|
|
|
// variants, please open a github discussion if you wish to use
|
|
|
|
// this in other places.
|
|
|
|
func encodeResponseList(response interface{}) []byte {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
buf.WriteString(xxml.Header)
|
|
|
|
if err := xxml.NewEncoder(&buf).Encode(response); err != nil {
|
2024-04-04 13:04:40 +01:00
|
|
|
bugLogIf(GlobalContext, err)
|
2023-01-17 05:08:33 +05:30
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return buf.Bytes()
|
2015-02-11 03:23:15 -08:00
|
|
|
}
|
|
|
|
|
2018-01-22 14:54:55 -08:00
|
|
|
// Encodes the response headers into JSON format.
|
|
|
|
func encodeResponseJSON(response interface{}) []byte {
|
|
|
|
var bytesBuffer bytes.Buffer
|
|
|
|
e := json.NewEncoder(&bytesBuffer)
|
|
|
|
e.Encode(response)
|
|
|
|
return bytesBuffer.Bytes()
|
|
|
|
}
|
|
|
|
|
2020-06-10 09:22:15 -07:00
|
|
|
// Write parts count
|
|
|
|
func setPartsCountHeaders(w http.ResponseWriter, objInfo ObjectInfo) {
|
|
|
|
if strings.Contains(objInfo.ETag, "-") && len(objInfo.Parts) > 0 {
|
|
|
|
w.Header()[xhttp.AmzMpPartsCount] = []string{strconv.Itoa(len(objInfo.Parts))}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-23 16:46:48 -08:00
|
|
|
// Write object header
|
2024-05-15 11:04:16 -07:00
|
|
|
func setObjectHeaders(ctx context.Context, w http.ResponseWriter, objInfo ObjectInfo, rs *HTTPRangeSpec, opts ObjectOptions) (err error) {
|
2015-07-28 19:33:56 -07:00
|
|
|
// set common headers
|
2016-03-06 12:16:22 -08:00
|
|
|
setCommonHeaders(w)
|
|
|
|
|
2016-07-22 20:31:45 -07:00
|
|
|
// Set last modified time.
|
2016-04-08 23:07:38 +05:30
|
|
|
lastModified := objInfo.ModTime.UTC().Format(http.TimeFormat)
|
2019-07-02 22:34:32 -07:00
|
|
|
w.Header().Set(xhttp.LastModified, lastModified)
|
2016-03-06 12:16:22 -08:00
|
|
|
|
2016-07-22 20:31:45 -07:00
|
|
|
// Set Etag if available.
|
2017-05-14 12:05:51 -07:00
|
|
|
if objInfo.ETag != "" {
|
2019-07-02 22:34:32 -07:00
|
|
|
w.Header()[xhttp.ETag] = []string{"\"" + objInfo.ETag + "\""}
|
2016-01-28 22:57:23 -05:00
|
|
|
}
|
2016-07-22 20:31:45 -07:00
|
|
|
|
2017-10-13 03:56:16 -07:00
|
|
|
if objInfo.ContentType != "" {
|
2019-07-02 22:34:32 -07:00
|
|
|
w.Header().Set(xhttp.ContentType, objInfo.ContentType)
|
2017-10-13 03:56:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if objInfo.ContentEncoding != "" {
|
2019-07-02 22:34:32 -07:00
|
|
|
w.Header().Set(xhttp.ContentEncoding, objInfo.ContentEncoding)
|
2017-10-13 03:56:16 -07:00
|
|
|
}
|
|
|
|
|
2019-02-28 11:01:25 -08:00
|
|
|
if !objInfo.Expires.IsZero() {
|
2019-07-02 22:34:32 -07:00
|
|
|
w.Header().Set(xhttp.Expires, objInfo.Expires.UTC().Format(http.TimeFormat))
|
2019-02-28 11:01:25 -08:00
|
|
|
}
|
2020-04-20 22:01:59 -07:00
|
|
|
|
2020-01-20 22:15:59 +05:30
|
|
|
// Set tag count if object has tags
|
2020-11-02 15:15:12 -08:00
|
|
|
if len(objInfo.UserTags) > 0 {
|
2023-07-22 07:19:43 -07:00
|
|
|
tags, _ := tags.ParseObjectTags(objInfo.UserTags)
|
2024-05-28 12:44:00 +08:00
|
|
|
if tags != nil && tags.Count() > 0 {
|
2023-07-22 07:19:43 -07:00
|
|
|
w.Header()[xhttp.AmzTagCount] = []string{strconv.Itoa(tags.Count())}
|
|
|
|
if opts.Tagging {
|
|
|
|
// This is MinIO only extension to return back tags along with the count.
|
|
|
|
w.Header()[xhttp.AmzObjectTagging] = []string{objInfo.UserTags}
|
|
|
|
}
|
2020-11-02 15:15:12 -08:00
|
|
|
}
|
2020-01-20 22:15:59 +05:30
|
|
|
}
|
|
|
|
|
2016-07-22 20:31:45 -07:00
|
|
|
// Set all other user defined metadata.
|
2016-07-12 15:45:17 -04:00
|
|
|
for k, v := range objInfo.UserDefined {
|
2022-03-01 11:04:47 -08:00
|
|
|
// Empty values for object lock and retention can be skipped.
|
|
|
|
if v == "" && equals(k, xhttp.AmzObjectLockMode, xhttp.AmzObjectLockRetainUntilDate) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-07-06 16:02:08 -07:00
|
|
|
if stringsHasPrefixFold(k, ReservedMetadataPrefixLower) {
|
2018-03-01 16:15:53 -08:00
|
|
|
// Do not need to send any internal metadata
|
|
|
|
// values to client.
|
|
|
|
continue
|
|
|
|
}
|
2020-08-11 08:29:29 -07:00
|
|
|
|
|
|
|
// https://github.com/google/security-research/security/advisories/GHSA-76wf-9vgp-pj7w
|
2021-02-03 20:41:33 -08:00
|
|
|
if equals(k, xhttp.AmzMetaUnencryptedContentLength, xhttp.AmzMetaUnencryptedContentMD5) {
|
2020-08-11 08:29:29 -07:00
|
|
|
continue
|
|
|
|
}
|
2021-02-03 20:41:33 -08:00
|
|
|
|
2020-05-25 16:51:32 -07:00
|
|
|
var isSet bool
|
|
|
|
for _, userMetadataPrefix := range userMetadataKeyPrefixes {
|
2023-07-06 16:02:08 -07:00
|
|
|
if !stringsHasPrefixFold(k, userMetadataPrefix) {
|
2020-05-25 16:51:32 -07:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
w.Header()[strings.ToLower(k)] = []string{v}
|
|
|
|
isSet = true
|
|
|
|
break
|
|
|
|
}
|
2021-02-03 20:41:33 -08:00
|
|
|
|
2020-05-25 16:51:32 -07:00
|
|
|
if !isSet {
|
|
|
|
w.Header().Set(k, v)
|
|
|
|
}
|
2016-07-12 15:45:17 -04:00
|
|
|
}
|
2016-03-06 12:16:22 -08:00
|
|
|
|
2020-10-01 23:41:12 +01:00
|
|
|
var start, rangeLen int64
|
2020-05-24 11:19:17 -07:00
|
|
|
totalObjectSize, err := objInfo.GetActualSize()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-09-20 19:22:09 -07:00
|
|
|
}
|
|
|
|
|
2021-04-07 14:37:10 -07:00
|
|
|
if rs == nil && opts.PartNumber > 0 {
|
|
|
|
rs = partNumberToRangeSpec(objInfo, opts.PartNumber)
|
|
|
|
}
|
|
|
|
|
2020-12-08 22:12:42 +01:00
|
|
|
// For providing ranged content
|
|
|
|
start, rangeLen, err = rs.GetOffsetLength(totalObjectSize)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-07-28 19:33:56 -07:00
|
|
|
}
|
2018-09-20 19:22:09 -07:00
|
|
|
|
|
|
|
// Set content length.
|
2019-07-02 22:34:32 -07:00
|
|
|
w.Header().Set(xhttp.ContentLength, strconv.FormatInt(rangeLen, 10))
|
2018-09-20 19:22:09 -07:00
|
|
|
if rs != nil {
|
|
|
|
contentRange := fmt.Sprintf("bytes %d-%d/%d", start, start+rangeLen-1, totalObjectSize)
|
2019-07-02 22:34:32 -07:00
|
|
|
w.Header().Set(xhttp.ContentRange, contentRange)
|
2018-09-20 19:22:09 -07:00
|
|
|
}
|
|
|
|
|
2020-06-12 20:04:01 -07:00
|
|
|
// Set the relevant version ID as part of the response header.
|
2023-06-13 13:52:33 -07:00
|
|
|
if objInfo.VersionID != "" && objInfo.VersionID != nullVersionID {
|
2020-06-12 20:04:01 -07:00
|
|
|
w.Header()[xhttp.AmzVersionID] = []string{objInfo.VersionID}
|
|
|
|
}
|
2021-03-09 22:19:08 +01:00
|
|
|
|
2020-07-21 17:49:56 -07:00
|
|
|
if objInfo.ReplicationStatus.String() != "" {
|
|
|
|
w.Header()[xhttp.AmzBucketReplicationStatus] = []string{objInfo.ReplicationStatus.String()}
|
|
|
|
}
|
2021-03-09 22:19:08 +01:00
|
|
|
|
2021-08-03 17:35:52 -07:00
|
|
|
if objInfo.IsRemote() {
|
|
|
|
// Check if object is being restored. For more information on x-amz-restore header see
|
|
|
|
// https://docs.aws.amazon.com/AmazonS3/latest/API/API_HeadObject.html#API_HeadObject_ResponseSyntax
|
2024-05-15 11:04:16 -07:00
|
|
|
w.Header()[xhttp.AmzStorageClass] = []string{filterStorageClass(ctx, objInfo.TransitionedObject.Tier)}
|
2021-08-03 17:35:52 -07:00
|
|
|
}
|
|
|
|
|
2020-06-12 20:04:01 -07:00
|
|
|
if lc, err := globalLifecycleSys.Get(objInfo.Bucket); err == nil {
|
2021-07-20 17:36:55 -07:00
|
|
|
lc.SetPredictionHeaders(w, objInfo.ToLifecycleOpts())
|
2020-06-12 20:04:01 -07:00
|
|
|
}
|
|
|
|
|
2022-07-24 07:15:49 -07:00
|
|
|
if v, ok := objInfo.UserDefined[ReservedMetadataPrefix+"compression"]; ok {
|
|
|
|
if i := strings.LastIndexByte(v, '/'); i >= 0 {
|
|
|
|
v = v[i+1:]
|
|
|
|
}
|
|
|
|
w.Header()[xhttp.MinIOCompressed] = []string{v}
|
|
|
|
}
|
|
|
|
|
2018-09-20 19:22:09 -07:00
|
|
|
return nil
|
2015-03-11 01:01:49 -07:00
|
|
|
}
|