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.

107 lines
2.9 KiB

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