Closed
Description
What did you do?
package main
import (
"context"
"fmt"
"net/http"
)
func main() {
http.Handle("/foo/", func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
h.ServeHTTP(w, r)
// log user id
fmt.Printf("UserID -> %#v\n", r.Context().Value("UserID"))
})
}(
http.StripPrefix("/foo/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// set user id
*r = *r.WithContext(context.WithValue(r.Context(), "UserID", "123"))
})),
//http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// // set user id
// *r = *r.WithContext(context.WithValue(r.Context(), "UserID", "123"))
//}),
))
go func() {
if err := http.ListenAndServe(":8888", nil); err != nil {
panic(err)
}
}()
resp, err := http.Get("http://localhost:8888/foo/")
if err != nil {
panic(err)
}
defer resp.Body.Close()
}
What did you expect to see?
UserID -> "123"
The commented out handler without StripPrefix outputs as above.
What did you see instead?
UserID -> <nil>
Does this issue reproduce with the latest release (go1.9)?
yes
System details
go version devel +d39649087b Thu Aug 31 03:30:43 2017 +0000 linux/amd64
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/reus/go"
GORACE=""
GOROOT="/home/reus/gotip"
GOTOOLDIR="/home/reus/gotip/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build879880846=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOROOT/bin/go version: go version devel +d39649087b Thu Aug 31 03:30:43 2017 +0000 linux/amd64
GOROOT/bin/go tool compile -V: compile version devel +d39649087b Thu Aug 31 03:30:43 2017 +0000
uname -sr: Linux 4.12.8-2-ARCH
LSB Version: 1.4
Distributor ID: Arch
Description: Arch Linux
Release: rolling
Codename: n/a
/usr/lib/libc.so.6: GNU C Library (GNU libc) stable release version 2.25, by Roland McGrath et al.