test/jdk/java/net/httpclient/UnknownBodyLengthTest.java
author chegar
Fri, 27 Sep 2019 09:55:35 +0100
changeset 58365 73950479184b
parent 54086 ccb4a50bee06
permissions -rw-r--r--
8231504: Update networking tests to avoid implicit dependency on the system proxies Reviewed-by: dfuchs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
51462
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
     1
/*
53522
40eb23e0a8c5 8210130: java/net/httpclient/UnknownBodyLengthTest.java failed
dfuchs
parents: 52121
diff changeset
     2
 * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
51462
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
     4
 *
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
     8
 *
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    13
 * accompanied this code).
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    14
 *
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    18
 *
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    21
 * questions.
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    22
 */
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    23
import java.io.InputStream;
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    24
import java.io.OutputStream;
54086
ccb4a50bee06 8220083: Use InetAddress.getLoopbackAddress() in place of 127.0.0.1 for some tests
aeubanks
parents: 53572
diff changeset
    25
import java.net.InetAddress;
51462
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    26
import java.net.InetSocketAddress;
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    27
import java.net.ServerSocket;
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    28
import java.net.Socket;
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    29
import java.net.URI;
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    30
import java.net.SocketTimeoutException;
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    31
import java.time.Duration;
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    32
import javax.net.ssl.SSLServerSocketFactory;
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    33
import javax.net.ServerSocketFactory;
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    34
import javax.net.ssl.SSLContext;
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    35
import javax.net.ssl.SSLParameters;
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    36
import java.net.http.HttpClient;
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    37
import java.net.http.HttpRequest;
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    38
import java.net.http.HttpResponse;
53572
f5480f924571 8216562: UnknownBodyLength sometimes fails due to "Connection reset by peer"
dfuchs
parents: 53522
diff changeset
    39
import java.util.List;
f5480f924571 8216562: UnknownBodyLength sometimes fails due to "Connection reset by peer"
dfuchs
parents: 53522
diff changeset
    40
import java.util.concurrent.CopyOnWriteArrayList;
f5480f924571 8216562: UnknownBodyLength sometimes fails due to "Connection reset by peer"
dfuchs
parents: 53522
diff changeset
    41
52121
934969c63223 8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents: 51462
diff changeset
    42
