test/jdk/java/net/URLConnection/DisconnectAfterEOF.java
author chegar
Fri, 27 Sep 2019 09:55:35 +0100
changeset 58365 73950479184b
parent 54822 f542a3a135bd
permissions -rw-r--r--
8231504: Update networking tests to avoid implicit dependency on the system proxies Reviewed-by: dfuchs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
54822
f542a3a135bd 8223632: Replace wildcard address with loopback or local host in tests - part 5
dfuchs
parents: 47216
diff changeset
     2
 * Copyright (c) 2002, 2019, 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 4774503
54822
f542a3a135bd 8223632: Replace wildcard address with loopback or local host in tests - part 5
dfuchs
parents: 47216
diff changeset
    27
 * @library /test/lib
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @summary Calling HttpURLConnection's disconnect method after the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *          response has been received causes havoc with persistent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *          connections.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.*;
54822
f542a3a135bd 8223632: Replace wildcard address with loopback or local host in tests - part 5
dfuchs
parents: 47216
diff changeset
    35
import jdk.test.lib.net.URIBuilder;
58365
73950479184b 8231504: Update networking tests to avoid implicit dependency on the system proxies
chegar
parents: 54822
diff changeset
    36
import static java.net.Proxy.NO_PROXY;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
public class DisconnectAfterEOF {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
     * Worker thread to service single connection - can service
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
     * multiple http requests on same connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    static class Worker extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        Socket s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        Worker(Socket s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            this.s = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
                InputStream in = s.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                PrintStream out = new PrintStream(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                                        new BufferedOutputStream(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                                                s.getOutputStream() ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                byte b[] = new byte[1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                int n = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                int cl = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                int remaining = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                StringBuffer sb = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                boolean close = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                boolean inBody = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                    boolean sendResponse = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                        n = in.read(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                    } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                        n = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                    if (n <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                        if (inBody) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                            System.err.println("ERROR: Client closed before before " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                                "entire request received.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                    // reading entity-body
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                    if (inBody) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                        if (n > remaining) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                            System.err.println("Receiving more than expected!!!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                        remaining -= n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                        if (remaining == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                            sendResponse = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                            n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                    // reading headers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                    for (int i=0; i<n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                        char c = (char)b[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                        if (c != '\n') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                            sb.append(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                        // Got end-of-line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                        int len = sb.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                        if (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                            if (sb.charAt(len-1) != '\r') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                                System.err.println("Unexpected CR in header!!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                        sb.setLength(len-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                        // empty line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                        if (sb.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                            if (cl < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                                System.err.println("Content-Length not found!!!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                            // the surplus is body data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                            int dataRead = n - (i+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                            remaining = cl - dataRead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                            if (remaining > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                                inBody = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                                // entire body has been read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                                sendResponse = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                            // non-empty line - check for Content-Length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                            String line = sb.toString().toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                            if (line.startsWith("content-length")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                                StringTokenizer st = new StringTokenizer(line, ":");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                                st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                                cl = Integer.parseInt(st.nextToken().trim());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                            if (line.startsWith("connection")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                                StringTokenizer st = new StringTokenizer(line, ":");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                                st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                                if (st.nextToken().trim().equals("close")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                                    close =true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                        sb = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                   if (sendResponse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                        // send a large response
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                        int rspLen = 32000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                        out.print("HTTP/1.1 200 OK\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                        out.print("Content-Length: " + rspLen + "\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                        out.print("\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                        if (rspLen > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                            out.write(new byte[rspLen]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                        out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                        if (close)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                        sendResponse = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                        inBody = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                        cl = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                    s.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                } catch (Exception e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                System.out.println("+ Worker thread shutdown.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * Server thread to accept connection and create worker threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * to service each connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    static class Server extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        ServerSocket ss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        Server(ServerSocket ss) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            this.ss = ss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                    Socket s = ss.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                    Worker w = new Worker(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                    w.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            System.out.println("+ Server shutdown.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        public void shutdown() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                ss.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            } catch (IOException ioe) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    static URLConnection doRequest(String uri) throws IOException {
58365
73950479184b 8231504: Update networking tests to avoid implicit dependency on the system proxies
chegar
parents: 54822
diff changeset
   219
        URLConnection uc = (new URL(uri)).openConnection(NO_PROXY);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        uc.setDoOutput(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        OutputStream out = uc.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        out.write(new byte[16000]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        // force the request to be sent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        uc.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        return uc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    static URLConnection doResponse(URLConnection uc) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        int cl = ((HttpURLConnection)uc).getContentLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        byte b[] = new byte[4096];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            n = uc.getInputStream().read(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            if (n > 0) cl -= n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        } while (n > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        if (cl != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            throw new RuntimeException("ERROR: content-length mismatch");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        return uc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        // start server
54822
f542a3a135bd 8223632: Replace wildcard address with loopback or local host in tests - part 5
dfuchs
parents: 47216
diff changeset
   245
        InetAddress loopback = InetAddress.getLoopbackAddress();
f542a3a135bd 8223632: Replace wildcard address with loopback or local host in tests - part 5
dfuchs
parents: 47216
diff changeset
   246
        ServerSocket ss = new ServerSocket();
f542a3a135bd 8223632: Replace wildcard address with loopback or local host in tests - part 5
dfuchs
parents: 47216
diff changeset
   247
        ss.bind(new InetSocketAddress(loopback, 0));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        Server svr = new Server(ss);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        svr.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
54822
f542a3a135bd 8223632: Replace wildcard address with loopback or local host in tests - part 5
dfuchs
parents: 47216
diff changeset
   251
        String uri = URIBuilder.newBuilder()
f542a3a135bd 8223632: Replace wildcard address with loopback or local host in tests - part 5
dfuchs
parents: 47216
diff changeset
   252
                     .scheme("http")
f542a3a135bd 8223632: Replace wildcard address with loopback or local host in tests - part 5
dfuchs
parents: 47216
diff changeset
   253
                     .loopback()
f542a3a135bd 8223632: Replace wildcard address with loopback or local host in tests - part 5
dfuchs
parents: 47216
diff changeset
   254
                     .port(ss.getLocalPort())
f542a3a135bd 8223632: Replace wildcard address with loopback or local host in tests - part 5
dfuchs
parents: 47216
diff changeset
   255
                     .path("/foo.html")
f542a3a135bd 8223632: Replace wildcard address with loopback or local host in tests - part 5
dfuchs
parents: 47216
diff changeset
   256
                     .build().toString();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
         * The following is the test scenario we create here :-
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
         * 1. We do a http request/response and read the response
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
         *    to EOF. As it's a persistent connection the idle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
         *    connection should go into the keep-alive cache for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
         *    few seconds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
         * 2. We start a second request but don't read the response.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
         *    As the request is to the same server we can assume it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
         *    (for our implementation anyway) that it will use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
         *    same TCP connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
         * 3. We "disconnect" the first HttpURLConnection. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
         *    should be no-op because the connection is in use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
         *    but another request. However with 1.3.1 and 1.4/1.4.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
         *    this causes the TCP connection for the second request
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
         *    to be closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        URLConnection uc1 = doRequest(uri);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        doResponse(uc1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   281
        Thread.sleep(2000);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        URLConnection uc2 = doRequest(uri);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        ((HttpURLConnection)uc1).disconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        IOException ioe = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            doResponse(uc2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        } catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            ioe = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        ((HttpURLConnection)uc2).disconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
         * Shutdown server as we are done. Worker threads created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
         * by the server will shutdown automatically when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
         * client connection closes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        svr.shutdown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        if (ioe != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
}