test/jdk/sun/net/www/protocol/http/CloseOptionHeader.java
author dfuchs
Thu, 09 May 2019 14:23:52 +0100
changeset 54787 7748aa47b4e2
parent 47216 71c04702a3d5
permissions -rw-r--r--
8223573: Replace wildcard address with loopback or local host in tests - part 4 Summary: Makes a few intermittently failing tests more resilient to port reuse issues by ensuring they bind to the loopback address instead of the wildcard. Reviewed-by: alanb, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
54787
7748aa47b4e2 8223573: Replace wildcard address with loopback or local host in tests - part 4
dfuchs
parents: 47216
diff changeset
     2
 * Copyright (c) 2004, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 6189206
30820
0d4717a011d3 8081347: Add @modules to jdk_core tests
mchung
parents: 5506
diff changeset
    27
 * @modules java.base/sun.net.www
54787
7748aa47b4e2 8223573: Replace wildcard address with loopback or local host in tests - part 4
dfuchs
parents: 47216
diff changeset
    28
 * @library /test/lib
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @run main/othervm -Dhttp.keepAlive=false CloseOptionHeader
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @summary  HTTP client should set "Connection: close" header in request when keepalive is disabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.net.www.MessageHeader;
54787
7748aa47b4e2 8223573: Replace wildcard address with loopback or local host in tests - part 4
dfuchs
parents: 47216
diff changeset
    37
import jdk.test.lib.net.URIBuilder;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public class CloseOptionHeader implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    static ServerSocket ss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    static boolean hasCloseHeader = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
     * "Our" http server
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            Socket s = ss.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            /* check the request to find close connection option header */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            InputStream is = s.getInputStream ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            MessageHeader mh = new MessageHeader(is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            String connHeader = mh.findValue("Connection");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            if (connHeader != null && connHeader.equalsIgnoreCase("close")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                hasCloseHeader = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            PrintStream out = new PrintStream(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                                 new BufferedOutputStream(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                                    s.getOutputStream() ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            /* response 200 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            out.print("HTTP/1.1 200 OK\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            out.print("Content-Type: text/html; charset=iso-8859-1\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            out.print("Content-Length: 0\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            out.print("Connection: close\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            out.print("\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            out.print("\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            s.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            ss.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        Thread tester = new Thread(new CloseOptionHeader());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        /* start the server */
54787
7748aa47b4e2 8223573: Replace wildcard address with loopback or local host in tests - part 4
dfuchs
parents: 47216
diff changeset
    83
        InetAddress loopback = InetAddress.getLoopbackAddress();
7748aa47b4e2 8223573: Replace wildcard address with loopback or local host in tests - part 4
dfuchs
parents: 47216
diff changeset
    84
        ss = new ServerSocket();
7748aa47b4e2 8223573: Replace wildcard address with loopback or local host in tests - part 4
dfuchs
parents: 47216
diff changeset
    85
        ss.bind(new InetSocketAddress(loopback, 0));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        tester.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        /* connect to the server just started
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
         * server then check the request to see whether
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
         * there is a close connection option header in it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
         */
54787
7748aa47b4e2 8223573: Replace wildcard address with loopback or local host in tests - part 4
dfuchs
parents: 47216
diff changeset
    92
        URL url = URIBuilder.newBuilder()
7748aa47b4e2 8223573: Replace wildcard address with loopback or local host in tests - part 4
dfuchs
parents: 47216
diff changeset
    93
            .scheme("http")
7748aa47b4e2 8223573: Replace wildcard address with loopback or local host in tests - part 4
dfuchs
parents: 47216
diff changeset
    94
            .host(ss.getInetAddress())
7748aa47b4e2 8223573: Replace wildcard address with loopback or local host in tests - part 4
dfuchs
parents: 47216
diff changeset
    95
            .port(ss.getLocalPort())
7748aa47b4e2 8223573: Replace wildcard address with loopback or local host in tests - part 4
dfuchs
parents: 47216
diff changeset
    96
            .toURL();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        HttpURLConnection huc = (HttpURLConnection)url.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        huc.connect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        huc.getResponseCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        huc.disconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        tester.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        if (!hasCloseHeader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            throw new RuntimeException("Test failed : should see 'close' connection header");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
}