github.com/searKing/golang/go@v1.2.117/net/http/internal/handler_interceptor.go (about) 1 // Copyright 2020 The searKing Author. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package internal 6 7 import "net/http" 8 9 var DefaultPreHandler = func(w http.ResponseWriter, r *http.Request) error { return nil } 10 var DefaultWrapHandler = func(h http.Handler) http.Handler { return h } 11 var DefaultPostHandler = func(w http.ResponseWriter, r *http.Request) {} 12 var DefaultAfterCompletion = func(w http.ResponseWriter, r *http.Request, err any) { 13 if err != nil { 14 panic(err) 15 } 16 } 17 18 type HandlerInterceptor struct { 19 // Intercept the execution of a handler. 20 // The default implementation returns true. 21 // Parameters: 22 // request - current HTTP request 23 // response - current HTTP response 24 // handler - chosen handler to execute, for type and/or instance evaluation 25 // Returns: 26 // true if the execution chain should proceed with the next interceptor or the handler itself. 27 // Else, DispatcherServlet assumes that this interceptor has already dealt with the response itself. 28 PreHandleFunc func(w http.ResponseWriter, r *http.Request) error 29 30 // Intercept the execution of a handler. 31 // The default implementation is empty. 32 // Parameters: 33 // handler - current HTTP handler 34 // Returns: 35 // handler - wrapped HTTP handler 36 WrapHandleFunc func(h http.Handler) http.Handler 37 38 // Intercept the execution of a handler. 39 // The default implementation is empty. 40 // Parameters: 41 // request - current HTTP request 42 // response - current HTTP response 43 // handler - the handler (or HandlerMethod) that started asynchronous execution, for type and/or instance examination 44 PostHandleFunc func(w http.ResponseWriter, r *http.Request) 45 // Callback after completion of request processing, that is, after rendering the view. 46 // The default implementation is empty. 47 // Parameters: 48 // request - current HTTP request 49 // response - current HTTP response 50 // handler - the handler (or HandlerMethod) that started asynchronous execution, for type and/or instance examination 51 // ex - any exception thrown on handler execution, if any; this does not include exceptions that have been handled through an exception resolver 52 AfterCompletionFunc func(w http.ResponseWriter, r *http.Request, err any) 53 } 54 55 func (filter HandlerInterceptor) PreHandle(w http.ResponseWriter, r *http.Request) error { 56 if filter.PreHandleFunc == nil { 57 return DefaultPreHandler(w, r) 58 } 59 return filter.PreHandleFunc(w, r) 60 } 61 62 func (filter HandlerInterceptor) WrapHandle(h http.Handler) http.Handler { 63 if filter.WrapHandleFunc == nil { 64 return DefaultWrapHandler(h) 65 } 66 return filter.WrapHandleFunc(h) 67 } 68 69 func (filter HandlerInterceptor) PostHandle(w http.ResponseWriter, r *http.Request) { 70 if filter.PostHandleFunc == nil { 71 DefaultPostHandler(w, r) 72 return 73 } 74 filter.PostHandleFunc(w, r) 75 } 76 77 func (filter HandlerInterceptor) AfterCompletion(w http.ResponseWriter, r *http.Request, err any) { 78 if filter.AfterCompletionFunc == nil { 79 DefaultAfterCompletion(w, r, err) 80 return 81 } 82 83 filter.AfterCompletionFunc(w, r, err) 84 }