jdk/test/com/sun/net/httpserver/bugs/B6886436.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:
4047
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4047
diff changeset
     2
 * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
4047
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
     4
 *
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
     8
 *
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    13
 * accompanied this code).
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    14
 *
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4047
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4047
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4047
diff changeset
    21
 * questions.
4047
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    22
 */
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    23
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    24
/**
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    25
 * @test
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    26
 * @bug 6886436
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    27
 * @summary
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    28
 */
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    29
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    30
import com.sun.net.httpserver.*;
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    31
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    32
import java.util.*;
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    33
import java.util.concurrent.*;
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    34
import java.util.logging.*;
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    35
import java.io.*;
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    36
import java.net.*;
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    37
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    38
public class B6886436 {
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    39
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    40
    public static void main (String[] args) throws Exception {
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    41
        Logger logger = Logger.getLogger ("com.sun.net.httpserver");
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    42
        ConsoleHandler c = new ConsoleHandler();
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    43
        c.setLevel (Level.WARNING);
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    44
        logger.addHandler (c);
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    45
        logger.setLevel (Level.WARNING);
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    46
        Handler handler = new Handler();
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    47
        InetSocketAddress addr = new InetSocketAddress (0);
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    48
        HttpServer server = HttpServer.create (addr, 0);
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    49
        HttpContext ctx = server.createContext ("/test", handler);
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    50
        ExecutorService executor = Executors.newCachedThreadPool();
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    51
        server.setExecutor (executor);
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    52
        server.start ();
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    53
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    54
        URL url = new URL ("http://localhost:"+server.getAddress().getPort()+"/test/foo.html");
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    55
        HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    56
        try {
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    57
            InputStream is = urlc.getInputStream();
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    58
            while (is.read()!= -1) ;
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    59
            is.close ();
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    60
            urlc = (HttpURLConnection)url.openConnection ();
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    61
            urlc.setReadTimeout (3000);
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    62
            is = urlc.getInputStream();
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    63
            while (is.read()!= -1);
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    64
            is.close ();
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    65
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    66
        } catch (IOException e) {
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    67
            server.stop(2);
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    68
            executor.shutdown();
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    69
            throw new RuntimeException ("Test failed");
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    70
        }
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    71
        server.stop(2);
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    72
        executor.shutdown();
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    73
        System.out.println ("OK");
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    74
    }
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    75
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    76
    public static boolean error = false;
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    77
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    78
    static class Handler implements HttpHandler {
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    79
        int invocation = 1;
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    80
        public void handle (HttpExchange t)
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    81
            throws IOException
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    82
        {
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    83
            InputStream is = t.getRequestBody();
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    84
            Headers map = t.getRequestHeaders();
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    85
            Headers rmap = t.getResponseHeaders();
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    86
            while (is.read () != -1) ;
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    87
            is.close();
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    88
            // send a 204 response with an empty chunked body
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    89
            t.sendResponseHeaders (204, 0);
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    90
            t.close();
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    91
        }
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    92
    }
f5dcf30f9206 6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff changeset
    93
}