Search in sources :

Example 36 with Request

use of org.apache.catalina.connector.Request in project tomcat by apache.

the class TestRemoteIpValve method testInvokeAllProxiesAreTrustedAndRemoteAddrMatchRegexp.

@Test
public void testInvokeAllProxiesAreTrustedAndRemoteAddrMatchRegexp() throws Exception {
    // PREPARE
    RemoteIpValve remoteIpValve = new RemoteIpValve();
    remoteIpValve.setInternalProxies("127\\.0\\.0\\.1|192\\.168\\..*|another-internal-proxy");
    remoteIpValve.setTrustedProxies("proxy1|proxy2|proxy3");
    remoteIpValve.setRemoteIpHeader("x-forwarded-for");
    remoteIpValve.setProxiesHeader("x-forwarded-by");
    RemoteAddrAndHostTrackerValve remoteAddrAndHostTrackerValve = new RemoteAddrAndHostTrackerValve();
    remoteIpValve.setNext(remoteAddrAndHostTrackerValve);
    Request request = new MockRequest();
    request.setCoyoteRequest(new org.apache.coyote.Request());
    request.setRemoteAddr("192.168.0.10");
    request.setRemoteHost("remote-host-original-value");
    request.getCoyoteRequest().getMimeHeaders().addValue("x-forwarded-for").setString("140.211.11.130");
    request.getCoyoteRequest().getMimeHeaders().addValue("x-forwarded-for").setString("proxy1");
    request.getCoyoteRequest().getMimeHeaders().addValue("x-forwarded-for").setString("proxy2");
    // TEST
    remoteIpValve.invoke(request, null);
    // VERIFY
    String actualXForwardedFor = remoteAddrAndHostTrackerValve.getForwardedFor();
    Assert.assertNull("all proxies are trusted, x-forwarded-for must be null", actualXForwardedFor);
    String actualXForwardedBy = remoteAddrAndHostTrackerValve.getForwardedBy();
    Assert.assertEquals("all proxies are trusted, they must appear in x-forwarded-by", "proxy1,proxy2", actualXForwardedBy);
    String actualRemoteAddr = remoteAddrAndHostTrackerValve.getRemoteAddr();
    Assert.assertEquals("remoteAddr", "140.211.11.130", actualRemoteAddr);
    String actualRemoteHost = remoteAddrAndHostTrackerValve.getRemoteHost();
    Assert.assertEquals("remoteHost", "140.211.11.130", actualRemoteHost);
    String actualPostInvokeRemoteAddr = request.getRemoteAddr();
    Assert.assertEquals("postInvoke remoteAddr", "192.168.0.10", actualPostInvokeRemoteAddr);
    String actualPostInvokeRemoteHost = request.getRemoteHost();
    Assert.assertEquals("postInvoke remoteAddr", "remote-host-original-value", actualPostInvokeRemoteHost);
}
Also used : Request(org.apache.catalina.connector.Request) Test(org.junit.Test)

Example 37 with Request

use of org.apache.catalina.connector.Request in project tomcat70 by apache.

the class TestDigestAuthenticator method bug54521.

@Test
public void bug54521() throws LifecycleException {
    DigestAuthenticator digestAuthenticator = new DigestAuthenticator();
    digestAuthenticator.setContainer(new TesterContext());
    digestAuthenticator.start();
    Request request = new TesterRequest();
    final int count = 1000;
    Set<String> nonces = new HashSet<String>();
    for (int i = 0; i < count; i++) {
        nonces.add(digestAuthenticator.generateNonce(request));
    }
    Assert.assertEquals(count, nonces.size());
}
Also used : Request(org.apache.catalina.connector.Request) TesterContext(org.apache.tomcat.unittest.TesterContext) SecurityConstraint(org.apache.catalina.deploy.SecurityConstraint) HashSet(java.util.HashSet) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 38 with Request

use of org.apache.catalina.connector.Request in project tomcat70 by apache.

the class TestPersistentManager method testBug62175.

@Test
public void testBug62175() throws Exception {
    final PersistentManager manager = new PersistentManager();
    final AtomicInteger sessionExpireCounter = new AtomicInteger();
    Store mockStore = EasyMock.createNiceMock(Store.class);
    EasyMock.expect(mockStore.load(EasyMock.anyString())).andAnswer(new IAnswer<Session>() {

        @Override
        public Session answer() throws Throwable {
            return timedOutSession(manager, sessionExpireCounter);
        }
    }).anyTimes();
    EasyMock.replay(mockStore);
    manager.setStore(mockStore);
    Host host = new TesterHost();
    final RequestCachingSessionListener requestCachingSessionListener = new RequestCachingSessionListener();
    final Context context = new TesterContext() {

        @Override
        public Object[] getApplicationLifecycleListeners() {
            return new Object[] { requestCachingSessionListener };
        }

        @Override
        public Manager getManager() {
            return manager;
        }
    };
    context.setParent(host);
    Request req = new Request();
    req.setContext(context);
    req.setRequestedSessionId("invalidSession");
    HttpServletRequest request = new RequestFacade(req);
    requestCachingSessionListener.request = request;
    manager.setContainer(context);
    manager.start();
    Assert.assertNull(request.getSession(false));
    EasyMock.verify(mockStore);
    Assert.assertEquals(1, sessionExpireCounter.get());
}
Also used : Context(org.apache.catalina.Context) TesterContext(org.apache.tomcat.unittest.TesterContext) Request(org.apache.catalina.connector.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) Store(org.apache.catalina.Store) TesterHost(org.apache.tomcat.unittest.TesterHost) Host(org.apache.catalina.Host) RequestFacade(org.apache.catalina.connector.RequestFacade) HttpServletRequest(javax.servlet.http.HttpServletRequest) IAnswer(org.easymock.IAnswer) TesterHost(org.apache.tomcat.unittest.TesterHost) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TesterContext(org.apache.tomcat.unittest.TesterContext) Test(org.junit.Test)

Example 39 with Request

use of org.apache.catalina.connector.Request in project tomcat70 by apache.

the class ApplicationDispatcher method wrapRequest.

/**
 * Create and return a request wrapper that has been inserted in the
 * appropriate spot in the request chain.
 */
private ServletRequest wrapRequest(State state) {
    // Locate the request we should insert in front of
    ServletRequest previous = null;
    ServletRequest current = state.outerRequest;
    while (current != null) {
        if (state.hrequest == null && (current instanceof HttpServletRequest))
            state.hrequest = (HttpServletRequest) current;
        if (!(current instanceof ServletRequestWrapper))
            break;
        if (current instanceof ApplicationHttpRequest)
            break;
        if (current instanceof ApplicationRequest)
            break;
        previous = current;
        current = ((ServletRequestWrapper) current).getRequest();
    }
    // Instantiate a new wrapper at this point and insert it in the chain
    ServletRequest wrapper = null;
    if ((current instanceof ApplicationHttpRequest) || (current instanceof Request) || (current instanceof HttpServletRequest)) {
        // Compute a crossContext flag
        HttpServletRequest hcurrent = (HttpServletRequest) current;
        boolean crossContext = false;
        if ((state.outerRequest instanceof ApplicationHttpRequest) || (state.outerRequest instanceof Request) || (state.outerRequest instanceof HttpServletRequest)) {
            HttpServletRequest houterRequest = (HttpServletRequest) state.outerRequest;
            Object contextPath = houterRequest.getAttribute(RequestDispatcher.INCLUDE_CONTEXT_PATH);
            if (contextPath == null) {
                // Forward
                contextPath = houterRequest.getContextPath();
            }
            crossContext = !(context.getPath().equals(contextPath));
        }
        wrapper = new ApplicationHttpRequest(hcurrent, context, crossContext);
    } else {
        wrapper = new ApplicationRequest(current);
    }
    if (previous == null)
        state.outerRequest = wrapper;
    else
        ((ServletRequestWrapper) previous).setRequest(wrapper);
    state.wrapRequest = wrapper;
    return (wrapper);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) ServletRequestWrapper(javax.servlet.ServletRequestWrapper) HttpServletRequest(javax.servlet.http.HttpServletRequest) Request(org.apache.catalina.connector.Request) ServletRequest(javax.servlet.ServletRequest)

Example 40 with Request

use of org.apache.catalina.connector.Request in project tomcat70 by apache.

the class CometConnectionManagerValve method sessionDestroyed.

@Override
public void sessionDestroyed(HttpSessionEvent se) {
    // Close all Comet connections associated with this session
    Request[] reqs = (Request[]) se.getSession().getAttribute(cometRequestsAttribute);
    if (reqs != null) {
        for (int i = 0; i < reqs.length; i++) {
            Request req = reqs[i];
            try {
                CometEventImpl event = req.getEvent();
                event.setEventType(CometEvent.EventType.END);
                event.setEventSubType(CometEvent.EventSubType.SESSION_END);
                ((CometProcessor) req.getWrapper().getServlet()).event(event);
                event.close();
            } catch (Exception e) {
                req.getWrapper().getParent().getLogger().warn(sm.getString("cometConnectionManagerValve.listenerEvent"), e);
            }
        }
    }
}
Also used : CometProcessor(org.apache.catalina.comet.CometProcessor) CometEventImpl(org.apache.catalina.connector.CometEventImpl) Request(org.apache.catalina.connector.Request) LifecycleException(org.apache.catalina.LifecycleException) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Aggregations

Request (org.apache.catalina.connector.Request)80 Test (org.junit.Test)44 Response (org.apache.catalina.connector.Response)16 HttpServletRequest (javax.servlet.http.HttpServletRequest)14 IOException (java.io.IOException)9 HttpSession (javax.servlet.http.HttpSession)9 Context (org.apache.catalina.Context)9 ServletRequest (javax.servlet.ServletRequest)8 Valve (org.apache.catalina.Valve)7 RequestFacade (org.apache.catalina.connector.RequestFacade)7 TesterContext (org.apache.tomcat.unittest.TesterContext)7 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)5 HttpSession (jakarta.servlet.http.HttpSession)5 ServletException (javax.servlet.ServletException)5 Connector (org.apache.catalina.connector.Connector)5 ServletRequest (jakarta.servlet.ServletRequest)4 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)4 ServletRequestWrapper (javax.servlet.ServletRequestWrapper)3 LifecycleException (org.apache.catalina.LifecycleException)3 ServletException (jakarta.servlet.ServletException)2