use of org.eclipse.jetty.servlet.ServletHolder in project jetty.project by eclipse.
the class CrossOriginFilterTest method testSimpleRequestWithMatchingMultipleOrigins.
@Test
public void testSimpleRequestWithMatchingMultipleOrigins() throws Exception {
FilterHolder filterHolder = new FilterHolder(new CrossOriginFilter());
String origin = "http://localhost";
String otherOrigin = origin.replace("localhost", "127.0.0.1");
filterHolder.setInitParameter(CrossOriginFilter.ALLOWED_ORIGINS_PARAM, origin + "," + otherOrigin);
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" + // Use 2 spaces as separator to test that the implementation does not fail
"Origin: " + otherOrigin + " " + " " + 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));
}
use of org.eclipse.jetty.servlet.ServletHolder in project jetty.project by eclipse.
the class CrossOriginFilterTest method testSimpleRequestWithMatchingWildcardOrigin.
@Test
public void testSimpleRequestWithMatchingWildcardOrigin() throws Exception {
FilterHolder filterHolder = new FilterHolder(new CrossOriginFilter());
String origin = "http://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));
}
use of org.eclipse.jetty.servlet.ServletHolder in project jetty.project by eclipse.
the class CrossOriginFilterTest method testSimpleRequestWithWildcardOrigin.
@Test
public void testSimpleRequestWithWildcardOrigin() 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 origin = "http://foo.example.com";
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));
}
use of org.eclipse.jetty.servlet.ServletHolder in project jetty.project by eclipse.
the class DataRateLimitedServletTest method init.
@Before
public void init() throws Exception {
server = new Server();
connector = new LocalConnector(server);
connector.getConnectionFactory(HttpConfiguration.ConnectionFactory.class).getHttpConfiguration().setSendServerVersion(false);
context = new ServletContextHandler();
context.setContextPath("/context");
context.setWelcomeFiles(new String[] { "index.html", "index.jsp", "index.htm" });
File baseResourceDir = testdir.getEmptyPathDir().toFile();
// Use resolved real path for Windows and OSX
Path baseResourcePath = baseResourceDir.toPath().toRealPath();
context.setBaseResource(Resource.newResource(baseResourcePath.toFile()));
ServletHolder holder = context.addServlet(DataRateLimitedServlet.class, "/stream/*");
holder.setInitParameter("buffersize", "" + BUFFER);
holder.setInitParameter("pause", "" + PAUSE);
server.setHandler(context);
server.addConnector(connector);
server.start();
}
use of org.eclipse.jetty.servlet.ServletHolder in project jetty.project by eclipse.
the class JsrBrowserDebugTool method setupServer.
private void setupServer(int port) throws DeploymentException, ServletException, URISyntaxException, MalformedURLException, IOException {
server = new Server();
HttpConfiguration httpConf = new HttpConfiguration();
httpConf.setSendServerVersion(true);
ServerConnector connector = new ServerConnector(server, new HttpConnectionFactory(httpConf));
connector.setPort(port);
server.addConnector(connector);
String resourcePath = "/jsr-browser-debug-tool/index.html";
URL urlStatics = JsrBrowserDebugTool.class.getResource(resourcePath);
Objects.requireNonNull(urlStatics, "Unable to find " + resourcePath + " in classpath");
String urlBase = urlStatics.toURI().toASCIIString().replaceFirst("/[^/]*$", "/");
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
context.setBaseResource(Resource.newResource(urlBase));
ServletHolder holder = context.addServlet(DefaultServlet.class, "/");
holder.setInitParameter("dirAllowed", "true");
server.setHandler(context);
ServerContainer container = WebSocketServerContainerInitializer.configureContext(context);
container.addEndpoint(JsrBrowserSocket.class);
LOG.info("{} setup on port {}", this.getClass().getName(), port);
}
Aggregations