test/jdk/java/net/httpclient/EchoHandler.java
author chegar
Tue, 17 Apr 2018 08:54:17 -0700
changeset 49765 ee6f7a61f3a5
parent 48083 b1c1b4ef4be2
child 51778 300523d8b7b3
child 56451 9585061fdb04
permissions -rw-r--r--
8197564: HTTP Client implementation Reviewed-by: chegar, dfuchs, michaelm, prappo Contributed-by: Chris Hegarty <chris.hegarty@oracle.com>, Daniel Fuchs <daniel.fuchs@oracle.com>, Michael McMahon <michael.x.mcmahon@oracle.com>, Pavel Rappo <pavel.rappo@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     1
/*
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
     2
 * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
37720
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.*;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    26
import java.net.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;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    36
import static java.net.http.HttpRequest.*;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    37
import static java.net.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
}