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.

114 lines
3.1 KiB

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