test/jdk/com/sun/net/httpserver/bugs/B6401598.java
author aeubanks
Wed, 27 Mar 2019 09:06:43 -0700
changeset 54314 46cf212cdcca
parent 47216 71c04702a3d5
child 55309 8081b181bba8
permissions -rw-r--r--
8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder Reviewed-by: dfuchs, chegar Contributed-by: aeubanks@google.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 7271
diff changeset
     2
 * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
54314
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
    26
 * @library /test/lib
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @bug 6401598
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @summary  new HttpServer cannot serve binary stream data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.net.HttpURLConnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.net.MalformedURLException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.net.URL;
54314
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
    35
import java.net.InetAddress;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.net.InetSocketAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.concurrent.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
54314
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
    39
import jdk.test.lib.net.URIBuilder;
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
    40
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import com.sun.net.httpserver.HttpExchange;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import com.sun.net.httpserver.HttpHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import com.sun.net.httpserver.HttpServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
public class B6401598 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        static class MyHandler implements HttpHandler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
                public MyHandler() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
                public void handle(HttpExchange arg0) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                                InputStream is = arg0.getRequestBody();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                                OutputStream os = arg0.getResponseBody();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                                DataInputStream dis = new DataInputStream(is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                                short input = dis.readShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                                while (dis.read() != -1) ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                                dis.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                                DataOutputStream dos = new DataOutputStream(os);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                                arg0.sendResponseHeaders(200, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                                dos.writeShort(input);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                                dos.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                                dos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                                error = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        static int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        static boolean error = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        static ExecutorService exec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        static HttpServer server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                        server = HttpServer.create(new InetSocketAddress(0), 400);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                        server.createContext("/server/", new MyHandler());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                        exec = Executors.newFixedThreadPool(3);
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
    90
                        server.setExecutor(exec);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                        port = server.getAddress().getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                        server.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                        short counter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                        for (counter = 0; counter < 1000; counter++) {
54314
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
    97
                                URL url = URIBuilder.newBuilder()
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
    98
                                    .scheme("http")
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
    99
                                    .loopback()
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
   100
                                    .port(port)
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
   101
                                    .path("/server/")
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
   102
                                    .toURLUnchecked();
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
   103
                                System.out.println("URL: " + url);
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
   104
                                HttpURLConnection connection = getHttpURLConnection(url, 10000);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                                OutputStream os = connection.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                                DataOutputStream dos = new DataOutputStream(os);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                                dos.writeShort(counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                                dos.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                                dos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                                counter++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                                InputStream is = connection.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                                DataInputStream dis = new DataInputStream(is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                                short ret = dis.readShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                                dis.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                        System.out.println ("Stopping");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                        server.stop (1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                        exec.shutdown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                        // TODO Auto-generated catch block
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                        e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                        server.stop (1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                        exec.shutdown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        static HttpURLConnection getHttpURLConnection(URL url, int timeout) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                httpURLConnection.setConnectTimeout(40000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                httpURLConnection.setReadTimeout(timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                httpURLConnection.setDoOutput(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                httpURLConnection.setDoInput(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                httpURLConnection.setUseCaches(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                httpURLConnection.setAllowUserInteraction(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                httpURLConnection.setRequestMethod("POST");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                // HttpURLConnection httpURLConnection = new MyHttpURLConnection(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                return httpURLConnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
}