MCPcopy Create free account
hub / github.com/devfeel/dotweb / ServeHTTP

Method ServeHTTP

router.go:212–287  ·  view source on GitHub ↗

ServeHTTP makes the router implement the http.Handler interface.

(ctx Context)

Source from the content-addressed store, hash-verified

210
211// ServeHTTP makes the router implement the http.Handler interface.
212func (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)

Callers

nothing calls this directly

Calls 12

allowedMethod · 0.95
WriterMethod · 0.80
getValueMethod · 0.80
StringMethod · 0.80
RequestMethod · 0.65
ResponseMethod · 0.65
setRouterParamsMethod · 0.65
setRouterNodeMethod · 0.65
RedirectMethod · 0.65
SetMethod · 0.65
HeaderMethod · 0.45

Tested by

no test coverage detected