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.

92 lines
2.5 KiB

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