Search in sources :

Example 96 with Socket

use of java.net.Socket in project jetty.project by eclipse.

the class RFC2616BaseTest method test8_2_ExpectNormal.

/**
     * Test Message Transmission Requirements
     * 
     * @see <a href="http://tools.ietf.org/html/rfc2616#section-8.2">RFC 2616 (section 8.2)</a>
     */
@Test
public void test8_2_ExpectNormal() throws Exception {
    // Expect 100
    StringBuffer req4 = new StringBuffer();
    req4.append("GET /echo/R1 HTTP/1.1\n");
    req4.append("Host: localhost\n");
    req4.append("Connection: close\n");
    // Valid Expect header.
    req4.append("Expect: 100-continue\n");
    req4.append("Content-Type: text/plain\n");
    req4.append("Content-Length: 7\n");
    // No body
    req4.append("\n");
    Socket sock = http.open();
    try {
        http.send(sock, req4);
        http.setTimeoutMillis(2000);
        HttpTester.Response response = http.readAvailable(sock);
        assertEquals("8.2.3 expect 100", HttpStatus.CONTINUE_100, response.getStatus());
        // Now send the data
        http.send(sock, "654321\n");
        response = http.read(sock);
        assertEquals("8.2.3 expect 100", HttpStatus.OK_200, response.getStatus());
        assertThat("8.2.3 expect 100", response.getContent(), Matchers.containsString("654321\n"));
    } finally {
        http.close(sock);
    }
}
Also used : Socket(java.net.Socket) HttpSocket(org.eclipse.jetty.test.support.rawhttp.HttpSocket) HttpTester(org.eclipse.jetty.http.HttpTester) Test(org.junit.Test)

Example 97 with Socket

use of java.net.Socket in project jetty.project by eclipse.

the class AsyncRequestReadTest method testPartialReadThenClose.

