Search in sources :

Example 26 with InputStream

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

the class HttpServerTestBase method testBlockingWhileWritingResponseContent.

@Test
public void testBlockingWhileWritingResponseContent() throws Exception {
    configureServer(new DataHandler());
    long start = System.currentTimeMillis();
    int total = 0;
    try (Socket client = newSocket(_serverURI.getHost(), _serverURI.getPort())) {
        OutputStream os = client.getOutputStream();
        InputStream is = client.getInputStream();
        os.write(("GET /data?writes=256&block=1024 HTTP/1.1\r\n" + "host: " + _serverURI.getHost() + ":" + _serverURI.getPort() + "\r\n" + "connection: close\r\n" + "content-type: unknown\r\n" + "\r\n").getBytes());
        os.flush();
        int len = 0;
        byte[] buf = new byte[1024 * 32];
        int sleeps = 0;
        while (len >= 0) {
            len = is.read(buf);
            if (len > 0) {
                total += len;
                if ((total / 10240) > sleeps) {
                    Thread.sleep(200);
                    sleeps++;
                }
            }
        }
        assertTrue(total > (256 * 1024));
        assertTrue(30000L > (System.currentTimeMillis() - start));
    }
}
Also used : ServletInputStream(javax.servlet.ServletInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ServletOutputStream(javax.servlet.ServletOutputStream) OutputStream(java.io.OutputStream) EndPoint(org.eclipse.jetty.io.EndPoint) Socket(java.net.Socket) Test(org.junit.Test)

Example 27 with InputStream

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

the class HttpServerTestBase method testBlockingWhileReadingRequestContent.

@Test
public void testBlockingWhileReadingRequestContent() throws Exception {
    configureServer(new DataHandler());
    long start = System.currentTimeMillis();
    try (Socket client = newSocket(_serverURI.getHost(), _serverURI.getPort())) {
        OutputStream os = client.getOutputStream();
        InputStream is = client.getInputStream();
        os.write(("GET /data?writes=1024&block=256 HTTP/1.1\r\n" + "host: " + _serverURI.getHost() + ":" + _serverURI.getPort() + "\r\n" + "connection: close\r\n" + "content-type: unknown\r\n" + "content-length: 30\r\n" + "\r\n").getBytes());
        os.flush();
        Thread.sleep(200);
        os.write(("\r\n23456890").getBytes());
        os.flush();
        Thread.sleep(1000);
        os.write(("abcdefghij").getBytes());
        os.flush();
        Thread.sleep(1000);
        os.write(("0987654321\r\n").getBytes());
        os.flush();
        int total = 0;
        int len = 0;
        byte[] buf = new byte[1024 * 64];
        int sleeps = 0;
        while (len >= 0) {
            len = is.read(buf);
            if (len > 0) {
                total += len;
                if ((total / 10240) > sleeps) {
                    sleeps++;
                    Thread.sleep(100);
                }
            }
        }
        assertTrue(total > (1024 * 256));
        assertTrue(30000L > (System.currentTimeMillis() - start));
    }
}
Also used : ServletInputStream(javax.servlet.ServletInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ServletOutputStream(javax.servlet.ServletOutputStream) OutputStream(java.io.OutputStream) Socket(java.net.Socket) EndPoint(org.eclipse.jetty.io.EndPoint) Test(org.junit.Test)

Example 28 with InputStream

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

the class HttpServerTestBase method testHead.

@Test
public void testHead() throws Exception {
    configureServer(new EchoHandler(false));
    try (Socket client = newSocket(_serverURI.getHost(), _serverURI.getPort())) {
        OutputStream os = client.getOutputStream();
        InputStream is = client.getInputStream();
        os.write(("POST /R1 HTTP/1.1\015\012" + "Host: " + _serverURI.getHost() + ":" + _serverURI.getPort() + "\r\n" + "content-type: text/plain; charset=utf-8\r\n" + "content-length: 10\r\n" + "\015\012" + "123456789\n" + "HEAD /R2 HTTP/1.1\015\012" + "Host: " + _serverURI.getHost() + ":" + _serverURI.getPort() + "\015\012" + "content-type: text/plain; charset=utf-8\r\n" + "content-length: 10\r\n" + "\015\012" + "ABCDEFGHI\n" + "POST /R3 HTTP/1.1\015\012" + "Host: " + _serverURI.getHost() + ":" + _serverURI.getPort() + "\015\012" + "content-type: text/plain; charset=utf-8\r\n" + "content-length: 10\r\n" + "Connection: close\015\012" + "\015\012" + "abcdefghi\n").getBytes(StandardCharsets.ISO_8859_1));
        String in = IO.toString(is);
        Assert.assertThat(in, containsString("123456789"));
        Assert.assertThat(in, not(containsString("ABCDEFGHI")));
        Assert.assertThat(in, containsString("abcdefghi"));
    }
}
Also used : ServletInputStream(javax.servlet.ServletInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ServletOutputStream(javax.servlet.ServletOutputStream) OutputStream(java.io.OutputStream) Matchers.containsString(org.hamcrest.Matchers.containsString) Socket(java.net.Socket) Test(org.junit.Test)

Example 29 with InputStream

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

the class HttpServerTestBase method testRecycledWriters.

@Test
public void testRecycledWriters() throws Exception {
    configureServer(new EchoHandler());
    try (Socket client = newSocket(_serverURI.getHost(), _serverURI.getPort())) {
        OutputStream os = client.getOutputStream();
        InputStream is = client.getInputStream();
        os.write(("POST /echo?charset=utf-8 HTTP/1.1\r\n" + "host: " + _serverURI.getHost() + ":" + _serverURI.getPort() + "\r\n" + "content-type: text/plain; charset=utf-8\r\n" + "content-length: 10\r\n" + "\r\n").getBytes(StandardCharsets.ISO_8859_1));
        os.write(("123456789\n").getBytes("utf-8"));
        os.write(("POST /echo?charset=utf-8 HTTP/1.1\r\n" + "host: " + _serverURI.getHost() + ":" + _serverURI.getPort() + "\r\n" + "content-type: text/plain; charset=utf-8\r\n" + "content-length: 10\r\n" + "\r\n").getBytes(StandardCharsets.ISO_8859_1));
        os.write(("abcdefghZ\n").getBytes("utf-8"));
        String content = "Wibble";
        byte[] contentB = content.getBytes("utf-8");
        os.write(("POST /echo?charset=utf-16 HTTP/1.1\r\n" + "host: " + _serverURI.getHost() + ":" + _serverURI.getPort() + "\r\n" + "content-type: text/plain; charset=utf-8\r\n" + "content-length: " + contentB.length + "\r\n" + "connection: close\r\n" + "\r\n").getBytes(StandardCharsets.ISO_8859_1));
        os.write(contentB);
        os.flush();
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        IO.copy(is, bout);
        byte[] b = bout.toByteArray();
        //System.err.println("OUTPUT: "+new String(b));
        int i = 0;
        while (b[i] != 'Z') i++;
        int state = 0;
        while (state != 4) {
            switch(b[i++]) {
                case '\r':
                    if (state == 0 || state == 2)
                        state++;
                    continue;
                case '\n':
                    if (state == 1 || state == 3)
                        state++;
                    continue;
                default:
                    state = 0;
            }
        }
        String in = new String(b, 0, i, StandardCharsets.UTF_8);
        assertThat(in, containsString("123456789"));
        assertThat(in, containsString("abcdefghZ"));
        assertFalse(in.contains("Wibble"));
        in = new String(b, i, b.length - i, StandardCharsets.UTF_16);
        assertEquals("Wibble\n", in);
    }
}
Also used : ServletInputStream(javax.servlet.ServletInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ServletOutputStream(javax.servlet.ServletOutputStream) OutputStream(java.io.OutputStream) Matchers.containsString(org.hamcrest.Matchers.containsString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Socket(java.net.Socket) EndPoint(org.eclipse.jetty.io.EndPoint) Test(org.junit.Test)

Example 30 with InputStream

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

the class HttpServerTestBase method testCommittedError.

@Test
public void testCommittedError() throws Exception {
    CommittedErrorHandler handler = new CommittedErrorHandler();
    configureServer(handler);
    try (Socket client = newSocket(_serverURI.getHost(), _serverURI.getPort());
        StacklessLogging stackless = new StacklessLogging(HttpChannel.class)) {
        ((AbstractLogger) Log.getLogger(HttpChannel.class)).info("Expecting exception after commit then could not send 500....");
        OutputStream os = client.getOutputStream();
        InputStream is = client.getInputStream();
        // Send a request
        os.write(("GET / HTTP/1.1\r\n" + "Host: " + _serverURI.getHost() + ":" + _serverURI.getPort() + "\r\n" + "\r\n").getBytes());
        os.flush();
        client.setSoTimeout(2000);
        String in = IO.toString(is);
        // Closed by error!
        assertEquals(-1, is.read());
        assertThat(in, containsString("HTTP/1.1 200 OK"));
        assertTrue(in.indexOf("Transfer-Encoding: chunked") > 0);
        assertTrue(in.indexOf("Now is the time for all good men to come to the aid of the party") > 0);
        assertThat(in, Matchers.not(Matchers.containsString("\r\n0\r\n")));
        client.close();
        Thread.sleep(200);
        assertTrue(!handler._endp.isOpen());
    }
}
Also used : ServletInputStream(javax.servlet.ServletInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ServletOutputStream(javax.servlet.ServletOutputStream) OutputStream(java.io.OutputStream) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) Matchers.containsString(org.hamcrest.Matchers.containsString) Socket(java.net.Socket) AbstractLogger(org.eclipse.jetty.util.log.AbstractLogger) Test(org.junit.Test)

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