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.

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