mirror of
https://github.com/cloudflare/cloudflared.git
synced 2025-07-29 10:09:58 +00:00
TUN-528: Move cloudflared into a separate repo
This commit is contained in:
61
vendor/github.com/mholt/caddy/caddyhttp/pprof/pprof.go
generated
vendored
Normal file
61
vendor/github.com/mholt/caddy/caddyhttp/pprof/pprof.go
generated
vendored
Normal file
@@ -0,0 +1,61 @@
|
||||
// Copyright 2015 Light Code Labs, LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package pprof
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
pp "net/http/pprof"
|
||||
|
||||
"github.com/mholt/caddy/caddyhttp/httpserver"
|
||||
)
|
||||
|
||||
// BasePath is the base path to match for all pprof requests.
|
||||
const BasePath = "/debug/pprof"
|
||||
|
||||
// Handler is a simple struct whose ServeHTTP will delegate pprof
|
||||
// endpoints to their equivalent net/http/pprof handlers.
|
||||
type Handler struct {
|
||||
Next httpserver.Handler
|
||||
Mux *http.ServeMux
|
||||
}
|
||||
|
||||
// ServeHTTP handles requests to BasePath with pprof, or passes
|
||||
// all other requests up the chain.
|
||||
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
if httpserver.Path(r.URL.Path).Matches(BasePath) {
|
||||
h.Mux.ServeHTTP(w, r)
|
||||
return 0, nil
|
||||
}
|
||||
return h.Next.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
// NewMux returns a new http.ServeMux that routes pprof requests.
|
||||
// It pretty much copies what the std lib pprof does on init:
|
||||
// https://golang.org/src/net/http/pprof/pprof.go#L67
|
||||
func NewMux() *http.ServeMux {
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc(BasePath+"/", func(w http.ResponseWriter, r *http.Request) {
|
||||
// this endpoint, as implemented in the standard library, doesn't set
|
||||
// its Content-Type header, so using this can confuse clients, especially
|
||||
// if gzipping...
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
pp.Index(w, r)
|
||||
})
|
||||
mux.HandleFunc(BasePath+"/cmdline", pp.Cmdline)
|
||||
mux.HandleFunc(BasePath+"/profile", pp.Profile)
|
||||
mux.HandleFunc(BasePath+"/symbol", pp.Symbol)
|
||||
mux.HandleFunc(BasePath+"/trace", pp.Trace)
|
||||
return mux
|
||||
}
|
69
vendor/github.com/mholt/caddy/caddyhttp/pprof/pprof_test.go
generated
vendored
Normal file
69
vendor/github.com/mholt/caddy/caddyhttp/pprof/pprof_test.go
generated
vendored
Normal file
@@ -0,0 +1,69 @@
|
||||
// Copyright 2015 Light Code Labs, LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package pprof
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/mholt/caddy/caddyhttp/httpserver"
|
||||
)
|
||||
|
||||
func TestServeHTTP(t *testing.T) {
|
||||
h := Handler{
|
||||
Next: httpserver.HandlerFunc(nextHandler),
|
||||
Mux: NewMux(),
|
||||
}
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
r, err := http.NewRequest("GET", "/debug/pprof", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
status, err := h.ServeHTTP(w, r)
|
||||
|
||||
if status != 0 {
|
||||
t.Errorf("Expected status %d but got %d", 0, status)
|
||||
}
|
||||
if err != nil {
|
||||
t.Errorf("Expected nil error, but got: %v", err)
|
||||
}
|
||||
if w.Body.String() == "content" {
|
||||
t.Errorf("Expected pprof to handle request, but it didn't")
|
||||
}
|
||||
|
||||
w = httptest.NewRecorder()
|
||||
r, err = http.NewRequest("GET", "/foo", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
status, err = h.ServeHTTP(w, r)
|
||||
if status != http.StatusNotFound {
|
||||
t.Errorf("Test two: Expected status %d but got %d", http.StatusNotFound, status)
|
||||
}
|
||||
if err != nil {
|
||||
t.Errorf("Test two: Expected nil error, but got: %v", err)
|
||||
}
|
||||
if w.Body.String() != "content" {
|
||||
t.Errorf("Expected pprof to pass the request through, but it didn't; got: %s", w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func nextHandler(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
fmt.Fprintf(w, "content")
|
||||
return http.StatusNotFound, nil
|
||||
}
|
51
vendor/github.com/mholt/caddy/caddyhttp/pprof/setup.go
generated
vendored
Normal file
51
vendor/github.com/mholt/caddy/caddyhttp/pprof/setup.go
generated
vendored
Normal file
@@ -0,0 +1,51 @@
|
||||
// Copyright 2015 Light Code Labs, LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package pprof
|
||||
|
||||
import (
|
||||
"github.com/mholt/caddy"
|
||||
"github.com/mholt/caddy/caddyhttp/httpserver"
|
||||
)
|
||||
|
||||
func init() {
|
||||
caddy.RegisterPlugin("pprof", caddy.Plugin{
|
||||
ServerType: "http",
|
||||
Action: setup,
|
||||
})
|
||||
}
|
||||
|
||||
// setup returns a new instance of a pprof handler. It accepts no arguments or options.
|
||||
func setup(c *caddy.Controller) error {
|
||||
found := false
|
||||
|
||||
for c.Next() {
|
||||
if found {
|
||||
return c.Err("pprof can only be specified once")
|
||||
}
|
||||
if len(c.RemainingArgs()) != 0 {
|
||||
return c.ArgErr()
|
||||
}
|
||||
if c.NextBlock() {
|
||||
return c.ArgErr()
|
||||
}
|
||||
found = true
|
||||
}
|
||||
|
||||
httpserver.GetConfig(c).AddMiddleware(func(next httpserver.Handler) httpserver.Handler {
|
||||
return &Handler{Next: next, Mux: NewMux()}
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
46
vendor/github.com/mholt/caddy/caddyhttp/pprof/setup_test.go
generated
vendored
Normal file
46
vendor/github.com/mholt/caddy/caddyhttp/pprof/setup_test.go
generated
vendored
Normal file
@@ -0,0 +1,46 @@
|
||||
// Copyright 2015 Light Code Labs, LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package pprof
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/mholt/caddy"
|
||||
)
|
||||
|
||||
func TestSetup(t *testing.T) {
|
||||
tests := []struct {
|
||||
input string
|
||||
shouldErr bool
|
||||
}{
|
||||
{`pprof`, false},
|
||||
{`pprof {}`, true},
|
||||
{`pprof /foo`, true},
|
||||
{`pprof {
|
||||
a b
|
||||
}`, true},
|
||||
{`pprof
|
||||
pprof`, true},
|
||||
}
|
||||
for i, test := range tests {
|
||||
c := caddy.NewTestController("http", test.input)
|
||||
err := setup(c)
|
||||
if test.shouldErr && err == nil {
|
||||
t.Errorf("Test %v: Expected error but found nil", i)
|
||||
} else if !test.shouldErr && err != nil {
|
||||
t.Errorf("Test %v: Expected no error but found error: %v", i, err)
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user