Fix session cleanup on error paths
Signed-off-by: Sean Du <do4suki@gmail.com>
This commit is contained in:
@@ -29,10 +29,15 @@ func Common(ctx *gin.Context) {
|
||||
|
||||
ss := session.Attach(ctx)
|
||||
ctx.Set("session", ss)
|
||||
defer ss.FinalizeOrRollback()
|
||||
|
||||
if ctx.Request.URL.Path != "/login/authkey" && ctx.Request.URL.Path != "/login/login" {
|
||||
if ctx.IsAborted() {
|
||||
return
|
||||
}
|
||||
|
||||
if !honokautils.IsMainLoginEndpoint(ctx.Request.URL.Path) {
|
||||
if ss.UserPref.ForceRelogin {
|
||||
honokautils.AbortMaintenanceJSON(ctx, http.StatusNotFound, honokautils.NewNotFoundContent(ctx.Request.URL.String()))
|
||||
ss.AbortWithStatus(http.StatusNotFound, honokautils.NewNotFoundContent(ctx.Request.URL.Path))
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -40,7 +45,7 @@ func Common(ctx *gin.Context) {
|
||||
authorize := ctx.GetHeader("Authorize")
|
||||
params, err := url.ParseQuery(authorize)
|
||||
if err != nil {
|
||||
honokautils.AbortMaintenanceJSON(ctx, http.StatusNotFound, honokautils.NewNotFoundContent(ctx.Request.URL.String()))
|
||||
ss.AbortWithStatus(http.StatusNotFound, honokautils.NewNotFoundContent(ctx.Request.URL.Path))
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -21,18 +21,18 @@ func Recovery() gin.HandlerFunc {
|
||||
stack := debug.Stack()
|
||||
log.Printf("panic serving %s %s: %v\n%s", ctx.Request.Method, ctx.Request.URL.String(), recovered, stack)
|
||||
|
||||
if value, ok := ctx.Get("session"); ok {
|
||||
if ss, ok := value.(*session.Session); ok {
|
||||
ss.Rollback()
|
||||
}
|
||||
}
|
||||
|
||||
if !honokautils.IsMainPHPRequest(ctx.Request.URL.Path) {
|
||||
ctx.AbortWithStatus(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
content := honokautils.NewPanicContent(recovered, stack)
|
||||
if value, ok := ctx.Get("session"); ok {
|
||||
value.(*session.Session).AbortWithStatus(http.StatusInternalServerError, content)
|
||||
return
|
||||
}
|
||||
|
||||
honokautils.AbortMaintenanceJSON(ctx, http.StatusInternalServerError, content)
|
||||
honokautils.AbortMaintenanceJSON(ctx, http.StatusInternalServerError, honokautils.NewPanicContent())
|
||||
}()
|
||||
|
||||
ctx.Next()
|
||||
|
||||
Reference in New Issue
Block a user