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)
| 173 | // When there is a match, the route variables can be retrieved calling |
| 174 | // mux.Vars(request). |
| 175 | func (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. |
| 216 | func (r *Router) Get(name string) *Route { |