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.

100 lines
2.6 KiB

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/pb/filer_pb"
  8. "github.com/chrislusf/seaweedfs/weed/util"
  9. "net/http"
  10. "net/url"
  11. "time"
  12. )
  13. type mimeType string
  14. const (
  15. mimeNone mimeType = ""
  16. mimeJSON mimeType = "application/json"
  17. mimeXML mimeType = "application/xml"
  18. )
  19. func setCommonHeaders(w http.ResponseWriter) {
  20. w.Header().Set("x-amz-request-id", fmt.Sprintf("%d", time.Now().UnixNano()))
  21. w.Header().Set("Accept-Ranges", "bytes")
  22. }
  23. // Encodes the response headers into XML format.
  24. func encodeResponse(response interface{}) []byte {
  25. var bytesBuffer bytes.Buffer
  26. bytesBuffer.WriteString(xml.Header)
  27. e := xml.NewEncoder(&bytesBuffer)
  28. e.Encode(response)
  29. return bytesBuffer.Bytes()
  30. }
  31. func (s3a *S3ApiServer) withFilerClient(fn func(filer_pb.SeaweedFilerClient) error) error {
  32. grpcConnection, err := util.GrpcDial(s3a.option.FilerGrpcAddress)
  33. if err != nil {
  34. return fmt.Errorf("fail to dial %s: %v", s3a.option.FilerGrpcAddress, err)
  35. }
  36. defer grpcConnection.Close()
  37. client := filer_pb.NewSeaweedFilerClient(grpcConnection)
  38. return fn(client)
  39. }
  40. // If none of the http routes match respond with MethodNotAllowed
  41. func notFoundHandler(w http.ResponseWriter, r *http.Request) {
  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. w.Write(response)
  66. w.(http.Flusher).Flush()
  67. }
  68. }
  69. func writeSuccessResponseXML(w http.ResponseWriter, response []byte) {
  70. writeResponse(w, http.StatusOK, response, mimeXML)
  71. }
  72. func writeSuccessResponseEmpty(w http.ResponseWriter) {
  73. writeResponse(w, http.StatusOK, nil, mimeNone)
  74. }
  75. func validateContentMd5(h http.Header) ([]byte, error) {
  76. md5B64, ok := h["Content-Md5"]
  77. if ok {
  78. if md5B64[0] == "" {
  79. return nil, fmt.Errorf("Content-Md5 header set to empty value")
  80. }
  81. return base64.StdEncoding.DecodeString(md5B64[0])
  82. }
  83. return []byte{}, nil
  84. }