jdk/test/java/net/httpclient/EchoHandler.java
author dfuchs
Tue, 11 Apr 2017 16:32:38 +0100
changeset 44639 5c2838d882a5
parent 42460 7133f144981a
permissions -rw-r--r--
8178147: Race conditions in timeout handling code in http/2 incubator client Summary: Timeout handling code (in particular cancel(IOException)) is modified to take into account cancellation arising before exchange implementation is established or response completable future is created. TimeoutEvent::compareTo method is modified to be consistent with TimeoutEvent::equals. Reviewed-by: prappo, chegar, michaelm
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     1
/*
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     2
 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     4
 *
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     8
 *
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    13
 * accompanied this code).
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    14
 *
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    18
 *
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    21
 * questions.
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    22
 */
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    23
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    24
import com.sun.net.httpserver.*;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    25
import java.net.*;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
    26
import jdk.incubator.http.*;
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    27
import java.io.*;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    28
import java.util.concurrent.*;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    29
import javax.net.ssl.*;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    30
import java.nio.file.*;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    31
import java.util.HashSet;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    32
import java.util.LinkedList;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    33
import java.util.List;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    34
import java.util.Random;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    35
import jdk.testlibrary.SimpleSSLContext;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
    36
import static jdk.incubator.http.HttpRequest.*;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
    37
import static jdk.incubator.http.HttpResponse.*;
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    38
import java.util.logging.ConsoleHandler;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    39
import java.util.logging.Level;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    40
import java.util.logging.Logger;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    41
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    42
public class EchoHandler implements HttpHandler {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    43
    public EchoHandler() {}
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    44
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    45
    @Override
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    46
    public void handle(HttpExchange t)
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    47
            throws IOException {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    48
        try {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    49
            System.err.println("EchoHandler received request to " + t.getRequestURI());
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    50
            InputStream is = t.getRequestBody();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    51
            Headers map = t.getRequestHeaders();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    52
            Headers map1 = t.getResponseHeaders();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    53
            map1.add("X-Hello", "world");
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    54
            map1.add("X-Bye", "universe");
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    55
            String fixedrequest = map.getFirst("XFixed");
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    56
            File outfile = File.createTempFile("foo", "bar");
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    57
            FileOutputStream fos = new FileOutputStream(outfile);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    58
            int count = (int) is.transferTo(fos);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    59
            is.close();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    60
            fos.close();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    61
            InputStream is1 = new FileInputStream(outfile);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    62
            OutputStream os = null;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    63
            // return the number of bytes received (no echo)
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    64
            String summary = map.getFirst("XSummary");
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    65
            if (fixedrequest != null && summary == null) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    66
                t.sendResponseHeaders(200, count);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    67
                os = t.getResponseBody();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    68
                is1.transferTo(os);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    69
            } else {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    70
                t.sendResponseHeaders(200, 0);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    71
                os = t.getResponseBody();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    72
                is1.transferTo(os);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    73
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    74
                if (summary != null) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    75
                    String s = Integer.toString(count);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    76
                    os.write(s.getBytes());
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    77
                }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    78
            }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    79
            outfile.delete();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    80
            os.close();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    81
            is1.close();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    82
        } catch (Throwable e) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    83
            e.printStackTrace();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    84
            throw new IOException(e);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    85
        }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    86
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    87
}