Skip to content
This repository
tree: 1c465306bd
Fetching contributors…

Cannot retrieve contributors at this time

file 1024 lines (859 sloc) 34.627 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
/*
* Copyright 2007-2011 WorldWide Conferencing, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.liftweb
package http

import xml.{Node, NodeSeq, Group}

import common._
import actor._
import util._
import util.Helpers._
import js._
import auth._
import provider._
import json.JsonAST.JValue


class LiftServlet extends Loggable {
  private var servletContext: HTTPContext = null

  def this(ctx: HTTPContext) = {
    this ()
    this.servletContext = ctx
  }

  def getContext: HTTPContext = servletContext

  def destroy = {
    try {
      LiftRules.ending = true

      tryo {
        SessionMaster.shutDownAllSessions()
      }

      val cur = millis

      // wait 10 seconds or until the request count is zero
      while (LiftRules.reqCnt.get > 0 && (millis - cur) < 10000L) {
        Thread.sleep(20)
      }

      tryo {
        Schedule.shutdown
      }
      tryo {
        LAScheduler.shutdown()
      }

      tryo {
        LAPinger.shutdown
      }

      LiftRules.runUnloadHooks()
      logger.debug("Destroyed Lift handler.")
      // super.destroy
    } catch {
      case e => logger.error("Destruction failure", e)
    }
  }

  def init = {
    LiftRules.ending = false
  }

  def getLiftSession(request: Req): LiftSession = LiftRules.getLiftSession(request)

  private def wrapState[T](req: Req, session: Box[LiftSession])(f: => T): T = {
    session match {
      case Full(ses) => S.init(req, ses)(f)
      case _ => CurrentReq.doWith(req)(f)
    }
  }

  private def handleGenericContinuation(reqOrg: Req, resp: HTTPResponse, session: Box[LiftSession], func: ((=> LiftResponse) => Unit) => Unit): Boolean = {

    val req = if (null eq reqOrg) reqOrg else reqOrg.snapshot

    def runFunction(doAnswer: LiftResponse => Unit) {
      Schedule.schedule(() => {
        val answerFunc: (=> LiftResponse) => Unit = response =>
          doAnswer(wrapState(req, session)(response))

        func(answerFunc)

      }, 5 millis)
    }

    if (reqOrg.request.suspendResumeSupport_?) {
      runFunction(liftResponse => {
        // do the actual write on a separate thread
        Schedule.schedule(() => {
          reqOrg.request.resume(reqOrg, liftResponse)
        }, 0 seconds)
      })

      reqOrg.request.suspend(cometTimeout)
      false
    } else {
      val future = new LAFuture[LiftResponse]

      runFunction(answer => future.satisfy(answer))

      future.get(cometTimeout) match {
        case Full(answer) => sendResponse(answer, resp, req); true
        case _ => false
      }
    }
  }

  /**
* Processes the HTTP requests
*/
  def service(req: Req, resp: HTTPResponse): Boolean = {
    try {
      def doIt: Boolean = {
        if (LiftRules.logServiceRequestTiming) {
          logTime {
            val ret = doService(req, resp)
            val msg = "Service request (" + req.request.method + ") " + req.request.uri + " returned " + resp.getStatus + ","
            (msg, ret)
          }
        } else {
          doService(req, resp)
        }
      }

      req.request.resumeInfo match {
        case None => doIt
        case r if r eq null => doIt
        case Some((or: Req, r: LiftResponse)) if (req.path == or.path) => sendResponse(r, resp, req); true
        case _ => doIt
      }
    } catch {
      case rest.ContinuationException(theReq, sesBox, func) =>
        handleGenericContinuation(theReq, resp, sesBox, func); true // we have to return true to hold onto the request

      case e if e.getClass.getName.endsWith("RetryRequest") => throw e
      case e => logger.info("Request for " + req.request.uri + " failed " + e.getMessage, e); throw e
    }
  }

  private def flatten(in: List[Any]): List[Any] = in match {
    case Nil => Nil
    case Some(x: AnyRef) :: xs => x :: flatten(xs)
    case Full(x: AnyRef) :: xs => x :: flatten(xs)
    case (lst: Iterable[_]) :: xs => lst.toList ::: flatten(xs)
    case (x: AnyRef) :: xs => x :: flatten(xs)
    case x :: xs => flatten(xs)
  }

  private def authPassed_?(req: Req): Boolean = {

    val checkRoles: (Role, List[Role]) => Boolean = {
      case (resRole, roles) => (false /: roles)((l, r) => l || resRole.isChildOf(r.name))
    }

    val role = NamedPF.applyBox(req, LiftRules.httpAuthProtectedResource.toList)
    role.map(_ match {
      case Full(r) =>
        LiftRules.authentication.verified_?(req) match {
          case true => checkRoles(r, userRoles.get)
          case _ => false
        }
      case _ => LiftRules.authentication.verified_?(req)
    }) openOr true
  }

  private val recent: LRUMap[String, Int] = new LRUMap(2000)

  private def registerRecentlyChecked(id: String): Unit =
    synchronized {
      val next = recent.get(id) match {
        case Full(x) => x + 1
        case _ => 1
      }

      recent(id) = next
    }

  private def recentlyChecked(id: Box[String]): Int = synchronized {
    id.flatMap(recent.get).openOr(0)
  }

  /**
* Service the HTTP request
*/
  def doService(req: Req, response: HTTPResponse): Boolean = {
    var tmpStatelessHolder: Box[Box[LiftResponse]] = Empty

    tryo {
      LiftRules.onBeginServicing.toList.foreach(_(req))
    }

    def hasSession(idb: Box[String]): Boolean = {
      idb.flatMap {
        id =>
          registerRecentlyChecked(id)
          SessionMaster.getSession(id, Empty)
      }.isDefined
    }

    val wp = req.path.wholePath
    val pathLen = wp.length
    def isComet: Boolean = {
      if (pathLen < 2) false
      else {
        val kindaComet = wp.head == LiftRules.cometPath
        val cometScript = (pathLen >= 3 && kindaComet &&
          wp(2) == LiftRules.cometScriptName())

        (kindaComet && !cometScript) && req.acceptsJavaScript_?
      }
    }
    def isAjax: Boolean = {
      if (pathLen < 2) false
      else {
        val kindaAjax = wp.head == LiftRules.ajaxPath
        val ajaxScript = kindaAjax &&
          wp(1) == LiftRules.ajaxScriptName()

        (kindaAjax && !ajaxScript) && req.acceptsJavaScript_?
      }
    }

    val sessionIdCalc = new SessionIdCalc(req)

    val resp: Box[LiftResponse] = try {
      // if the application is shutting down, return a 404
      if (LiftRules.ending) {
        LiftRules.notFoundOrIgnore(req, Empty)
      } else if (!authPassed_?(req)) {
        Full(LiftRules.authentication.unauthorizedResponse)
      } else if (LiftRules.redirectAsyncOnSessionLoss && !hasSession(sessionIdCalc.id) && (isComet || isAjax)) {
        val theId = sessionIdCalc.id

        // okay after 2 attempts to redirect, just ignore calls to the
        // async URL
        if (recentlyChecked(theId) > 1) {
          Empty
        } else {
          val cmd =
            if (isComet)
              js.JE.JsRaw(LiftRules.noCometSessionCmd.vend.toJsCmd + ";lift_toWatch = {};").cmd
            else
              js.JE.JsRaw(LiftRules.noAjaxSessionCmd.vend.toJsCmd).cmd

          Full(new JsCommands(cmd :: Nil).toResponse)
        }
      }
      // if the request is matched is defined in the stateless table, dispatch
      if (S.statelessInit(req) {
        tmpStatelessHolder = NamedPF.applyBox(req,
          LiftRules.statelessDispatch.toList).map(_.apply() match {
          case Full(a) => Full(LiftRules.convertResponse((a, Nil, S.responseCookies, req)))
          case r => r
        });
        tmpStatelessHolder.isDefined
      }) {
        val f = tmpStatelessHolder.open_!
        f match {
          case Full(v) => Full(v)
          case Empty => LiftRules.notFoundOrIgnore(req, Empty)
          case f: Failure => Full(req.createNotFound(f))
        }
      } else {
        // otherwise do a stateful response
        val liftSession = getLiftSession(req)

        def doSession(r2: Req, s2: LiftSession, continue: Box[() => Nothing]): () => Box[LiftResponse] = {
          try {
            S.init(r2, s2) {
              dispatchStatefulRequest(S.request.open_!, liftSession, r2, continue)
            }
          } catch {
            case cre: ContinueResponseException =>
              r2.destroyServletSession()
              doSession(r2, getLiftSession(r2), Full(cre.continue))
          }
        }

        val lzy: () => Box[LiftResponse] = doSession(req, liftSession, Empty)

        lzy()
      }
    } catch {
      case foc: LiftFlowOfControlException => throw foc
      case e: Exception if !e.getClass.getName.endsWith("RetryRequest") => {
        val bundle = (Props.mode, req, e)
        S.assertExceptionThrown()
        NamedPF.applyBox(bundle, LiftRules.exceptionHandler.toList)
      }
    }

    tryo {
      LiftRules.onEndServicing.toList.foreach(_(req, resp))
    }

    resp match {
      case Full(EmptyResponse) =>
        true

      case Full(cresp) =>
        sendResponse(cresp, response, req)
        true

      case _ => {
        false
      }
    }
  }

  private def dispatchStatefulRequest(req: Req,
                                      liftSession: LiftSession,
                                      originalRequest: Req,
                                      continuation: Box[() => Nothing]): () => Box[LiftResponse] = {
    val toMatch = req

    val dispatch: (Boolean, Box[LiftResponse]) =
      NamedPF.find(toMatch, LiftRules.dispatchTable(req.request)) match {
        case Full(pf) =>
          LiftSession.onBeginServicing.foreach(_(liftSession, req))
          val ret: (Boolean, Box[LiftResponse]) =
            try {
              try {
                // run the continuation in the new session
                // if there is a continuation
                continuation match {
                  case Full(func) => {
                    func()
                    S.redirectTo("/")
                  }
                  case _ => // do nothing
                }

                liftSession.runParams(req)
                S.functionLifespan(true) {
                  pf(toMatch)() match {
                    case Full(v) =>
                      (true, Full(LiftRules.convertResponse((liftSession.checkRedirect(v), Nil,
                        S.responseCookies, req))))

                    case Empty =>
                      (true, LiftRules.notFoundOrIgnore(req, Full(liftSession)))

                    case f: Failure =>
                      (true, Full(liftSession.checkRedirect(req.createNotFound(f))))
                  }
                }
              } catch {
                case ite: java.lang.reflect.InvocationTargetException if (ite.getCause.isInstanceOf[ResponseShortcutException]) =>
                  (true, Full(liftSession.handleRedirect(ite.getCause.asInstanceOf[ResponseShortcutException], req)))

                case rd: net.liftweb.http.ResponseShortcutException => (true, Full(liftSession.handleRedirect(rd, req)))
              }
            } finally {
              if (S.functionMap.size > 0) {
                liftSession.updateFunctionMap(S.functionMap, S.renderVersion, millis)
                S.clearFunctionMap
              }
              liftSession.notices = S.getNotices
            }

          LiftSession.onEndServicing.foreach(_(liftSession, req,
            ret._2))
          ret

        case _ => (false, Empty)
      }

    val wp = req.path.wholePath

    if (LiftRules.enableContainerSessions && !req.stateless_?) {
      req.request.session
    }

    def respToFunc(in: Box[LiftResponse]): () => Box[LiftResponse] = {
      val ret = in.map(LiftRules.performTransform)
      () => ret
    }

    val toReturn: () => Box[LiftResponse] =
      if (dispatch._1) {
        respToFunc(dispatch._2)
      } else if (wp.length == 3 && wp.head == LiftRules.cometPath &&
        wp(2) == LiftRules.cometScriptName()) {
        respToFunc(LiftRules.serveCometScript(liftSession, req))
      } else if ((wp.length >= 1) && wp.head == LiftRules.cometPath) {
        handleComet(req, liftSession, originalRequest) match {
          case Left(x) => respToFunc(x)
          case Right(x) => x
        }
      } else if (wp.length == 2 && wp.head == LiftRules.ajaxPath &&
        wp(1) == LiftRules.ajaxScriptName()) {
        respToFunc(LiftRules.serveAjaxScript(liftSession, req))
      } else if (wp.length >= 1 && wp.head == LiftRules.ajaxPath) {
        respToFunc(handleAjax(liftSession, req))
      } else {
        respToFunc(liftSession.processRequest(req, continuation))
      }

    toReturn
  }

  private object AjaxVersions {
    def unapply(ajaxPathPart: String) : Option[(String,Int)] = {
      val dash = ajaxPathPart.indexOf("-")
      if (dash > -1 && ajaxPathPart.length > dash + 1)
        Some(
          (ajaxPathPart.substring(0, dash),
           ajaxPathPart.charAt(dash + 1))
        )
      else
        None
    }
  }
  /**
* Extracts two versions from a given AJAX path:
* - The RenderVersion, which is used for GC purposes.
* - The requestVersion, which lets us determine if this is
* a request we've already dealt with or are currently dealing
* with (so we don't rerun the associated handler). See
* handleVersionedAjax for more.
*
* The requestVersion is passed to the function that is passed in.
*/
  private def extractVersions[T](path: List[String])(f: (Box[Int]) => T): T = {
    path match {
      case first :: AjaxVersions(renderVersion, requestVersion) :: _ =>
        RenderVersion.doWith(renderVersion)(f(Full(requestVersion)))
      case _ => f(Empty)
    }
  }

  /**
* An actor that manages AJAX continuations from container (Jetty style).
*/
  class AjaxContinuationActor(request: Req, session: LiftSession,
                              onBreakout: Box[LiftResponse] => Unit) extends LiftActor {
    private var response: Box[LiftResponse] = Empty
    private var done = false

    def messageHandler = {
      case AjaxResponseComplete(completeResponse) =>
        response = completeResponse
        LAPinger.schedule(this, BreakOut(), 5 millis)

      case BreakOut() if ! done =>
        done = true
        session.exitComet(this)
        onBreakout(response)

      case _ =>
    }

    override def toString = "AJAX Continuation Actor"
  }

  private case class AjaxResponseComplete(response: Box[LiftResponse])

  /**
* existingResponse is Empty if we have no response for this request
* yet. pendingActors is a list of actors who want to be notified when
* this response is received.
*/
  private case class AjaxRequestInfo(requestVersion:Int, existingResponse:Box[Box[LiftResponse]], pendingActors:List[LiftActor])

  private lazy val ajaxPostTimeout: Long = LiftRules.ajaxPostTimeout * 1000L
  private val currentAjaxRequests = scala.collection.mutable.Map[String,AjaxRequestInfo]()

  /**
* Runs the actual AJAX processing. This includes handling __lift__GC,
* or running the parameters in the session. onComplete is run when the
* AJAX request has completed with a response that is meant for the
* user. In cases where the request is taking too long to respond,
* an LAFuture may be used to delay the real response (and thus the
* invocation of onComplete) while this function returns an empty
* response.
*/
  private def runAjax(requestState: Req,
              liftSession: LiftSession,
              onCompleteFn: Box[(LiftResponse)=>Unit]): Box[LiftResponse] = {
    // We don't want to call the onComplete function if we're returning
    // an error response, as if there is a comet timeout while
    // processing. This is because the onComplete function is meant to
    // indicate a successful completion, and will short-circuit any
    // other AJAX requests with the same version with the same
    // response. Error responses are specific to the AJAX request,
    // rather than to the version of the AJAX request. Successful
    // responses are for all AJAX requests with the same version.
    var callCompleteFn = true

    val ret = try {
      requestState.param("__lift__GC") match {
        case Full(_) =>
          val renderVersion = RenderVersion.get
          liftSession.updateFuncByOwner(renderVersion, millis)

          // FIXME We need to clean up currentAjaxRequests entries when
          // FIXME the page expires.

          Full(JavaScriptResponse(js.JsCmds.Noop))

        case _ =>
          try {
            val what = flatten(try {
              liftSession.runParams(requestState)
            } catch {
              case ResponseShortcutException(_, Full(to), _) =>
                import js.JsCmds._
                List(RedirectTo(to))
            })

            def processResponse(response: List[Any]): LiftResponse = {
              val what2 = response.flatMap {
                case js: JsCmd => List(js)
                case jv: JValue => List(jv)
                case n: NodeSeq => List(n)
                case js: JsCommands => List(js)
                case r: LiftResponse => List(r)

                // This strange case comes from ajax requests run in
                // a comet context that don't complete in the
                // cometTimeout time frame. The js is the result of the
                // comet processing timeout handler, while the future
                // gives us access to the eventual result of the ajax
                // request.
                case (js, future: LAFuture[List[Any]]) if onCompleteFn.isDefined =>
                  // Wait for the future on a separate thread.
                  Schedule.schedule(() => {
                    val result = flatten(future.get)
                    onCompleteFn.foreach(_(processResponse(result)))
                  }, 0 seconds)

                  // But this request is done for. Return the comet
                  // processing timeout result, but don't mark the
                  // request complete; that happens whenever we satisfy
                  // the future above.
                  callCompleteFn = false
                  List(js)
                case s => Nil
              }

              what2 match {
                case (json: JsObj) :: Nil => JsonResponse(json)
                case (jv: JValue) :: Nil => JsonResponse(jv)
                case (js: JsCmd) :: xs => {
                  (JsCommands(S.noticesToJsCmd :: Nil) &
                    ((js :: xs).flatMap {
                      case js: JsCmd => List(js)
                      case _ => Nil
                    }.reverse) &
                    S.jsToAppend).toResponse
                }

                case (n: Node) :: _ => XmlResponse(n)
                case (ns: NodeSeq) :: _ => XmlResponse(Group(ns))
                case (r: LiftResponse) :: _ => r
                case _ => JsCommands(S.noticesToJsCmd :: JsCmds.Noop :: S.jsToAppend).toResponse
              }
            }

            val ret: LiftResponse = processResponse(what)

            LiftRules.cometLogger.debug("AJAX Response: " + liftSession.uniqueId + " " + ret)

            Full(ret)
          } finally {
            if (S.functionMap.size > 0) {
              liftSession.updateFunctionMap(S.functionMap, RenderVersion.get, millis)
              S.clearFunctionMap
            }
          }
      }
    } catch {
      case foc: LiftFlowOfControlException => throw foc
      case e => S.assertExceptionThrown() ; NamedPF.applyBox((Props.mode, requestState, e), LiftRules.exceptionHandler.toList);
    }

    for {
      response <- ret if callCompleteFn
      onComplete <- onCompleteFn
    } {
      onComplete(response)
    }

    ret
  }

  /**
* Handles the details of versioned AJAX requests. If the version is
* Empty, runs the request through a normal AJAX flow with no
* continuations or special handling. Repeated calls will cause
* repeated evaluation.
*
* If the version is Full, the request is tracked. Subsequent requests
* for the same version will suspend until a response is available,
* then they will return the response.
*
* cont is the AjaxContinuationActor for this request, and
* suspendRequest is the function that will suspend the request.
* These are present to support non-continuation containers; these
* will present a no-op to suspendRequest.
*/
  private def handleVersionedAjax(handlerVersion: Box[Int],
                          cont: AjaxContinuationActor,
                          suspendRequest: () => Any,
                          requestState: Req,
                          liftSession: LiftSession): Box[LiftResponse] = {
    handlerVersion match {
      case Full(handlerVersion) =>
        val renderVersion = RenderVersion.get

        val toReturn: Box[Box[LiftResponse]] =
          currentAjaxRequests.synchronized {
            currentAjaxRequests.get(renderVersion).collect {
              case AjaxRequestInfo(storedVersion, _, pendingActors) if handlerVersion != storedVersion =>
                // Break out of any actors for the stale version.
                pendingActors.foreach(_ ! BreakOut())

                // Evict the older version's info.
                currentAjaxRequests +=
                  (renderVersion ->
                    AjaxRequestInfo(handlerVersion, Empty, cont :: Nil))

                suspendRequest()

                Empty // no response available, triggers the actual AJAX computation below

              case AjaxRequestInfo(storedVersion, existingResponseBox @ Full(_), _) =>
                existingResponseBox // return the Full response Box

              case AjaxRequestInfo(storedVersion, _, pendingActors) =>
                currentAjaxRequests +=
                  (renderVersion ->
                    AjaxRequestInfo(handlerVersion, Empty, cont :: pendingActors))

                suspendRequest()

                Full(Full(EmptyResponse))
            } openOr {
              currentAjaxRequests +=
                (renderVersion ->
                  AjaxRequestInfo(handlerVersion, Empty, cont :: Nil))

              suspendRequest()

              Empty // no response available, triggers the actual AJAX computation below
            }
          }

        toReturn or {
          Full(runAjax(requestState, liftSession, Full((result: LiftResponse) => {
            // When we get the response, synchronizedly check that the
            // versions are still the same in the map, and, if so, update
            // any waiting actors then clear the actor list and update the
            // request info to include the response in case any other
            // requests come in with this version.
            currentAjaxRequests.synchronized {
              currentAjaxRequests.get(renderVersion).collect {
                case AjaxRequestInfo(storedVersion, _, pendingActors) if storedVersion == handlerVersion =>
                  pendingActors.foreach(_ ! AjaxResponseComplete(Full(result)))
                  currentAjaxRequests +=
                    (renderVersion ->
                      AjaxRequestInfo(handlerVersion, Full(Full(result)), Nil))
              }
            }
          })))
        } openOr Empty

      case _ =>
        runAjax(requestState, liftSession, Empty)
    }
  }

  /**
* Kick off AJAX handling. Extracts relevant versions and handles the
* begin/end servicing requests, as well as generation of
* ContinuationActors and choosing between continuation and
* continuationless request handling.
*/
  private def handleAjax(liftSession: LiftSession,
                         requestState: Req): Box[LiftResponse] = {
    extractVersions(requestState.path.partPath) { handlerVersion =>
      LiftRules.cometLogger.debug("AJAX Request: " + liftSession.uniqueId + " " + requestState.params)
      tryo {
        LiftSession.onBeginServicing.foreach(_(liftSession, requestState))
      }

      def suspendingActor = {
        new AjaxContinuationActor(requestState, liftSession,
          response => {
            requestState.request.resume((requestState, S.init(requestState, liftSession)
              (response.map(LiftRules.performTransform) openOr EmptyResponse)))})
      }

      def waitingActorForFuture(future: LAFuture[Box[LiftResponse]]) = {
        new AjaxContinuationActor(requestState, liftSession,
          response => future.satisfy(response))
      }

      val possibleFuture =
        if (requestState.request.suspendResumeSupport_?)
          Empty
        else
          Full(new LAFuture[Box[LiftResponse]])
      val (cont, suspendRequest) =
        possibleFuture.map { f =>
          (waitingActorForFuture(f), () => ())
        } openOr {
          (suspendingActor, () => requestState.request.suspend(ajaxPostTimeout + 500L))
        }

      val ret: Box[LiftResponse] = try {
        liftSession.enterComet(cont -> requestState)

        val result: Box[LiftResponse] =
          handleVersionedAjax(handlerVersion, cont, suspendRequest, requestState, liftSession)

        possibleFuture.map(_.get(ajaxPostTimeout) match {
          case Full(response) => response
          case _ => Empty
        }) openOr result
      } finally {
        if (! requestState.request.suspendResumeSupport_?)
          liftSession.exitComet(cont)
      }

      tryo {
        LiftSession.onEndServicing.foreach(_(liftSession, requestState, ret))
      }
      ret
    }
  }

  /**
* An actor that manages comet continuations from container (Jetty style)
*/
  class CometContinuationActor(request: Req, session: LiftSession,
                          actors: List[(LiftCometActor, Long)],
                          onBreakout: List[AnswerRender] => Unit) extends LiftActor {
    private var answers: List[AnswerRender] = Nil
    private var done = false
    val seqId = Helpers.nextNum

    def messageHandler = {
      case BeginContinuation =>
        val sendItToMe: AnswerRender => Unit = ah => this ! ah

        actors.foreach {
          case (act, when) => act ! Listen(when, ListenerId(seqId), sendItToMe)
        }

      case ar: AnswerRender =>
        answers = ar :: answers
        LAPinger.schedule(this, BreakOut(), 5 millis)

      case BreakOut() if !done =>
        done = true
        session.exitComet(this)
        actors.foreach {
          case (act, _) => tryo(act ! Unlisten(ListenerId(seqId)))
        }
        onBreakout(answers)

      case _ =>
    }

    override def toString = "Continuation Actor dude " + seqId
  }

  private object BeginContinuation

  private lazy val cometTimeout: Long = (LiftRules.cometRequestTimeout openOr 120) * 1000L

  private def setupCometContinuation(request: Req, session: LiftSession, actors: List[(LiftCometActor, Long)]): Any = {
    val cont = new CometContinuationActor(request, session, actors,
      answers => request.request.resume(
        (request, S.init(request, session)
          (LiftRules.performTransform(
            convertAnswersToCometResponse(session,
              answers.toList, actors))))))


    try {
      session.enterComet(cont -> request)

      LAPinger.schedule(cont, BreakOut(), TimeSpan(cometTimeout))

      request.request.suspend(cometTimeout + 2000L)
    } finally {
      cont ! BeginContinuation
    }
  }

  private def handleComet(requestState: Req, sessionActor: LiftSession, originalRequest: Req): Either[Box[LiftResponse], () => Box[LiftResponse]] = {
    val actors: List[(LiftCometActor, Long)] =
      requestState.params.toList.flatMap {
        case (name, when) =>
          sessionActor.getAsyncComponent(name).toList.map(c => (c, toLong(when)))
      }

    if (actors.isEmpty) Left(Full(new JsCommands(LiftRules.noCometSessionCmd.vend :: js.JE.JsRaw("lift_toWatch = {};").cmd :: Nil).toResponse))
    else requestState.request.suspendResumeSupport_? match {
      case true => {
        setupCometContinuation(requestState, sessionActor, actors)
        Left(Full(EmptyResponse))
      }

      case _ => {
        Right(handleNonContinuationComet(requestState, sessionActor, actors, originalRequest))
      }
    }
  }

  private def convertAnswersToCometResponse(session: LiftSession, ret: Seq[AnswerRender], actors: List[(LiftCometActor, Long)]): LiftResponse = {
    val ret2: List[AnswerRender] = ret.toList
    val jsUpdateTime = ret2.map(ar => "if (lift_toWatch['" + ar.who.uniqueId + "'] !== undefined) lift_toWatch['" + ar.who.uniqueId + "'] = '" + ar.when + "';").mkString("\n")
    val jsUpdateStuff = ret2.map {
      ar => {
        val ret = ar.response.toJavaScript(session, ar.displayAll)

        if (!S.functionMap.isEmpty) {
          session.updateFunctionMap(S.functionMap,
            ar.who.uniqueId, ar.when)
          S.clearFunctionMap
        }

        ret
      }
    }

    actors foreach (_._1 ! ClearNotices)

    val addl: List[JsCmd] =
      (for {
        req <- S.request
        rendVer <- extractRenderVersion(req.path.partPath)
      } yield RenderVersion.doWith(rendVer) {
          S.jsToAppend
        }) openOr Nil

    (new JsCommands(JsCmds.Run(jsUpdateTime) :: jsUpdateStuff ::: addl)).toResponse
  }

  private def extractRenderVersion(in: List[String]): Box[String] = in match {
    case _ :: _ :: _ :: rv :: _ => Full(rv)
    case _ => Empty
  }

  private def handleNonContinuationComet(request: Req, session: LiftSession, actors: List[(LiftCometActor, Long)],
                                         originalRequest: Req): () => Box[LiftResponse] = () => {
    val f = new LAFuture[List[AnswerRender]]
    val cont = new CometContinuationActor(request, session, actors,
      answers => f.satisfy(answers))

    try {
      cont ! BeginContinuation

      session.enterComet(cont -> request)

      LAPinger.schedule(cont, BreakOut(), TimeSpan(cometTimeout))

      val ret2 = f.get(cometTimeout) openOr Nil

      Full(S.init(originalRequest, session) {
        convertAnswersToCometResponse(session, ret2, actors)
      })
    } finally {
      session.exitComet(cont)
    }
  }

  val dumpRequestResponse = Props.getBool("dump.request.response", false)

  private def logIfDump(request: Req, response: BasicResponse) {
    if (dumpRequestResponse) {
      val toDump = request.uri + "\n" +
        request.params + "\n" +
        response.headers + "\n" +
        (
          response match {
            case InMemoryResponse(data, _, _, _) => new String(data, "UTF-8")
            case _ => "data"
          }
          )

      logger.trace(toDump)
    }
  }

  /**
* Sends the { @code HTTPResponse } to the browser using data from the
* { @link Response } and { @link Req }.
*/
  private[http] def sendResponse(liftResp: LiftResponse, response: HTTPResponse, request: Req) {
    def fixHeaders(headers: List[(String, String)]) = headers map ((v) => v match {
      case ("Location", uri) =>
        val u = request
        (v._1, (
          (for (
            updated <- Full((if (!LiftRules.excludePathFromContextPathRewriting.vend(uri)) u.contextPath else "") + uri).filter(ignore => uri.startsWith("/"));
            rwf <- URLRewriter.rewriteFunc) yield rwf(updated)) openOr uri
          ))
      case _ => v
    })

    def pairFromRequest(req: Req): (Box[Req], Box[String]) = {
      val acceptHeader = for (innerReq <- Box.legacyNullTest(req.request);
                              accept <- innerReq.header("Accept")) yield accept

      (Full(req), acceptHeader)
    }

    val resp = liftResp.toResponse

    logIfDump(request, resp)

    def insureField(headers: List[(String, String)], toInsure: List[(String, String)]): List[(String, String)] = {
      val org = Map(headers: _*)

      toInsure.foldLeft(org) {
        case (map, (key, value)) =>
          if (map.contains(key)) map
          else map + (key -> value)
      }.toList

    }


    val len = resp.size
    // insure that certain header fields are set
    val header = if (resp.code == 304 || resp.code == 303)
      fixHeaders(resp.headers)
    else
      insureField(fixHeaders(resp.headers),
        LiftRules.defaultHeaders(NodeSeq.Empty -> request) :::
          /* List(("Content-Type",
LiftRules.determineContentType(pairFromRequest(request)))) ::: */
          (if (len >= 0) List(("Content-Length", len.toString)) else Nil))

    LiftRules.beforeSend.toList.foreach(f => tryo(f(resp, response, header, Full(request))))
    // set the cookies
    response.addCookies(resp.cookies)

    // send the response
    response.addHeaders(header.map {
      case (name, value) => HTTPParam(name, value)
    })
    LiftRules.supplimentalHeaders(response)

    liftResp match {
      case ResponseWithReason(_, reason) => response setStatusWithReason (resp.code, reason)
      case _ => response setStatus resp.code
    }

    try {
      resp match {
        case EmptyResponse =>
        case InMemoryResponse(bytes, _, _, _) =>
          response.outputStream.write(bytes)
          response.outputStream.flush()

        case StreamingResponse(stream, endFunc, _, _, _, _) =>
          try {
            var len = 0
            val ba = new Array[Byte](8192)
            val os = response.outputStream
            stream match {
              case jio: java.io.InputStream => len = jio.read(ba)
              case stream => len = stream.read(ba)
            }
            while (len >= 0) {
              if (len > 0) os.write(ba, 0, len)
              stream match {
                case jio: java.io.InputStream => len = jio.read(ba)
                case stream => len = stream.read(ba)
              }
            }
            response.outputStream.flush()
          } finally {
            endFunc()
          }

        case OutputStreamResponse(out, _, _, _, _) =>
          out(response.outputStream)
          response.outputStream.flush()
      }
    } catch {
      case e: java.io.IOException => // ignore IO exceptions... they happen
    }

    LiftRules.afterSend.toList.foreach(f => tryo(f(resp, response, header, Full(request))))
  }
}

import provider.servlet._

class LiftFilter extends ServletFilterProvider

private class SessionIdCalc(req: Req) {
  private val CometPath = LiftRules.cometPath
  lazy val id: Box[String] = req.request.sessionId match {
    case Full(id) => Full(id)
    case _ => req.path.wholePath match {
      case CometPath :: _ :: id :: _ if id != LiftRules.cometScriptName() => Full(id)
      case _ => Empty
    }
  }
}
Something went wrong with that request. Please try again.