routeHTTP routes a http.Request through the Mux routing tree to serve the matching handler for a particular http method.
(w http.ResponseWriter, r *http.Request)
| 439 | // routeHTTP routes a http.Request through the Mux routing tree to serve |
| 440 | // the matching handler for a particular http method. |
| 441 | func (mx *Mux) routeHTTP(w http.ResponseWriter, r *http.Request) { |
| 442 | // Grab the route context object |
| 443 | rctx := r.Context().Value(RouteCtxKey).(*Context) |
| 444 | |
| 445 | // The request routing path |
| 446 | routePath := rctx.RoutePath |
| 447 | if routePath == "" { |
| 448 | if r.URL.RawPath != "" { |
| 449 | routePath = r.URL.RawPath |
| 450 | } else { |
| 451 | routePath = r.URL.Path |
| 452 | } |
| 453 | if routePath == "" { |
| 454 | routePath = "/" |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | // Check if method is supported by chi |
| 459 | if rctx.RouteMethod == "" { |
| 460 | rctx.RouteMethod = r.Method |
| 461 | } |
| 462 | method, ok := methodMap[rctx.RouteMethod] |
| 463 | if !ok { |
| 464 | mx.MethodNotAllowedHandler().ServeHTTP(w, r) |
| 465 | return |
| 466 | } |
| 467 | |
| 468 | // Find the route |
| 469 | if _, _, h := mx.tree.FindRoute(rctx, method, routePath); h != nil { |
| 470 | // Set http.Request path values from our request context |
| 471 | for i, key := range rctx.URLParams.Keys { |
| 472 | value := rctx.URLParams.Values[i] |
| 473 | r.SetPathValue(key, value) |
| 474 | } |
| 475 | r.Pattern = rctx.RoutePattern() |
| 476 | |
| 477 | h.ServeHTTP(w, r) |
| 478 | return |
| 479 | } |
| 480 | if rctx.methodNotAllowed { |
| 481 | mx.MethodNotAllowedHandler(rctx.methodsAllowed...).ServeHTTP(w, r) |
| 482 | } else { |
| 483 | mx.NotFoundHandler().ServeHTTP(w, r) |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | func (mx *Mux) nextRoutePath(rctx *Context) string { |
| 488 | routePath := "/" |
nothing calls this directly
no test coverage detected