MCPcopy
hub / github.com/go-chi/chi / Handler

Method Handler

middleware/route_headers.go:77–108  ·  view source on GitHub ↗
(next http.Handler)

Source from the content-addressed store, hash-verified

75}
76
77func (hr HeaderRouter) Handler(next http.Handler) http.Handler {
78 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
79 if len(hr) == 0 {
80 // skip if no routes set
81 next.ServeHTTP(w, r)
82 return
83 }
84
85 // find first matching header route, and continue
86 for header, matchers := range hr {
87 headerValue := r.Header.Get(header)
88 if headerValue == "" {
89 continue
90 }
91 headerValue = strings.ToLower(headerValue)
92 for _, matcher := range matchers {
93 if matcher.IsMatch(headerValue) {
94 matcher.Middleware(next).ServeHTTP(w, r)
95 return
96 }
97 }
98 }
99
100 // if no match, check for "*" default route
101 matcher, ok := hr["*"]
102 if !ok || matcher[0].Middleware == nil {
103 next.ServeHTTP(w, r)
104 return
105 }
106 matcher[0].Middleware(next).ServeHTTP(w, r)
107 })
108}
109
110type HeaderRoute struct {
111 Middleware func(next http.Handler) http.Handler

Callers

nothing calls this directly

Calls 4

HandlerFuncMethod · 0.80
IsMatchMethod · 0.80
GetMethod · 0.65
ServeHTTPMethod · 0.45

Tested by

no test coverage detected