<commit_before>package xitrum.handler.up

import java.lang.reflect.Method
import scala.collection.mutable.{ArrayBuffer, Map => MMap}

import org.jboss.netty.channel._
import org.jboss.netty.handler.codec.http._
import ChannelHandler.Sharable
import HttpResponseStatus._
import HttpVersion._

import xitrum.{Config, Controller, SkipCSRFCheck, Cache, Logger}
import xitrum.controller.Action
import xitrum.exception.{InvalidAntiCSRFToken, InvalidInput, MissingParam, SessionExpired}
import xitrum.handler.{AccessLog, HandlerEnv}
import xitrum.handler.down.{ResponseCacher, XSendFile}
import xitrum.routing.{ControllerReflection, Routes}
import xitrum.scope.request.RequestEnv
import xitrum.scope.session.CSRF
import xitrum.sockjs.SockJsController

object Dispatcher extends Logger {
  def dispatchWithFailsafe(actionMethod: Method, env: HandlerEnv) {
    val beginTimestamp = System.currentTimeMillis()
    var hit            = false

    val (controller, withActionMethod) = ControllerReflection.newControllerAndAction(actionMethod)
    controller(env)

    // Set pathPrefix for SockJsController
    if (controller.isInstanceOf[SockJsController])
      controller.pathPrefix = controller.pathInfo.tokens(0)

    env.action     = withActionMethod
    env.controller = controller

    try {
      // Check for CSRF (CSRF has been checked if "postback" is true)
      if ((controller.request.getMethod == HttpMethod.POST ||
           controller.request.getMethod == HttpMethod.PUT ||
           controller.request.getMethod == HttpMethod.DELETE) &&
          !controller.isInstanceOf[SkipCSRFCheck] &&
          !CSRF.isValidToken(controller)) throw new InvalidAntiCSRFToken

      val cacheSeconds = withActionMethod.cacheSeconds

      if (cacheSeconds > 0) {     // Page cache
        hit = tryCache(controller) {
          val passed = controller.callBeforeFilters()
          if (passed) runAroundAndAfterFilters(controller, withActionMethod)
        }
      } else {
        val passed = controller.callBeforeFilters()
        if (passed) {
          if (cacheSeconds < 0)  // Action cache
            hit = tryCache(controller) { runAroundAndAfterFilters(controller, withActionMethod) }
          else                   // No cache
            runAroundAndAfterFilters(controller, withActionMethod)
        }
      }

      if (!controller.forwarding) AccessLog.logDynamicContentAccess(controller, beginTimestamp, cacheSeconds, hit)<commit_msg>Add comments about the result of before filters<commit_after>package xitrum.handler.up

import java.lang.reflect.Method
import scala.collection.mutable.{ArrayBuffer, Map => MMap}

import org.jboss.netty.channel._
import org.jboss.netty.handler.codec.http._
import ChannelHandler.Sharable
import HttpResponseStatus._
import HttpVersion._

import xitrum.{Config, Controller, SkipCSRFCheck, Cache, Logger}
import xitrum.controller.Action
import xitrum.exception.{InvalidAntiCSRFToken, InvalidInput, MissingParam, SessionExpired}
import xitrum.handler.{AccessLog, HandlerEnv}
import xitrum.handler.down.{ResponseCacher, XSendFile}
import xitrum.routing.{ControllerReflection, Routes}
import xitrum.scope.request.RequestEnv
import xitrum.scope.session.CSRF
import xitrum.sockjs.SockJsController

object Dispatcher extends Logger {
  def dispatchWithFailsafe(actionMethod: Method, env: HandlerEnv) {
    val beginTimestamp = System.currentTimeMillis()
    var hit            = false

    val (controller, withActionMethod) = ControllerReflection.newControllerAndAction(actionMethod)
    controller(env)

    // Set pathPrefix for SockJsController
    if (controller.isInstanceOf[SockJsController])
      controller.pathPrefix = controller.pathInfo.tokens(0)

    env.action     = withActionMethod
    env.controller = controller

    try {
      // Check for CSRF (CSRF has been checked if "postback" is true)
      if ((controller.request.getMethod == HttpMethod.POST ||
           controller.request.getMethod == HttpMethod.PUT ||
           controller.request.getMethod == HttpMethod.DELETE) &&
          !controller.isInstanceOf[SkipCSRFCheck] &&
          !CSRF.isValidToken(controller)) throw new InvalidAntiCSRFToken

      val cacheSeconds = withActionMethod.cacheSeconds

      // Before filters:
      // When not passed, the before filters must explicitly respond to client,
      // with appropriate response status code, error description etc.
      // This logic is app-specific, Xitrum cannot does it for the app.

      if (cacheSeconds > 0) {     // Page cache
        hit = tryCache(controller) {
          val passed = controller.callBeforeFilters()
          if (passed) runAroundAndAfterFilters(controller, withActionMethod)
        }
      } else {
        val passed = controller.callBeforeFilters()
        if (passed) {
          if (cacheSeconds < 0)  // Action cache
            hit = tryCache(controller) { runAroundAndAfterFilters(controller, withActionMethod) }
          else                   // No cache
            runAroundAndAfterFilters(controller, withActionMethod)
        }
      }

      if (!controller.forwarding) AccessLog.logDynamicContentAccess(controller, beginTimestamp, cacheSeconds, hit)
{'fraction_non_alphanumeric': 0.08418613631975284, 'fraction_numerical': 0.0011585248117397182, 'mean_word_length': 3.4732297063903284, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1783920', 'n_tokens_mistral': 1416, 'n_tokens_neox': 1391, 'n_words': 329}