jdk/test/sun/net/www/protocol/http/6550798/test.java
author michaelm
Tue, 28 Sep 2010 11:59:57 +0100
changeset 6685 36f070045e17
child 38557 5c485e1ea6fa
permissions -rw-r--r--
6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6685
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
     1
/*
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
     2
 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
     4
 *
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
     8
 *
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    13
 * accompanied this code).
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    14
 *
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    18
 *
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    21
 * questions.
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    22
 */
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    23
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    24
/**
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    25
 * @test
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    26
 * @bug 6550798
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    27
 * @summary Using InputStream.skip with ResponseCache will cause partial data to be cached
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    28
 * @run main/othervm test
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    29
 */
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    30
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    31
import java.net.*;
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    32
import com.sun.net.httpserver.*;
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    33
import java.io.*;
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    34
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    35
public class test {
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    36
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    37
    final static int LEN = 16 * 1024;
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    38
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    39
    public static void main(String[] args)  throws Exception {
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    40
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    41
        TestCache.reset();
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    42
        HttpServer s = HttpServer.create (new InetSocketAddress(0), 10);
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    43
        s.createContext ("/", new HttpHandler () {
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    44
            public void handle (HttpExchange e) {
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    45
                try {
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    46
                    byte[] buf = new byte [LEN];
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    47
                    OutputStream o = e.getResponseBody();
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    48
                    e.sendResponseHeaders(200, LEN);
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    49
                    o.write (buf);
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    50
                    e.close();
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    51
                } catch (IOException ex) {
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    52
                    ex.printStackTrace();
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    53
                    TestCache.fail = true;
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    54
                }
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    55
            }
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    56
        });
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    57
        s.start();
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    58
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    59
        System.out.println("http request with cache hander");
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    60
        URL u = new URL("http://127.0.0.1:"+s.getAddress().getPort()+"/f");
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    61
        URLConnection conn = u.openConnection();
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    62
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    63
        InputStream is = null;
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    64
        try {
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    65
            // this calls into TestCache.get
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    66
            byte[] buf = new byte[8192];
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    67
            is = new BufferedInputStream(conn.getInputStream());
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    68
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    69
            is.skip(1000);
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    70
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    71
            while (is.read(buf) != -1) {
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    72
            }
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    73
        } finally {
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    74
            if (is != null) {
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    75
                // this calls into TestCache.put
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    76
                // TestCache.put will check if the resource
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    77
                // should be cached
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    78
                is.close();
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    79
            }
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    80
            s.stop(0);
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    81
        }
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    82
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    83
        if (TestCache.fail) {
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    84
            System.out.println ("TEST FAILED");
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    85
            throw new RuntimeException ();
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    86
        } else {
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    87
            System.out.println ("TEST OK");
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    88
        }
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    89
    }
36f070045e17 6550798: Using InputStream.skip with ResponseCache will cause partial data to be cached
michaelm
parents:
diff changeset
    90
}