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