Jetty
  1. Jetty
  2. JETTY-797

Requests are not forwarded to correct contxt.

    Details

    • Type: Bug Bug
    • Status: Closed Closed
    • Priority: Critical Critical
    • Resolution: Not A Bug
    • Affects Version/s: 6.1.6
    • Fix Version/s: None
    • Component/s: HTTP
    • Labels:
      None
    • Number of attachments :
      0

      Description

      I am trying to write a http server which can handle multiple request depending on the port and uri the listeners are listening on.

      I add 2 contextHandlers to listen on port 80 and context path's
      /test and /test/123
      ContextHandler context1 = new ContextHandler();
      context1.setContextPath("/test/123");
      context1.setAllowNullPathInfo(true);
      Handler handler1=new HelloHandler1();
      context1.setHandler(handler1);

      ContextHandler context0 = new ContextHandler();
      context0.setContextPath("/test");
      context0.setAllowNullPathInfo(true);
      Handler handler0=new HelloHandler();
      context0.setHandler(handler0);

      Now if the context handler /test/123 is added first followed by context handler /test the request are forwarded in correct manner cause jetty takes out handlers from collection one by one and use this code to compare the context path:

      // Nope - so check the target.
      if (dispatch==REQUEST)
      {
      if (target.equals(_contextPath))
      {
      if (!_allowNullPathInfo && !target.endsWith(URIUtil.SLASH))

      { base_request.setHandled(true); if (request.getQueryString()!=null) response.sendRedirect(URIUtil.addPaths(request.getRequestURI(),URIUtil.SLASH)+"?"+request.getQueryString()); else response.sendRedirect(URIUtil.addPaths(request.getRequestURI(),URIUtil.SLASH)); return; }

      if (_contextPath.length()>1)

      { target=URIUtil.SLASH; request.setAttribute("org.mortbay.jetty.nullPathInfo",target); }

      }
      else if (target.startsWith(_contextPath) && (_contextPath.length()==1 || target.charAt(_contextPath.length())=='/'))

      { if (_contextPath.length()>1) target=target.substring(_contextPath.length()); }

      else

      { // Not for this context! return; }

      }
      }

      But if /test context handler is added before the /test/123 than requests belonging to /test/123 are also dispatched to /test context handler due to the fact that
      if (target.equals(_contextPath)) return false but if (target.startsWith(_contextPath) && (_contextPath.length()==1 || target.charAt(_contextPath.length())=='/')) returns true.

      This is not the right behavior as order of adding handlers decide to which handler the request will be dispatched rather than the corrcet context path.
      Can you please tell me if there is any work around for this or if this can be fixed.

      Just to summarize:

      Context handlers registered :
      /test
      /test/123

      Request sent to http://host:port/test is forwarded to /test
      Request sent to http://host:port/test/123 is forwarded to /test---Incorrect behavior

      Context handlers registered :
      /test/123
      /test

      Request sent to http://host:port/test is forwarded to /test
      Request sent to http://host:port/test/123 is forwarded to /test/123

      First case should also work fine and there should be an option so that only absolute context paths are compared and not the startsWith api.

        Activity

        Hide
        David Yu added a comment -

        "Can you please tell me if there is any work around for this or if this can be fixed."
        As a temporary workaround ...you said it yourself... add the /test/123 context first before /test ...
        Its a matter of configuring it properly. (shouldn't be too hard)

        We'll definitely take a look at this and tweak the code if its reasonable (performance wise).

        Show
        David Yu added a comment - "Can you please tell me if there is any work around for this or if this can be fixed." As a temporary workaround ...you said it yourself... add the /test/123 context first before /test ... Its a matter of configuring it properly. (shouldn't be too hard) We'll definitely take a look at this and tweak the code if its reasonable (performance wise).
        Hide
        Pankaj Arora added a comment -

        Hi David,
        Problem is our product dynamically allows user to add listeners to the server. We cannot dictate the fact that /test/123 is added before or after /test or /test/1 etc. This in a way blocks us from using the server properly. Will appreciate if you can provide a quick fix.

        Show
        Pankaj Arora added a comment - Hi David, Problem is our product dynamically allows user to add listeners to the server. We cannot dictate the fact that /test/123 is added before or after /test or /test/1 etc. This in a way blocks us from using the server properly. Will appreciate if you can provide a quick fix.
        Hide
        Jan Bartel added a comment -

        Pankaj,

        It sounds like you're using the HandlerCollection and adding the ContextHandlers to it, but you should be using the ContextHandlerCollection instead. The ContextHandlerCollection will pick the best matching context path.

        I'll close this issue for now, but if you use the ContextHandlerCollection and it does not select the best match, please reopen.

        regards
        Jan

        Show
        Jan Bartel added a comment - Pankaj, It sounds like you're using the HandlerCollection and adding the ContextHandlers to it, but you should be using the ContextHandlerCollection instead. The ContextHandlerCollection will pick the best matching context path. I'll close this issue for now, but if you use the ContextHandlerCollection and it does not select the best match, please reopen. regards Jan

          People

          • Assignee:
            Unassigned
            Reporter:
            Pankaj Arora
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: