Browse Source

refactoring

pull/2427/head
Chris Lu 3 years ago
parent
commit
006b4ec0f8
  1. 8
      weed/iamapi/iamapi_handlers.go
  2. 14
      weed/iamapi/iamapi_management_handlers.go
  3. 14
      weed/s3api/s3api_bucket_handlers.go
  4. 8
      weed/s3api/s3api_handlers.go
  5. 6
      weed/s3api/s3api_object_copy_handlers.go
  6. 4
      weed/s3api/s3api_object_handlers.go
  7. 8
      weed/s3api/s3api_object_handlers_postpolicy.go
  8. 12
      weed/s3api/s3api_object_multipart_handlers.go
  9. 2
      weed/s3api/s3api_object_tagging_handlers.go
  10. 4
      weed/s3api/s3api_objects_list_handlers.go
  11. 2
      weed/s3api/s3api_status_handlers.go
  12. 16
      weed/s3api/s3err/error_handler.go

8
weed/iamapi/iamapi_handlers.go

@ -8,7 +8,7 @@ import (
"net/http" "net/http"
) )
func writeIamErrorResponse(w http.ResponseWriter, err error, object string, value string, msg error) {
func writeIamErrorResponse(w http.ResponseWriter, r *http.Request, err error, object string, value string, msg error) {
errCode := err.Error() errCode := err.Error()
errorResp := ErrorResponse{} errorResp := ErrorResponse{}
errorResp.Error.Type = "Sender" errorResp.Error.Type = "Sender"
@ -22,10 +22,10 @@ func writeIamErrorResponse(w http.ResponseWriter, err error, object string, valu
case iam.ErrCodeNoSuchEntityException: case iam.ErrCodeNoSuchEntityException:
msg := fmt.Sprintf("The %s with name %s cannot be found.", object, value) msg := fmt.Sprintf("The %s with name %s cannot be found.", object, value)
errorResp.Error.Message = &msg errorResp.Error.Message = &msg
s3err.WriteXMLResponse(w, http.StatusNotFound, errorResp)
s3err.WriteXMLResponse(w, r, http.StatusNotFound, errorResp)
case iam.ErrCodeServiceFailureException: case iam.ErrCodeServiceFailureException:
s3err.WriteXMLResponse(w, http.StatusInternalServerError, errorResp)
s3err.WriteXMLResponse(w, r, http.StatusInternalServerError, errorResp)
default: default:
s3err.WriteXMLResponse(w, http.StatusInternalServerError, errorResp)
s3err.WriteXMLResponse(w, r, http.StatusInternalServerError, errorResp)
} }
} }

14
weed/iamapi/iamapi_management_handlers.go

