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.

109 lines
3.1 KiB

4 years ago
4 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
3 years ago
4 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
  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-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. }