pgweb/vendor/github.com/gin-gonic/gin/response_writer.go

116 lines
2.4 KiB
Go
Raw Normal View History

2014-10-28 16:07:33 -07:00
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package gin
import (
"bufio"
2017-01-20 13:21:18 -06:00
"io"
2014-10-28 16:07:33 -07:00
"net"
"net/http"
)
2015-02-09 01:31:16 -06:00
const (
2017-01-20 13:21:18 -06:00
noWritten = -1
defaultStatus = http.StatusOK
2015-02-09 01:31:16 -06:00
)
type responseWriterBase interface {
http.ResponseWriter
http.Hijacker
http.Flusher
http.CloseNotifier
2014-10-28 16:07:33 -07:00
// Returns the HTTP response status code of the current request.
Status() int
2017-01-20 13:21:18 -06:00
// Returns the number of bytes already written into the response http body.
// See Written()
Size() int
2017-01-20 13:21:18 -06:00
// Writes the string into the response body.
WriteString(string) (int, error)
2017-01-20 13:21:18 -06:00
// Returns true if the response body was already written.
Written() bool
2017-01-20 13:21:18 -06:00
// Forces to write the http header (status code + headers).
WriteHeaderNow()
}
2014-10-28 16:07:33 -07:00
type responseWriter struct {
http.ResponseWriter
size int
status int
}
2014-10-28 16:07:33 -07:00
2017-01-20 13:21:18 -06:00
var _ ResponseWriter = &responseWriter{}
2014-10-28 16:07:33 -07:00
func (w *responseWriter) reset(writer http.ResponseWriter) {
w.ResponseWriter = writer
2017-01-20 13:21:18 -06:00
w.size = noWritten
w.status = defaultStatus
2014-10-28 16:07:33 -07:00
}
func (w *responseWriter) WriteHeader(code int) {
2017-01-20 13:21:18 -06:00
if code > 0 && w.status != code {
2015-02-09 01:31:16 -06:00
if w.Written() {
2017-01-20 13:21:18 -06:00
debugPrint("[WARNING] Headers were already written. Wanted to override status code %d with %d", w.status, code)
2014-10-28 16:07:33 -07:00
}
2017-01-20 13:21:18 -06:00
w.status = code
2014-10-28 16:07:33 -07:00
}
}
func (w *responseWriter) WriteHeaderNow() {
2015-02-09 01:31:16 -06:00
if !w.Written() {
w.size = 0
2014-10-28 16:07:33 -07:00
w.ResponseWriter.WriteHeader(w.status)
}
}
func (w *responseWriter) Write(data []byte) (n int, err error) {
w.WriteHeaderNow()
2015-02-09 01:31:16 -06:00
n, err = w.ResponseWriter.Write(data)
w.size += n
return
2014-10-28 16:07:33 -07:00
}
2017-01-20 13:21:18 -06:00
func (w *responseWriter) WriteString(s string) (n int, err error) {
w.WriteHeaderNow()
n, err = io.WriteString(w.ResponseWriter, s)
w.size += n
return
}
2014-10-28 16:07:33 -07:00
func (w *responseWriter) Status() int {
return w.status
}
2015-02-09 01:31:16 -06:00
func (w *responseWriter) Size() int {
return w.size
}
2014-10-28 16:07:33 -07:00
func (w *responseWriter) Written() bool {
2017-01-20 13:21:18 -06:00
return w.size != noWritten
2014-10-28 16:07:33 -07:00
}
// Hijack implements the http.Hijacker interface.
2014-10-28 16:07:33 -07:00
func (w *responseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
2017-01-20 13:21:18 -06:00
if w.size < 0 {
w.size = 0
2014-10-28 16:07:33 -07:00
}
2017-01-20 13:21:18 -06:00
return w.ResponseWriter.(http.Hijacker).Hijack()
2014-10-28 16:07:33 -07:00
}
// CloseNotify implements the http.CloseNotify interface.
2014-10-28 16:07:33 -07:00
func (w *responseWriter) CloseNotify() <-chan bool {
return w.ResponseWriter.(http.CloseNotifier).CloseNotify()
}
// Flush implements the http.Flush interface.
2014-10-28 16:07:33 -07:00
func (w *responseWriter) Flush() {
w.WriteHeaderNow()
2017-01-20 13:21:18 -06:00
w.ResponseWriter.(http.Flusher).Flush()
2014-10-28 16:07:33 -07:00
}