Search in sources :

Example 36 with InputStream

use of java.io.InputStream in project jetty.project by eclipse.

the class SniSslConnectionFactoryTest method testSameConnectionRequestsForManyWildDomains.

@Test
public void testSameConnectionRequestsForManyWildDomains() throws Exception {
    SslContextFactory clientContextFactory = new SslContextFactory(true);
    clientContextFactory.start();
    SSLSocketFactory factory = clientContextFactory.getSslContext().getSocketFactory();
    try (SSLSocket sslSocket = (SSLSocket) factory.createSocket("127.0.0.1", _port)) {
        SNIHostName serverName = new SNIHostName("www.domain.com");
        SSLParameters params = sslSocket.getSSLParameters();
        params.setServerNames(Collections.singletonList(serverName));
        sslSocket.setSSLParameters(params);
        sslSocket.startHandshake();
        String request = "" + "GET /ctx/path HTTP/1.1\r\n" + "Host: www.domain.com\r\n" + "\r\n";
        OutputStream output = sslSocket.getOutputStream();
        output.write(request.getBytes(StandardCharsets.UTF_8));
        output.flush();
        InputStream input = sslSocket.getInputStream();
        String response = response(input);
        Assert.assertTrue(response.startsWith("HTTP/1.1 200 "));
        // Now, on the same socket, send a request for a different valid domain.
        request = "" + "GET /ctx/path HTTP/1.1\r\n" + "Host: assets.domain.com\r\n" + "\r\n";
        output.write(request.getBytes(StandardCharsets.UTF_8));
        output.flush();
        response = response(input);
        Assert.assertTrue(response.startsWith("HTTP/1.1 200 "));
        // Now make a request for an invalid domain for this connection.
        request = "" + "GET /ctx/path HTTP/1.1\r\n" + "Host: www.example.com\r\n" + "\r\n";
        output.write(request.getBytes(StandardCharsets.UTF_8));
        output.flush();
        response = response(input);
        Assert.assertTrue(response.startsWith("HTTP/1.1 400 "));
        Assert.assertThat(response, Matchers.containsString("Host does not match SNI"));
    } finally {
        clientContextFactory.stop();
    }
}
Also used : SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) SSLParameters(javax.net.ssl.SSLParameters) SNIHostName(javax.net.ssl.SNIHostName) InputStream(java.io.InputStream) SSLSocket(javax.net.ssl.SSLSocket) OutputStream(java.io.OutputStream) Matchers.containsString(org.hamcrest.Matchers.containsString) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) Test(org.junit.Test)

Example 37 with InputStream

use of java.io.InputStream in project jetty.project by eclipse.

the class SniSslConnectionFactoryTest method testSameConnectionRequestsForManyDomains.

@Test
public void testSameConnectionRequestsForManyDomains() throws Exception {
    SslContextFactory clientContextFactory = new SslContextFactory(true);
    clientContextFactory.start();
    SSLSocketFactory factory = clientContextFactory.getSslContext().getSocketFactory();
    try (SSLSocket sslSocket = (SSLSocket) factory.createSocket("127.0.0.1", _port)) {
        SNIHostName serverName = new SNIHostName("m.san.com");
        SSLParameters params = sslSocket.getSSLParameters();
        params.setServerNames(Collections.singletonList(serverName));
        sslSocket.setSSLParameters(params);
        sslSocket.startHandshake();
        // The first request binds the socket to an alias.
        String request = "" + "GET /ctx/path HTTP/1.1\r\n" + "Host: m.san.com\r\n" + "\r\n";
        OutputStream output = sslSocket.getOutputStream();
        output.write(request.getBytes(StandardCharsets.UTF_8));
        output.flush();
        InputStream input = sslSocket.getInputStream();
        String response = response(input);
        Assert.assertTrue(response.startsWith("HTTP/1.1 200 "));
        // Same socket, send a request for a different domain but same alias.
        request = "" + "GET /ctx/path HTTP/1.1\r\n" + "Host: www.san.com\r\n" + "\r\n";
        output.write(request.getBytes(StandardCharsets.UTF_8));
        output.flush();
        response = response(input);
        Assert.assertTrue(response.startsWith("HTTP/1.1 200 "));
        // Same socket, send a request for a different domain but different alias.
        request = "" + "GET /ctx/path HTTP/1.1\r\n" + "Host: www.example.com\r\n" + "\r\n";
        output.write(request.getBytes(StandardCharsets.UTF_8));
        output.flush();
        response = response(input);
        assertThat(response, startsWith("HTTP/1.1 400 "));
        assertThat(response, containsString("Host does not match SNI"));
    } finally {
        clientContextFactory.stop();
    }
}
Also used : SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) SSLParameters(javax.net.ssl.SSLParameters) SNIHostName(javax.net.ssl.SNIHostName) InputStream(java.io.InputStream) SSLSocket(javax.net.ssl.SSLSocket) OutputStream(java.io.OutputStream) Matchers.containsString(org.hamcrest.Matchers.containsString) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) Test(org.junit.Test)

