jdk/test/java/net/URLConnection/ChunkedEncoding.java
author amurillo
Wed, 15 Aug 2012 16:49:38 -0700
changeset 13465 d3fc5d192448
parent 5506 202f599c92aa
permissions -rw-r--r--
7191765: make jdk8 the default jprt release for hs24 Reviewed-by: jcoomes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2001, 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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4333920
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @bug 4394548
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @summary Check that chunked encoding response doesn't cause
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *          getInputStream to block until last chunk arrives.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *          Also regression against NPE in ChunkedInputStream.
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.Random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
public class ChunkedEncoding implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    ServerSocket ss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
     * Our "http" server to return a chunked response
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
            Socket s = ss.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
            PrintStream out = new PrintStream(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
                                 new BufferedOutputStream(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
                                    s.getOutputStream() ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            /* send the header */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            out.print("HTTP/1.1 200\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            out.print("Transfer-Encoding: chunked\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            out.print("Content-Type: text/html\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            out.print("\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            /* delay the server before first chunk */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            Thread.sleep(5000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
             * Our response will be of random length
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
             * but > 32k
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            Random rand = new Random();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            int len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                len = rand.nextInt(128*1024);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            } while (len < 32*1024);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
             * Our chunk size will be 2-32k
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            int chunkSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                chunkSize = rand.nextInt(len / 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            } while (chunkSize < 2*1024);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
             * Generate random content and check sum it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            byte buf[] = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            int cs = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            for (int i=0; i<len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                buf[i] = (byte)('a' + rand.nextInt(26));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                cs = (cs + buf[i]) % 65536;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
             * Stream the chunks to the client
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            int remaining = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            int pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            while (remaining > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                int size = Math.min(remaining, chunkSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                out.print( Integer.toHexString(size) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                out.print("\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                out.write( buf, pos, size );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                pos += size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                remaining -= size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                out.print("\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            /* send EOF chunk */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            out.print("0\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
             * Send trailer with checksum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            String trailer = "Checksum:" + cs + "\r\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            out.print(trailer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            out.print("\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            s.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            ss.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    ChunkedEncoding() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        /* start the server */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        ss = new ServerSocket(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        (new Thread(this)).start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        /* establish http connection to server */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        String uri = "http://localhost:" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                     Integer.toString(ss.getLocalPort()) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                     "/foo";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        URL url = new URL(uri);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        HttpURLConnection http = (HttpURLConnection)url.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
         * Server should only send headers if TE:trailers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
         * specified - see updated HTTP 1.1 spec.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        http.setRequestProperty("TE", "trailers");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        /* Time how long the getInputStream takes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        long ts = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        InputStream in = http.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        long te = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
         * If getInputStream takes >2 seconds it probably means
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
         * that the implementation is waiting for the chunks to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
         * arrive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        if ( (te-ts) > 2000) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            throw new Exception("getInputStream didn't return immediately");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
         * Read the stream and checksum it as it arrives
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        int nread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        int cs = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        byte b[] = new byte[1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            nread = in.read(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            if (nread > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                for (int i=0; i<nread; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                    cs = (cs + b[i]) % 65536;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        } while (nread > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
         * Verify that the checksums match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        String trailer = http.getHeaderField("Checksum");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        if (trailer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            throw new Exception("Checksum trailer missing from response");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        int rcvd_cs = Integer.parseInt(trailer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        if (rcvd_cs != cs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            throw new Exception("Trailer checksum doesn't equal calculated checksum");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        http.disconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        new ChunkedEncoding();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
}