error_handler.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package s3err
  2. import (
  3. "bytes"
  4. "encoding/xml"
  5. "fmt"
  6. "github.com/aws/aws-sdk-go/private/protocol/xml/xmlutil"
  7. "github.com/gorilla/mux"
  8. "github.com/seaweedfs/seaweedfs/weed/glog"
  9. "net/http"
  10. "strconv"
  11. "strings"
  12. "time"
  13. )
  14. type mimeType string
  15. const (
  16. mimeNone mimeType = ""
  17. MimeXML mimeType = "application/xml"
  18. )
  19. func WriteAwsXMLResponse(w http.ResponseWriter, r *http.Request, statusCode int, result interface{}) {
  20. var bytesBuffer bytes.Buffer
  21. err := xmlutil.BuildXML(result, xml.NewEncoder(&bytesBuffer))
  22. if err != nil {
  23. WriteErrorResponse(w, r, ErrInternalError)
  24. return
  25. }
  26. WriteResponse(w, r, statusCode, bytesBuffer.Bytes(), MimeXML)
  27. }
  28. func WriteXMLResponse(w http.ResponseWriter, r *http.Request, statusCode int, response interface{}) {
  29. WriteResponse(w, r, statusCode, EncodeXMLResponse(response), MimeXML)
  30. }
  31. func WriteEmptyResponse(w http.ResponseWriter, r *http.Request, statusCode int) {
  32. WriteResponse(w, r, statusCode, []byte{}, mimeNone)
  33. PostLog(r, statusCode, ErrNone)
  34. }
  35. func WriteErrorResponse(w http.ResponseWriter, r *http.Request, errorCode ErrorCode) {
  36. vars := mux.Vars(r)
  37. bucket := vars["bucket"]
  38. object := vars["object"]
  39. if strings.HasPrefix(object, "/") {
  40. object = object[1:]
  41. }
  42. apiError := GetAPIError(errorCode)
  43. errorResponse := getRESTErrorResponse(apiError, r.URL.Path, bucket, object)
  44. WriteXMLResponse(w, r, apiError.HTTPStatusCode, errorResponse)
  45. PostLog(r, apiError.HTTPStatusCode, errorCode)
  46. }
  47. func getRESTErrorResponse(err APIError, resource string, bucket, object string) RESTErrorResponse {
  48. return RESTErrorResponse{
  49. Code: err.Code,
  50. BucketName: bucket,
  51. Key: object,
  52. Message: err.Description,
  53. Resource: resource,
  54. RequestID: fmt.Sprintf("%d", time.Now().UnixNano()),
  55. }
  56. }
  57. // Encodes the response headers into XML format.
  58. func EncodeXMLResponse(response interface{}) []byte {
  59. var bytesBuffer bytes.Buffer
  60. bytesBuffer.WriteString(xml.Header)
  61. e := xml.NewEncoder(&bytesBuffer)
  62. e.Encode(response)
  63. return bytesBuffer.Bytes()
  64. }
  65. func setCommonHeaders(w http.ResponseWriter, r *http.Request) {
  66. w.Header().Set("x-amz-request-id", fmt.Sprintf("%d", time.Now().UnixNano()))
  67. w.Header().Set("Accept-Ranges", "bytes")
  68. if r.Header.Get("Origin") != "" {
  69. w.Header().Set("Access-Control-Allow-Origin", "*")
  70. w.Header().Set("Access-Control-Expose-Headers", "*")
  71. w.Header().Set("Access-Control-Allow-Credentials", "true")
  72. }
  73. }
  74. func WriteResponse(w http.ResponseWriter, r *http.Request, statusCode int, response []byte, mType mimeType) {
  75. setCommonHeaders(w, r)
  76. if response != nil {
  77. w.Header().Set("Content-Length", strconv.Itoa(len(response)))
  78. }
  79. if mType != mimeNone {
  80. w.Header().Set("Content-Type", string(mType))
  81. }
  82. w.WriteHeader(statusCode)
  83. if response != nil {
  84. glog.V(4).Infof("status %d %s: %s", statusCode, mType, string(response))
  85. _, err := w.Write(response)
  86. if err != nil {
  87. glog.V(0).Infof("write err: %v", err)
  88. }
  89. w.(http.Flusher).Flush()
  90. }
  91. }
  92. // If none of the http routes match respond with MethodNotAllowed
  93. func NotFoundHandler(w http.ResponseWriter, r *http.Request) {
  94. glog.V(0).Infof("unsupported %s %s", r.Method, r.RequestURI)
  95. WriteErrorResponse(w, r, ErrMethodNotAllowed)
  96. }