test/jdk/java/net/httpclient/Response204.java
author jboes
Fri, 08 Nov 2019 11:15:16 +0000
changeset 59029 3786a0962570
parent 53701 e57bcfd7bf79
permissions -rw-r--r--
8232853: AuthenticationFilter.Cache::remove may throw ConcurrentModificationException Summary: Change implementation to use iterator instead of plain LinkedList Reviewed-by: dfuchs, vtewari
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
53701
chegar
parents: 53524 53700
diff changeset
    26
 * @bug 8211437 8216974 8218662
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;
53700
4ce47bc1fb92 8218662: Allow 204 responses with Content-Length:0
chegar
parents: 52196
diff changeset
    36
import java.net.http.HttpResponse.BodyHandlers;
52196
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.*;
53700
4ce47bc1fb92 8218662: Allow 204 responses with Content-Length:0
chegar
parents: 52196
diff changeset
    42
import static java.net.http.HttpClient.Builder.NO_PROXY;
52196
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
/**
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    45
 * 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
    46
 */
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    47
public class Response204 {
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    48
53300
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52196
diff changeset
    49
    // check for 8216974
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52196
diff changeset
    50
    static final AtomicReference<Exception> serverError = new AtomicReference<>();
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52196
diff changeset
    51
52196
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    52
    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
    53
        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
    54
        ConsoleHandler c = new ConsoleHandler();
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    55
        c.setLevel (Level.WARNING);
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    56
        logger.addHandler (c);
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    57
        logger.setLevel (Level.WARNING);
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    58
        Handler handler = new Handler();
53524
90865744493d 8217903: java/net/httpclient/Response204.java fails with 404
dfuchs
parents: 53300
diff changeset
    59
        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
    60
        HttpServer server = HttpServer.create (addr, 0);
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    61
        HttpContext ctx = server.createContext ("/test", handler);
53700
4ce47bc1fb92 8218662: Allow 204 responses with Content-Length:0
chegar
parents: 52196
diff changeset
    62
        server.createContext ("/zero", new ZeroContentLengthHandler());
52196
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    63
        ExecutorService executor = Executors.newCachedThreadPool();
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    64
        server.setExecutor (executor);
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    65
        server.start ();
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    66
53524
90865744493d 8217903: java/net/httpclient/Response204.java fails with 404
dfuchs
parents: 53300
diff changeset
    67
        URI uri = new URI("http", null,
90865744493d 8217903: java/net/httpclient/Response204.java fails with 404
dfuchs
parents: 53300
diff changeset
    68
                server.getAddress().getHostString(),
90865744493d 8217903: java/net/httpclient/Response204.java fails with 404
dfuchs
parents: 53300
diff changeset
    69
                server.getAddress().getPort(),
90865744493d 8217903: java/net/httpclient/Response204.java fails with 404
dfuchs
parents: 53300
diff changeset
    70
                "/test/foo.html", null, null);
52196
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    71
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    72
        try {
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    73
            HttpClient client = HttpClient.newHttpClient();
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    74
            HttpRequest request = HttpRequest.newBuilder(uri)
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    75
                    .GET()
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    76
                    .build();
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    77
            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
    78
            if (response.statusCode() != 204)
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    79
                throw new RuntimeException("wrong response code");
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    80
            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
    81
                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
    82
            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
    83
            System.out.println ("OK 1");
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    84
            // 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
    85
            // 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
    86
            try {
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    87
                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
    88
                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
    89
            } catch (IOException ioe) {
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    90
                System.out.println("OK 2");
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
    91
            }
53700
4ce47bc1fb92 8218662: Allow 204 responses with Content-Length:0
chegar
parents: 52196
diff changeset
    92
53300
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52196
diff changeset
    93
            // check for 8216974
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52196
diff changeset
    94
            Exception error = serverError.get();
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52196
diff changeset
    95
            if (error != null) throw error;
53701
chegar
parents: 53524 53700
diff changeset
    96
53700
4ce47bc1fb92 8218662: Allow 204 responses with Content-Length:0
chegar
parents: 52196
diff changeset
    97
            // Test 3
4ce47bc1fb92 8218662: Allow 204 responses with Content-Length:0
chegar
parents: 52196
diff changeset
    98
            testZeroContentLength(uri.resolve("/zero/xxyy"));
4ce47bc1fb92 8218662: Allow 204 responses with Content-Length:0
chegar
parents: 52196
diff changeset
    99
            System.out.println ("OK 3");
52196
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
   100
        } finally {
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
   101
            server.stop(2);
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
   102
            executor.shutdown();
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
   103
        }
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
   104
    }
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
   105
