MCPcopy
hub / github.com/gorilla/mux / ServeHTTP

Method ServeHTTP

mux.go:175–213  ·  view source on GitHub ↗

ServeHTTP dispatches the handler registered in the matched route. When there is a match, the route variables can be retrieved calling mux.Vars(request).

(w http.ResponseWriter, req *http.Request)

Source from the content-addressed store, hash-verified

173// When there is a match, the route variables can be retrieved calling
174// mux.Vars(request).
175func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
176 if !r.skipClean {
177 path := req.URL.Path
178 if r.useEncodedPath {
179 path = req.URL.EscapedPath()
180 }
181 // Clean path to canonical form and redirect.
182 if p := cleanPath(path); p != path {
183
184 // Added 3 lines (Philip Schlump) - It was dropping the query string and #whatever from query.
185 // This matches with fix in go 1.2 r.c. 4 for same problem. Go Issue:
186 // http://code.google.com/p/go/issues/detail?id=5252
187 url := *req.URL
188 url.Path = p
189 p = url.String()
190
191 w.Header().Set("Location", p)
192 w.WriteHeader(http.StatusMovedPermanently)
193 return
194 }
195 }
196 var match RouteMatch
197 var handler http.Handler
198 if r.Match(req, &match) {
199 handler = match.Handler
200 req = requestWithVars(req, match.Vars)
201 req = requestWithRoute(req, match.Route)
202 }
203
204 if handler == nil && match.MatchErr == ErrMethodMismatch {
205 handler = methodNotAllowedHandler()
206 }
207
208 if handler == nil {
209 handler = http.NotFoundHandler()
210 }
211
212 handler.ServeHTTP(w, req)
213}
214
215// Get returns a route registered with the given name.
216func (r *Router) Get(name string) *Route {

Callers 15

TestMiddlewareFunction · 0.95
TestMiddlewareSubrouterFunction · 0.95
TestMiddlewareExecutionFunction · 0.95
TestMiddlewareNotFoundFunction · 0.95
TestCORSMethodMiddlewareFunction · 0.95
Test301RedirectFunction · 0.95
TestSkipCleanFunction · 0.95

Calls 7

MatchMethod · 0.95
cleanPathFunction · 0.85
requestWithVarsFunction · 0.85
requestWithRouteFunction · 0.85
methodNotAllowedHandlerFunction · 0.85
HeaderMethod · 0.45
WriteHeaderMethod · 0.45

Tested by 15

TestMiddlewareFunction · 0.76
TestMiddlewareSubrouterFunction · 0.76
TestMiddlewareExecutionFunction · 0.76
TestMiddlewareNotFoundFunction · 0.76
TestCORSMethodMiddlewareFunction · 0.76
Test301RedirectFunction · 0.76
TestSkipCleanFunction · 0.76