test/jdk/com/sun/net/httpserver/bugs/B6401598.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 7668 jdk/test/com/sun/net/httpserver/bugs/B6401598.java@d4a77089c587
child 54314 46cf212cdcca
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 6401598
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary  new HttpServer cannot serve binary stream data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.net.HttpURLConnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.net.MalformedURLException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.net.InetSocketAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.concurrent.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import com.sun.net.httpserver.HttpExchange;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import com.sun.net.httpserver.HttpHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import com.sun.net.httpserver.HttpServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
public class B6401598 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        static class MyHandler implements HttpHandler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
                public MyHandler() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
                public void handle(HttpExchange arg0) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
                                InputStream is = arg0.getRequestBody();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
                                OutputStream os = arg0.getResponseBody();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                                DataInputStream dis = new DataInputStream(is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                                short input = dis.readShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                                while (dis.read() != -1) ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                                dis.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                                DataOutputStream dos = new DataOutputStream(os);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                                arg0.sendResponseHeaders(200, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                                dos.writeShort(input);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                                dos.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                                dos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                                error = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        static int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        static boolean error = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        static ExecutorService exec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        static HttpServer server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                        server = HttpServer.create(new InetSocketAddress(0), 400);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                        server.createContext("/server/", new MyHandler());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                        exec = Executors.newFixedThreadPool(3);
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
    86
                        server.setExecutor(exec);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                        port = server.getAddress().getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                        server.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                        short counter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                        for (counter = 0; counter < 1000; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                                HttpURLConnection connection = getHttpURLConnection(new URL("http://127.0.0.1:"+port+"/server/"), 10000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                                OutputStream os = connection.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                                DataOutputStream dos = new DataOutputStream(os);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                                dos.writeShort(counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                                dos.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                                dos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                                counter++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                                InputStream is = connection.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                                DataInputStream dis = new DataInputStream(is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                                short ret = dis.readShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                                dis.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                        System.out.println ("Stopping");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                        server.stop (1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                        exec.shutdown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                        // TODO Auto-generated catch block
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                        e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                        server.stop (1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                        exec.shutdown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        static HttpURLConnection getHttpURLConnection(URL url, int timeout) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                httpURLConnection.setConnectTimeout(40000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                httpURLConnection.setReadTimeout(timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                httpURLConnection.setDoOutput(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                httpURLConnection.setDoInput(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                httpURLConnection.setUseCaches(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                httpURLConnection.setAllowUserInteraction(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                httpURLConnection.setRequestMethod("POST");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                // HttpURLConnection httpURLConnection = new MyHttpURLConnection(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                return httpURLConnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
}