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

nethttp: implement http.Hijacker in response wrapper #25

Closed
Closed
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
19 changes: 17 additions & 2 deletions nethttp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
package nethttp

import (
"bufio"
"errors"
"net"
"net/http"

opentracing "github.com/opentracing/opentracing-go"
Expand All @@ -11,7 +14,18 @@ import (

type statusCodeTracker struct {
http.ResponseWriter
status int
hijacker http.Hijacker
status int
}

// Hijack implements the http.Hijacker interface. This expands
// the Response to fulfill http.Hijacker if the underlying
// http.ResponseWriter supports it.
func (w *statusCodeTracker) Hijack() (net.Conn, *bufio.ReadWriter, error) {
if w.hijacker == nil {
return nil, nil, errors.New("http.Hijacker not implemented by underlying http.ResponseWriter")
}
return w.hijacker.Hijack()
}

func (w *statusCodeTracker) WriteHeader(status int) {
Expand Down Expand Up @@ -107,7 +121,8 @@ func MiddlewareFunc(tr opentracing.Tracer, h http.HandlerFunc, options ...MWOpti
}
ext.Component.Set(sp, componentName)

w = &statusCodeTracker{w, 200}
hijacker, _ := w.(http.Hijacker)
w = &statusCodeTracker{ResponseWriter: w, hijacker: hijacker, status: 200}
r = r.WithContext(opentracing.ContextWithSpan(r.Context(), sp))

h(w, r)
Expand Down