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.

102 lines
2.8 KiB

7 years ago
5 years ago
7 years ago
5 years ago
  1. package s3api
  2. import (
  3. "bytes"
  4. "encoding/base64"
  5. "encoding/xml"
  6. "fmt"
  7. "net/http"
  8. "net/url"
  9. "time"
  10. "google.golang.org/grpc"
  11. "github.com/chrislusf/seaweedfs/weed/glog"
  12. "github.com/chrislusf/seaweedfs/weed/pb"
  13. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  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 (s3a *S3ApiServer) withFilerClient(fn func(filer_pb.SeaweedFilerClient) error) error {
  34. return pb.WithCachedGrpcClient(func(grpcConnection *grpc.ClientConn) error {
  35. client := filer_pb.NewSeaweedFilerClient(grpcConnection)
  36. return fn(client)
  37. }, s3a.option.FilerGrpcAddress, s3a.option.GrpcDialOption)
  38. }
  39. // If none of the http routes match respond with MethodNotAllowed
  40. func notFoundHandler(w http.ResponseWriter, r *http.Request) {
  41. glog.V(0).Infof("unsupported %s %s", r.Method, r.RequestURI)
  42. writeErrorResponse(w, ErrMethodNotAllowed, r.URL)
  43. }
  44. func writeErrorResponse(w http.ResponseWriter, errorCode ErrorCode, reqURL *url.URL) {
  45. apiError := getAPIError(errorCode)
  46. errorResponse := getRESTErrorResponse(apiError, reqURL.Path)
  47. encodedErrorResponse := encodeResponse(errorResponse)
  48. writeResponse(w, apiError.HTTPStatusCode, encodedErrorResponse, mimeXML)
  49. }
  50. func getRESTErrorResponse(err APIError, resource string) RESTErrorResponse {
  51. return RESTErrorResponse{
  52. Code: err.Code,
  53. Message: err.Description,
  54. Resource: resource,
  55. RequestID: fmt.Sprintf("%d", time.Now().UnixNano()),
  56. }
  57. }
  58. func writeResponse(w http.ResponseWriter, statusCode int, response []byte, mType mimeType) {
  59. setCommonHeaders(w)
  60. if mType != mimeNone {
  61. w.Header().Set("Content-Type", string(mType))
  62. }
  63. w.WriteHeader(statusCode)
  64. if response != nil {
  65. glog.V(4).Infof("status %d %s: %s", statusCode, mType, string(response))
  66. w.Write(response)
  67. w.(http.Flusher).Flush()
  68. }
  69. }
  70. func writeSuccessResponseXML(w http.ResponseWriter, response []byte) {
  71. writeResponse(w, http.StatusOK, response, mimeXML)
  72. }
  73. func writeSuccessResponseEmpty(w http.ResponseWriter) {
  74. writeResponse(w, http.StatusOK, nil, mimeNone)
  75. }
  76. func validateContentMd5(h http.Header) ([]byte, error) {
  77. md5B64, ok := h["Content-Md5"]
  78. if ok {
  79. if md5B64[0] == "" {
  80. return nil, fmt.Errorf("Content-Md5 header set to empty value")
  81. }
  82. return base64.StdEncoding.DecodeString(md5B64[0])
  83. }
  84. return []byte{}, nil
  85. }