Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stop using deprecated CloseNotifier #633

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions hlog/hlog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"net/url"
"reflect"
"testing"
"time"

"github.com/rs/xid"
"github.com/rs/zerolog"
Expand Down Expand Up @@ -273,6 +274,33 @@ func TestResponseHeaderHandler(t *testing.T) {
}
}

func TestAccessHandler(t *testing.T) {
out := &bytes.Buffer{}
w := httptest.NewRecorder()
r := &http.Request{}

var respSize int
f := func(r *http.Request, status, size int, duration time.Duration) {
respSize = size
}

h := AccessHandler(f)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte{'a'})
w.WriteHeader(http.StatusOK)
}))

h2 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
h.ServeHTTP(w, r)
l := FromRequest(r)
l.Log().Int("size", respSize).Msg("")
})
h3 := NewHandler(zerolog.New(out))(h2)
h3.ServeHTTP(w, r)
if want, got := `{"size":1}`+"\n", decodeIfBinary(out); want != got {
t.Errorf("Invalid log output, got: %s, want: %s", got, want)
}
}

func TestProtoHandler(t *testing.T) {
out := &bytes.Buffer{}
r := &http.Request{
Expand Down
4 changes: 3 additions & 1 deletion hlog/internal/mutil/writer_proxy.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build !go1.8
// +build !go1.8

package mutil

import (
Expand Down Expand Up @@ -150,5 +153,4 @@ var (
_ http.Flusher = &fancyWriter{}
_ http.Hijacker = &fancyWriter{}
_ io.ReaderFrom = &fancyWriter{}
_ http.Flusher = &flushWriter{}
)
132 changes: 132 additions & 0 deletions hlog/internal/mutil/writer_proxy1_8.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
//go:build go1.8
// +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) 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.Flusher = &fancyWriter{}
_ http.Hijacker = &fancyWriter{}
_ io.ReaderFrom = &fancyWriter{}
)
Loading