jdk/test/java/net/URLConnection/ZeroContentLength.java
author dsamersoff
Thu, 17 Oct 2013 16:08:01 +0400
changeset 21069 728330d2593a
parent 7668 d4a77089c587
permissions -rw-r--r--
8025812: tmtools/jmap/heap_config tests fail on Linux-ia32 because it Cant attach to the core file Summary: Coredump store memsz elf field rounded up to page Reviewed-by: dholmes, sla
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) 2001, 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 4507412
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @bug 4506998
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @summary Check that a 304 "Not-Modified" response from a server
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *          doesn't cause http client to close a keep-alive
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *          connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *          Check that a content-length of 0 results in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *          empty input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class ZeroContentLength {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
     * Is debugging enabled - start with -d to enable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    static boolean debug = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    static void debug(String msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        if (debug)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
            System.out.println(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * The response string and content-length that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * the server should return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    static String response;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    static int contentLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    static synchronized void setResponse(String rsp, int cl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        response = rsp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        contentLength = cl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    61
    static synchronized String getResponse() {
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    62
        return response;
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    63
    }
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    64
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    65
    static synchronized int getContentLength() {
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    66
        return contentLength;
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    67
    }
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    68
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * Worker thread to service single connection - can service
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * multiple http requests on same connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    class Worker extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        Socket s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        int id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        Worker(Socket s, int id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            this.s = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            this.id = id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    82
        final int CR = '\r';
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    83
        final int LF = '\n';
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    84
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                s.setSoTimeout(2000);
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    89
                int max = 20; // there should only be 20 connections
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    90
                InputStream in = new BufferedInputStream(s.getInputStream());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                for (;;) {
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    93
                    // read entire request from client, until CR LF CR LF
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    94
                    int c, total=0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                    try {
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    97
                        while ((c = in.read()) > 0) {
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    98
                            total++;
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    99
                            if (c == CR) {
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   100
                                if ((c = in.read()) > 0) {
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   101
                                    total++;
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   102
                                    if (c == LF) {
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   103
                                        if ((c = in.read()) > 0) {
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   104
                                            total++;
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   105
                                            if (c == CR) {
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   106
                                                if ((c = in.read()) > 0) {
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   107
                                                    total++;
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   108
                                                    if (c == LF) {
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   109
                                                        break;
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   110
                                                    }
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   111
                                                }
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   112
                                            }
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   113
                                        }
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   114
                                    }
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   115
                                }
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   116
                            }
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   117
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   118
                        }
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   119
                    } catch (SocketTimeoutException e) {}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                    debug("worker " + id +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                        ": Read request from client " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                        "(" + total + " bytes).");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                    if (total == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                        debug("worker: " + id + ": Shutdown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    // response to client
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                    PrintStream out = new PrintStream(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                                        new BufferedOutputStream(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                                                s.getOutputStream() ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   135
                    out.print("HTTP/1.1 " + getResponse() + "\r\n");
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   136
                    int clen = getContentLength();
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   137
                    if (clen >= 0) {
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   138
                        out.print("Content-Length: " + clen +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                                    "\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                    out.print("\r\n");
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   142
                    for (int i=0; i<clen; i++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                        out.write( (byte)'.' );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                    out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                    debug("worked " + id +
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   148
                        ": Sent response to client, length: " + clen);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                    if (--max == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                        s.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                    s.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                } catch (Exception e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * Server thread to accept connection and create worker threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * to service each connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    class Server extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        ServerSocket ss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        int connectionCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        boolean shutdown = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        Server(ServerSocket ss) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            this.ss = ss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        public synchronized int connectionCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            return connectionCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        public synchronized void shutdown() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            shutdown = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                ss.setSoTimeout(2000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                    Socket s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                        debug("server: Waiting for connections");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                        s = ss.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                    } catch (SocketTimeoutException te) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                            if (shutdown) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                                debug("server: Shuting down.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                    int id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                    synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                        id = connectionCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                    Worker w = new Worker(s, id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                    w.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                    debug("server: Started worker " + id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                    ss.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                } catch (Exception e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * Make a single http request and return the content length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * received. Also do sanity check to ensure that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * content-length header matches the total received on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * the input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    int doRequest(String uri) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        URL url = new URL(uri);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        HttpURLConnection http = (HttpURLConnection)url.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        int cl = http.getContentLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        InputStream in = http.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        byte b[] = new byte[100];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        int total = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            n = in.read(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            if (n > 0) total += n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        } while (n > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        if (cl >= 0 && total != cl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            System.err.println("content-length header indicated: " + cl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            System.err.println("Actual received: " + total);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            throw new Exception("Content-length didn't match actual received");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        return total;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * Send http requests to "server" and check that they all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * use the same network connection and that the content
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * length corresponds to the content length expected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    ZeroContentLength() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        /* start the server */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        ServerSocket ss = new ServerSocket(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        Server svr = new Server(ss);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        svr.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        String uri = "http://localhost:" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                     Integer.toString(ss.getLocalPort()) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                     "/foo.html";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        int expectedTotal = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        int actualTotal = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        System.out.println("**********************************");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        System.out.println("200 OK, content-length:1024 ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        setResponse("200 OK", 1024);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        for (int i=0; i<5; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            actualTotal += doRequest(uri);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            expectedTotal += 1024;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        System.out.println("**********************************");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        System.out.println("200 OK, content-length:0 ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        setResponse("200 OK", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        for (int i=0; i<5; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            actualTotal += doRequest(uri);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        System.out.println("**********************************");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        System.out.println("304 Not-Modified, (no content-length) ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        setResponse("304 Not-Modifed", -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        for (int i=0; i<5; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            actualTotal += doRequest(uri);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        System.out.println("**********************************");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        System.out.println("204 No-Content, (no content-length) ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        setResponse("204 No-Content", -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        for (int i=0; i<5; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            actualTotal += doRequest(uri);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        // shutdown server - we're done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        svr.shutdown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        System.out.println("**********************************");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        if (actualTotal == expectedTotal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            System.out.println("Passed: Actual total equal to expected total");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            throw new Exception("Actual total != Expected total!!!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        int cnt = svr.connectionCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        if (cnt == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            System.out.println("Passed: Only 1 connection established");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            throw new Exception("Test failed: Number of connections " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                "established: " + cnt + " - see log for details.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        if (args.length > 0 && args[0].equals("-d")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            debug = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        new ZeroContentLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
}