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.

107 lines
2.8 KiB

7 years ago
7 years ago
7 years ago
7 years ago
  1. package s3api
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/base64"
  6. "encoding/xml"
  7. "fmt"
  8. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  9. "github.com/chrislusf/seaweedfs/weed/util"
  10. "github.com/gorilla/mux"
  11. "net/http"
  12. "net/url"
  13. "time"
  14. )
  15. type mimeType string
  16. const (
  17. mimeNone mimeType = ""
  18. mimeJSON mimeType = "application/json"
  19. mimeXML mimeType = "application/xml"
  20. )
  21. func setCommonHeaders(w http.ResponseWriter) {
  22. w.Header().Set("x-amz-request-id", fmt.Sprintf("%d", time.Now().UnixNano()))
  23. w.Header().Set("Accept-Ranges", "bytes")
  24. }
  25. // Encodes the response headers into XML format.
  26. func encodeResponse(response interface{}) []byte {
  27. var bytesBuffer bytes.Buffer
  28. bytesBuffer.WriteString(xml.Header)
  29. e := xml.NewEncoder(&bytesBuffer)
  30. e.Encode(response)
  31. return bytesBuffer.Bytes()
  32. }
  33. func newContext(r *http.Request, api string) context.Context {
  34. vars := mux.Vars(r)
  35. return context.WithValue(context.Background(), "bucket", vars["bucket"])
  36. }
  37. func (s3a *S3ApiServer) withFilerClient(fn func(filer_pb.SeaweedFilerClient) error) error {
  38. grpcConnection, err := util.GrpcDial(s3a.option.FilerGrpcAddress)
  39. if err != nil {
  40. return fmt.Errorf("fail to dial %s: %v", s3a.option.FilerGrpcAddress, err)
  41. }
  42. defer grpcConnection.Close()
  43. client := filer_pb.NewSeaweedFilerClient(grpcConnection)
  44. return fn(client)
  45. }
  46. // If none of the http routes match respond with MethodNotAllowed
  47. func notFoundHandler(w http.ResponseWriter, r *http.Request) {
  48. writeErrorResponse(w, ErrMethodNotAllowed, r.URL)
  49. }
  50. func writeErrorResponse(w http.ResponseWriter, errorCode ErrorCode, reqURL *url.URL) {
  51. apiError := getAPIError(errorCode)
  52. errorResponse := getRESTErrorResponse(apiError, reqURL.Path)
  53. encodedErrorResponse := encodeResponse(errorResponse)
  54. writeResponse(w, apiError.HTTPStatusCode, encodedErrorResponse, mimeXML)
  55. }
  56. func getRESTErrorResponse(err APIError, resource string) RESTErrorResponse {
  57. return 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 mType != mimeNone {
  67. w.Header().Set("Content-Type", string(mType))
  68. }
  69. w.WriteHeader(statusCode)
  70. if response != nil {
  71. w.Write(response)
  72. w.(http.Flusher).Flush()
  73. }
  74. }
  75. func writeSuccessResponseXML(w http.ResponseWriter, response []byte) {
  76. writeResponse(w, http.StatusOK, response, mimeXML)
  77. }
  78. func writeSuccessResponseEmpty(w http.ResponseWriter) {
  79. writeResponse(w, http.StatusOK, nil, mimeNone)
  80. }
  81. func validateContentMd5(h http.Header) ([]byte, error) {
  82. md5B64, ok := h["Content-Md5"]
  83. if ok {
  84. if md5B64[0] == "" {
  85. return nil, fmt.Errorf("Content-Md5 header set to empty value")
  86. }
  87. return base64.StdEncoding.DecodeString(md5B64[0])
  88. }
  89. return []byte{}, nil
  90. }