2015-02-15 09:48:15 +01:00
|
|
|
/*
|
2019-04-09 20:39:42 +02:00
|
|
|
* MinIO Cloud Storage, (C) 2015, 2016, 2017 MinIO, Inc.
|
2015-02-15 09:48:15 +01:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2016-08-19 01:23:42 +02:00
|
|
|
package cmd
|
2015-02-11 12:23:15 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-01-22 23:54:55 +01:00
|
|
|
"encoding/json"
|
2015-02-11 12:23:15 +01:00
|
|
|
"encoding/xml"
|
2017-01-11 01:43:48 +01:00
|
|
|
"fmt"
|
2015-02-11 12:23:15 +01:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
2017-01-11 01:43:48 +01:00
|
|
|
"time"
|
2018-09-21 04:22:09 +02:00
|
|
|
|
|
|
|
"github.com/minio/minio/cmd/crypto"
|
2019-07-03 07:34:32 +02:00
|
|
|
xhttp "github.com/minio/minio/cmd/http"
|
2015-02-11 12:23:15 +01:00
|
|
|
)
|
|
|
|
|
2017-01-11 01:43:48 +01: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 23:29:56 +02:00
|
|
|
}
|
|
|
|
|
2015-02-24 01:46:48 +01:00
|
|
|
// Write http common headers
|
2016-01-08 09:40:06 +01:00
|
|
|
func setCommonHeaders(w http.ResponseWriter) {
|
2019-07-03 07:34:32 +02:00
|
|
|
w.Header().Set(xhttp.ServerInfo, "MinIO/"+ReleaseTag)
|
2017-06-24 01:05:40 +02:00
|
|
|
// Set `x-amz-bucket-region` only if region is set on the server
|
|
|
|
// by default minio uses an empty region.
|
2019-10-23 07:59:13 +02:00
|
|
|
if region := globalServerRegion; region != "" {
|
2019-07-03 07:34:32 +02:00
|
|
|
w.Header().Set(xhttp.AmzBucketRegion, region)
|
2017-06-24 01:05:40 +02:00
|
|
|
}
|
2019-07-03 07:34:32 +02:00
|
|
|
w.Header().Set(xhttp.AcceptRanges, "bytes")
|
2018-09-25 21:39:46 +02:00
|
|
|
|
|
|
|
// Remove sensitive information
|
|
|
|
crypto.RemoveSensitiveHeaders(w.Header())
|
2015-02-11 12:23:15 +01:00
|
|
|
}
|
|
|
|
|
2016-03-06 21:16:22 +01:00
|
|
|
// Encodes the response headers into XML format.
|
|
|
|
func encodeResponse(response interface{}) []byte {
|
2015-02-11 12:23:15 +01:00
|
|
|
var bytesBuffer bytes.Buffer
|
2015-12-10 00:38:40 +01:00
|
|
|
bytesBuffer.WriteString(xml.Header)
|
2015-10-04 09:27:49 +02:00
|
|
|
e := xml.NewEncoder(&bytesBuffer)
|
2015-09-10 00:11:37 +02:00
|
|
|
e.Encode(response)
|
2015-02-11 12:23:15 +01:00
|
|
|
return bytesBuffer.Bytes()
|
|
|
|
}
|
|
|
|
|
2018-01-22 23:54:55 +01: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()
|
|
|
|
}
|
|
|
|
|
2015-02-24 01:46:48 +01:00
|
|
|
// Write object header
|
2018-09-21 04:22:09 +02:00
|
|
|
func setObjectHeaders(w http.ResponseWriter, objInfo ObjectInfo, rs *HTTPRangeSpec) (err error) {
|
2015-07-29 04:33:56 +02:00
|
|
|
// set common headers
|
2016-03-06 21:16:22 +01:00
|
|
|
setCommonHeaders(w)
|
|
|
|
|
2016-07-23 05:31:45 +02:00
|
|
|
// Set last modified time.
|
2016-04-08 19:37:38 +02:00
|
|
|
lastModified := objInfo.ModTime.UTC().Format(http.TimeFormat)
|
2019-07-03 07:34:32 +02:00
|
|
|
w.Header().Set(xhttp.LastModified, lastModified)
|
2016-03-06 21:16:22 +01:00
|
|
|
|
2016-07-23 05:31:45 +02:00
|
|
|
// Set Etag if available.
|
2017-05-14 21:05:51 +02:00
|
|
|
if objInfo.ETag != "" {
|
2019-07-03 07:34:32 +02:00
|
|
|
w.Header()[xhttp.ETag] = []string{"\"" + objInfo.ETag + "\""}
|
2016-01-29 04:57:23 +01:00
|
|
|
}
|
2016-07-23 05:31:45 +02:00
|
|
|
|
2017-10-13 12:56:16 +02:00
|
|
|
if objInfo.ContentType != "" {
|
2019-07-03 07:34:32 +02:00
|
|
|
w.Header().Set(xhttp.ContentType, objInfo.ContentType)
|
2017-10-13 12:56:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if objInfo.ContentEncoding != "" {
|
2019-07-03 07:34:32 +02:00
|
|
|
w.Header().Set(xhttp.ContentEncoding, objInfo.ContentEncoding)
|
2017-10-13 12:56:16 +02:00
|
|
|
}
|
|
|
|
|
2019-02-28 20:01:25 +01:00
|
|
|
if !objInfo.Expires.IsZero() {
|
2019-07-03 07:34:32 +02:00
|
|
|
w.Header().Set(xhttp.Expires, objInfo.Expires.UTC().Format(http.TimeFormat))
|
2019-02-28 20:01:25 +01:00
|
|
|
}
|
2019-04-09 01:54:46 +02:00
|
|
|
|
2016-07-23 05:31:45 +02:00
|
|
|
// Set all other user defined metadata.
|
2016-07-12 21:45:17 +02:00
|
|
|
for k, v := range objInfo.UserDefined {
|
2019-12-06 08:16:06 +01:00
|
|
|
if HasPrefix(k, ReservedMetadataPrefix) {
|
2018-03-02 01:15:53 +01:00
|
|
|
// Do not need to send any internal metadata
|
|
|
|
// values to client.
|
|
|
|
continue
|
|
|
|
}
|
2016-07-20 21:40:20 +02:00
|
|
|
w.Header().Set(k, v)
|
2016-07-12 21:45:17 +02:00
|
|
|
}
|
2016-03-06 21:16:22 +01:00
|
|
|
|
2018-09-21 04:22:09 +02:00
|
|
|
var totalObjectSize int64
|
|
|
|
switch {
|
|
|
|
case crypto.IsEncrypted(objInfo.UserDefined):
|
|
|
|
totalObjectSize, err = objInfo.DecryptedSize()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-09-28 05:36:17 +02:00
|
|
|
case objInfo.IsCompressed():
|
|
|
|
totalObjectSize = objInfo.GetActualSize()
|
|
|
|
if totalObjectSize < 0 {
|
|
|
|
return errInvalidDecompressedSize
|
|
|
|
}
|
2018-09-21 04:22:09 +02:00
|
|
|
default:
|
|
|
|
totalObjectSize = objInfo.Size
|
|
|
|
}
|
|
|
|
|
2016-03-06 21:16:22 +01:00
|
|
|
// for providing ranged content
|
2018-09-21 04:22:09 +02:00
|
|
|
start, rangeLen, err := rs.GetOffsetLength(totalObjectSize)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-07-29 04:33:56 +02:00
|
|
|
}
|
2018-09-21 04:22:09 +02:00
|
|
|
|
|
|
|
// Set content length.
|
2019-07-03 07:34:32 +02:00
|
|
|
w.Header().Set(xhttp.ContentLength, strconv.FormatInt(rangeLen, 10))
|
2018-09-21 04:22:09 +02:00
|
|
|
if rs != nil {
|
|
|
|
contentRange := fmt.Sprintf("bytes %d-%d/%d", start, start+rangeLen-1, totalObjectSize)
|
2019-07-03 07:34:32 +02:00
|
|
|
w.Header().Set(xhttp.ContentRange, contentRange)
|
2018-09-21 04:22:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2015-03-11 09:01:49 +01:00
|
|
|
}
|