jdk/test/com/sun/net/httpserver/Test10.java
author katleman
Wed, 25 May 2011 13:32:36 -0700
changeset 9734 b33a24d77590
parent 7537 b7d2d2428f31
child 14342 8435a30053c1
permissions -rw-r--r--
7044486: open jdk repos have files with incorrect copyright headers, which can end up in src bundles Reviewed-by: ohair, trims
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7537
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
     1
/*
9734
b33a24d77590 7044486: open jdk repos have files with incorrect copyright headers, which can end up in src bundles
katleman
parents: 7537
diff changeset
     2
 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
7537
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
     4
 *
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
     8
 *
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    13
 * accompanied this code).
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    14
 *
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    18
 *
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    21
 * questions.
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    22
 */
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    23
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    24
/**
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    25
 * @test
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    26
 * @bug 7005016
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    27
 * @summary  pit jdk7 b121  sqe test jhttp/HttpServer150013 failing
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    28
 * @run main/othervm -Dsun.net.httpserver.clockTick=1000 -Dsun.net.httpserver.idleInterval=3 Test10
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    29
 */
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    30
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    31
import com.sun.net.httpserver.*;
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    32
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    33
import java.io.*;
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    34
import java.net.*;
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    35
import java.util.concurrent.*;
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    36
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    37
/*
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    38
 * Test handling of empty Http headers
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    39
 */
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    40
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    41
public class Test10 extends Test {
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    42
    public static void main (String[] args) throws Exception {
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    43
        System.out.print ("Test10: ");
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    44
        Handler handler = new Handler();
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    45
        InetSocketAddress addr = new InetSocketAddress (0);
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    46
        HttpServer server = HttpServer.create (addr, 0);
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    47
        int port = server.getAddress().getPort();
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    48
        HttpContext c2 = server.createContext ("/test", handler);
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    49
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    50
        ExecutorService exec = Executors.newCachedThreadPool();
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    51
        server.setExecutor (exec);
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    52
        try {
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    53
            server.start ();
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    54
            doClient(port);
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    55
            System.out.println ("OK");
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    56
        } finally {
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    57
            delay();
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    58
            if (server != null)
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    59
                server.stop(2);
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    60
            if (exec != null)
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    61
                exec.shutdown();
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    62
        }
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    63
    }
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    64
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    65
    static class Handler implements HttpHandler {
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    66
        volatile int invocation = 0;
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    67
        public void handle (HttpExchange t)
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    68
            throws IOException
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    69
        {
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    70
            InputStream is = t.getRequestBody();
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    71
            while (is.read() != -1);
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    72
            Headers map = t.getRequestHeaders();
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    73
            t.sendResponseHeaders (200, -1);
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    74
            t.close();
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    75
        }
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    76
    }
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    77
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    78
    public static void doClient (int port) throws Exception {
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    79
        String s = "GET /test/1.html HTTP/1.1\r\n\r\n";
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    80
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    81
        Socket socket = new Socket ("localhost", port);
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    82
        OutputStream os = socket.getOutputStream();
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    83
        os.write (s.getBytes());
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    84
        socket.setSoTimeout (10 * 1000);
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    85
        InputStream is = socket.getInputStream();
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    86
        int c;
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    87
        byte[] b = new byte [1024];
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    88
        while ((c=is.read(b)) != -1) ;
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    89
        is.close();
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    90
        socket.close();
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    91
    }
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents:
diff changeset
    92
}