s3api_handlers.go 2.8 KB

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