You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
99 lines
2.8 KiB
99 lines
2.8 KiB
10 years ago
|
/*
|
||
10 years ago
|
* Minio Cloud Storage, (C) 2015 Minio, Inc.
|
||
10 years ago
|
*
|
||
|
* 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.
|
||
|
*/
|
||
|
|
||
9 years ago
|
package main
|
||
10 years ago
|
|
||
|
import (
|
||
|
"bytes"
|
||
10 years ago
|
"crypto/rand"
|
||
10 years ago
|
"encoding/xml"
|
||
|
"net/http"
|
||
9 years ago
|
"runtime"
|
||
10 years ago
|
"strconv"
|
||
|
|
||
9 years ago
|
"github.com/minio/minio/pkg/fs"
|
||
10 years ago
|
)
|
||
|
|
||
10 years ago
|
//// helpers
|
||
|
|
||
10 years ago
|
// Static alphaNumeric table used for generating unique request ids
|
||
|
var alphaNumericTable = []byte("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
||
|
|
||
|
// generateRequestID generate request id
|
||
|
func generateRequestID() []byte {
|
||
|
alpha := make([]byte, 16)
|
||
|
rand.Read(alpha)
|
||
|
for i := 0; i < 16; i++ {
|
||
|
alpha[i] = alphaNumericTable[alpha[i]%byte(len(alphaNumericTable))]
|
||
|
}
|
||
|
return alpha
|
||
|
}
|
||
|
|
||
10 years ago
|
// Write http common headers
|
||
9 years ago
|
func setCommonHeaders(w http.ResponseWriter, contentLength int) {
|
||
10 years ago
|
// set unique request ID for each reply
|
||
|
w.Header().Set("X-Amz-Request-Id", string(generateRequestID()))
|
||
9 years ago
|
w.Header().Set("Server", ("Minio/" + minioReleaseTag + " (" + runtime.GOOS + ";" + runtime.GOARCH + ")"))
|
||
10 years ago
|
w.Header().Set("Accept-Ranges", "bytes")
|
||
10 years ago
|
w.Header().Set("Connection", "close")
|
||
10 years ago
|
// should be set to '0' by default
|
||
10 years ago
|
w.Header().Set("Content-Length", strconv.Itoa(contentLength))
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
// Write error response headers
|
||
9 years ago
|
func encodeErrorResponse(response interface{}) []byte {
|
||
10 years ago
|
var bytesBuffer bytes.Buffer
|
||
|
// write common headers
|
||
9 years ago
|
e := xml.NewEncoder(&bytesBuffer)
|
||
9 years ago
|
e.Encode(response)
|
||
10 years ago
|
return bytesBuffer.Bytes()
|
||
|
}
|
||
|
|
||
10 years ago
|
// Write object header
|
||
9 years ago
|
func setObjectHeaders(w http.ResponseWriter, metadata fs.ObjectMetadata, contentRange *httpRange) {
|
||
10 years ago
|
// set common headers
|
||
|
if contentRange != nil {
|
||
|
if contentRange.length > 0 {
|
||
9 years ago
|
setCommonHeaders(w, int(contentRange.length))
|
||
10 years ago
|
} else {
|
||
9 years ago
|
setCommonHeaders(w, int(metadata.Size))
|
||
10 years ago
|
}
|
||
|
} else {
|
||
9 years ago
|
setCommonHeaders(w, int(metadata.Size))
|
||
10 years ago
|
}
|
||
|
// set object headers
|
||
10 years ago
|
lastModified := metadata.Created.Format(http.TimeFormat)
|
||
10 years ago
|
// object related headers
|
||
9 years ago
|
w.Header().Set("Content-Type", "application/octet-stream")
|
||
|
w.Header().Set("ETag", "\""+metadata.Md5+"\"")
|
||
10 years ago
|
w.Header().Set("Last-Modified", lastModified)
|
||
|
|
||
10 years ago
|
// set content range
|
||
10 years ago
|
if contentRange != nil {
|
||
|
if contentRange.start > 0 || contentRange.length > 0 {
|
||
|
w.Header().Set("Content-Range", contentRange.String())
|
||
|
w.WriteHeader(http.StatusPartialContent)
|
||
|
}
|
||
|
}
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
func encodeSuccessResponse(response interface{}) []byte {
|
||
10 years ago
|
var bytesBuffer bytes.Buffer
|
||
9 years ago
|
e := xml.NewEncoder(&bytesBuffer)
|
||
9 years ago
|
e.Encode(response)
|
||
10 years ago
|
return bytesBuffer.Bytes()
|
||
|
}
|