error_handler.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. encodedErrorResponse := EncodeXMLResponse(errorResponse)
  45. WriteResponse(w, r, apiError.HTTPStatusCode, encodedErrorResponse, MimeXML)
  46. PostLog(r, apiError.HTTPStatusCode, errorCode)
  47. }
  48. func getRESTErrorResponse(err APIError, resource string, bucket, object string) RESTErrorResponse {
  49. return RESTErrorResponse{
  50. Code: err.Code,
  51. BucketName: bucket,
  52. Key: object,
  53. Message: err.Description,
  54. Resource: resource,
  55. RequestID: fmt.Sprintf("%d", time.Now().UnixNano()),
  56. }
  57. }
  58. // Encodes the response headers into XML format.
  59. func EncodeXMLResponse(response interface{}) []byte {
  60. var bytesBuffer bytes.Buffer
  61. bytesBuffer.WriteString(xml.Header)
  62. e := xml.NewEncoder(&bytesBuffer)
  63. e.Encode(response)
  64. return bytesBuffer.Bytes()
  65. }
  66. func setCommonHeaders(w http.ResponseWriter, r *http.Request) {
  67. w.Header().Set("x-amz-request-id", fmt.Sprintf("%d", time.Now().UnixNano()))
  68. w.Header().Set("Accept-Ranges", "bytes")
  69. if r.Header.Get("Origin") != "" {
  70. w.Header().Set("Access-Control-Allow-Origin", "*")
  71. w.Header().Set("Access-Control-Expose-Headers", "*")
  72. w.Header().Set("Access-Control-Allow-Credentials", "true")
  73. }
  74. }
  75. func WriteResponse(w http.ResponseWriter, r *http.Request, statusCode int, response []byte, mType mimeType) {
  76. setCommonHeaders(w, r)
  77. if response != nil {
  78. w.Header().Set("Content-Length", strconv.Itoa(len(response)))
  79. }
  80. if mType != mimeNone {
  81. w.Header().Set("Content-Type", string(mType))
  82. }
  83. w.WriteHeader(statusCode)
  84. if response != nil {
  85. glog.V(4).Infof("status %d %s: %s", statusCode, mType, string(response))
  86. _, err := w.Write(response)
  87. if err != nil {
  88. glog.V(0).Infof("write err: %v", err)
  89. }
  90. w.(http.Flusher).Flush()
  91. }
  92. }
  93. // If none of the http routes match respond with MethodNotAllowed
  94. func NotFoundHandler(w http.ResponseWriter, r *http.Request) {
  95. glog.V(0).Infof("unsupported %s %s", r.Method, r.RequestURI)
  96. WriteErrorResponse(w, r, ErrMethodNotAllowed)
  97. }