seaweedfs/weed/s3api/s3api_handlers.go

116 lines
3.1 KiB
Go
Raw Permalink Normal View History

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"
2020-09-19 21:09:58 +00:00
"github.com/chrislusf/seaweedfs/weed/s3api/s3err"
2018-07-22 00:39:10 +00:00
"net/http"
"net/url"
"strconv"
2018-07-22 00:39:10 +00:00
"time"
"google.golang.org/grpc"
"github.com/chrislusf/seaweedfs/weed/util/log"
2020-03-04 08:39:47 +00:00
"github.com/chrislusf/seaweedfs/weed/pb"
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
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()
}
2020-04-29 20:26:02 +00:00
var _ = filer_pb.FilerClient(&S3ApiServer{})
2020-03-23 06:52:55 +00:00
func (s3a *S3ApiServer) WithFilerClient(fn func(filer_pb.SeaweedFilerClient) error) error {
2018-07-18 09:37:09 +00:00
2020-03-04 08:39:47 +00:00
return pb.WithCachedGrpcClient(func(grpcConnection *grpc.ClientConn) error {
2019-04-06 03:31:58 +00:00
client := filer_pb.NewSeaweedFilerClient(grpcConnection)
return fn(client)
}, s3a.option.FilerGrpcAddress, s3a.option.GrpcDialOption)
2018-07-18 09:37:09 +00:00
}
func (s3a *S3ApiServer) AdjustedUrl(location *filer_pb.Location) string {
return location.Url
2020-03-23 07:06:24 +00:00
}
2018-07-18 09:37:09 +00:00
// If none of the http routes match respond with MethodNotAllowed
func notFoundHandler(w http.ResponseWriter, r *http.Request) {
log.Infof("unsupported %s %s", r.Method, r.RequestURI)
2020-09-19 21:09:58 +00:00
writeErrorResponse(w, s3err.ErrMethodNotAllowed, r.URL)
2018-07-18 09:37:09 +00:00
}
2020-09-19 21:09:58 +00:00
func writeErrorResponse(w http.ResponseWriter, errorCode s3err.ErrorCode, reqURL *url.URL) {
apiError := s3err.GetAPIError(errorCode)
2018-07-18 09:37:09 +00:00
errorResponse := getRESTErrorResponse(apiError, reqURL.Path)
encodedErrorResponse := encodeResponse(errorResponse)
writeResponse(w, apiError.HTTPStatusCode, encodedErrorResponse, mimeXML)
}
2020-09-19 21:09:58 +00:00
func getRESTErrorResponse(err s3err.APIError, resource string) s3err.RESTErrorResponse {
return s3err.RESTErrorResponse{
2018-07-18 09:37:09 +00:00
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 response != nil {
w.Header().Set("Content-Length", strconv.Itoa(len(response)))
}
2018-07-18 09:37:09 +00:00
if mType != mimeNone {
w.Header().Set("Content-Type", string(mType))
}
w.WriteHeader(statusCode)
if response != nil {
log.Tracef("status %d %s: %s", statusCode, mType, string(response))
_, err := w.Write(response)
if err != nil {
log.Infof("write err: %v", err)
}
2018-07-18 09:37:09 +00:00
w.(http.Flusher).Flush()
}
}
func writeSuccessResponseXML(w http.ResponseWriter, response []byte) {
writeResponse(w, http.StatusOK, response, mimeXML)
}
func writeSuccessResponseEmpty(w http.ResponseWriter) {
writeResponse(w, http.StatusOK, nil, mimeNone)
}
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
}