@ -392,7 +392,7 @@ func (iama *IamApiServer) DoActions(w http.ResponseWriter, r *http.Request) {
userName := values.Get("UserName") userName := values.Get("UserName")
response, err = iama.GetUser(s3cfg, userName) response, err = iama.GetUser(s3cfg, userName)
if err != nil { if err != nil {
writeIamErrorResponse(w, err, "user", userName, nil)
writeIamErrorResponse(w, r, err, "user", userName, nil)
return return
} }
changed = false changed = false
@ -400,7 +400,7 @@ func (iama *IamApiServer) DoActions(w http.ResponseWriter, r *http.Request) {
userName := values.Get("UserName") userName := values.Get("UserName")
response, err = iama.DeleteUser(s3cfg, userName) response, err = iama.DeleteUser(s3cfg, userName)
if err != nil { if err != nil {
writeIamErrorResponse(w, err, "user", userName, nil)
writeIamErrorResponse(w, r, err, "user", userName, nil)
return return
} }
case "CreateAccessKey": case "CreateAccessKey":
@ -424,20 +424,20 @@ func (iama *IamApiServer) DoActions(w http.ResponseWriter, r *http.Request) {
case "GetUserPolicy": case "GetUserPolicy":
response, err = iama.GetUserPolicy(s3cfg, values) response, err = iama.GetUserPolicy(s3cfg, values)
if err != nil { if err != nil {
writeIamErrorResponse(w, err, "user", values.Get("UserName"), nil)
writeIamErrorResponse(w, r, err, "user", values.Get("UserName"), nil)
return return
} }
changed = false changed = false
case "DeleteUserPolicy": case "DeleteUserPolicy":
if response, err = iama.DeleteUserPolicy(s3cfg, values); err != nil { if response, err = iama.DeleteUserPolicy(s3cfg, values); err != nil {
writeIamErrorResponse(w, err, "user", values.Get("UserName"), nil)
writeIamErrorResponse(w, r, err, "user", values.Get("UserName"), nil)
} }
default: default:
errNotImplemented := s3err.GetAPIError(s3err.ErrNotImplemented) errNotImplemented := s3err.GetAPIError(s3err.ErrNotImplemented)
errorResponse := ErrorResponse{} errorResponse := ErrorResponse{}
errorResponse.Error.Code = &errNotImplemented.Code errorResponse.Error.Code = &errNotImplemented.Code
errorResponse.Error.Message = &errNotImplemented.Description errorResponse.Error.Message = &errNotImplemented.Description
s3err.WriteXMLResponse(w, errNotImplemented.HTTPStatusCode, errorResponse)
s3err.WriteXMLResponse(w, r, errNotImplemented.HTTPStatusCode, errorResponse)
return return
} }
if changed { if changed {
@ -445,9 +445,9 @@ func (iama *IamApiServer) DoActions(w http.ResponseWriter, r *http.Request) {
err := iama.s3ApiConfig.PutS3ApiConfiguration(s3cfg) err := iama.s3ApiConfig.PutS3ApiConfiguration(s3cfg)
s3cfgLock.Unlock() s3cfgLock.Unlock()
if err != nil { if err != nil {
writeIamErrorResponse(w, fmt.Errorf(iam.ErrCodeServiceFailureException), "", "", err)
writeIamErrorResponse(w, r, fmt.Errorf(iam.ErrCodeServiceFailureException), "", "", err)
return return
} }
} }
s3err.WriteXMLResponse(w, http.StatusOK, response)
s3err.WriteXMLResponse(w, r, http.StatusOK, response)
} }

14
weed/s3api/s3api_bucket_handlers.go

