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.

95 lines
2.5 KiB

7 years ago
7 years ago
  1. package s3api
  2. import (
  3. "net/http"
  4. "net/url"
  5. "fmt"
  6. "time"
  7. "github.com/gorilla/mux"
  8. "context"
  9. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  10. "github.com/chrislusf/seaweedfs/weed/util"
  11. "bytes"
  12. "encoding/xml"
  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 newContext(r *http.Request, api string) context.Context {
  33. vars := mux.Vars(r)
  34. return context.WithValue(context.Background(), "bucket", vars["bucket"])
  35. }
  36. func (s3a *S3ApiServer) withFilerClient(fn func(filer_pb.SeaweedFilerClient) error) error {
  37. grpcConnection, err := util.GrpcDial(s3a.option.FilerGrpcAddress)
  38. if err != nil {
  39. return fmt.Errorf("fail to dial %s: %v", s3a.option.FilerGrpcAddress, err)
  40. }
  41. defer grpcConnection.Close()
  42. client := filer_pb.NewSeaweedFilerClient(grpcConnection)
  43. return fn(client)
  44. }
  45. // If none of the http routes match respond with MethodNotAllowed
  46. func notFoundHandler(w http.ResponseWriter, r *http.Request) {
  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 mType != mimeNone {
  66. w.Header().Set("Content-Type", string(mType))
  67. }
  68. w.WriteHeader(statusCode)
  69. if response != nil {
  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. }