@Test
public void testPartialReadThenClose() throws Exception {
    server.setHandler(new PartialReaderHandler());
    server.start();
    try (final Socket socket = new Socket("localhost", connector.getLocalPort())) {
        socket.setSoTimeout(1000);
        byte[] content = new byte[32 * 4096];
        Arrays.fill(content, (byte) 88);
        OutputStream out = socket.getOutputStream();
        String header = "POST /?read=10 HTTP/1.1\r\n" + "Host: localhost\r\n" + "Content-Length: " + content.length + "\r\n" + "Content-Type: bytes\r\n" + "\r\n";
        byte[] h = header.getBytes(StandardCharsets.ISO_8859_1);
        out.write(h);
        out.write(content, 0, 4096);
        out.flush();
        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        assertThat(in.readLine(), containsString("HTTP/1.1 200 OK"));
        assertThat(in.readLine(), containsString("Content-Length:"));
        assertThat(in.readLine(), containsString("Server:"));
        in.readLine();
        assertThat(in.readLine(), containsString("XXXXXXX"));
        socket.close();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) OutputStream(java.io.OutputStream) BufferedReader(java.io.BufferedReader) Matchers.containsString(org.hamcrest.Matchers.containsString) Socket(java.net.Socket) Test(org.junit.Test)

Example 98 with Socket

use of java.net.Socket in project jetty.project by eclipse.

the class AsyncRequestReadTest method testPartialRead.

@Test
public void testPartialRead() throws Exception {
    server.setHandler(new PartialReaderHandler());
    server.start();
    try (final Socket socket = new Socket("localhost", connector.getLocalPort())) {
        socket.setSoTimeout(10000);
        byte[] content = new byte[32 * 4096];
        Arrays.fill(content, (byte) 88);
        OutputStream out = socket.getOutputStream();
        String header = "POST /?read=10 HTTP/1.1\r\n" + "Host: localhost\r\n" + "Content-Length: " + content.length + "\r\n" + "Content-Type: bytes\r\n" + "\r\n";
        byte[] h = header.getBytes(StandardCharsets.ISO_8859_1);
        out.write(h);
        out.write(content);
        header = "POST /?read=10 HTTP/1.1\r\n" + "Host: localhost\r\n" + "Content-Length: " + content.length + "\r\n" + "Content-Type: bytes\r\n" + "Connection: close\r\n" + "\r\n";
        h = header.getBytes(StandardCharsets.ISO_8859_1);
        out.write(h);
        out.write(content);
        out.flush();
        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        assertThat(in.readLine(), containsString("HTTP/1.1 200 OK"));
        assertThat(in.readLine(), containsString("Content-Length: 11"));
        assertThat(in.readLine(), containsString("Server:"));
        in.readLine();
        assertThat(in.readLine(), containsString("XXXXXXX"));
        assertThat(in.readLine(), containsString("HTTP/1.1 200 OK"));
        assertThat(in.readLine(), containsString("Connection: close"));
        assertThat(in.readLine(), containsString("Content-Length: 11"));
        assertThat(in.readLine(), containsString("Server:"));
        in.readLine();
        assertThat(in.readLine(), containsString("XXXXXXX"));
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) OutputStream(java.io.OutputStream) BufferedReader(java.io.BufferedReader) Matchers.containsString(org.hamcrest.Matchers.containsString) Socket(java.net.Socket) Test(org.junit.Test)

Example 99 with Socket

use of java.net.Socket in project jetty.project by eclipse.

the class AsyncRequestReadTest method testPipelined.

@Test
public void testPipelined() throws Exception {
    server.setHandler(new AsyncStreamHandler());
    server.start();
    try (final Socket socket = new Socket("localhost", connector.getLocalPort())) {
        socket.setSoTimeout(1000);
        byte[] content = new byte[32 * 4096];
        Arrays.fill(content, (byte) 120);
        OutputStream out = socket.getOutputStream();
        String header = "POST / HTTP/1.1\r\n" + "Host: localhost\r\n" + "Content-Length: " + content.length + "\r\n" + "Content-Type: bytes\r\n" + "\r\n";
        byte[] h = header.getBytes(StandardCharsets.ISO_8859_1);
        out.write(h);
        out.write(content);
        header = "POST / HTTP/1.1\r\n" + "Host: localhost\r\n" + "Content-Length: " + content.length + "\r\n" + "Content-Type: bytes\r\n" + "Connection: close\r\n" + "\r\n";
        h = header.getBytes(StandardCharsets.ISO_8859_1);
        out.write(h);
        out.write(content);
        out.flush();
        InputStream in = socket.getInputStream();
        String response = IO.toString(in);
        assertTrue(response.indexOf("200 OK") > 0);
        long total = __total.poll(5, TimeUnit.SECONDS);
        assertEquals(content.length, total);
        total = __total.poll(5, TimeUnit.SECONDS);
        assertEquals(content.length, total);
    }
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) Matchers.containsString(org.hamcrest.Matchers.containsString) Socket(java.net.Socket) Test(org.junit.Test)

Example 100 with Socket

use of java.net.Socket in project jetty.project by eclipse.

the class AsyncStressTest method doConnections.

private void doConnections(int connections, final int loops) throws Throwable {
    Socket[] socket = new Socket[connections];
    int[][] path = new int[connections][loops];
    for (int i = 0; i < connections; i++) {
        socket[i] = new Socket(_addr, _port);
        socket[i].setSoTimeout(30000);
        if (i % 10 == 0)
            Thread.sleep(50);
        if (i % 80 == 0)
            System.err.println();
        System.err.print('+');
    }
    System.err.println();
    LOG.info("Bound " + connections);
    for (int l = 0; l < loops; l++) {
        for (int i = 0; i < connections; i++) {
            int p = path[i][l] = _random.nextInt(__paths.length);
            int period = _random.nextInt(290) + 10;
            String uri = __paths[p][0].replace("<PERIOD>", Integer.toString(period));
            long start = System.currentTimeMillis();
            String request = "GET " + uri + " HTTP/1.1\r\n" + "Host: localhost\r\n" + "start: " + start + "\r\n" + "result: " + __paths[p][1] + "\r\n" + ((l + 1 < loops) ? "" : "Connection: close\r\n") + "\r\n";
            socket[i].getOutputStream().write(request.getBytes(StandardCharsets.UTF_8));
            socket[i].getOutputStream().flush();
        }
        if (l % 80 == 0)
            System.err.println();
        System.err.print('.');
        Thread.sleep(_random.nextInt(290) + 10);
    }
    System.err.println();
    LOG.info("Sent " + (loops * __paths.length) + " requests");
    String[] results = new String[connections];
    for (int i = 0; i < connections; i++) {
        results[i] = IO.toString(socket[i].getInputStream(), StandardCharsets.UTF_8);
        if (i % 80 == 0)
            System.err.println();
        System.err.print('-');
    }
    System.err.println();
    LOG.info("Read " + connections + " connections");
    for (int i = 0; i < connections; i++) {
        int offset = 0;
        String result = results[i];
        for (int l = 0; l < loops; l++) {
            String expect = __paths[path[i][l]][1];
            expect = expect + " " + expect;
            offset = result.indexOf("200 OK", offset) + 6;
            offset = result.indexOf("\r\n\r\n", offset) + 4;
            int end = result.indexOf("\n", offset);
            String r = result.substring(offset, end).trim();
            assertEquals(i + "," + l, expect, r);
            offset = end;
        }
    }
}
Also used : Socket(java.net.Socket)

Aggregations

Socket (java.net.Socket)1553 IOException (java.io.IOException)664 ServerSocket (java.net.ServerSocket)548 OutputStream (java.io.OutputStream)364 InetSocketAddress (java.net.InetSocketAddress)350 Test (org.junit.Test)348 InputStream (java.io.InputStream)249 InputStreamReader (java.io.InputStreamReader)170 BufferedReader (java.io.BufferedReader)151 SocketException (java.net.SocketException)132 SSLSocket (javax.net.ssl.SSLSocket)103 SocketTimeoutException (java.net.SocketTimeoutException)97 UnknownHostException (java.net.UnknownHostException)84 ConnectException (java.net.ConnectException)82 ByteArrayOutputStream (java.io.ByteArrayOutputStream)75 InetAddress (java.net.InetAddress)73 OutputStreamWriter (java.io.OutputStreamWriter)70 ServletOutputStream (javax.servlet.ServletOutputStream)68 DataOutputStream (java.io.DataOutputStream)67 CountDownLatch (java.util.concurrent.CountDownLatch)64