mirror of
https://github.com/cloudflare/cloudflared.git
synced 2025-07-29 11:22:07 +00:00
TUN-528: Move cloudflared into a separate repo
This commit is contained in:
54
vendor/github.com/mholt/caddy/caddyhttp/requestid/requestid.go
generated
vendored
Normal file
54
vendor/github.com/mholt/caddy/caddyhttp/requestid/requestid.go
generated
vendored
Normal file
@@ -0,0 +1,54 @@
|
||||
// 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 requestid
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/mholt/caddy/caddyhttp/httpserver"
|
||||
)
|
||||
|
||||
// Handler is a middleware handler
|
||||
type Handler struct {
|
||||
Next httpserver.Handler
|
||||
HeaderName string // (optional) header from which to read an existing ID
|
||||
}
|
||||
|
||||
func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
var reqid uuid.UUID
|
||||
|
||||
uuidFromHeader := r.Header.Get(h.HeaderName)
|
||||
if h.HeaderName != "" && uuidFromHeader != "" {
|
||||
// use the ID in the header field if it exists
|
||||
var err error
|
||||
reqid, err = uuid.Parse(uuidFromHeader)
|
||||
if err != nil {
|
||||
log.Printf("[NOTICE] Parsing request ID from %s header: %v", h.HeaderName, err)
|
||||
reqid = uuid.New()
|
||||
}
|
||||
} else {
|
||||
// otherwise, create a new one
|
||||
reqid = uuid.New()
|
||||
}
|
||||
|
||||
// set the request ID on the context
|
||||
c := context.WithValue(r.Context(), httpserver.RequestIDCtxKey, reqid.String())
|
||||
r = r.WithContext(c)
|
||||
|
||||
return h.Next.ServeHTTP(w, r)
|
||||
}
|
67
vendor/github.com/mholt/caddy/caddyhttp/requestid/requestid_test.go
generated
vendored
Normal file
67
vendor/github.com/mholt/caddy/caddyhttp/requestid/requestid_test.go
generated
vendored
Normal file
@@ -0,0 +1,67 @@
|
||||
// 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 requestid
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/mholt/caddy/caddyhttp/httpserver"
|
||||
)
|
||||
|
||||
func TestRequestIDHandler(t *testing.T) {
|
||||
handler := Handler{
|
||||
Next: httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
value, _ := r.Context().Value(httpserver.RequestIDCtxKey).(string)
|
||||
if value == "" {
|
||||
t.Error("Request ID should not be empty")
|
||||
}
|
||||
return 0, nil
|
||||
}),
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("GET", "http://localhost/", nil)
|
||||
if err != nil {
|
||||
t.Fatal("Could not create HTTP request:", err)
|
||||
}
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
handler.ServeHTTP(rec, req)
|
||||
}
|
||||
|
||||
func TestRequestIDFromHeader(t *testing.T) {
|
||||
headerName := "X-Request-ID"
|
||||
headerValue := "71a75329-d9f9-4d25-957e-e689a7b68d78"
|
||||
handler := Handler{
|
||||
Next: httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
value, _ := r.Context().Value(httpserver.RequestIDCtxKey).(string)
|
||||
if value != headerValue {
|
||||
t.Errorf("Request ID should be '%s' but got '%s'", headerValue, value)
|
||||
}
|
||||
return 0, nil
|
||||
}),
|
||||
HeaderName: headerName,
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("GET", "http://localhost/", nil)
|
||||
if err != nil {
|
||||
t.Fatal("Could not create HTTP request:", err)
|
||||
}
|
||||
req.Header.Set(headerName, headerValue)
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
handler.ServeHTTP(rec, req)
|
||||
}
|
46
vendor/github.com/mholt/caddy/caddyhttp/requestid/setup.go
generated
vendored
Normal file
46
vendor/github.com/mholt/caddy/caddyhttp/requestid/setup.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 requestid
|
||||
|
||||
import (
|
||||
"github.com/mholt/caddy"
|
||||
"github.com/mholt/caddy/caddyhttp/httpserver"
|
||||
)
|
||||
|
||||
func init() {
|
||||
caddy.RegisterPlugin("request_id", caddy.Plugin{
|
||||
ServerType: "http",
|
||||
Action: setup,
|
||||
})
|
||||
}
|
||||
|
||||
func setup(c *caddy.Controller) error {
|
||||
var headerName string
|
||||
|
||||
for c.Next() {
|
||||
if c.NextArg() {
|
||||
headerName = c.Val()
|
||||
}
|
||||
if c.NextArg() {
|
||||
return c.ArgErr()
|
||||
}
|
||||
}
|
||||
|
||||
httpserver.GetConfig(c).AddMiddleware(func(next httpserver.Handler) httpserver.Handler {
|
||||
return Handler{Next: next, HeaderName: headerName}
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
65
vendor/github.com/mholt/caddy/caddyhttp/requestid/setup_test.go
generated
vendored
Normal file
65
vendor/github.com/mholt/caddy/caddyhttp/requestid/setup_test.go
generated
vendored
Normal file
@@ -0,0 +1,65 @@
|
||||
// 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 requestid
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/mholt/caddy"
|
||||
"github.com/mholt/caddy/caddyhttp/httpserver"
|
||||
)
|
||||
|
||||
func TestSetup(t *testing.T) {
|
||||
c := caddy.NewTestController("http", `requestid`)
|
||||
err := setup(c)
|
||||
if err != nil {
|
||||
t.Errorf("Expected no errors, got: %v", err)
|
||||
}
|
||||
mids := httpserver.GetConfig(c).Middleware()
|
||||
if len(mids) == 0 {
|
||||
t.Fatal("Expected middleware, got 0 instead")
|
||||
}
|
||||
|
||||
handler := mids[0](httpserver.EmptyNext)
|
||||
myHandler, ok := handler.(Handler)
|
||||
|
||||
if !ok {
|
||||
t.Fatalf("Expected handler to be type Handler, got: %#v", handler)
|
||||
}
|
||||
|
||||
if !httpserver.SameNext(myHandler.Next, httpserver.EmptyNext) {
|
||||
t.Error("'Next' field of handler was not set properly")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetupWithArg(t *testing.T) {
|
||||
c := caddy.NewTestController("http", `requestid X-Request-ID`)
|
||||
err := setup(c)
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetupWithTooManyArgs(t *testing.T) {
|
||||
c := caddy.NewTestController("http", `requestid foo bar`)
|
||||
err := setup(c)
|
||||
if err == nil {
|
||||
t.Errorf("Expected an error, got: %v", err)
|
||||
}
|
||||
mids := httpserver.GetConfig(c).Middleware()
|
||||
if len(mids) != 0 {
|
||||
t.Fatal("Expected no middleware")
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user