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

117 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 = 200
2015-02-09 01:31:16 -06:00
)
2014-10-28 16:07:33 -07:00
type (
ResponseWriter interface {
http.ResponseWriter
http.Hijacker
http.Flusher
http.CloseNotifier
2017-01-20 13:21:18 -06:00
// Returns the HTTP response status code of the current request.
2014-10-28 16:07:33 -07:00
Status() int
2017-01-20 13:21:18 -06:00
// Returns the number of bytes already written into the response http body.
// See Written()
2015-02-09 01:31:16 -06:00
Size() int
2017-01-20 13:21:18 -06:00
// Writes the string into the response body.
WriteString(string) (int, error)
// Returns true if the response body was already written.
2014-10-28 16:07:33 -07:00
Written() bool
2017-01-20 13:21:18 -06:00
// Forces to write the http header (status code + headers).
2014-10-28 16:07:33 -07:00
WriteHeaderNow()
}
responseWriter struct {
http.ResponseWriter
2015-02-09 01:31:16 -06:00
size int
2017-01-20 13:21:18 -06:00
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
}
// Implements the http.Hijacker interface
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
}
// Implements the http.CloseNotify interface
func (w *responseWriter) CloseNotify() <-chan bool {
return w.ResponseWriter.(http.CloseNotifier).CloseNotify()
}
// Implements the http.Flush interface
func (w *responseWriter) Flush() {
2017-01-20 13:21:18 -06:00
w.ResponseWriter.(http.Flusher).Flush()
2014-10-28 16:07:33 -07:00
}