Path: blob/master/test/jdk/java/net/URLConnection/ChunkedEncoding.java
41149 views
/*1* Copyright (c) 2001, 2019, 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* @test25* @bug 433392026* @bug 439454827* @library /test/lib28* @summary Check that chunked encoding response doesn't cause29* getInputStream to block until last chunk arrives.30* Also regression against NPE in ChunkedInputStream.31*/32import java.net.*;33import java.io.*;34import java.util.Random;35import jdk.test.lib.net.URIBuilder;3637public class ChunkedEncoding implements Runnable {3839ServerSocket ss;4041/*42* Our "http" server to return a chunked response43*/44public void run() {45try {46Socket s = ss.accept();4748PrintStream out = new PrintStream(49new BufferedOutputStream(50s.getOutputStream() ));5152/* send the header */53out.print("HTTP/1.1 200\r\n");54out.print("Transfer-Encoding: chunked\r\n");55out.print("Content-Type: text/html\r\n");56out.print("\r\n");57out.flush();5859/* delay the server before first chunk */60Thread.sleep(5000);6162/*63* Our response will be of random length64* but > 32k65*/66Random rand = new Random();6768int len;69do {70len = rand.nextInt(128*1024);71} while (len < 32*1024);7273/*74* Our chunk size will be 2-32k75*/76int chunkSize;77do {78chunkSize = rand.nextInt(len / 3);79} while (chunkSize < 2*1024);8081/*82* Generate random content and check sum it83*/84byte buf[] = new byte[len];85int cs = 0;86for (int i=0; i<len; i++) {87buf[i] = (byte)('a' + rand.nextInt(26));88cs = (cs + buf[i]) % 65536;89}9091/*92* Stream the chunks to the client93*/94int remaining = len;95int pos = 0;96while (remaining > 0) {97int size = Math.min(remaining, chunkSize);98out.print( Integer.toHexString(size) );99out.print("\r\n");100out.write( buf, pos, size );101pos += size;102remaining -= size;103out.print("\r\n");104out.flush();105}106107/* send EOF chunk */108out.print("0\r\n");109out.flush();110111/*112* Send trailer with checksum113*/114String trailer = "Checksum:" + cs + "\r\n";115out.print(trailer);116out.print("\r\n");117out.flush();118119/*120* Sleep added to avoid connection reset121* on the client side122*/123Thread.sleep(1000);124s.close();125ss.close();126} catch (Exception e) {127e.printStackTrace();128}129}130131ChunkedEncoding() throws Exception {132133/* start the server */134ss = new ServerSocket();135ss.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));136(new Thread(this)).start();137/* establish http connection to server */138URL url = URIBuilder.newBuilder()139.scheme("http")140.loopback()141.port(ss.getLocalPort())142.path("/foo")143.toURL();144HttpURLConnection http = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);145146/*147* Server should only send headers if TE:trailers148* specified - see updated HTTP 1.1 spec.149*/150http.setRequestProperty("TE", "trailers");151152/* Time how long the getInputStream takes */153long ts = System.currentTimeMillis();154InputStream in = http.getInputStream();155long te = System.currentTimeMillis();156157/*158* If getInputStream takes >2 seconds it probably means159* that the implementation is waiting for the chunks to160* arrive.161*/162if ( (te-ts) > 2000) {163throw new Exception("getInputStream didn't return immediately");164}165166/*167* Read the stream and checksum it as it arrives168*/169int nread;170int cs = 0;171byte b[] = new byte[1024];172do {173nread = in.read(b);174if (nread > 0) {175for (int i=0; i<nread; i++) {176cs = (cs + b[i]) % 65536;177}178}179} while (nread > 0);180181/*182* Verify that the checksums match183*/184String trailer = http.getHeaderField("Checksum");185if (trailer == null) {186throw new Exception("Checksum trailer missing from response");187}188int rcvd_cs = Integer.parseInt(trailer);189if (rcvd_cs != cs) {190throw new Exception("Trailer checksum doesn't equal calculated checksum");191}192193http.disconnect();194195}196197public static void main(String args[]) throws Exception {198new ChunkedEncoding();199}200201}202203204