53700
4ce47bc1fb92 8218662: Allow 204 responses with Content-Length:0
chegar
parents: 52196
diff changeset
   106
    static void testZeroContentLength(URI uri) throws Exception {
4ce47bc1fb92 8218662: Allow 204 responses with Content-Length:0
chegar
parents: 52196
diff changeset
   107
        System.out.println("--- testZeroContentLength ---");
4ce47bc1fb92 8218662: Allow 204 responses with Content-Length:0
chegar
parents: 52196
diff changeset
   108
        HttpClient client = HttpClient.newBuilder().proxy(NO_PROXY).build();
4ce47bc1fb92 8218662: Allow 204 responses with Content-Length:0
chegar
parents: 52196
diff changeset
   109
        HttpRequest request = HttpRequest.newBuilder(uri).build();
4ce47bc1fb92 8218662: Allow 204 responses with Content-Length:0
chegar
parents: 52196
diff changeset
   110
        HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
4ce47bc1fb92 8218662: Allow 204 responses with Content-Length:0
chegar
parents: 52196
diff changeset
   111
        System.out.println("Received response:" + response);
4ce47bc1fb92 8218662: Allow 204 responses with Content-Length:0
chegar
parents: 52196
diff changeset
   112
        System.out.println("Received headers:" + response.headers());
4ce47bc1fb92 8218662: Allow 204 responses with Content-Length:0
chegar
parents: 52196
diff changeset
   113
        if (response.statusCode() != 204)
4ce47bc1fb92 8218662: Allow 204 responses with Content-Length:0
chegar
parents: 52196
diff changeset
   114
            throw new RuntimeException("Expected 204, got:" + response.statusCode());
4ce47bc1fb92 8218662: Allow 204 responses with Content-Length:0
chegar
parents: 52196
diff changeset
   115
        if (response.body() != null && !response.body().equals(""))
4ce47bc1fb92 8218662: Allow 204 responses with Content-Length:0
chegar
parents: 52196
diff changeset
   116
            throw new RuntimeException("Expected empty response, got: " + response.body());
4ce47bc1fb92 8218662: Allow 204 responses with Content-Length:0
chegar
parents: 52196
diff changeset
   117
        if (response.headers().firstValueAsLong("Content-Length").orElse(-1L) != 0L)
4ce47bc1fb92 8218662: Allow 204 responses with Content-Length:0
chegar
parents: 52196
diff changeset
   118
            throw new RuntimeException("Expected Content-Length:0, in: " + response.headers());
4ce47bc1fb92 8218662: Allow 204 responses with Content-Length:0
chegar
parents: 52196
diff changeset
   119
    }
4ce47bc1fb92 8218662: Allow 204 responses with Content-Length:0
chegar
parents: 52196
diff changeset
   120
52196
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
   121
    public static boolean error = false;
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
   122
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
   123
    static class Handler implements HttpHandler {
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
   124
        volatile int counter = 0;
53300
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52196
diff changeset
   125
        volatile InetSocketAddress remote;
52196
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
   126
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
   127
        public void handle(HttpExchange t)
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
   128
                throws IOException {
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
   129
            InputStream is = t.getRequestBody();
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
   130
            Headers map = t.getRequestHeaders();
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
   131
            Headers rmap = t.getResponseHeaders();
53300
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52196
diff changeset
   132
            if (counter % 2 == 0) {
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52196
diff changeset
   133
                // store the client's address
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52196
diff changeset
   134
                remote = t.getRemoteAddress();
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52196
diff changeset
   135
                System.out.println("Server received request from: " + remote);
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52196
diff changeset
   136
            }
52196
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
   137
            while (is.read() != -1) ;
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
   138
            is.close();
53300
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52196
diff changeset
   139
            if ((++counter) % 2 == 0) {
52196
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
   140
                // pretend there is a body
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
   141
                rmap.set("Content-length", "10");
53300
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52196
diff changeset
   142
                // 8216974: the client should have returned the connection
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52196
diff changeset
   143
                // 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
   144
                // remote address.
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52196
diff changeset
   145
                if (!t.getRemoteAddress().equals(remote)) {
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52196
diff changeset
   146
                    String msg = "Unexpected remote address: "
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52196
diff changeset
   147
                            + t.getRemoteAddress()
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52196
diff changeset
   148
                            + " - should have been " + remote;
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52196
diff changeset
   149
                    System.out.println(msg);
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52196
diff changeset
   150
                    serverError.set(new Exception(msg));
54aa3ea04fe8 8216974: HttpConnection not returned to the pool after 204 response
dfuchs
parents: 52196
diff changeset
   151
                }
52196
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
   152
            }
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
   153
            t.sendResponseHeaders(204, -1);
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
   154
            t.close();
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
   155
        }
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
   156
    }
53700
4ce47bc1fb92 8218662: Allow 204 responses with Content-Length:0
chegar
parents: 52196
diff changeset
   157
4ce47bc1fb92 8218662: Allow 204 responses with Content-Length:0
chegar
parents: 52196
diff changeset
   158
    // A handler that returns a 204 with a `Content-Length: 0` header/value
4ce47bc1fb92 8218662: Allow 204 responses with Content-Length:0
chegar
parents: 52196
diff changeset
   159
    static class ZeroContentLengthHandler implements HttpHandler {
4ce47bc1fb92 8218662: Allow 204 responses with Content-Length:0
chegar
parents: 52196
diff changeset
   160
        public void handle(HttpExchange t) throws IOException {
4ce47bc1fb92 8218662: Allow 204 responses with Content-Length:0
chegar
parents: 52196
diff changeset
   161
            try (InputStream is = t.getRequestBody()) {
4ce47bc1fb92 8218662: Allow 204 responses with Content-Length:0
chegar
parents: 52196
diff changeset
   162
                is.readAllBytes();
4ce47bc1fb92 8218662: Allow 204 responses with Content-Length:0
chegar
parents: 52196
diff changeset
   163
            }
4ce47bc1fb92 8218662: Allow 204 responses with Content-Length:0
chegar
parents: 52196
diff changeset
   164
            t.getResponseHeaders().set("Content-length", "0");
4ce47bc1fb92 8218662: Allow 204 responses with Content-Length:0
chegar
parents: 52196
diff changeset
   165
            t.sendResponseHeaders(204, -1);
4ce47bc1fb92 8218662: Allow 204 responses with Content-Length:0
chegar
parents: 52196
diff changeset
   166
            t.close();
4ce47bc1fb92 8218662: Allow 204 responses with Content-Length:0
chegar
parents: 52196
diff changeset
   167
        }
4ce47bc1fb92 8218662: Allow 204 responses with Content-Length:0
chegar
parents: 52196
diff changeset
   168
    }
52196
420445d16008 8211437: java.net.http.HttpClient hangs on 204 reply without Content-length 0
michaelm
parents:
diff changeset
   169
}