@ -73,7 +73,7 @@ func (s3a *S3ApiServer) ListBucketsHandler(w http.ResponseWriter, r *http.Reques
Buckets: buckets, Buckets: buckets,
} }
writeSuccessResponseXML(w, response)
writeSuccessResponseXML(w, r, response)
} }
func (s3a *S3ApiServer) PutBucketHandler(w http.ResponseWriter, r *http.Request) { func (s3a *S3ApiServer) PutBucketHandler(w http.ResponseWriter, r *http.Request) {
@ -127,7 +127,7 @@ func (s3a *S3ApiServer) PutBucketHandler(w http.ResponseWriter, r *http.Request)
return return
} }
writeSuccessResponseEmpty(w)
writeSuccessResponseEmpty(w, r)
} }
func (s3a *S3ApiServer) DeleteBucketHandler(w http.ResponseWriter, r *http.Request) { func (s3a *S3ApiServer) DeleteBucketHandler(w http.ResponseWriter, r *http.Request) {
@ -162,7 +162,7 @@ func (s3a *S3ApiServer) DeleteBucketHandler(w http.ResponseWriter, r *http.Reque
return return
} }
s3err.WriteEmptyResponse(w, http.StatusNoContent)
s3err.WriteEmptyResponse(w, r, http.StatusNoContent)
} }
func (s3a *S3ApiServer) HeadBucketHandler(w http.ResponseWriter, r *http.Request) { func (s3a *S3ApiServer) HeadBucketHandler(w http.ResponseWriter, r *http.Request) {
@ -175,7 +175,7 @@ func (s3a *S3ApiServer) HeadBucketHandler(w http.ResponseWriter, r *http.Request
return return
} }
writeSuccessResponseEmpty(w)
writeSuccessResponseEmpty(w, r)
} }
func (s3a *S3ApiServer) checkBucket(r *http.Request, bucket string) s3err.ErrorCode { func (s3a *S3ApiServer) checkBucket(r *http.Request, bucket string) s3err.ErrorCode {
@ -245,7 +245,7 @@ func (s3a *S3ApiServer) GetBucketAclHandler(w http.ResponseWriter, r *http.Reque
}) })
} }
} }
writeSuccessResponseXML(w, response)
writeSuccessResponseXML(w, r, response)
} }
// GetBucketLifecycleConfigurationHandler Get Bucket Lifecycle configuration // GetBucketLifecycleConfigurationHandler Get Bucket Lifecycle configuration
@ -285,7 +285,7 @@ func (s3a *S3ApiServer) GetBucketLifecycleConfigurationHandler(w http.ResponseWr
Expiration: Expiration{Days: days, set: true}, Expiration: Expiration{Days: days, set: true},
}) })
} }
writeSuccessResponseXML(w, response)
writeSuccessResponseXML(w, r, response)
} }
// PutBucketLifecycleConfigurationHandler Put Bucket Lifecycle configuration // PutBucketLifecycleConfigurationHandler Put Bucket Lifecycle configuration
@ -300,6 +300,6 @@ func (s3a *S3ApiServer) PutBucketLifecycleConfigurationHandler(w http.ResponseWr
// https://docs.aws.amazon.com/AmazonS3/latest/API/API_DeleteBucketLifecycle.html // https://docs.aws.amazon.com/AmazonS3/latest/API/API_DeleteBucketLifecycle.html
func (s3a *S3ApiServer) DeleteBucketLifecycleHandler(w http.ResponseWriter, r *http.Request) { func (s3a *S3ApiServer) DeleteBucketLifecycleHandler(w http.ResponseWriter, r *http.Request) {
s3err.WriteEmptyResponse(w, http.StatusNoContent)
s3err.WriteEmptyResponse(w, r, http.StatusNoContent)
} }

8
weed/s3api/s3api_handlers.go

@ -26,12 +26,12 @@ func (s3a *S3ApiServer) AdjustedUrl(location *filer_pb.Location) string {
return location.Url return location.Url
} }
func writeSuccessResponseXML(w http.ResponseWriter, response interface{}) {
s3err.WriteXMLResponse(w, http.StatusOK, response)
func writeSuccessResponseXML(w http.ResponseWriter, r *http.Request, response interface{}) {
s3err.WriteXMLResponse(w, r, http.StatusOK, response)
} }
func writeSuccessResponseEmpty(w http.ResponseWriter) {
s3err.WriteEmptyResponse(w, http.StatusOK)
func writeSuccessResponseEmpty(w http.ResponseWriter, r *http.Request) {
s3err.WriteEmptyResponse(w, r, http.StatusOK)
} }
func validateContentMd5(h http.Header) ([]byte, error) { func validateContentMd5(h http.Header) ([]byte, error) {

6
weed/s3api/s3api_object_copy_handlers.go

@ -43,7 +43,7 @@ func (s3a *S3ApiServer) CopyObjectHandler(w http.ResponseWriter, r *http.Request
s3err.WriteErrorResponse(w, s3err.ErrInvalidCopySource, r) s3err.WriteErrorResponse(w, s3err.ErrInvalidCopySource, r)
return return
} }
writeSuccessResponseXML(w, CopyObjectResult{
writeSuccessResponseXML(w, r, CopyObjectResult{
ETag: fmt.Sprintf("%x", entry.Attributes.Md5), ETag: fmt.Sprintf("%x", entry.Attributes.Md5),
LastModified: time.Now().UTC(), LastModified: time.Now().UTC(),
}) })
@ -95,7 +95,7 @@ func (s3a *S3ApiServer) CopyObjectHandler(w http.ResponseWriter, r *http.Request
LastModified: time.Now().UTC(), LastModified: time.Now().UTC(),
} }
writeSuccessResponseXML(w, response)
writeSuccessResponseXML(w, r, response)
} }
@ -178,7 +178,7 @@ func (s3a *S3ApiServer) CopyObjectPartHandler(w http.ResponseWriter, r *http.Req
LastModified: time.Now().UTC(), LastModified: time.Now().UTC(),
} }
writeSuccessResponseXML(w, response)
writeSuccessResponseXML(w, r, response)
} }

4
weed/s3api/s3api_object_handlers.go

@ -120,7 +120,7 @@ func (s3a *S3ApiServer) PutObjectHandler(w http.ResponseWriter, r *http.Request)
setEtag(w, etag) setEtag(w, etag)
} }
writeSuccessResponseEmpty(w)
writeSuccessResponseEmpty(w, r)
} }
func urlPathEscape(object string) string { func urlPathEscape(object string) string {
@ -273,7 +273,7 @@ func (s3a *S3ApiServer) DeleteMultipleObjectsHandler(w http.ResponseWriter, r *h
} }
deleteResp.Errors = deleteErrors deleteResp.Errors = deleteErrors
writeSuccessResponseXML(w, deleteResp)
writeSuccessResponseXML(w, r, deleteResp)
} }

