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.
121 lines
3.5 KiB
121 lines
3.5 KiB
10 years ago
|
/*
|
||
9 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"
|
||
9 years ago
|
"crypto/rand"
|
||
10 years ago
|
"encoding/json"
|
||
|
"encoding/xml"
|
||
|
"net/http"
|
||
9 years ago
|
"runtime"
|
||
10 years ago
|
"strconv"
|
||
|
|
||
10 years ago
|
"github.com/minio/minio/pkg/donut"
|
||
10 years ago
|
)
|
||
|
|
||
10 years ago
|
// No encoder interface exists, so we create one.
|
||
|
type encoder interface {
|
||
|
Encode(v interface{}) error
|
||
|
}
|
||
|
|
||
10 years ago
|
//// helpers
|
||
|
|
||
9 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
|
||
10 years ago
|
func setCommonHeaders(w http.ResponseWriter, acceptsType string, contentLength int) {
|
||
9 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 + ")"))
|
||
9 years ago
|
|
||
10 years ago
|
w.Header().Set("Accept-Ranges", "bytes")
|
||
10 years ago
|
w.Header().Set("Content-Type", acceptsType)
|
||
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
|
||
10 years ago
|
func encodeErrorResponse(response interface{}, acceptsType contentType) []byte {
|
||
10 years ago
|
var bytesBuffer bytes.Buffer
|
||
9 years ago
|
var e encoder
|
||
10 years ago
|
// write common headers
|
||
|
switch acceptsType {
|
||
10 years ago
|
case xmlContentType:
|
||
9 years ago
|
e = xml.NewEncoder(&bytesBuffer)
|
||
10 years ago
|
case jsonContentType:
|
||
9 years ago
|
e = json.NewEncoder(&bytesBuffer)
|
||
10 years ago
|
// by default even if unknown Accept header received handle it by sending XML contenttype response
|
||
|
default:
|
||
9 years ago
|
e = xml.NewEncoder(&bytesBuffer)
|
||
10 years ago
|
}
|
||
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 donut.ObjectMetadata, contentRange *httpRange) {
|
||
|
// set common headers
|
||
|
if contentRange != nil {
|
||
|
if contentRange.length > 0 {
|
||
|
setCommonHeaders(w, metadata.Metadata["contentType"], int(contentRange.length))
|
||
|
} else {
|
||
|
setCommonHeaders(w, metadata.Metadata["contentType"], int(metadata.Size))
|
||
|
}
|
||
|
} else {
|
||
|
setCommonHeaders(w, metadata.Metadata["contentType"], int(metadata.Size))
|
||
|
}
|
||
|
// set object headers
|
||
10 years ago
|
lastModified := metadata.Created.Format(http.TimeFormat)
|
||
10 years ago
|
// object related headers
|
||
10 years ago
|
w.Header().Set("ETag", "\""+metadata.MD5Sum+"\"")
|
||
10 years ago
|
w.Header().Set("Last-Modified", lastModified)
|
||
|
|
||
10 years ago
|
// set content range
|
||
9 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
|
}
|
||
|
|
||
10 years ago
|
func encodeSuccessResponse(response interface{}, acceptsType contentType) []byte {
|
||
9 years ago
|
var e encoder
|
||
10 years ago
|
var bytesBuffer bytes.Buffer
|
||
10 years ago
|
switch acceptsType {
|
||
10 years ago
|
case xmlContentType:
|
||
9 years ago
|
e = xml.NewEncoder(&bytesBuffer)
|
||
10 years ago
|
case jsonContentType:
|
||
9 years ago
|
e = json.NewEncoder(&bytesBuffer)
|
||
10 years ago
|
}
|
||
9 years ago
|
e.Encode(response)
|
||
10 years ago
|
return bytesBuffer.Bytes()
|
||
|
}
|