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

server/requestlog: responseStats should implement http.Hijacker #2737

Merged
merged 1 commit into from
Feb 5, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 19 additions & 5 deletions server/requestlog/requestlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package requestlog // import "gocloud.dev/server/requestlog"

import (
"bufio"
"errors"
"io"
"io/ioutil"
Expand Down Expand Up @@ -80,7 +81,7 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.h.ServeHTTP(w2, r2)

ent.Latency = time.Since(start)
if rcc.err == nil && rcc.r != nil {
if rcc.err == nil && rcc.r != nil && !w2.hijacked {
// If the handler hasn't encountered an error in the Body (like EOF),
// then consume the rest of the Body to provide an accurate rcc.n.
io.Copy(ioutil.Discard, rcc)
Expand Down Expand Up @@ -161,10 +162,11 @@ func headerSize(h http.Header) int64 {
}

type responseStats struct {
w http.ResponseWriter
hsize int64
wc writeCounter
code int
w http.ResponseWriter
hsize int64
wc writeCounter
code int
hijacked bool
}

func (r *responseStats) Header() http.Header {
Expand Down Expand Up @@ -198,3 +200,15 @@ func (r *responseStats) size() (hdr, body int64) {
// which we don't want to count.
return r.hsize, int64(r.wc)
}

func (r *responseStats) Hijack() (_ net.Conn, _ *bufio.ReadWriter, err error) {
defer func() {
if err == nil {
r.hijacked = true
}
}()
if hj, ok := r.w.(http.Hijacker); ok {
return hj.Hijack()
}
return nil, nil, errors.New("underlying ResponseWriter does not support hijacking")
}