8
weed/s3api/s3api_object_handlers_postpolicy.go

@ -126,7 +126,7 @@ func (s3a *S3ApiServer) PostPolicyBucketHandler(w http.ResponseWriter, r *http.R
// Replace raw query params.. // Replace raw query params..
redirectURL.RawQuery = getRedirectPostRawQuery(bucket, object, etag) redirectURL.RawQuery = getRedirectPostRawQuery(bucket, object, etag)
w.Header().Set("Location", redirectURL.String()) w.Header().Set("Location", redirectURL.String())
s3err.WriteEmptyResponse(w, http.StatusSeeOther)
s3err.WriteEmptyResponse(w, r, http.StatusSeeOther)
return return
} }
@ -141,11 +141,11 @@ func (s3a *S3ApiServer) PostPolicyBucketHandler(w http.ResponseWriter, r *http.R
ETag: `"` + etag + `"`, ETag: `"` + etag + `"`,
Location: w.Header().Get("Location"), Location: w.Header().Get("Location"),
} }
s3err.WriteXMLResponse(w, http.StatusCreated, resp)
s3err.WriteXMLResponse(w, r, http.StatusCreated, resp)
case "200": case "200":
s3err.WriteEmptyResponse(w, http.StatusOK)
s3err.WriteEmptyResponse(w, r, http.StatusOK)
default: default:
writeSuccessResponseEmpty(w)
writeSuccessResponseEmpty(w, r)
} }
} }

12
weed/s3api/s3api_object_multipart_handlers.go

@ -49,7 +49,7 @@ func (s3a *S3ApiServer) NewMultipartUploadHandler(w http.ResponseWriter, r *http
return return
} }
writeSuccessResponseXML(w, response)
writeSuccessResponseXML(w, r, response)
} }
@ -73,7 +73,7 @@ func (s3a *S3ApiServer) CompleteMultipartUploadHandler(w http.ResponseWriter, r
return return
} }
writeSuccessResponseXML(w, response)
writeSuccessResponseXML(w, r, response)
} }
@ -97,7 +97,7 @@ func (s3a *S3ApiServer) AbortMultipartUploadHandler(w http.ResponseWriter, r *ht
glog.V(2).Info("AbortMultipartUploadHandler", string(s3err.EncodeXMLResponse(response))) glog.V(2).Info("AbortMultipartUploadHandler", string(s3err.EncodeXMLResponse(response)))
writeSuccessResponseXML(w, response)
writeSuccessResponseXML(w, r, response)
} }
@ -137,7 +137,7 @@ func (s3a *S3ApiServer) ListMultipartUploadsHandler(w http.ResponseWriter, r *ht
// TODO handle encodingType // TODO handle encodingType
writeSuccessResponseXML(w, response)
writeSuccessResponseXML(w, r, response)
} }
// ListObjectPartsHandler - Lists object parts in a multipart upload. // ListObjectPartsHandler - Lists object parts in a multipart upload.
@ -169,7 +169,7 @@ func (s3a *S3ApiServer) ListObjectPartsHandler(w http.ResponseWriter, r *http.Re
return return
} }
writeSuccessResponseXML(w, response)
writeSuccessResponseXML(w, r, response)
} }
@ -231,7 +231,7 @@ func (s3a *S3ApiServer) PutObjectPartHandler(w http.ResponseWriter, r *http.Requ
setEtag(w, etag) setEtag(w, etag)
writeSuccessResponseEmpty(w)
writeSuccessResponseEmpty(w, r)
} }

2
weed/s3api/s3api_object_tagging_handlers.go

@ -34,7 +34,7 @@ func (s3a *S3ApiServer) GetObjectTaggingHandler(w http.ResponseWriter, r *http.R
return return
} }
writeSuccessResponseXML(w, FromTags(tags))
writeSuccessResponseXML(w, r, FromTags(tags))
} }

4
weed/s3api/s3api_objects_list_handlers.go

