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.

105 lines
3.0 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. package iamapi
  2. import (
  3. "bytes"
  4. "encoding/xml"
  5. "fmt"
  6. "strconv"
  7. "net/http"
  8. "net/url"
  9. "time"
  10. "github.com/chrislusf/seaweedfs/weed/glog"
  11. "github.com/chrislusf/seaweedfs/weed/s3api/s3err"
  12. "github.com/aws/aws-sdk-go/service/iam"
  13. )
  14. type mimeType string
  15. const (
  16. mimeNone mimeType = ""
  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. // If none of the http routes match respond with MethodNotAllowed
  32. func notFoundHandler(w http.ResponseWriter, r *http.Request) {
  33. glog.V(0).Infof("unsupported %s %s", r.Method, r.RequestURI)
  34. writeErrorResponse(w, s3err.ErrMethodNotAllowed, r.URL)
  35. }
  36. func writeErrorResponse(w http.ResponseWriter, errorCode s3err.ErrorCode, reqURL *url.URL) {
  37. apiError := s3err.GetAPIError(errorCode)
  38. errorResponse := getRESTErrorResponse(apiError, reqURL.Path)
  39. encodedErrorResponse := encodeResponse(errorResponse)
  40. writeResponse(w, apiError.HTTPStatusCode, encodedErrorResponse, mimeXML)
  41. }
  42. func writeIamErrorResponse(w http.ResponseWriter, err error, object string, value string, msg error) {
  43. errCode := err.Error()
  44. errorResp := ErrorResponse{}
  45. errorResp.Error.Type = "Sender"
  46. errorResp.Error.Code = &errCode
  47. if msg != nil {
  48. errMsg := msg.Error()
  49. errorResp.Error.Message = &errMsg
  50. }
  51. glog.Errorf("Response %+v", err)
  52. switch errCode {
  53. case iam.ErrCodeNoSuchEntityException:
  54. msg := fmt.Sprintf("The %s with name %s cannot be found.", object, value)
  55. errorResp.Error.Message = &msg
  56. writeResponse(w, http.StatusNotFound, encodeResponse(errorResp), mimeXML)
  57. case iam.ErrCodeServiceFailureException:
  58. writeResponse(w, http.StatusInternalServerError, encodeResponse(errorResp), mimeXML)
  59. default:
  60. writeResponse(w, http.StatusInternalServerError, encodeResponse(errorResp), mimeXML)
  61. }
  62. }
  63. func getRESTErrorResponse(err s3err.APIError, resource string) s3err.RESTErrorResponse {
  64. return s3err.RESTErrorResponse{
  65. Code: err.Code,
  66. Message: err.Description,
  67. Resource: resource,
  68. RequestID: fmt.Sprintf("%d", time.Now().UnixNano()),
  69. }
  70. }
  71. func writeResponse(w http.ResponseWriter, statusCode int, response []byte, mType mimeType) {
  72. setCommonHeaders(w)
  73. if response != nil {
  74. w.Header().Set("Content-Length", strconv.Itoa(len(response)))
  75. }
  76. if mType != mimeNone {
  77. w.Header().Set("Content-Type", string(mType))
  78. }
  79. w.WriteHeader(statusCode)
  80. if response != nil {
  81. glog.V(4).Infof("status %d %s: %s", statusCode, mType, string(response))
  82. _, err := w.Write(response)
  83. if err != nil {
  84. glog.V(0).Infof("write err: %v", err)
  85. }
  86. w.(http.Flusher).Flush()
  87. }
  88. }
  89. func writeSuccessResponseXML(w http.ResponseWriter, response []byte) {
  90. writeResponse(w, http.StatusOK, response, mimeXML)
  91. }