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.

103 lines
2.8 KiB

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