Skip to content

Commit

Permalink
Remove CloseNotifier for go 1.8+
Browse files Browse the repository at this point in the history
http.CloseNotifier is deprecated as of Go 1.11, with a note that "the
CloseNotifier interface predates Go's context package. New code should
use Request.Context instead."

Signed-off-by: Maxim Eremenko <moeryomenko@gmail.com>
  • Loading branch information
moeryomenko authored and zenazn committed Jun 2, 2019
1 parent 84fcf9f commit a16712d
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 6 deletions.
24 changes: 18 additions & 6 deletions web/mutil/writer_proxy.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build !go1.8

package mutil

import (
Expand Down Expand Up @@ -62,6 +64,7 @@ func (b *basicWriter) WriteHeader(code int) {
b.ResponseWriter.WriteHeader(code)
}
}

func (b *basicWriter) Write(buf []byte) (int, error) {
b.WriteHeader(http.StatusOK)
n, err := b.ResponseWriter.Write(buf)
Expand All @@ -75,20 +78,25 @@ func (b *basicWriter) Write(buf []byte) (int, error) {
b.bytes += n
return n, err
}

func (b *basicWriter) maybeWriteHeader() {
if !b.wroteHeader {
b.WriteHeader(http.StatusOK)
}
}

func (b *basicWriter) Status() int {
return b.code
}

func (b *basicWriter) BytesWritten() int {
return b.bytes
}

func (b *basicWriter) Tee(w io.Writer) {
b.tee = w
}

func (b *basicWriter) Unwrap() http.ResponseWriter {
return b.ResponseWriter
}
Expand All @@ -105,14 +113,17 @@ func (f *fancyWriter) CloseNotify() <-chan bool {
cn := f.basicWriter.ResponseWriter.(http.CloseNotifier)
return cn.CloseNotify()
}

func (f *fancyWriter) Flush() {
fl := f.basicWriter.ResponseWriter.(http.Flusher)
fl.Flush()
}

func (f *fancyWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
hj := f.basicWriter.ResponseWriter.(http.Hijacker)
return hj.Hijack()
}

func (f *fancyWriter) ReadFrom(r io.Reader) (int64, error) {
if f.basicWriter.tee != nil {
return io.Copy(&f.basicWriter, r)
Expand All @@ -122,11 +133,6 @@ func (f *fancyWriter) ReadFrom(r io.Reader) (int64, error) {
return rf.ReadFrom(r)
}

var _ http.CloseNotifier = &fancyWriter{}
var _ http.Flusher = &fancyWriter{}
var _ http.Hijacker = &fancyWriter{}
var _ io.ReaderFrom = &fancyWriter{}

type flushWriter struct {
basicWriter
}
Expand All @@ -136,4 +142,10 @@ func (f *flushWriter) Flush() {
fl.Flush()
}

var _ http.Flusher = &flushWriter{}
var (
_ http.CloseNotifier = &fancyWriter{}
_ http.Flusher = &fancyWriter{}
_ http.Hijacker = &fancyWriter{}
_ io.ReaderFrom = &fancyWriter{}
_ http.Flusher = &flushWriter{}
)
136 changes: 136 additions & 0 deletions web/mutil/writer_proxy_go1_8.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
// +build go1.8

package mutil

import (
"bufio"
"io"
"net"
"net/http"
)

// WriterProxy is a proxy around an http.ResponseWriter that allows you to hook
// into various parts of the response process.
type WriterProxy interface {
http.ResponseWriter
// Status returns the HTTP status of the request, or 0 if one has not
// yet been sent.
Status() int
// BytesWritten returns the total number of bytes sent to the client.
BytesWritten() int
// Tee causes the response body to be written to the given io.Writer in
// addition to proxying the writes through. Only one io.Writer can be
// tee'd to at once: setting a second one will overwrite the first.
// Writes will be sent to the proxy before being written to this
// io.Writer. It is illegal for the tee'd writer to be modified
// concurrently with writes.
Tee(io.Writer)
// Unwrap returns the original proxied target.
Unwrap() http.ResponseWriter
}

// WrapWriter wraps an http.ResponseWriter, returning a proxy that allows you to
// hook into various parts of the response process.
func WrapWriter(w http.ResponseWriter) WriterProxy {
_, fl := w.(http.Flusher)
_, hj := w.(http.Hijacker)
_, rf := w.(io.ReaderFrom)

bw := basicWriter{ResponseWriter: w}
if fl && hj && rf {
return &fancyWriter{bw}
}
return &bw
}

// basicWriter wraps a http.ResponseWriter that implements the minimal
// http.ResponseWriter interface.
type basicWriter struct {
http.ResponseWriter
wroteHeader bool
code int
bytes int
tee io.Writer
}

func (b *basicWriter) WriteHeader(code int) {
if !b.wroteHeader {
b.code = code
b.wroteHeader = true
b.ResponseWriter.WriteHeader(code)
}
}

func (b *basicWriter) Write(buf []byte) (int, error) {
b.WriteHeader(http.StatusOK)
n, err := b.ResponseWriter.Write(buf)
if b.tee != nil {
_, err2 := b.tee.Write(buf[:n])
// Prefer errors generated by the proxied writer.
if err == nil {
err = err2
}
}
b.bytes += n
return n, err
}

func (b *basicWriter) maybeWriteHeader() {
if !b.wroteHeader {
b.WriteHeader(http.StatusOK)
}
}

func (b *basicWriter) Status() int {
return b.code
}

func (b *basicWriter) BytesWritten() int {
return b.bytes
}

func (b *basicWriter) Tee(w io.Writer) {
b.tee = w
}

func (b *basicWriter) Unwrap() http.ResponseWriter {
return b.ResponseWriter
}

// fancyWriter is a writer that additionally satisfies http.Pusher,
// http.Flusher, http.Hijacker, and io.ReaderFrom. It exists for the common case
// of wrapping the http.ResponseWriter that package http gives you, in order to
// make the proxied object support the full method set of the proxied object.
type fancyWriter struct {
basicWriter
}

func (f *fancyWriter) Push(target string, opts *http.PushOptions) error {
return f.basicWriter.ResponseWriter.(http.Pusher).Push(target, opts)
}

func (f *fancyWriter) Flush() {
fl := f.basicWriter.ResponseWriter.(http.Flusher)
fl.Flush()
}

func (f *fancyWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
hj := f.basicWriter.ResponseWriter.(http.Hijacker)
return hj.Hijack()
}

func (f *fancyWriter) ReadFrom(r io.Reader) (int64, error) {
if f.basicWriter.tee != nil {
return io.Copy(&f.basicWriter, r)
}
rf := f.basicWriter.ResponseWriter.(io.ReaderFrom)
f.basicWriter.maybeWriteHeader()
return rf.ReadFrom(r)
}

var (
_ http.Pusher = &fancyWriter{}
_ http.Flusher = &fancyWriter{}
_ http.Hijacker = &fancyWriter{}
_ io.ReaderFrom = &fancyWriter{}
)

0 comments on commit a16712d

Please sign in to comment.