test/jdk/com/sun/net/httpserver/bugs/B6373555.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
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 10075
diff changeset
     2
 * Copyright (c) 2006, 2011, 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 6373555
54314
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
    27
 * @library /test/lib
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @summary HTTP Server failing to answer client requests
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.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import com.sun.net.httpserver.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.concurrent.*;
54314
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
    36
import jdk.test.lib.net.URIBuilder;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
public class B6373555 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private static int s_received = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private static int sent = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private static int received = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private static int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private static volatile boolean error = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    static HttpServer httpServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    static ExecutorService pool, execs;
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    49
    static int NUM = 1000;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            if (args.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                NUM = Integer.parseInt (args[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            execs = Executors.newFixedThreadPool(5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            httpServer = createHttpServer(execs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            port = httpServer.getAddress().getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            pool = Executors.newFixedThreadPool(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            httpServer.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            for (int i=0; i < NUM; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                pool.execute(new Client());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                if (error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                    throw new Exception ("error in test");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            System.out.println("Main thread waiting");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            pool.shutdown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            long latest = System.currentTimeMillis() + 200 * 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            while (System.currentTimeMillis() < latest) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                if (pool.awaitTermination(2000L, TimeUnit.MILLISECONDS)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                    System.out.println("Main thread done!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                if (error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                    throw new Exception ("error in test");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            throw new Exception ("error in test: timed out");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            httpServer.stop(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            pool.shutdownNow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            execs.shutdownNow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public static class Client implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        byte[] getBuf () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            byte[] buf = new byte [5200];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            for (int i=0; i< 5200; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                buf [i] = (byte)i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            return buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                Thread.sleep(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                byte[] buf = getBuf();
54314
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
   101
                URL url = URIBuilder.newBuilder()
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
   102
                    .scheme("http")
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
   103
                    .loopback()
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
   104
                    .port(port)
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
   105
                    .path("/test")
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
   106
                    .toURLUnchecked();
46cf212cdcca 8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents: 47216
diff changeset
   107
                System.out.println("URL: " + url);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                HttpURLConnection con = (HttpURLConnection)url.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                con.setDoOutput(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                con.setDoInput(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                con.setRequestMethod("POST");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                con.setRequestProperty(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    "Content-Type",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    "Multipart/Related; type=\"application/xop+xml\"; boundary=\"----=_Part_0_6251267.1128549570165\"; start-info=\"text/xml\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                OutputStream out = con.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                out.write(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                InputStream in = con.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                byte[] newBuf = readFully(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                if (buf.length != newBuf.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                    System.out.println("Doesn't match");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                    error = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            catch(Exception e) {
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   127
                e.printStackTrace();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                System.out.print (".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                error = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    private static byte[] readFully(InputStream istream) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        byte[] buf = new byte[1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        int num = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        if (istream != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            while ((num = istream.read(buf)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                bout.write(buf, 0, num);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        byte[] ret = bout.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    private static HttpServer createHttpServer(ExecutorService execs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        InetSocketAddress inetAddress = new InetSocketAddress(0);
10075
c8d9bac189cc 7058832: com/sun/net/httpserver/bugs/B6373555.java failing intermittently
michaelm
parents: 7668
diff changeset
   152
        HttpServer testServer = HttpServer.create(inetAddress, 15);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        testServer.setExecutor(execs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        HttpContext context = testServer.createContext("/test");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        context.setHandler(new HttpHandler() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            public void handle(HttpExchange msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    String method = msg.getRequestMethod();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                        if (method.equals("POST")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                        InputStream is = msg.getRequestBody();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                            byte[] buf = readFully(is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                            is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                            writePostReply(msg, buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                        System.out.println("****** METHOD not handled ***** "+method);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                            System.out.println("Received="+s_received);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                catch(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                    e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    msg.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        return testServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    private static void writePostReply(HttpExchange msg, byte[] buf)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        msg.getResponseHeaders().add("Content-Type", "text/xml");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        msg.sendResponseHeaders(200, buf.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        OutputStream out = msg.getResponseBody();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        out.write(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
}