Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/URLConnection/ChunkedEncoding.java
38811 views
/*1* Copyright (c) 2001, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24*25* @bug 433392026* @bug 439454827* @summary Check that chunked encoding response doesn't cause28* getInputStream to block until last chunk arrives.29* Also regression against NPE in ChunkedInputStream.30*/31import java.net.*;32import java.io.*;33import java.util.Random;3435public class ChunkedEncoding implements Runnable {3637ServerSocket ss;3839/*40* Our "http" server to return a chunked response41*/42public void run() {43try {44Socket s = ss.accept();4546PrintStream out = new PrintStream(47new BufferedOutputStream(48s.getOutputStream() ));4950/* send the header */51out.print("HTTP/1.1 200\r\n");52out.print("Transfer-Encoding: chunked\r\n");53out.print("Content-Type: text/html\r\n");54out.print("\r\n");55out.flush();5657/* delay the server before first chunk */58Thread.sleep(5000);5960/*61* Our response will be of random length62* but > 32k63*/64Random rand = new Random();6566int len;67do {68len = rand.nextInt(128*1024);69} while (len < 32*1024);7071/*72* Our chunk size will be 2-32k73*/74int chunkSize;75do {76chunkSize = rand.nextInt(len / 3);77} while (chunkSize < 2*1024);7879/*80* Generate random content and check sum it81*/82byte buf[] = new byte[len];83int cs = 0;84for (int i=0; i<len; i++) {85buf[i] = (byte)('a' + rand.nextInt(26));86cs = (cs + buf[i]) % 65536;87}8889/*90* Stream the chunks to the client91*/92int remaining = len;93int pos = 0;94while (remaining > 0) {95int size = Math.min(remaining, chunkSize);96out.print( Integer.toHexString(size) );97out.print("\r\n");98out.write( buf, pos, size );99pos += size;100remaining -= size;101out.print("\r\n");102out.flush();103}104105/* send EOF chunk */106out.print("0\r\n");107out.flush();108109/*110* Send trailer with checksum111*/112String trailer = "Checksum:" + cs + "\r\n";113out.print(trailer);114out.print("\r\n");115out.flush();116117s.close();118ss.close();119} catch (Exception e) {120e.printStackTrace();121}122}123124ChunkedEncoding() throws Exception {125126/* start the server */127ss = new ServerSocket(0);128(new Thread(this)).start();129130/* establish http connection to server */131String uri = "http://localhost:" +132Integer.toString(ss.getLocalPort()) +133"/foo";134URL url = new URL(uri);135HttpURLConnection http = (HttpURLConnection)url.openConnection();136137/*138* Server should only send headers if TE:trailers139* specified - see updated HTTP 1.1 spec.140*/141http.setRequestProperty("TE", "trailers");142143/* Time how long the getInputStream takes */144long ts = System.currentTimeMillis();145InputStream in = http.getInputStream();146long te = System.currentTimeMillis();147148/*149* If getInputStream takes >2 seconds it probably means150* that the implementation is waiting for the chunks to151* arrive.152*/153if ( (te-ts) > 2000) {154throw new Exception("getInputStream didn't return immediately");155}156157/*158* Read the stream and checksum it as it arrives159*/160int nread;161int cs = 0;162byte b[] = new byte[1024];163do {164nread = in.read(b);165if (nread > 0) {166for (int i=0; i<nread; i++) {167cs = (cs + b[i]) % 65536;168}169}170} while (nread > 0);171172/*173* Verify that the checksums match174*/175String trailer = http.getHeaderField("Checksum");176if (trailer == null) {177throw new Exception("Checksum trailer missing from response");178}179int rcvd_cs = Integer.parseInt(trailer);180if (rcvd_cs != cs) {181throw new Exception("Trailer checksum doesn't equal calculated checksum");182}183184http.disconnect();185186}187188public static void main(String args[]) throws Exception {189new ChunkedEncoding();190}191192}193194195