2018-07-18 09:37:09 +00:00
|
|
|
package s3api
|
|
|
|
|
|
|
|
import (
|
2018-07-22 00:39:10 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/xml"
|
|
|
|
"fmt"
|
2018-07-23 08:16:17 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
2018-07-18 09:37:09 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
2018-07-22 00:39:10 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"time"
|
2018-07-18 09:37:09 +00:00
|
|
|
)
|
|
|
|
|
2018-07-18 09:39:12 +00:00
|
|
|
type mimeType string
|
|
|
|
|
|
|
|
const (
|
|
|
|
mimeNone mimeType = ""
|
|
|
|
mimeJSON mimeType = "application/json"
|
|
|
|
mimeXML mimeType = "application/xml"
|
|
|
|
)
|
|
|
|
|
|
|
|
func setCommonHeaders(w http.ResponseWriter) {
|
|
|
|
w.Header().Set("x-amz-request-id", fmt.Sprintf("%d", time.Now().UnixNano()))
|
|
|
|
w.Header().Set("Accept-Ranges", "bytes")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encodes the response headers into XML format.
|
|
|
|
func encodeResponse(response interface{}) []byte {
|
|
|
|
var bytesBuffer bytes.Buffer
|
|
|
|
bytesBuffer.WriteString(xml.Header)
|
|
|
|
e := xml.NewEncoder(&bytesBuffer)
|
|
|
|
e.Encode(response)
|
|
|
|
return bytesBuffer.Bytes()
|
|
|
|
}
|
|
|
|
|
2018-07-18 09:37:09 +00:00
|
|
|
func (s3a *S3ApiServer) withFilerClient(fn func(filer_pb.SeaweedFilerClient) error) error {
|
|
|
|
|
2019-02-18 20:11:52 +00:00
|
|
|
grpcConnection, err := util.GrpcDial(s3a.option.FilerGrpcAddress, s3a.option.GrpcDialOption)
|
2018-07-18 09:37:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("fail to dial %s: %v", s3a.option.FilerGrpcAddress, err)
|
|
|
|
}
|
|
|
|
defer grpcConnection.Close()
|
|
|
|
|
|
|
|
client := filer_pb.NewSeaweedFilerClient(grpcConnection)
|
|
|
|
|
|
|
|
return fn(client)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If none of the http routes match respond with MethodNotAllowed
|
|
|
|
func notFoundHandler(w http.ResponseWriter, r *http.Request) {
|
2018-07-23 02:55:53 +00:00
|
|
|
glog.V(0).Infof("unsupported %s %s", r.Method, r.RequestURI)
|
2018-07-18 09:37:09 +00:00
|
|
|
writeErrorResponse(w, ErrMethodNotAllowed, r.URL)
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeErrorResponse(w http.ResponseWriter, errorCode ErrorCode, reqURL *url.URL) {
|
|
|
|
apiError := getAPIError(errorCode)
|
|
|
|
errorResponse := getRESTErrorResponse(apiError, reqURL.Path)
|
|
|
|
encodedErrorResponse := encodeResponse(errorResponse)
|
|
|
|
writeResponse(w, apiError.HTTPStatusCode, encodedErrorResponse, mimeXML)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getRESTErrorResponse(err APIError, resource string) RESTErrorResponse {
|
|
|
|
return RESTErrorResponse{
|
|
|
|
Code: err.Code,
|
|
|
|
Message: err.Description,
|
|
|
|
Resource: resource,
|
|
|
|
RequestID: fmt.Sprintf("%d", time.Now().UnixNano()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeResponse(w http.ResponseWriter, statusCode int, response []byte, mType mimeType) {
|
|
|
|
setCommonHeaders(w)
|
|
|
|
if mType != mimeNone {
|
|
|
|
w.Header().Set("Content-Type", string(mType))
|
|
|
|
}
|
|
|
|
w.WriteHeader(statusCode)
|
|
|
|
if response != nil {
|
2019-01-02 19:36:29 +00:00
|
|
|
glog.V(4).Infof("status %d %s: %s", statusCode, mType, string(response))
|
2018-07-18 09:37:09 +00:00
|
|
|
w.Write(response)
|
|
|
|
w.(http.Flusher).Flush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeSuccessResponseXML(w http.ResponseWriter, response []byte) {
|
|
|
|
writeResponse(w, http.StatusOK, response, mimeXML)
|
|
|
|
}
|
2018-07-19 08:21:44 +00:00
|
|
|
|
|
|
|
func writeSuccessResponseEmpty(w http.ResponseWriter) {
|
|
|
|
writeResponse(w, http.StatusOK, nil, mimeNone)
|
|
|
|
}
|
2018-07-21 17:39:02 +00:00
|
|
|
|
|
|
|
func validateContentMd5(h http.Header) ([]byte, error) {
|
|
|
|
md5B64, ok := h["Content-Md5"]
|
|
|
|
if ok {
|
|
|
|
if md5B64[0] == "" {
|
|
|
|
return nil, fmt.Errorf("Content-Md5 header set to empty value")
|
|
|
|
}
|
|
|
|
return base64.StdEncoding.DecodeString(md5B64[0])
|
|
|
|
}
|
|
|
|
return []byte{}, nil
|
|
|
|
}
|