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.

106 lines
2.8 KiB

7 years ago
5 years ago
7 years ago
5 years ago
5 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. func (s3a *S3ApiServer) AdjustedUrl(hostAndPort string) string {
  40. return hostAndPort
  41. }
  42. // If none of the http routes match respond with MethodNotAllowed
  43. func notFoundHandler(w http.ResponseWriter, r *http.Request) {
  44. glog.V(0).Infof("unsupported %s %s", r.Method, r.RequestURI)
  45. writeErrorResponse(w, ErrMethodNotAllowed, r.URL)
  46. }
  47. func writeErrorResponse(w http.ResponseWriter, errorCode ErrorCode, reqURL *url.URL) {
  48. apiError := getAPIError(errorCode)
  49. errorResponse := getRESTErrorResponse(apiError, reqURL.Path)
  50. encodedErrorResponse := encodeResponse(errorResponse)
  51. writeResponse(w, apiError.HTTPStatusCode, encodedErrorResponse, mimeXML)
  52. }
  53. func getRESTErrorResponse(err APIError, resource string) RESTErrorResponse {
  54. return RESTErrorResponse{
  55. Code: err.Code,
  56. Message: err.Description,
  57. Resource: resource,
  58. RequestID: fmt.Sprintf("%d", time.Now().UnixNano()),
  59. }
  60. }
  61. func writeResponse(w http.ResponseWriter, statusCode int, response []byte, mType mimeType) {
  62. setCommonHeaders(w)
  63. if mType != mimeNone {
  64. w.Header().Set("Content-Type", string(mType))
  65. }
  66. w.WriteHeader(statusCode)
  67. if response != nil {
  68. glog.V(4).Infof("status %d %s: %s", statusCode, mType, string(response))
  69. w.Write(response)
  70. w.(http.Flusher).Flush()
  71. }
  72. }
  73. func writeSuccessResponseXML(w http.ResponseWriter, response []byte) {
  74. writeResponse(w, http.StatusOK, response, mimeXML)
  75. }
  76. func writeSuccessResponseEmpty(w http.ResponseWriter) {
  77. writeResponse(w, http.StatusOK, nil, mimeNone)
  78. }
  79. func validateContentMd5(h http.Header) ([]byte, error) {
  80. md5B64, ok := h["Content-Md5"]
  81. if ok {
  82. if md5B64[0] == "" {
  83. return nil, fmt.Errorf("Content-Md5 header set to empty value")
  84. }
  85. return base64.StdEncoding.DecodeString(md5B64[0])
  86. }
  87. return []byte{}, nil
  88. }