import jdk.test.lib.net.SimpleSSLContext;
51462
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    43
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    44
/**
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    45
 * @test
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    46
 * @bug 8207966
52121
934969c63223 8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents: 51462
diff changeset
    47
 * @library /test/lib
934969c63223 8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents: 51462
diff changeset
    48
 * @build jdk.test.lib.net.SimpleSSLContext
53522
40eb23e0a8c5 8210130: java/net/httpclient/UnknownBodyLengthTest.java failed
dfuchs
parents: 52121
diff changeset
    49
 * @run main/othervm -Djdk.httpclient.enableAllMethodRetry
40eb23e0a8c5 8210130: java/net/httpclient/UnknownBodyLengthTest.java failed
dfuchs
parents: 52121
diff changeset
    50
 *                   -Djdk.tls.acknowledgeCloseNotify=true UnknownBodyLengthTest plain false
40eb23e0a8c5 8210130: java/net/httpclient/UnknownBodyLengthTest.java failed
dfuchs
parents: 52121
diff changeset
    51
 * @run main/othervm -Djdk.httpclient.enableAllMethodRetry
40eb23e0a8c5 8210130: java/net/httpclient/UnknownBodyLengthTest.java failed
dfuchs
parents: 52121
diff changeset
    52
 *                   -Djdk.tls.acknowledgeCloseNotify=true UnknownBodyLengthTest SSL false
40eb23e0a8c5 8210130: java/net/httpclient/UnknownBodyLengthTest.java failed
dfuchs
parents: 52121
diff changeset
    53
 * @run main/othervm -Djdk.httpclient.enableAllMethodRetry
40eb23e0a8c5 8210130: java/net/httpclient/UnknownBodyLengthTest.java failed
dfuchs
parents: 52121
diff changeset
    54
 *                   -Djdk.tls.acknowledgeCloseNotify=true UnknownBodyLengthTest plain true
40eb23e0a8c5 8210130: java/net/httpclient/UnknownBodyLengthTest.java failed
dfuchs
parents: 52121
diff changeset
    55
 * @run main/othervm -Djdk.httpclient.enableAllMethodRetry
40eb23e0a8c5 8210130: java/net/httpclient/UnknownBodyLengthTest.java failed
dfuchs
parents: 52121
diff changeset
    56
 *                   -Djdk.tls.acknowledgeCloseNotify=true UnknownBodyLengthTest SSL true
51462
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    57
 */
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    58
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    59
public class UnknownBodyLengthTest {
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    60
    static final byte[] BUF = new byte[32 * 10234 + 2];
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    61
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    62
    volatile SSLContext ctx;
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    63
    volatile ServerSocketFactory factory;
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    64
    volatile String clientURL;
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    65
    volatile int port;
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    66
    final ServerSocket ss;
53572
f5480f924571 8216562: UnknownBodyLength sometimes fails due to "Connection reset by peer"
dfuchs
parents: 53522
diff changeset
    67
    final List<Socket> acceptedList = new CopyOnWriteArrayList<>();
51462
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    68
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    69
    UnknownBodyLengthTest(boolean useSSL) throws Exception {
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    70
        ctx = new SimpleSSLContext().get();
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    71
        SSLContext.setDefault(ctx);
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    72
        factory = useSSL ? SSLServerSocketFactory.getDefault()
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    73
                         : ServerSocketFactory.getDefault();
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    74
        ss = factory.createServerSocket();
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    75
        ss.setReuseAddress(true);
54086
ccb4a50bee06 8220083: Use InetAddress.getLoopbackAddress() in place of 127.0.0.1 for some tests
aeubanks
parents: 53572
diff changeset
    76
        ss.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
51462
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    77
        System.out.println("ServerSocket = " + ss.getClass() + " " + ss);
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    78
        port = ss.getLocalPort();
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    79
        clientURL = (useSSL ? "https" : "http") + "://localhost:"
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    80
            + Integer.toString(port) + "/test";
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    81
    }
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    82
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    83
    static void fillBuf(byte[] buf) {
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    84
        for (int i=0; i<buf.length; i++)
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    85
            buf[i] = (byte)i;
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    86
    }
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    87
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    88
    static void checkBuf(byte[] buf) {
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    89
        if (buf.length != BUF.length)
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    90
            throw new RuntimeException("buffer lengths not the same");
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    91
        for (int i=0; i<buf.length; i++)
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    92
            if (buf[i] != BUF[i])
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    93
                throw new RuntimeException("error at position " + i);
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    94
    }
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    95
53522
40eb23e0a8c5 8210130: java/net/httpclient/UnknownBodyLengthTest.java failed
dfuchs
parents: 52121
diff changeset
    96
    volatile boolean stopped;
40eb23e0a8c5 8210130: java/net/httpclient/UnknownBodyLengthTest.java failed
dfuchs
parents: 52121
diff changeset
    97
51462
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    98
    void server(final boolean withContentLength) {
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
    99
        fillBuf(BUF);
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
   100
        try {
53522
40eb23e0a8c5 8210130: java/net/httpclient/UnknownBodyLengthTest.java failed
dfuchs
parents: 52121
diff changeset
   101
            while (!stopped) {
53572
f5480f924571 8216562: UnknownBodyLength sometimes fails due to "Connection reset by peer"
dfuchs
parents: 53522
diff changeset
   102
                try {
f5480f924571 8216562: UnknownBodyLength sometimes fails due to "Connection reset by peer"
dfuchs
parents: 53522
diff changeset
   103
                    Socket s = ss.accept();
f5480f924571 8216562: UnknownBodyLength sometimes fails due to "Connection reset by peer"
dfuchs
parents: 53522
diff changeset
   104
                    acceptedList.add(s);
53522
40eb23e0a8c5 8210130: java/net/httpclient/UnknownBodyLengthTest.java failed
dfuchs
parents: 52121
diff changeset
   105
                    s.setTcpNoDelay(true);
53572
f5480f924571 8216562: UnknownBodyLength sometimes fails due to "Connection reset by peer"
dfuchs
parents: 53522
diff changeset
   106
                    // if we use linger=1 we still see some
f5480f924571 8216562: UnknownBodyLength sometimes fails due to "Connection reset by peer"
dfuchs
parents: 53522
diff changeset
   107
                    // intermittent failures caused by IOException
f5480f924571 8216562: UnknownBodyLength sometimes fails due to "Connection reset by peer"
dfuchs
parents: 53522
diff changeset
   108
                    // "Connection reset by peer".
f5480f924571 8216562: UnknownBodyLength sometimes fails due to "Connection reset by peer"
dfuchs
parents: 53522
diff changeset
   109
                    // The client side is expecting EOF, but gets reset instead.
f5480f924571 8216562: UnknownBodyLength sometimes fails due to "Connection reset by peer"
dfuchs
parents: 53522
diff changeset
   110
                    // 30 is a 'magic' value that may need to be adjusted again.
f5480f924571 8216562: UnknownBodyLength sometimes fails due to "Connection reset by peer"
dfuchs
parents: 53522
diff changeset
   111
                    s.setSoLinger(true, 30);
53522
40eb23e0a8c5 8210130: java/net/httpclient/UnknownBodyLengthTest.java failed
dfuchs
parents: 52121
diff changeset
   112
                    System.out.println("Accepted: " + s.getRemoteSocketAddress());
40eb23e0a8c5 8210130: java/net/httpclient/UnknownBodyLengthTest.java failed
dfuchs
parents: 52121
diff changeset
   113
                    System.out.println("Accepted: " + s);
40eb23e0a8c5 8210130: java/net/httpclient/UnknownBodyLengthTest.java failed
dfuchs
parents: 52121
diff changeset
   114
                    OutputStream os = s.getOutputStream();
40eb23e0a8c5 8210130: java/net/httpclient/UnknownBodyLengthTest.java failed
dfuchs
parents: 52121
diff changeset
   115
                    InputStream is = s.getInputStream();
40eb23e0a8c5 8210130: java/net/httpclient/UnknownBodyLengthTest.java failed
dfuchs
parents: 52121
diff changeset
   116
                    boolean done = false;
40eb23e0a8c5 8210130: java/net/httpclient/UnknownBodyLengthTest.java failed
dfuchs
parents: 52121
diff changeset
   117
                    byte[] buf = new byte[1024];
40eb23e0a8c5 8210130: java/net/httpclient/UnknownBodyLengthTest.java failed
dfuchs
parents: 52121
diff changeset
   118
                    String rsp = "";
40eb23e0a8c5 8210130: java/net/httpclient/UnknownBodyLengthTest.java failed
dfuchs
parents: 52121
diff changeset
   119
                    while (!done) {
40eb23e0a8c5 8210130: java/net/httpclient/UnknownBodyLengthTest.java failed
dfuchs
parents: 52121
diff changeset
   120
                        int c = is.read(buf);
40eb23e0a8c5 8210130: java/net/httpclient/UnknownBodyLengthTest.java failed
dfuchs
parents: 52121
diff changeset
   121
                        if (c < 0) break;
40eb23e0a8c5 8210130: java/net/httpclient/UnknownBodyLengthTest.java failed
dfuchs
parents: 52121
diff changeset
   122
                        String s1 = new String(buf, 0, c, "ISO-8859-1");
40eb23e0a8c5 8210130: java/net/httpclient/UnknownBodyLengthTest.java failed
dfuchs
parents: 52121
diff changeset
   123
                        rsp += s1;
40eb23e0a8c5 8210130: java/net/httpclient/UnknownBodyLengthTest.java failed
dfuchs
parents: 52121
diff changeset
   124
                        done = rsp.endsWith("!#!#");
40eb23e0a8c5 8210130: java/net/httpclient/UnknownBodyLengthTest.java failed
dfuchs
parents: 52121
diff changeset
   125
                    }
40eb23e0a8c5 8210130: java/net/httpclient/UnknownBodyLengthTest.java failed
dfuchs
parents: 52121
diff changeset
   126
                    String r = "HTTP/1.0 200 OK\r\nConnection: close\r\nContent-Type:" +
40eb23e0a8c5 8210130: java/net/httpclient/UnknownBodyLengthTest.java failed
dfuchs
parents: 52121
diff changeset
   127
                            " text/xml; charset=UTF-8\r\n";
40eb23e0a8c5 8210130: java/net/httpclient/UnknownBodyLengthTest.java failed
dfuchs
parents: 52121
diff changeset
   128
                    os.write(r.getBytes());
40eb23e0a8c5 8210130: java/net/httpclient/UnknownBodyLengthTest.java failed
dfuchs
parents: 52121
diff changeset
   129
                    String chdr = "Content-Length: " + Integer.toString(BUF.length) +
40eb23e0a8c5 8210130: java/net/httpclient/UnknownBodyLengthTest.java failed
dfuchs
parents: 52121
diff changeset
   130
                            "\r\n";
40eb23e0a8c5 8210130: java/net/httpclient/UnknownBodyLengthTest.java failed
dfuchs
parents: 52121
diff changeset
   131
                    System.out.println(chdr);
40eb23e0a8c5 8210130: java/net/httpclient/UnknownBodyLengthTest.java failed
dfuchs
parents: 52121
diff changeset
   132
                    if (withContentLength)
40eb23e0a8c5 8210130: java/net/httpclient/UnknownBodyLengthTest.java failed
dfuchs
parents: 52121
diff changeset
   133
                        os.write(chdr.getBytes());
40eb23e0a8c5 8210130: java/net/httpclient/UnknownBodyLengthTest.java failed
dfuchs
parents: 52121
diff changeset
   134
                    os.write("\r\n".getBytes());
40eb23e0a8c5 8210130: java/net/httpclient/UnknownBodyLengthTest.java failed
dfuchs
parents: 52121
diff changeset
   135
                    os.write(BUF);
53572
f5480f924571 8216562: UnknownBodyLength sometimes fails due to "Connection reset by peer"
dfuchs
parents: 53522
diff changeset
   136
                    if (is.available() > 0) {
f5480f924571 8216562: UnknownBodyLength sometimes fails due to "Connection reset by peer"
dfuchs
parents: 53522
diff changeset
   137
                        System.out.println("Draining input: " + s);
53522
40eb23e0a8c5 8210130: java/net/httpclient/UnknownBodyLengthTest.java failed
dfuchs
parents: 52121
diff changeset
   138
                        is.read(buf);
53572
f5480f924571 8216562: UnknownBodyLength sometimes fails due to "Connection reset by peer"
dfuchs
parents: 53522
diff changeset
   139
                    }
53522
40eb23e0a8c5 8210130: java/net/httpclient/UnknownBodyLengthTest.java failed
dfuchs
parents: 52121
diff changeset
   140
                    os.flush();
40eb23e0a8c5 8210130: java/net/httpclient/UnknownBodyLengthTest.java failed
dfuchs
parents: 52121
diff changeset
   141
                    s.shutdownOutput();
53572
f5480f924571 8216562: UnknownBodyLength sometimes fails due to "Connection reset by peer"
dfuchs
parents: 53522
diff changeset
   142
                    System.out.println("Closed output: " + s);
53522
40eb23e0a8c5 8210130: java/net/httpclient/UnknownBodyLengthTest.java failed
dfuchs
parents: 52121
diff changeset
   143
                } catch(Exception e) {
40eb23e0a8c5 8210130: java/net/httpclient/UnknownBodyLengthTest.java failed
dfuchs
parents: 52121
diff changeset
   144
                    if (!stopped) {
40eb23e0a8c5 8210130: java/net/httpclient/UnknownBodyLengthTest.java failed
dfuchs
parents: 52121
diff changeset
   145
                        System.out.println("Unexpected server exception: " + e);
40eb23e0a8c5 8210130: java/net/httpclient/UnknownBodyLengthTest.java failed
dfuchs
parents: 52121
diff changeset
   146
                        e.printStackTrace();
40eb23e0a8c5 8210130: java/net/httpclient/UnknownBodyLengthTest.java failed
dfuchs
parents: 52121
diff changeset
   147
                    }
40eb23e0a8c5 8210130: java/net/httpclient/UnknownBodyLengthTest.java failed
dfuchs
parents: 52121
diff changeset
   148
                }
51462
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
   149
            }
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
   150
        } catch(final Throwable t) {
53522
40eb23e0a8c5 8210130: java/net/httpclient/UnknownBodyLengthTest.java failed
dfuchs
parents: 52121
diff changeset
   151
            if (!stopped) t.printStackTrace();
51462
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
   152
        } finally {
53572
f5480f924571 8216562: UnknownBodyLength sometimes fails due to "Connection reset by peer"
dfuchs
parents: 53522
diff changeset
   153
            stop();
51462
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
   154
        }
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
   155
    }
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
   156
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
   157
    void client(boolean useSSL) throws Exception {
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
   158
        SSLContext ctx = SSLContext.getDefault();
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
   159
        HttpClient.Builder clientB = HttpClient.newBuilder()
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
   160
            .version(HttpClient.Version.HTTP_2);
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
   161
        if (useSSL) {
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
   162
            clientB = clientB.sslContext(ctx)
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
   163
                .sslParameters(ctx.getSupportedSSLParameters());
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
   164
        }
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
   165
        final HttpClient client = clientB.build();
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
   166
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
   167
        System.out.println("URL: " + clientURL);
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
   168
        final HttpResponse<byte[]> response = client
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
   169
            .send(HttpRequest
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
   170
                .newBuilder(new URI(clientURL))
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
   171
                .timeout(Duration.ofMillis(120_000))
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
   172
                .POST(HttpRequest.BodyPublishers.ofString("body!#!#"))
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
   173
                .build(), HttpResponse.BodyHandlers.ofByteArray());
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
   174
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
   175
        System.out.println("Received reply: " + response.statusCode());
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
   176
        byte[] bb = response.body();
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
   177
        checkBuf(bb);
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
   178
    }
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
   179
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
   180
    public static void main(final String[] args) throws Exception {
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
   181
        boolean ssl = args[0].equals("SSL");
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
   182
        boolean fixedlen = args[1].equals("true");
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
   183
        UnknownBodyLengthTest test = new UnknownBodyLengthTest(ssl);
53522
40eb23e0a8c5 8210130: java/net/httpclient/UnknownBodyLengthTest.java failed
dfuchs
parents: 52121
diff changeset
   184
        try {
40eb23e0a8c5 8210130: java/net/httpclient/UnknownBodyLengthTest.java failed
dfuchs
parents: 52121
diff changeset
   185
            test.run(ssl, fixedlen);
40eb23e0a8c5 8210130: java/net/httpclient/UnknownBodyLengthTest.java failed
dfuchs
parents: 52121
diff changeset
   186
        } finally {
40eb23e0a8c5 8210130: java/net/httpclient/UnknownBodyLengthTest.java failed
dfuchs
parents: 52121
diff changeset
   187
            test.stop();
40eb23e0a8c5 8210130: java/net/httpclient/UnknownBodyLengthTest.java failed
dfuchs
parents: 52121
diff changeset
   188
        }
51462
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
   189
    }
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
   190
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
   191
    public void run(boolean ssl, boolean fixedlen) throws Exception {
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
   192
        new Thread(()->server(fixedlen)).start();
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
   193
        client(ssl);
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
   194
    }
53522
40eb23e0a8c5 8210130: java/net/httpclient/UnknownBodyLengthTest.java failed
dfuchs
parents: 52121
diff changeset
   195
40eb23e0a8c5 8210130: java/net/httpclient/UnknownBodyLengthTest.java failed
dfuchs
parents: 52121
diff changeset
   196
    public void stop() {
40eb23e0a8c5 8210130: java/net/httpclient/UnknownBodyLengthTest.java failed
dfuchs
parents: 52121
diff changeset
   197
        stopped = true;
40eb23e0a8c5 8210130: java/net/httpclient/UnknownBodyLengthTest.java failed
dfuchs
parents: 52121
diff changeset
   198
        try { ss.close(); } catch (Throwable t) { }
53572
f5480f924571 8216562: UnknownBodyLength sometimes fails due to "Connection reset by peer"
dfuchs
parents: 53522
diff changeset
   199
        for (Socket s : acceptedList) {
f5480f924571 8216562: UnknownBodyLength sometimes fails due to "Connection reset by peer"
dfuchs
parents: 53522
diff changeset
   200
            try { s.close(); } catch (Throwable t) { }
f5480f924571 8216562: UnknownBodyLength sometimes fails due to "Connection reset by peer"
dfuchs
parents: 53522
diff changeset
   201
        }
53522
40eb23e0a8c5 8210130: java/net/httpclient/UnknownBodyLengthTest.java failed
dfuchs
parents: 52121
diff changeset
   202
    }
51462
9d7d74c6f2cb 8207966: HttpClient response without content-length does not return body
michaelm
parents:
diff changeset
   203
}