Skip to content

Commit

Permalink
pprofhandler: use bytes.HasPrefix for consistency (#1543)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandear committed Apr 26, 2023
1 parent 1aac293 commit 00c291b
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions pprofhandler/pprof.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package pprofhandler

import (
"bytes"
"net/http/pprof"
rtp "runtime/pprof"
"strings"

"github.com/valyala/fasthttp"
"github.com/valyala/fasthttp/fasthttpadaptor"
Expand All @@ -22,18 +22,19 @@ var (
// See https://pkg.go.dev/net/http/pprof for details.
func PprofHandler(ctx *fasthttp.RequestCtx) {
ctx.Response.Header.Set("Content-Type", "text/html")
if strings.HasPrefix(string(ctx.Path()), "/debug/pprof/cmdline") {
switch {
case bytes.HasPrefix(ctx.Path(), []byte("/debug/pprof/cmdline")):
cmdline(ctx)
} else if strings.HasPrefix(string(ctx.Path()), "/debug/pprof/profile") {
case bytes.HasPrefix(ctx.Path(), []byte("/debug/pprof/profile")):
profile(ctx)
} else if strings.HasPrefix(string(ctx.Path()), "/debug/pprof/symbol") {
case bytes.HasPrefix(ctx.Path(), []byte("/debug/pprof/symbol")):
symbol(ctx)
} else if strings.HasPrefix(string(ctx.Path()), "/debug/pprof/trace") {
case bytes.HasPrefix(ctx.Path(), []byte("/debug/pprof/trace")):
trace(ctx)
} else {
default:
for _, v := range rtp.Profiles() {
ppName := v.Name()
if strings.HasPrefix(string(ctx.Path()), "/debug/pprof/"+ppName) {
if bytes.HasPrefix(ctx.Path(), []byte("/debug/pprof/"+ppName)) {
namedHandler := fasthttpadaptor.NewFastHTTPHandlerFunc(pprof.Handler(ppName).ServeHTTP)
namedHandler(ctx)
return
Expand Down

0 comments on commit 00c291b

Please sign in to comment.