Chris Lu
4 years ago
19 changed files with 215 additions and 269 deletions
-
83weed/iamapi/iamapi_handlers.go
-
12weed/iamapi/iamapi_management_handlers.go
-
3weed/iamapi/iamapi_server.go
-
2weed/s3api/auth_credentials.go
-
5weed/s3api/filer_multipart_test.go
-
20weed/s3api/s3api_bucket_handlers.go
-
3weed/s3api/s3api_bucket_handlers_test.go
-
77weed/s3api/s3api_handlers.go
-
30weed/s3api/s3api_object_copy_handlers.go
-
30weed/s3api/s3api_object_handlers.go
-
32weed/s3api/s3api_object_handlers_postpolicy.go
-
48weed/s3api/s3api_object_multipart_handlers.go
-
24weed/s3api/s3api_object_tagging_handlers.go
-
20weed/s3api/s3api_objects_list_handlers.go
-
3weed/s3api/s3api_objects_list_handlers_test.go
-
3weed/s3api/s3api_server.go
-
5weed/s3api/s3api_test.go
-
81weed/s3api/s3err/error_handler.go
-
3weed/s3api/tags_test.go
@ -0,0 +1,81 @@ |
|||
package s3err |
|||
|
|||
import ( |
|||
"bytes" |
|||
"encoding/xml" |
|||
"fmt" |
|||
"github.com/chrislusf/seaweedfs/weed/glog" |
|||
"net/http" |
|||
"strconv" |
|||
"time" |
|||
) |
|||
|
|||
type mimeType string |
|||
|
|||
const ( |
|||
mimeNone mimeType = "" |
|||
MimeXML mimeType = "application/xml" |
|||
) |
|||
|
|||
func WriteXMLResponse(w http.ResponseWriter, statusCode int, response interface{}) { |
|||
WriteResponse(w, statusCode, EncodeXMLResponse(response), MimeXML) |
|||
} |
|||
|
|||
func WriteEmptyResponse(w http.ResponseWriter, statusCode int) { |
|||
WriteResponse(w, statusCode, []byte{}, mimeNone) |
|||
} |
|||
|
|||
func WriteErrorResponse(w http.ResponseWriter, errorCode ErrorCode, r *http.Request) { |
|||
apiError := GetAPIError(errorCode) |
|||
errorResponse := getRESTErrorResponse(apiError, r.URL.Path) |
|||
encodedErrorResponse := EncodeXMLResponse(errorResponse) |
|||
WriteResponse(w, apiError.HTTPStatusCode, encodedErrorResponse, MimeXML) |
|||
} |
|||
|
|||
func getRESTErrorResponse(err APIError, resource string) RESTErrorResponse { |
|||
return RESTErrorResponse{ |
|||
Code: err.Code, |
|||
Message: err.Description, |
|||
Resource: resource, |
|||
RequestID: fmt.Sprintf("%d", time.Now().UnixNano()), |
|||
} |
|||
} |
|||
|
|||
// Encodes the response headers into XML format.
|
|||
func EncodeXMLResponse(response interface{}) []byte { |
|||
var bytesBuffer bytes.Buffer |
|||
bytesBuffer.WriteString(xml.Header) |
|||
e := xml.NewEncoder(&bytesBuffer) |
|||
e.Encode(response) |
|||
return bytesBuffer.Bytes() |
|||
} |
|||
|
|||
func setCommonHeaders(w http.ResponseWriter) { |
|||
w.Header().Set("x-amz-request-id", fmt.Sprintf("%d", time.Now().UnixNano())) |
|||
w.Header().Set("Accept-Ranges", "bytes") |
|||
} |
|||
|
|||
func WriteResponse(w http.ResponseWriter, statusCode int, response []byte, mType mimeType) { |
|||
setCommonHeaders(w) |
|||
if response != nil { |
|||
w.Header().Set("Content-Length", strconv.Itoa(len(response))) |
|||
} |
|||
if mType != mimeNone { |
|||
w.Header().Set("Content-Type", string(mType)) |
|||
} |
|||
w.WriteHeader(statusCode) |
|||
if response != nil { |
|||
glog.V(4).Infof("status %d %s: %s", statusCode, mType, string(response)) |
|||
_, err := w.Write(response) |
|||
if err != nil { |
|||
glog.V(0).Infof("write err: %v", err) |
|||
} |
|||
w.(http.Flusher).Flush() |
|||
} |
|||
} |
|||
|
|||
// If none of the http routes match respond with MethodNotAllowed
|
|||
func NotFoundHandler(w http.ResponseWriter, r *http.Request) { |
|||
glog.V(0).Infof("unsupported %s %s", r.Method, r.RequestURI) |
|||
WriteErrorResponse(w, ErrMethodNotAllowed, r) |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue