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);
}
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());
}
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());
}
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);
}
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);
}
}
}
}
Aggregations