mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-10-29 10:57:44 +09:00 
			
		
		
		
	In order for web.Wrap to be able to detect if a response has been written we need to wrap any non-context.ResponseWriters as a such. Otherwise responses will be incorrectly detected as non-written to and handlers can double run. In the case of GZip this handler will change the response to a non-context.RW and this failure to correctly detect response writing causes fallthrough and a NPE. Fix #19839 Signed-off-by: Andrew Thornton <art27@cantab.net>
		
			
				
	
	
		
			111 lines
		
	
	
		
			3.7 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			111 lines
		
	
	
		
			3.7 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| // Copyright 2021 The Gitea Authors. All rights reserved.
 | |
| // Use of this source code is governed by a MIT-style
 | |
| // license that can be found in the LICENSE file.
 | |
| 
 | |
| package web
 | |
| 
 | |
| import (
 | |
| 	goctx "context"
 | |
| 	"fmt"
 | |
| 	"net/http"
 | |
| 
 | |
| 	"code.gitea.io/gitea/modules/context"
 | |
| 	"code.gitea.io/gitea/modules/web/routing"
 | |
| )
 | |
| 
 | |
| type wrappedHandlerFunc func(resp http.ResponseWriter, req *http.Request, others ...wrappedHandlerFunc) (done bool, deferrable func())
 | |
| 
 | |
| func convertHandler(handler interface{}) wrappedHandlerFunc {
 | |
| 	funcInfo := routing.GetFuncInfo(handler)
 | |
| 	switch t := handler.(type) {
 | |
| 	case http.HandlerFunc:
 | |
| 		return func(resp http.ResponseWriter, req *http.Request, others ...wrappedHandlerFunc) (done bool, deferrable func()) {
 | |
| 			routing.UpdateFuncInfo(req.Context(), funcInfo)
 | |
| 			if _, ok := resp.(context.ResponseWriter); !ok {
 | |
| 				resp = context.NewResponse(resp)
 | |
| 			}
 | |
| 			t(resp, req)
 | |
| 			if r, ok := resp.(context.ResponseWriter); ok && r.Status() > 0 {
 | |
| 				done = true
 | |
| 			}
 | |
| 			return
 | |
| 		}
 | |
| 	case func(http.ResponseWriter, *http.Request):
 | |
| 		return func(resp http.ResponseWriter, req *http.Request, others ...wrappedHandlerFunc) (done bool, deferrable func()) {
 | |
| 			routing.UpdateFuncInfo(req.Context(), funcInfo)
 | |
| 			t(resp, req)
 | |
| 			if r, ok := resp.(context.ResponseWriter); ok && r.Status() > 0 {
 | |
| 				done = true
 | |
| 			}
 | |
| 			return
 | |
| 		}
 | |
| 
 | |
| 	case func(ctx *context.Context):
 | |
| 		return func(resp http.ResponseWriter, req *http.Request, others ...wrappedHandlerFunc) (done bool, deferrable func()) {
 | |
| 			routing.UpdateFuncInfo(req.Context(), funcInfo)
 | |
| 			ctx := context.GetContext(req)
 | |
| 			t(ctx)
 | |
| 			done = ctx.Written()
 | |
| 			return
 | |
| 		}
 | |
| 	case func(ctx *context.Context) goctx.CancelFunc:
 | |
| 		return func(resp http.ResponseWriter, req *http.Request, others ...wrappedHandlerFunc) (done bool, deferrable func()) {
 | |
| 			routing.UpdateFuncInfo(req.Context(), funcInfo)
 | |
| 			ctx := context.GetContext(req)
 | |
| 			deferrable = t(ctx)
 | |
| 			done = ctx.Written()
 | |
| 			return
 | |
| 		}
 | |
| 	case func(*context.APIContext):
 | |
| 		return func(resp http.ResponseWriter, req *http.Request, others ...wrappedHandlerFunc) (done bool, deferrable func()) {
 | |
| 			routing.UpdateFuncInfo(req.Context(), funcInfo)
 | |
| 			ctx := context.GetAPIContext(req)
 | |
| 			t(ctx)
 | |
| 			done = ctx.Written()
 | |
| 			return
 | |
| 		}
 | |
| 	case func(*context.APIContext) goctx.CancelFunc:
 | |
| 		return func(resp http.ResponseWriter, req *http.Request, others ...wrappedHandlerFunc) (done bool, deferrable func()) {
 | |
| 			routing.UpdateFuncInfo(req.Context(), funcInfo)
 | |
| 			ctx := context.GetAPIContext(req)
 | |
| 			deferrable = t(ctx)
 | |
| 			done = ctx.Written()
 | |
| 			return
 | |
| 		}
 | |
| 	case func(*context.PrivateContext):
 | |
| 		return func(resp http.ResponseWriter, req *http.Request, others ...wrappedHandlerFunc) (done bool, deferrable func()) {
 | |
| 			routing.UpdateFuncInfo(req.Context(), funcInfo)
 | |
| 			ctx := context.GetPrivateContext(req)
 | |
| 			t(ctx)
 | |
| 			done = ctx.Written()
 | |
| 			return
 | |
| 		}
 | |
| 	case func(*context.PrivateContext) goctx.CancelFunc:
 | |
| 		return func(resp http.ResponseWriter, req *http.Request, others ...wrappedHandlerFunc) (done bool, deferrable func()) {
 | |
| 			routing.UpdateFuncInfo(req.Context(), funcInfo)
 | |
| 			ctx := context.GetPrivateContext(req)
 | |
| 			deferrable = t(ctx)
 | |
| 			done = ctx.Written()
 | |
| 			return
 | |
| 		}
 | |
| 	case func(http.Handler) http.Handler:
 | |
| 		return func(resp http.ResponseWriter, req *http.Request, others ...wrappedHandlerFunc) (done bool, deferrable func()) {
 | |
| 			next := http.HandlerFunc(func(http.ResponseWriter, *http.Request) {})
 | |
| 			if len(others) > 0 {
 | |
| 				next = wrapInternal(others)
 | |
| 			}
 | |
| 			routing.UpdateFuncInfo(req.Context(), funcInfo)
 | |
| 			if _, ok := resp.(context.ResponseWriter); !ok {
 | |
| 				resp = context.NewResponse(resp)
 | |
| 			}
 | |
| 			t(next).ServeHTTP(resp, req)
 | |
| 			if r, ok := resp.(context.ResponseWriter); ok && r.Status() > 0 {
 | |
| 				done = true
 | |
| 			}
 | |
| 			return
 | |
| 		}
 | |
| 	default:
 | |
| 		panic(fmt.Sprintf("Unsupported handler type: %#v", t))
 | |
| 	}
 | |
| }
 |