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.

96 lines
2.7 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. }
  24. func WriteErrorResponse(w http.ResponseWriter, r *http.Request, errorCode ErrorCode) {
  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, r, 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, r *http.Request) {
  55. w.Header().Set("x-amz-request-id", fmt.Sprintf("%d", time.Now().UnixNano()))
  56. w.Header().Set("Accept-Ranges", "bytes")
  57. if r.Header.Get("Origin") != "" {
  58. w.Header().Set("Access-Control-Allow-Origin", "*")
  59. w.Header().Set("Access-Control-Allow-Credentials", "true")
  60. }
  61. }
  62. func WriteResponse(w http.ResponseWriter, r *http.Request, statusCode int, response []byte, mType mimeType) {
  63. setCommonHeaders(w, r)
  64. if response != nil {
  65. w.Header().Set("Content-Length", strconv.Itoa(len(response)))
  66. }
  67. if mType != mimeNone {
  68. w.Header().Set("Content-Type", string(mType))
  69. }
  70. w.WriteHeader(statusCode)
  71. if response != nil {
  72. glog.V(4).Infof("status %d %s: %s", statusCode, mType, string(response))
  73. _, err := w.Write(response)
  74. if err != nil {
  75. glog.V(0).Infof("write err: %v", err)
  76. }
  77. w.(http.Flusher).Flush()
  78. }
  79. }
  80. // If none of the http routes match respond with MethodNotAllowed
  81. func NotFoundHandler(w http.ResponseWriter, r *http.Request) {
  82. glog.V(0).Infof("unsupported %s %s", r.Method, r.RequestURI)
  83. WriteErrorResponse(w, r, ErrMethodNotAllowed)
  84. }