jdk/test/com/sun/net/httpserver/bugs/B6744329.java
author never
Mon, 12 Jul 2010 22:27:18 -0700
changeset 5926 a36f90d986b6
parent 5506 202f599c92aa
permissions -rw-r--r--
6968385: malformed xml in sweeper logging Reviewed-by: kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1237
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1341
diff changeset
     2
 * Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved.
1237
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
     4
 *
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
     8
 *
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    13
 * accompanied this code).
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    14
 *
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1341
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1341
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1341
diff changeset
    21
 * questions.
1237
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    22
 */
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    23
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    24
/**
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    25
 * @test
1332
ace3e86ab10e 6750364: Error in test for 6744329
michaelm
parents: 1237
diff changeset
    26
 * @bug 6744329
1237
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    27
 * @summary  Exception in light weight Http server
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    28
 */
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    29
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    30
import com.sun.net.httpserver.*;
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    31
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    32
import java.util.*;
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    33
import java.util.concurrent.*;
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    34
import java.io.*;
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    35
import java.net.*;
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    36
import java.security.*;
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    37
import java.security.cert.*;
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    38
import javax.net.ssl.*;
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    39
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    40
public class B6744329 {
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    41
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    42
    public static void main (String[] args) throws Exception {
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    43
        Handler handler = new Handler();
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    44
        InetSocketAddress addr = new InetSocketAddress (0);
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    45
        HttpServer server = HttpServer.create (addr, 0);
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    46
        HttpContext ctx = server.createContext ("/test", handler);
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    47
        ExecutorService executor = Executors.newCachedThreadPool();
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    48
        server.setExecutor (executor);
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    49
        server.start ();
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    50
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    51
        URL url = new URL ("http://localhost:"+server.getAddress().getPort()+"/test/foo.html");
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    52
        HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    53
        try {
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    54
            InputStream is = urlc.getInputStream();
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    55
            int c = 0;
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    56
            while (is.read()!= -1) {
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    57
                c ++;
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    58
            }
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    59
            System.out.println ("OK");
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    60
        } catch (IOException e) {
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    61
            System.out.println ("exception");
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    62
            error = true;
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    63
        }
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    64
        server.stop(2);
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    65
        executor.shutdown();
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    66
        if (error) {
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    67
            throw new RuntimeException ("Test failed");
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    68
        }
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    69
    }
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    70
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    71
    public static boolean error = false;
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    72
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    73
    /* this must be the same size as in ChunkedOutputStream.java
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    74
     */
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    75
    final static int CHUNK_SIZE = 4096;
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    76
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    77
    static class Handler implements HttpHandler {
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    78
        int invocation = 1;
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    79
        public void handle (HttpExchange t)
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    80
            throws IOException
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    81
        {
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    82
            InputStream is = t.getRequestBody();
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    83
            Headers map = t.getRequestHeaders();
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    84
            Headers rmap = t.getResponseHeaders();
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    85
            while (is.read () != -1) ;
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    86
            is.close();
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    87
            /* chunked response */
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    88
            t.sendResponseHeaders (200, 0);
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    89
            OutputStream os = t.getResponseBody();
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    90
            byte[] first = new byte [CHUNK_SIZE * 2];
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    91
            byte[] second = new byte [2];
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    92
            os.write (first);
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    93
            os.write ('x');
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    94
            os.write ('x');
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    95
            /* An index out of bounds exception will be thrown
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    96
             * below, which is caught by server, and connection
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    97
             * will be closed. resulting in IOException to client
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    98
             * - if bug present
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
    99
             */
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
   100
            os.write ('x');
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
   101
            os.write ('x');
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
   102
            os.write ('x');
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
   103
            t.close();
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
   104
        }
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
   105
    }
e04c7e52d38f 6744329: Exception in light weight http server code
michaelm
parents:
diff changeset
   106
}