test/jdk/java/net/httpclient/Response204.java
author dfuchs
Mon, 28 Jan 2019 18:45:34 +0000
changeset 53524 90865744493d
parent 53300 54aa3ea04fe8
child 53701 e57bcfd7bf79
permissions -rw-r--r--
8217903: java/net/httpclient/Response204.java fails with 404 Summary: Fixed test to use loopback address - this looks like a port reuse issue. Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
52196
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
     1
/*
53524
90865744493d 8217903: java/net/httpclient/Response204.java fails with 404
dfuchs
parents: 53300
diff changeset
     2
 * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
52196
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
     4
 *
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
     8
 *
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    13
 * accompanied this code).
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    14
 *
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    18
 *
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    21
 * questions.
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    22
 */
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    23
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    24
/**
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    25
 * @test
53300
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52196
diff changeset
    26
 * @bug 8211437 8216974
52196
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    27
 * @run main/othervm -Djdk.httpclient.HttpClient.log=headers,requests Response204
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    28
 * @summary
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    29
 */
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    30
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    31
import com.sun.net.httpserver.*;
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    32
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    33
import java.net.http.HttpClient;
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    34
import java.net.http.HttpRequest;
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    35
import java.net.http.HttpResponse;
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    36
import java.util.*;
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    37
import java.util.concurrent.*;
53300
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52196
diff changeset
    38
import java.util.concurrent.atomic.AtomicReference;
52196
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    39
import java.util.logging.*;
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    40
import java.io.*;
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    41
import java.net.*;
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    42
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    43
/**
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    44
 * Verify that a 204 response code with no content-length is handled correctly
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    45
 */
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    46
public class Response204 {
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    47
53300
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52196
diff changeset
    48
    // check for 8216974
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52196
diff changeset
    49
    static final AtomicReference<Exception> serverError = new AtomicReference<>();
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52196
diff changeset
    50
52196
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    51
    public static void main (String[] args) throws Exception {
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    52
        Logger logger = Logger.getLogger ("com.sun.net.httpserver");
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    53
        ConsoleHandler c = new ConsoleHandler();
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    54
        c.setLevel (Level.WARNING);
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    55
        logger.addHandler (c);
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    56
        logger.setLevel (Level.WARNING);
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    57
        Handler handler = new Handler();
53524
90865744493d 8217903: java/net/httpclient/Response204.java fails with 404
dfuchs
parents: 53300
diff changeset
    58
        InetSocketAddress addr = new InetSocketAddress (InetAddress.getLoopbackAddress(), 0);
52196
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    59
        HttpServer server = HttpServer.create (addr, 0);
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    60
        HttpContext ctx = server.createContext ("/test", handler);
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    61
        ExecutorService executor = Executors.newCachedThreadPool();
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    62
        server.setExecutor (executor);
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    63
        server.start ();
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    64
53524
90865744493d 8217903: java/net/httpclient/Response204.java fails with 404
dfuchs
parents: 53300
diff changeset
    65
        URI uri = new URI("http", null,
90865744493d 8217903: java/net/httpclient/Response204.java fails with 404
dfuchs
parents: 53300
diff changeset
    66
                server.getAddress().getHostString(),
90865744493d 8217903: java/net/httpclient/Response204.java fails with 404
dfuchs
parents: 53300
diff changeset
    67
                server.getAddress().getPort(),
90865744493d 8217903: java/net/httpclient/Response204.java fails with 404
dfuchs
parents: 53300
diff changeset
    68
                "/test/foo.html", null, null);
52196
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    69
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    70
        try {
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    71
            HttpClient client = HttpClient.newHttpClient();
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    72
            HttpRequest request = HttpRequest.newBuilder(uri)
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    73
                    .GET()
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    74
                    .build();
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    75
            HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    76
            if (response.statusCode() != 204)
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    77
                throw new RuntimeException("wrong response code");
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    78
            if (response.body() != null && !response.body().equals(""))
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    79
                throw new RuntimeException("should have received empty response");
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    80
            System.out.println(response.headers().firstValue("content-length").orElse("nichts"));
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    81
            System.out.println ("OK 1");
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    82
            // Send a second time. This time we should get exception because the server
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    83
            // is going to send an invalid 204 with a Content-length
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    84
            try {
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    85
                response = client.send(request, HttpResponse.BodyHandlers.ofString());
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    86
                throw new RuntimeException("send should have thrown exception");
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    87
            } catch (IOException ioe) {
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    88
                System.out.println("OK 2");
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    89
            }
53300
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52196
diff changeset
    90
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52196
diff changeset
    91
            // check for 8216974
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52196
diff changeset
    92
            Exception error = serverError.get();
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52196
diff changeset
    93
            if (error != null) throw error;
52196
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    94
        } finally {
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    95
            server.stop(2);
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    96
            executor.shutdown();
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    97
        }
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    98
    }
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    99
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
   100
    public static boolean error = false;
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
   101
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
   102
    static class Handler implements HttpHandler {
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
   103
        volatile int counter = 0;
53300
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52196
diff changeset
   104
        volatile InetSocketAddress remote;
52196
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
   105
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
   106
        public void handle(HttpExchange t)
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
   107
                throws IOException {
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
   108
            InputStream is = t.getRequestBody();
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
   109
            Headers map = t.getRequestHeaders();
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
   110
            Headers rmap = t.getResponseHeaders();
53300
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52196
diff changeset
   111
            if (counter % 2 == 0) {
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52196
diff changeset
   112
                // store the client's address
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52196
diff changeset
   113
                remote = t.getRemoteAddress();
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52196
diff changeset
   114
                System.out.println("Server received request from: " + remote);
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52196
diff changeset
   115
            }
52196
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
   116
            while (is.read() != -1) ;
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
   117
            is.close();
53300
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52196
diff changeset
   118
            if ((++counter) % 2 == 0) {
52196
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
   119
                // pretend there is a body
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
   120
                rmap.set("Content-length", "10");
53300
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52196
diff changeset
   121
                // 8216974: the client should have returned the connection
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52196
diff changeset
   122
                // to the pool and should therefore have the same
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52196
diff changeset
   123
                // remote address.
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52196
diff changeset
   124
                if (!t.getRemoteAddress().equals(remote)) {
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52196
diff changeset
   125
                    String msg = "Unexpected remote address: "
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52196
diff changeset
   126
                            + t.getRemoteAddress()
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52196
diff changeset
   127
                            + " - should have been " + remote;
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52196
diff changeset
   128
                    System.out.println(msg);
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52196
diff changeset
   129
                    serverError.set(new Exception(msg));
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52196
diff changeset
   130
                }
52196
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
   131
            }
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
   132
            t.sendResponseHeaders(204, -1);
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
   133
            t.close();
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
   134
        }
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
   135
    }
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
   136
}