bump(github.com/emicklei/go-restful): 692a50017a7049b26cf7ea4ccfc0d8c77369a793

This commit is contained in:
deads2k
2014-12-04 08:48:26 -05:00
parent c31b3f04de
commit ff24a45d8f
11 changed files with 81 additions and 43 deletions

View File

@@ -27,11 +27,12 @@ type Response struct {
routeProduces []string // mime-types what the Route says it can produce
statusCode int // HTTP status code that has been written explicity (if zero then net/http has written 200)
contentLength int // number of bytes written for the response body
prettyPrint bool // controls the indentation feature of XML and JSON serialization. It is initialized using var PrettyPrintResponses.
}
// Creates a new response based on a http ResponseWriter.
func NewResponse(httpWriter http.ResponseWriter) *Response {
return &Response{httpWriter, "", []string{}, http.StatusOK, 0} // empty content-types
return &Response{httpWriter, "", []string{}, http.StatusOK, 0, PrettyPrintResponses} // empty content-types
}
// If Accept header matching fails, fall back to this type, otherwise
@@ -50,6 +51,11 @@ func (r Response) InternalServerError() Response {
return r
}
// PrettyPrint changes whether this response must produce pretty (line-by-line, indented) JSON or XML output.
func (r *Response) PrettyPrint(bePretty bool) {
r.prettyPrint = bePretty
}
// AddHeader is a shortcut for .Header().Add(header,value)
func (r Response) AddHeader(header string, value string) Response {
r.Header().Add(header, value)
@@ -119,7 +125,7 @@ func (r *Response) WriteAsXml(value interface{}) error {
if value == nil { // do not write a nil representation
return nil
}
if PrettyPrintResponses {
if r.prettyPrint {
output, err = xml.MarshalIndent(value, " ", " ")
} else {
output, err = xml.Marshal(value)
@@ -150,7 +156,7 @@ func (r *Response) WriteAsJson(value interface{}) error {
if value == nil { // do not write a nil representation
return nil
}
if PrettyPrintResponses {
if r.prettyPrint {
output, err = json.MarshalIndent(value, " ", " ")
} else {
output, err = json.Marshal(value)
@@ -192,13 +198,15 @@ func (r *Response) WriteErrorString(status int, errorReason string) error {
// WriteHeader is overridden to remember the Status Code that has been written.
// Note that using this method, the status value is only written when
// - calling WriteEntity
// - or directly WriteAsXml,WriteAsJson.
// - or if the status is 204 (http.StatusNoContent)
// - calling WriteEntity,
// - or directly calling WriteAsXml or WriteAsJson,
// - or if the status is one for which no response is allowed (i.e.,
// 204 (http.StatusNoContent) or 304 (http.StatusNotModified))
func (r *Response) WriteHeader(httpStatus int) {
r.statusCode = httpStatus
// if 204 then WriteEntity will not be called so we need to pass this code
if http.StatusNoContent == httpStatus {
if http.StatusNoContent == httpStatus ||
http.StatusNotModified == httpStatus {
r.ResponseWriter.WriteHeader(httpStatus)
}
}