aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/emicklei/go-restful/response.go
diff options
context:
space:
mode:
authorGravatar Yong Tang <yong.tang.github@outlook.com> 2017-11-03 00:20:15 -0700
committerGravatar Miek Gieben <miek@miek.nl> 2017-11-03 07:20:15 +0000
commit1fc0c16968304b169fb7eb52f162ffa4c7cdc55c (patch)
treee242db88ffc7aa291a6344d1ab1d6cd07293cba7 /vendor/github.com/emicklei/go-restful/response.go
parentaf6086d6535e3309e3bd1e521cb4d51ef113f850 (diff)
downloadcoredns-1fc0c16968304b169fb7eb52f162ffa4c7cdc55c.tar.gz
coredns-1fc0c16968304b169fb7eb52f162ffa4c7cdc55c.tar.zst
coredns-1fc0c16968304b169fb7eb52f162ffa4c7cdc55c.zip
Update vendor libraries except client-go, apimachinery and ugorji/go (#1197)
This fix updates vendor libraries except client-go, apimachinery and ugorji/go, as github.com/ugorji/go/codec is causing compatibilities issues. Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
Diffstat (limited to 'vendor/github.com/emicklei/go-restful/response.go')
-rw-r--r--vendor/github.com/emicklei/go-restful/response.go92
1 files changed, 58 insertions, 34 deletions
diff --git a/vendor/github.com/emicklei/go-restful/response.go b/vendor/github.com/emicklei/go-restful/response.go
index 3798f18c8..4d987d130 100644
--- a/vendor/github.com/emicklei/go-restful/response.go
+++ b/vendor/github.com/emicklei/go-restful/response.go
@@ -5,12 +5,13 @@ package restful
// that can be found in the LICENSE file.
import (
+ "bufio"
"errors"
+ "net"
"net/http"
- "strings"
)
-// DEPRECATED, use DefaultResponseContentType(mime)
+// DefaultResponseMimeType is DEPRECATED, use DefaultResponseContentType(mime)
var DefaultResponseMimeType string
//PrettyPrintResponses controls the indentation feature of XML and JSON serialization
@@ -20,19 +21,22 @@ var PrettyPrintResponses = true
// It provides several convenience methods to prepare and write response content.
type Response struct {
http.ResponseWriter
- requestAccept string // mime-type what the Http Request says it wants to receive
- 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.
- err error // err property is kept when WriteError is called
+ requestAccept string // mime-type what the Http Request says it wants to receive
+ routeProduces []string // mime-types what the Route says it can produce
+ statusCode int // HTTP status code that has been written explicitly (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.
+ err error // err property is kept when WriteError is called
+ hijacker http.Hijacker // if underlying ResponseWriter supports it
}
-// Creates a new response based on a http ResponseWriter.
+// NewResponse creates a new response based on a http ResponseWriter.
func NewResponse(httpWriter http.ResponseWriter) *Response {
- return &Response{httpWriter, "", []string{}, http.StatusOK, 0, PrettyPrintResponses, nil} // empty content-types
+ hijacker, _ := httpWriter.(http.Hijacker)
+ return &Response{ResponseWriter: httpWriter, routeProduces: []string{}, statusCode: http.StatusOK, prettyPrint: PrettyPrintResponses, hijacker: hijacker}
}
+// DefaultResponseContentType set a default.
// If Accept header matching fails, fall back to this type.
// Valid values are restful.MIME_JSON and restful.MIME_XML
// Example:
@@ -48,6 +52,16 @@ func (r Response) InternalServerError() Response {
return r
}
+// Hijack implements the http.Hijacker interface. This expands
+// the Response to fulfill http.Hijacker if the underlying
+// http.ResponseWriter supports it.
+func (r *Response) Hijack() (net.Conn, *bufio.ReadWriter, error) {
+ if r.hijacker == nil {
+ return nil, nil, errors.New("http.Hijacker not implemented by underlying http.ResponseWriter")
+ }
+ return r.hijacker.Hijack()
+}
+
// 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
@@ -68,38 +82,39 @@ func (r *Response) SetRequestAccepts(mime string) {
// can write according to what the request wants (Accept) and what the Route can produce or what the restful defaults say.
// If called before WriteEntity and WriteHeader then a false return value can be used to write a 406: Not Acceptable.
func (r *Response) EntityWriter() (EntityReaderWriter, bool) {
- for _, qualifiedMime := range strings.Split(r.requestAccept, ",") {
- mime := strings.Trim(strings.Split(qualifiedMime, ";")[0], " ")
- if 0 == len(mime) || mime == "*/*" {
- for _, each := range r.routeProduces {
- if MIME_JSON == each {
- return entityAccessRegistry.AccessorAt(MIME_JSON)
- }
- if MIME_XML == each {
- return entityAccessRegistry.AccessorAt(MIME_XML)
+ sorted := sortedMimes(r.requestAccept)
+ for _, eachAccept := range sorted {
+ for _, eachProduce := range r.routeProduces {
+ if eachProduce == eachAccept.media {
+ if w, ok := entityAccessRegistry.accessorAt(eachAccept.media); ok {
+ return w, true
}
}
- } else { // mime is not blank; see if we have a match in Produces
+ }
+ if eachAccept.media == "*/*" {
for _, each := range r.routeProduces {
- if mime == each {
- if MIME_JSON == each {
- return entityAccessRegistry.AccessorAt(MIME_JSON)
- }
- if MIME_XML == each {
- return entityAccessRegistry.AccessorAt(MIME_XML)
- }
+ if w, ok := entityAccessRegistry.accessorAt(each); ok {
+ return w, true
}
}
}
}
- writer, ok := entityAccessRegistry.AccessorAt(r.requestAccept)
+ // if requestAccept is empty
+ writer, ok := entityAccessRegistry.accessorAt(r.requestAccept)
if !ok {
// if not registered then fallback to the defaults (if set)
if DefaultResponseMimeType == MIME_JSON {
- return entityAccessRegistry.AccessorAt(MIME_JSON)
+ return entityAccessRegistry.accessorAt(MIME_JSON)
}
if DefaultResponseMimeType == MIME_XML {
- return entityAccessRegistry.AccessorAt(MIME_XML)
+ return entityAccessRegistry.accessorAt(MIME_XML)
+ }
+ // Fallback to whatever the route says it can produce.
+ // https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
+ for _, each := range r.routeProduces {
+ if w, ok := entityAccessRegistry.accessorAt(each); ok {
+ return w, true
+ }
}
if trace {
traceLogger.Printf("no registered EntityReaderWriter found for %s", r.requestAccept)
@@ -130,25 +145,25 @@ func (r *Response) WriteHeaderAndEntity(status int, value interface{}) error {
}
// WriteAsXml is a convenience method for writing a value in xml (requires Xml tags on the value)
-// It uses the standard encoding/xml package for marshalling the valuel ; not using a registered EntityReaderWriter.
+// It uses the standard encoding/xml package for marshalling the value ; not using a registered EntityReaderWriter.
func (r *Response) WriteAsXml(value interface{}) error {
return writeXML(r, http.StatusOK, MIME_XML, value)
}
// WriteHeaderAndXml is a convenience method for writing a status and value in xml (requires Xml tags on the value)
-// It uses the standard encoding/xml package for marshalling the valuel ; not using a registered EntityReaderWriter.
+// It uses the standard encoding/xml package for marshalling the value ; not using a registered EntityReaderWriter.
func (r *Response) WriteHeaderAndXml(status int, value interface{}) error {
return writeXML(r, status, MIME_XML, value)
}
// WriteAsJson is a convenience method for writing a value in json.
-// It uses the standard encoding/json package for marshalling the valuel ; not using a registered EntityReaderWriter.
+// It uses the standard encoding/json package for marshalling the value ; not using a registered EntityReaderWriter.
func (r *Response) WriteAsJson(value interface{}) error {
return writeJSON(r, http.StatusOK, MIME_JSON, value)
}
// WriteJson is a convenience method for writing a value in Json with a given Content-Type.
-// It uses the standard encoding/json package for marshalling the valuel ; not using a registered EntityReaderWriter.
+// It uses the standard encoding/json package for marshalling the value ; not using a registered EntityReaderWriter.
func (r *Response) WriteJson(value interface{}, contentType string) error {
return writeJSON(r, http.StatusOK, contentType, value)
}
@@ -184,6 +199,15 @@ func (r *Response) WriteErrorString(httpStatus int, errorReason string) error {
return nil
}
+// Flush implements http.Flusher interface, which sends any buffered data to the client.
+func (r *Response) Flush() {
+ if f, ok := r.ResponseWriter.(http.Flusher); ok {
+ f.Flush()
+ } else if trace {
+ traceLogger.Printf("ResponseWriter %v doesn't support Flush", r)
+ }
+}
+
// WriteHeader is overridden to remember the Status Code that has been written.
// Changes to the Header of the response have no effect after this.
func (r *Response) WriteHeader(httpStatus int) {