ServeHTTP makes the router implement the http.Handler interface.
(ctx Context)
| 210 | |
| 211 | // ServeHTTP makes the router implement the http.Handler interface. |
| 212 | func (r *router) ServeHTTP(ctx Context) { |
| 213 | req := ctx.Request().Request |
| 214 | w := ctx.Response().Writer() |
| 215 | path := req.URL.Path |
| 216 | if root := r.Nodes[req.Method]; root != nil { |
| 217 | if handle, ps, node, tsr := root.getValue(path); handle != nil { |
| 218 | ctx.setRouterParams(ps) |
| 219 | ctx.setRouterNode(node) |
| 220 | handle(ctx) |
| 221 | return |
| 222 | } else if req.Method != "CONNECT" && path != "/" { |
| 223 | code := 301 // Permanent redirect, request with GET method |
| 224 | if req.Method != "GET" { |
| 225 | // Temporary redirect, request with same method |
| 226 | // As of Go 1.3, Go does not support status code 308. |
| 227 | code = 307 |
| 228 | } |
| 229 | |
| 230 | if tsr && r.RedirectTrailingSlash { |
| 231 | if len(path) > 1 && path[len(path)-1] == '/' { |
| 232 | req.URL.Path = path[:len(path)-1] |
| 233 | } else { |
| 234 | req.URL.Path = path + "/" |
| 235 | } |
| 236 | http.Redirect(w, req, req.URL.String(), code) |
| 237 | return |
| 238 | } |
| 239 | |
| 240 | // Try to fix the request path |
| 241 | if r.RedirectFixedPath { |
| 242 | fixedPath, found := root.findCaseInsensitivePath( |
| 243 | // file.CleanPath(path), |
| 244 | paths.Clean(path), |
| 245 | r.RedirectTrailingSlash, |
| 246 | ) |
| 247 | if found { |
| 248 | req.URL.Path = string(fixedPath) |
| 249 | http.Redirect(w, req, req.URL.String(), code) |
| 250 | return |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | if req.Method == "OPTIONS" { |
| 257 | // Handle OPTIONS requests |
| 258 | if r.HandleOPTIONS { |
| 259 | if allow := r.allowed(path, req.Method); len(allow) > 0 { |
| 260 | w.Header().Set("Allow", allow) |
| 261 | return |
| 262 | } |
| 263 | } |
| 264 | } else { |
| 265 | // Handle 405 |
| 266 | if allow := r.allowed(path, req.Method); len(allow) > 0 { |
| 267 | w.Header().Set("Allow", allow) |
| 268 | // In DefaultMethodNotAllowedHandler will be call SetStatusCode(http.StatusMethodNotAllowed) |
| 269 | r.server.DotApp.MethodNotAllowedHandler(ctx) |
nothing calls this directly
no test coverage detected