Search in sources :

Example 56 with FilterHolder

use of org.eclipse.jetty.servlet.FilterHolder in project jetty.project by eclipse.

the class CrossOriginFilterTest method testSimpleRequestWithNonMatchingOrigin.

@Test
public void testSimpleRequestWithNonMatchingOrigin() throws Exception {
    FilterHolder filterHolder = new FilterHolder(new CrossOriginFilter());
    String origin = "http://localhost";
    filterHolder.setInitParameter(CrossOriginFilter.ALLOWED_ORIGINS_PARAM, origin);
    tester.getContext().addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST));
    CountDownLatch latch = new CountDownLatch(1);
    tester.getContext().addServlet(new ServletHolder(new ResourceServlet(latch)), "/*");
    String otherOrigin = origin.replace("localhost", "127.0.0.1");
    String request = "" + "GET / HTTP/1.1\r\n" + "Host: localhost\r\n" + "Connection: close\r\n" + "Origin: " + otherOrigin + "\r\n" + "\r\n";
    String response = tester.getResponses(request);
    Assert.assertTrue(response.contains("HTTP/1.1 200"));
    Assert.assertFalse(response.contains(CrossOriginFilter.ACCESS_CONTROL_ALLOW_ORIGIN_HEADER));
    Assert.assertFalse(response.contains(CrossOriginFilter.ACCESS_CONTROL_ALLOW_CREDENTIALS_HEADER));
    Assert.assertTrue(latch.await(1, TimeUnit.SECONDS));
}
Also used : FilterHolder(org.eclipse.jetty.servlet.FilterHolder) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 57 with FilterHolder

use of org.eclipse.jetty.servlet.FilterHolder in project jetty.project by eclipse.

the class CrossOriginFilterTest method testPUTRequestWithPreflight.

@Test
public void testPUTRequestWithPreflight() throws Exception {
    FilterHolder filterHolder = new FilterHolder(new CrossOriginFilter());
    filterHolder.setInitParameter(CrossOriginFilter.ALLOWED_METHODS_PARAM, "PUT");
    tester.getContext().addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST));
    CountDownLatch latch = new CountDownLatch(1);
    tester.getContext().addServlet(new ServletHolder(new ResourceServlet(latch)), "/*");
    // Preflight request
    String request = "" + "OPTIONS / HTTP/1.1\r\n" + "Host: localhost\r\n" + "Connection: close\r\n" + CrossOriginFilter.ACCESS_CONTROL_REQUEST_METHOD_HEADER + ": PUT\r\n" + "Origin: http://localhost\r\n" + "\r\n";
    String response = tester.getResponses(request);
    Assert.assertTrue(response.contains("HTTP/1.1 200"));
    Assert.assertTrue(response.contains(CrossOriginFilter.ACCESS_CONTROL_ALLOW_ORIGIN_HEADER));
    Assert.assertTrue(response.contains(CrossOriginFilter.ACCESS_CONTROL_ALLOW_CREDENTIALS_HEADER));
    Assert.assertTrue(response.contains(CrossOriginFilter.ACCESS_CONTROL_MAX_AGE_HEADER));
    Assert.assertTrue(response.contains(CrossOriginFilter.ACCESS_CONTROL_ALLOW_METHODS_HEADER));
    Assert.assertTrue(response.contains(CrossOriginFilter.ACCESS_CONTROL_ALLOW_HEADERS_HEADER));
    Assert.assertTrue(latch.await(1, TimeUnit.SECONDS));
    // Preflight request was ok, now make the actual request
    request = "" + "PUT / HTTP/1.1\r\n" + "Host: localhost\r\n" + "Connection: close\r\n" + "Origin: http://localhost\r\n" + "\r\n";
    response = tester.getResponses(request);
    Assert.assertTrue(response.contains("HTTP/1.1 200"));
    Assert.assertTrue(response.contains(CrossOriginFilter.ACCESS_CONTROL_ALLOW_ORIGIN_HEADER));
    Assert.assertTrue(response.contains(CrossOriginFilter.ACCESS_CONTROL_ALLOW_CREDENTIALS_HEADER));
}
Also used : FilterHolder(org.eclipse.jetty.servlet.FilterHolder) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 58 with FilterHolder

use of org.eclipse.jetty.servlet.FilterHolder in project jetty.project by eclipse.

the class CrossOriginFilterTest method testDELETERequestWithPreflightAndNotAllowedCustomHeaders.

