traefik/vendor/github.com/vulcand/oxy/utils/netutils.go

192 lines
4.9 KiB
Go
Raw Normal View History

2017-02-07 21:33:23 +00:00
package utils
import (
"bufio"
2017-11-22 17:20:03 +00:00
"fmt"
2017-02-07 21:33:23 +00:00
"io"
"net"
"net/http"
"net/url"
2017-11-22 17:20:03 +00:00
"reflect"
2018-01-22 11:16:03 +00:00
log "github.com/sirupsen/logrus"
2017-02-07 21:33:23 +00:00
)
// ProxyWriter calls recorder, used to debug logs
type ProxyWriter struct {
w http.ResponseWriter
code int
length int64
log *log.Logger
}
// NewProxyWriter creates a new ProxyWriter
func NewProxyWriter(w http.ResponseWriter) *ProxyWriter {
return NewProxyWriterWithLogger(w, log.StandardLogger())
2017-02-07 21:33:23 +00:00
}
// NewProxyWriterWithLogger creates a new ProxyWriter
func NewProxyWriterWithLogger(w http.ResponseWriter, l *log.Logger) *ProxyWriter {
return &ProxyWriter{
w: w,
log: l,
}
}
// StatusCode gets status code
func (p *ProxyWriter) StatusCode() int {
if p.code == 0 {
2017-02-07 21:33:23 +00:00
// per contract standard lib will set this to http.StatusOK if not set
// by user, here we avoid the confusion by mirroring this logic
return http.StatusOK
}
return p.code
}
// GetLength gets content length
func (p *ProxyWriter) GetLength() int64 {
return p.length
2017-02-07 21:33:23 +00:00
}
// Header gets response header
func (p *ProxyWriter) Header() http.Header {
return p.w.Header()
2017-02-07 21:33:23 +00:00
}
func (p *ProxyWriter) Write(buf []byte) (int, error) {
p.length = p.length + int64(len(buf))
return p.w.Write(buf)
2017-02-07 21:33:23 +00:00
}
// WriteHeader writes status code
func (p *ProxyWriter) WriteHeader(code int) {
p.code = code
p.w.WriteHeader(code)
2017-02-07 21:33:23 +00:00
}
// Flush flush the writer
func (p *ProxyWriter) Flush() {
if f, ok := p.w.(http.Flusher); ok {
2017-02-07 21:33:23 +00:00
f.Flush()
}
}
// CloseNotify returns a channel that receives at most a single value (true)
// when the client connection has gone away.
func (p *ProxyWriter) CloseNotify() <-chan bool {
if cn, ok := p.w.(http.CloseNotifier); ok {
2017-11-22 17:20:03 +00:00
return cn.CloseNotify()
}
p.log.Debugf("Upstream ResponseWriter of type %v does not implement http.CloseNotifier. Returning dummy channel.", reflect.TypeOf(p.w))
2017-11-22 17:20:03 +00:00
return make(<-chan bool)
}
// Hijack lets the caller take over the connection.
func (p *ProxyWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
if hi, ok := p.w.(http.Hijacker); ok {
2017-11-22 17:20:03 +00:00
return hi.Hijack()
}
p.log.Debugf("Upstream ResponseWriter of type %v does not implement http.Hijacker. Returning dummy channel.", reflect.TypeOf(p.w))
return nil, nil, fmt.Errorf("the response writer that was wrapped in this proxy, does not implement http.Hijacker. It is of type: %v", reflect.TypeOf(p.w))
2017-02-07 21:33:23 +00:00
}
// NewBufferWriter creates a new BufferWriter
2017-02-07 21:33:23 +00:00
func NewBufferWriter(w io.WriteCloser) *BufferWriter {
return &BufferWriter{
W: w,
H: make(http.Header),
}
}
// BufferWriter buffer writer
2017-02-07 21:33:23 +00:00
type BufferWriter struct {
H http.Header
Code int
W io.WriteCloser
}
// Close close the writer
2017-02-07 21:33:23 +00:00
func (b *BufferWriter) Close() error {
return b.W.Close()
}
// Header gets response header
2017-02-07 21:33:23 +00:00
func (b *BufferWriter) Header() http.Header {
return b.H
}
func (b *BufferWriter) Write(buf []byte) (int, error) {
return b.W.Write(buf)
}
// WriteHeader writes status code
2017-02-07 21:33:23 +00:00
func (b *BufferWriter) WriteHeader(code int) {
b.Code = code
}
// CloseNotify returns a channel that receives at most a single value (true)
// when the client connection has gone away.
2017-11-22 17:20:03 +00:00
func (b *BufferWriter) CloseNotify() <-chan bool {
if cn, ok := b.W.(http.CloseNotifier); ok {
return cn.CloseNotify()
}
log.Warningf("Upstream ResponseWriter of type %v does not implement http.CloseNotifier. Returning dummy channel.", reflect.TypeOf(b.W))
return make(<-chan bool)
}
// Hijack lets the caller take over the connection.
2017-02-07 21:33:23 +00:00
func (b *BufferWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
2017-11-22 17:20:03 +00:00
if hi, ok := b.W.(http.Hijacker); ok {
return hi.Hijack()
}
log.Debugf("Upstream ResponseWriter of type %v does not implement http.Hijacker. Returning dummy channel.", reflect.TypeOf(b.W))
return nil, nil, fmt.Errorf("the response writer that was wrapped in this proxy, does not implement http.Hijacker. It is of type: %v", reflect.TypeOf(b.W))
2017-02-07 21:33:23 +00:00
}
type nopWriteCloser struct {
io.Writer
}
func (*nopWriteCloser) Close() error { return nil }
// NopWriteCloser returns a WriteCloser with a no-op Close method wrapping
2017-02-07 21:33:23 +00:00
// the provided Writer w.
func NopWriteCloser(w io.Writer) io.WriteCloser {
return &nopWriteCloser{Writer: w}
2017-02-07 21:33:23 +00:00
}
// CopyURL provides update safe copy by avoiding shallow copying User field
func CopyURL(i *url.URL) *url.URL {
out := *i
if i.User != nil {
out.User = &(*i.User)
}
return &out
}
// CopyHeaders copies http headers from source to destination, it
// does not overide, but adds multiple headers
2017-11-22 17:20:03 +00:00
func CopyHeaders(dst http.Header, src http.Header) {
2017-02-07 21:33:23 +00:00
for k, vv := range src {
2017-11-22 17:20:03 +00:00
dst[k] = append(dst[k], vv...)
2017-02-07 21:33:23 +00:00
}
}
// HasHeaders determines whether any of the header names is present in the http headers
func HasHeaders(names []string, headers http.Header) bool {
for _, h := range names {
if headers.Get(h) != "" {
return true
}
}
return false
}
// RemoveHeaders removes the header with the given names from the headers map
func RemoveHeaders(headers http.Header, names ...string) {
for _, h := range names {
headers.Del(h)
}
}