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.

115 lines
3.1 KiB

7 years ago
4 years ago
7 years ago
7 years ago
5 years ago
7 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
4 years ago
  1. package s3api
  2. import (
  3. "bytes"
  4. "encoding/base64"
  5. "encoding/xml"
  6. "fmt"
  7. "github.com/chrislusf/seaweedfs/weed/s3api/s3err"
  8. "net/http"
  9. "net/url"
  10. "strconv"
  11. "time"
  12. "google.golang.org/grpc"
  13. "github.com/chrislusf/seaweedfs/weed/glog"
  14. "github.com/chrislusf/seaweedfs/weed/pb"
  15. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  16. )
  17. type mimeType string
  18. const (
  19. mimeNone mimeType = ""
  20. mimeJSON mimeType = "application/json"
  21. mimeXML mimeType = "application/xml"
  22. )
  23. func setCommonHeaders(w http.ResponseWriter) {
  24. w.Header().Set("x-amz-request-id", fmt.Sprintf("%d", time.Now().UnixNano()))
  25. w.Header().Set("Accept-Ranges", "bytes")
  26. }
  27. // Encodes the response headers into XML format.
  28. func encodeResponse(response interface{}) []byte {
  29. var bytesBuffer bytes.Buffer
  30. bytesBuffer.WriteString(xml.Header)
  31. e := xml.NewEncoder(&bytesBuffer)
  32. e.Encode(response)
  33. return bytesBuffer.Bytes()
  34. }
  35. var _ = filer_pb.FilerClient(&S3ApiServer{})
  36. func (s3a *S3ApiServer) WithFilerClient(fn func(filer_pb.SeaweedFilerClient) error) error {
  37. return pb.WithCachedGrpcClient(func(grpcConnection *grpc.ClientConn) error {
  38. client := filer_pb.NewSeaweedFilerClient(grpcConnection)
  39. return fn(client)
  40. }, s3a.option.FilerGrpcAddress, s3a.option.GrpcDialOption)
  41. }
  42. func (s3a *S3ApiServer) AdjustedUrl(location *filer_pb.Location) string {
  43. return location.Url
  44. }
  45. // If none of the http routes match respond with MethodNotAllowed
  46. func notFoundHandler(w http.ResponseWriter, r *http.Request) {
  47. glog.V(0).Infof("unsupported %s %s", r.Method, r.RequestURI)
  48. writeErrorResponse(w, s3err.ErrMethodNotAllowed, r.URL)
  49. }
  50. func writeErrorResponse(w http.ResponseWriter, errorCode s3err.ErrorCode, reqURL *url.URL) {
  51. apiError := s3err.GetAPIError(errorCode)
  52. errorResponse := getRESTErrorResponse(apiError, reqURL.Path)
  53. encodedErrorResponse := encodeResponse(errorResponse)
  54. writeResponse(w, apiError.HTTPStatusCode, encodedErrorResponse, mimeXML)
  55. }
  56. func getRESTErrorResponse(err s3err.APIError, resource string) s3err.RESTErrorResponse {
  57. return s3err.RESTErrorResponse{
  58. Code: err.Code,
  59. Message: err.Description,
  60. Resource: resource,
  61. RequestID: fmt.Sprintf("%d", time.Now().UnixNano()),
  62. }
  63. }
  64. func writeResponse(w http.ResponseWriter, statusCode int, response []byte, mType mimeType) {
  65. setCommonHeaders(w)
  66. if response != nil {
  67. w.Header().Set("Content-Length", strconv.Itoa(len(response)))
  68. }
  69. if mType != mimeNone {
  70. w.Header().Set("Content-Type", string(mType))
  71. }
  72. w.WriteHeader(statusCode)
  73. if response != nil {
  74. glog.V(4).Infof("status %d %s: %s", statusCode, mType, string(response))
  75. _, err := w.Write(response)
  76. if err != nil {
  77. glog.V(0).Infof("write err: %v", err)
  78. }
  79. w.(http.Flusher).Flush()
  80. }
  81. }
  82. func writeSuccessResponseXML(w http.ResponseWriter, response []byte) {
  83. writeResponse(w, http.StatusOK, response, mimeXML)
  84. }
  85. func writeSuccessResponseEmpty(w http.ResponseWriter) {
  86. writeResponse(w, http.StatusOK, nil, mimeNone)
  87. }
  88. func validateContentMd5(h http.Header) ([]byte, error) {
  89. md5B64, ok := h["Content-Md5"]
  90. if ok {
  91. if md5B64[0] == "" {
  92. return nil, fmt.Errorf("Content-Md5 header set to empty value")
  93. }
  94. return base64.StdEncoding.DecodeString(md5B64[0])
  95. }
  96. return []byte{}, nil
  97. }