@Test
public void testDELETERequestWithPreflightAndNotAllowedCustomHeaders() throws Exception {
    FilterHolder filterHolder = new FilterHolder(new CrossOriginFilter());
    filterHolder.setInitParameter(CrossOriginFilter.ALLOWED_METHODS_PARAM, "GET,HEAD,POST,PUT,DELETE");
    tester.getContext().addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST));
    CountDownLatch latch = new CountDownLatch(1);
    tester.getContext().addServlet(new ServletHolder(new ResourceServlet(latch)), "/*");
    // Preflight request
    String request = "" + "OPTIONS / HTTP/1.1\r\n" + "Host: localhost\r\n" + "Connection: close\r\n" + CrossOriginFilter.ACCESS_CONTROL_REQUEST_METHOD_HEADER + ": DELETE\r\n" + CrossOriginFilter.ACCESS_CONTROL_REQUEST_HEADERS_HEADER + ": origin,x-custom,x-requested-with\r\n" + "Origin: http://localhost\r\n" + "\r\n";
    String response = tester.getResponses(request);
    Assert.assertTrue(response.contains("HTTP/1.1 200"));
    Assert.assertFalse(response.contains(CrossOriginFilter.ACCESS_CONTROL_ALLOW_ORIGIN_HEADER));
    Assert.assertFalse(response.contains(CrossOriginFilter.ACCESS_CONTROL_ALLOW_CREDENTIALS_HEADER));
    Assert.assertTrue(latch.await(1, TimeUnit.SECONDS));
// The preflight request failed because header X-Custom is not allowed, actual request not issued
}
Also used : FilterHolder(org.eclipse.jetty.servlet.FilterHolder) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 59 with FilterHolder

use of org.eclipse.jetty.servlet.FilterHolder in project jetty.project by eclipse.

the class CrossOriginFilterTest method testCrossOriginFilterDisabledForWebSocketUpgrade.

@Test
public void testCrossOriginFilterDisabledForWebSocketUpgrade() throws Exception {
    FilterHolder filterHolder = new FilterHolder(new CrossOriginFilter());
    tester.getContext().addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST));
    CountDownLatch latch = new CountDownLatch(1);
    tester.getContext().addServlet(new ServletHolder(new ResourceServlet(latch)), "/*");
    String request = "" + "GET / HTTP/1.1\r\n" + "Host: localhost\r\n" + "Connection: Upgrade\r\n" + "Upgrade: WebSocket\r\n" + "Origin: http://localhost\r\n" + "\r\n";
    String response = tester.getResponses(request, 1, TimeUnit.SECONDS);
    Assert.assertTrue(response.contains("HTTP/1.1 200"));
    Assert.assertFalse(response.contains(CrossOriginFilter.ACCESS_CONTROL_ALLOW_ORIGIN_HEADER));
    Assert.assertFalse(response.contains(CrossOriginFilter.ACCESS_CONTROL_ALLOW_CREDENTIALS_HEADER));
    Assert.assertTrue(latch.await(1, TimeUnit.SECONDS));
}
Also used : FilterHolder(org.eclipse.jetty.servlet.FilterHolder) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 60 with FilterHolder

use of org.eclipse.jetty.servlet.FilterHolder in project jetty.project by eclipse.

the class CrossOriginFilterTest method testSimpleRequestWithMatchingWildcardOriginAndMultipleSubdomains.

@Test
public void testSimpleRequestWithMatchingWildcardOriginAndMultipleSubdomains() throws Exception {
    FilterHolder filterHolder = new FilterHolder(new CrossOriginFilter());
    String origin = "http://subdomain.subdomain.example.com";
    filterHolder.setInitParameter(CrossOriginFilter.ALLOWED_ORIGINS_PARAM, "http://*.example.com");
    tester.getContext().addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST));
    CountDownLatch latch = new CountDownLatch(1);
    tester.getContext().addServlet(new ServletHolder(new ResourceServlet(latch)), "/*");
    String request = "" + "GET / HTTP/1.1\r\n" + "Host: localhost\r\n" + "Connection: close\r\n" + "Origin: " + origin + "\r\n" + "\r\n";
    String response = tester.getResponses(request);
    Assert.assertTrue(response.contains("HTTP/1.1 200"));
    Assert.assertTrue(response.contains(CrossOriginFilter.ACCESS_CONTROL_ALLOW_ORIGIN_HEADER));
    Assert.assertTrue(response.contains(CrossOriginFilter.ACCESS_CONTROL_ALLOW_CREDENTIALS_HEADER));
    Assert.assertTrue(response.contains("Vary"));
    Assert.assertTrue(latch.await(1, TimeUnit.SECONDS));
}
Also used : FilterHolder(org.eclipse.jetty.servlet.FilterHolder) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

FilterHolder (org.eclipse.jetty.servlet.FilterHolder)84 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)44 Test (org.junit.Test)35 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)32 Server (org.eclipse.jetty.server.Server)21 CountDownLatch (java.util.concurrent.CountDownLatch)18 Filter (javax.servlet.Filter)11 IOException (java.io.IOException)10 URL (java.net.URL)10 DispatcherType (javax.servlet.DispatcherType)9 ServletException (javax.servlet.ServletException)8 AuthenticationToken (org.apache.hadoop.security.authentication.server.AuthenticationToken)8 ServerConnector (org.eclipse.jetty.server.ServerConnector)8 File (java.io.File)7 HttpURLConnection (java.net.HttpURLConnection)7 PrivilegedActionException (java.security.PrivilegedActionException)7 AuthenticationException (org.apache.hadoop.security.authentication.client.AuthenticationException)7 FilterMapping (org.eclipse.jetty.servlet.FilterMapping)7 WebAppContext (org.eclipse.jetty.webapp.WebAppContext)7 List (java.util.List)6