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

net/http: optimize Cookie.sanitizeOrWarn and timeoutHandler.ServeHTTP #71121

Open
wants to merge 1 commit 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
21 changes: 9 additions & 12 deletions src/net/http/cookie.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,25 +483,22 @@ func validCookiePathByte(b byte) bool {
}

func sanitizeOrWarn(fieldName string, valid func(byte) bool, v string) string {
ok := true
for i := 0; i < len(v); i++ {
if valid(v[i]) {
continue
}
log.Printf("net/http: invalid byte %q in %s; dropping invalid bytes", v[i], fieldName)
ok = false
break
}
if ok {
return v
}
buf := make([]byte, 0, len(v))
for i := 0; i < len(v); i++ {
if b := v[i]; valid(b) {
buf = append(buf, b)
buf := make([]byte, 0, len(v))
buf = append(buf, v[:i]...)
i++
for ; i < len(v); i++ {
if b := v[i]; valid(b) {
buf = append(buf, b)
}
}
return string(buf)
}
return string(buf)
return v
}

// parseCookieValue parses a cookie value according to RFC 6265.
Expand Down
8 changes: 4 additions & 4 deletions src/net/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3805,7 +3805,6 @@ func (h *timeoutHandler) ServeHTTP(w ResponseWriter, r *Request) {
defer cancelCtx()
}
r = r.WithContext(ctx)
done := make(chan struct{})
tw := &timeoutWriter{
w: w,
h: make(Header),
Expand All @@ -3819,12 +3818,13 @@ func (h *timeoutHandler) ServeHTTP(w ResponseWriter, r *Request) {
}
}()
h.handler.ServeHTTP(tw, r)
close(done)
close(panicChan)
}()
select {
case p := <-panicChan:
panic(p)
case <-done:
if p != nil {
panic(p)
}
tw.mu.Lock()
defer tw.mu.Unlock()
dst := w.Header()
Expand Down