@ -87,7 +87,7 @@ func (s3a *S3ApiServer) ListObjectsV2Handler(w http.ResponseWriter, r *http.Requ
StartAfter: startAfter, StartAfter: startAfter,
} }
writeSuccessResponseXML(w, responseV2)
writeSuccessResponseXML(w, r, responseV2)
} }
func (s3a *S3ApiServer) ListObjectsV1Handler(w http.ResponseWriter, r *http.Request) { func (s3a *S3ApiServer) ListObjectsV1Handler(w http.ResponseWriter, r *http.Request) {
@ -123,7 +123,7 @@ func (s3a *S3ApiServer) ListObjectsV1Handler(w http.ResponseWriter, r *http.Requ
} }
} }
writeSuccessResponseXML(w, response)
writeSuccessResponseXML(w, r, response)
} }
func (s3a *S3ApiServer) listFilerEntries(bucket string, originalPrefix string, maxKeys int, marker string, delimiter string) (response ListBucketResult, err error) { func (s3a *S3ApiServer) listFilerEntries(bucket string, originalPrefix string, maxKeys int, marker string, delimiter string) (response ListBucketResult, err error) {

2
weed/s3api/s3api_status_handlers.go

@ -4,5 +4,5 @@ import "net/http"
func (s3a *S3ApiServer) StatusHandler(w http.ResponseWriter, r *http.Request) { func (s3a *S3ApiServer) StatusHandler(w http.ResponseWriter, r *http.Request) {
// write out the response code and content type header // write out the response code and content type header
writeSuccessResponseEmpty(w)
writeSuccessResponseEmpty(w, r)
} }

16
weed/s3api/s3err/error_handler.go

@ -19,12 +19,12 @@ const (
MimeXML mimeType = "application/xml" MimeXML mimeType = "application/xml"
) )
func WriteXMLResponse(w http.ResponseWriter, statusCode int, response interface{}) {
WriteResponse(w, statusCode, EncodeXMLResponse(response), MimeXML)
func WriteXMLResponse(w http.ResponseWriter, r *http.Request, statusCode int, response interface{}) {
WriteResponse(w, r, statusCode, EncodeXMLResponse(response), MimeXML)
} }
func WriteEmptyResponse(w http.ResponseWriter, statusCode int) {
WriteResponse(w, statusCode, []byte{}, mimeNone)
func WriteEmptyResponse(w http.ResponseWriter, r *http.Request, statusCode int) {
WriteResponse(w, r, statusCode, []byte{}, mimeNone)
} }
func WriteErrorResponse(w http.ResponseWriter, errorCode ErrorCode, r *http.Request) { func WriteErrorResponse(w http.ResponseWriter, errorCode ErrorCode, r *http.Request) {
@ -38,7 +38,7 @@ func WriteErrorResponse(w http.ResponseWriter, errorCode ErrorCode, r *http.Requ
apiError := GetAPIError(errorCode) apiError := GetAPIError(errorCode)
errorResponse := getRESTErrorResponse(apiError, r.URL.Path, bucket, object) errorResponse := getRESTErrorResponse(apiError, r.URL.Path, bucket, object)
encodedErrorResponse := EncodeXMLResponse(errorResponse) encodedErrorResponse := EncodeXMLResponse(errorResponse)
WriteResponse(w, apiError.HTTPStatusCode, encodedErrorResponse, MimeXML)
WriteResponse(w, r, apiError.HTTPStatusCode, encodedErrorResponse, MimeXML)
} }
func getRESTErrorResponse(err APIError, resource string, bucket, object string) RESTErrorResponse { func getRESTErrorResponse(err APIError, resource string, bucket, object string) RESTErrorResponse {
@ -61,13 +61,13 @@ func EncodeXMLResponse(response interface{}) []byte {
return bytesBuffer.Bytes() return bytesBuffer.Bytes()
} }
func setCommonHeaders(w http.ResponseWriter) {
func setCommonHeaders(w http.ResponseWriter, r *http.Request) {
w.Header().Set("x-amz-request-id", fmt.Sprintf("%d", time.Now().UnixNano())) w.Header().Set("x-amz-request-id", fmt.Sprintf("%d", time.Now().UnixNano()))
w.Header().Set("Accept-Ranges", "bytes") w.Header().Set("Accept-Ranges", "bytes")
} }
func WriteResponse(w http.ResponseWriter, statusCode int, response []byte, mType mimeType) {
setCommonHeaders(w)
func WriteResponse(w http.ResponseWriter, r *http.Request, statusCode int, response []byte, mType mimeType) {
setCommonHeaders(w, r)
if response != nil { if response != nil {
w.Header().Set("Content-Length", strconv.Itoa(len(response))) w.Header().Set("Content-Length", strconv.Itoa(len(response)))
} }

Loading…
Cancel
Save