Example 38 with InputStream

use of java.io.InputStream in project jetty.project by eclipse.

the class ResourceTest method testResourceContent.

@Test
public void testResourceContent() throws Exception {
    assumeThat(data.content, notNullValue());
    InputStream in = data.resource.getInputStream();
    String c = IO.toString(in);
    assertThat("Content: " + data.test, c, startsWith(data.content));
}
Also used : InputStream(java.io.InputStream) Test(org.junit.Test)

Example 39 with InputStream

use of java.io.InputStream in project jetty.project by eclipse.

the class SslContextFactoryTest method testNoTsSetKs.

@Test
public void testNoTsSetKs() throws Exception {
    KeyStore ks = KeyStore.getInstance("JKS");
    try (InputStream keystoreInputStream = this.getClass().getResourceAsStream("keystore")) {
        ks.load(keystoreInputStream, "storepwd".toCharArray());
    }
    cf.setKeyStore(ks);
    cf.setKeyManagerPassword("keypwd");
    cf.start();
    assertTrue(cf.getSslContext() != null);
}
Also used : InputStream(java.io.InputStream) KeyStore(java.security.KeyStore) Test(org.junit.Test)

Example 40 with InputStream

use of java.io.InputStream in project jetty.project by eclipse.

the class JarScanner method matched.

public void matched(URI uri) throws Exception {
    LOG.debug("Search of {}", uri);
    if (uri.toString().toLowerCase(Locale.ENGLISH).endsWith(".jar")) {
        InputStream in = Resource.newResource(uri).getInputStream();
        if (in == null)
            return;
        JarInputStream jar_in = new JarInputStream(in);
        try {
            JarEntry entry = jar_in.getNextJarEntry();
            while (entry != null) {
                processEntry(uri, entry);
                entry = jar_in.getNextJarEntry();
            }
        } finally {
            jar_in.close();
        }
    }
}
Also used : JarInputStream(java.util.jar.JarInputStream) JarInputStream(java.util.jar.JarInputStream) InputStream(java.io.InputStream) JarEntry(java.util.jar.JarEntry)

Aggregations

InputStream (java.io.InputStream)33681 IOException (java.io.IOException)12617 ByteArrayInputStream (java.io.ByteArrayInputStream)7643 Test (org.junit.Test)7105 FileInputStream (java.io.FileInputStream)6956 File (java.io.File)5058 InputStreamReader (java.io.InputStreamReader)3045 URL (java.net.URL)3010 OutputStream (java.io.OutputStream)2893 BufferedInputStream (java.io.BufferedInputStream)2558 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2228 ArrayList (java.util.ArrayList)2193 FileOutputStream (java.io.FileOutputStream)2191 BufferedReader (java.io.BufferedReader)2039 Properties (java.util.Properties)1679 FileNotFoundException (java.io.FileNotFoundException)1503 HashMap (java.util.HashMap)1264 HttpURLConnection (java.net.HttpURLConnection)1144 Map (java.util.Map)866 Document (org.w3c.dom.Document)847