Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/http/ChunkedOutputStream/Test.java
38867 views
/*1* Copyright (c) 2004, 2010, 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 5026745 663104826* @run main/othervm/timeout=500 Test27* @summary Cannot flush output stream when writing to an HttpUrlConnection28*/2930import java.io.*;31import java.net.*;32import com.sun.net.httpserver.*;3334public class Test implements HttpHandler {3536static volatile int count = 0;3738static final String str1 = "Helloworld1234567890abcdefghijklmnopqrstuvwxyz"+39"1234567890abcdefkjsdlkjflkjsldkfjlsdkjflkj"+40"1434567890abcdefkjsdlkjflkjsldkfjlsdkjflkj";4142static final String str2 = "Helloworld1234567890abcdefghijklmnopqrstuvwxyz"+43"1234567890";4445public void handle(HttpExchange exchange) {46String reqbody;47try {48switch (exchange.getRequestURI().toString()) {49case "/test/test1": /* test1 -- keeps conn alive */50case "/test/test2": /* test2 -- closes conn */51printRequestURI(exchange);52reqbody = read(exchange.getRequestBody());53if (!reqbody.equals(str1)) {54exchange.sendResponseHeaders(500, 0);55break;56}5758Headers headers = exchange.getRequestHeaders();59String chunk = headers.getFirst("Transfer-encoding");6061if (!"chunked".equals (chunk)) {62exchange.sendResponseHeaders(501, 0);63break;64}6566exchange.sendResponseHeaders(200, reqbody.length());67write(exchange.getResponseBody(), reqbody);6869if (count == 1) {70Headers resHeaders = exchange.getResponseHeaders() ;71resHeaders.set("Connection", "close");72}73break;74case "/test/test3": /* test 3 */75printRequestURI(exchange);76reqbody = read(exchange.getRequestBody());7778if (!reqbody.equals(str2)) {79exchange.sendResponseHeaders(500, 0);80break;81}82headers = exchange.getRequestHeaders();83int clen = Integer.parseInt( headers.getFirst("Content-length"));8485if (clen != str2.length()) {86exchange.sendResponseHeaders(501, 0);87break;88}89Headers resHeaders = exchange.getResponseHeaders() ;90resHeaders.set("Connection", "close");9192exchange.sendResponseHeaders(200, reqbody.length());93write(exchange.getResponseBody(), reqbody);94break;95case "/test/test4": /* test 4 */96case "/test/test5": /* test 5 */97printRequestURI(exchange);98break;99case "/test/test6": /* test 6 */100printRequestURI(exchange);101resHeaders = exchange.getResponseHeaders() ;102resHeaders.set("Location", "http://foo.bar/");103resHeaders.set("Connection", "close");104exchange.sendResponseHeaders(307, 0);105break;106case "/test/test7": /* test 7 */107case "/test/test8": /* test 8 */108printRequestURI(exchange);109reqbody = read(exchange.getRequestBody());110if (reqbody != null && !"".equals(reqbody)) {111exchange.sendResponseHeaders(501, 0);112break;113}114resHeaders = exchange.getResponseHeaders() ;115resHeaders.set("Connection", "close");116exchange.sendResponseHeaders(200, 0);117break;118case "/test/test9": /* test 9 */119printRequestURI(exchange);120reqbody = read(exchange.getRequestBody());121if (!reqbody.equals(str1)) {122exchange.sendResponseHeaders(500, 0);123break;124}125126headers = exchange.getRequestHeaders();127chunk = headers.getFirst("Transfer-encoding");128if (!"chunked".equals(chunk)) {129exchange.sendResponseHeaders(501, 0);130break;131}132133exchange.sendResponseHeaders(200, reqbody.length());134write(exchange.getResponseBody(), reqbody);135break;136case "/test/test10": /* test10 */137printRequestURI(exchange);138InputStream is = exchange.getRequestBody();139String s = read (is, str1.length());140141boolean error = false;142for (int i=10; i< 200 * 1024; i++) {143byte c = (byte)is.read();144145if (c != (byte)i) {146error = true;147System.out.println ("error at position " + i);148}149}150if (!s.equals(str1) ) {151System.out.println ("received string : " + s);152exchange.sendResponseHeaders(500, 0);153} else if (error) {154System.out.println ("error");155exchange.sendResponseHeaders(500, 0);156} else {157exchange.sendResponseHeaders(200, 0);158}159break;160case "/test/test11": /* test11 */161printRequestURI(exchange);162is = exchange.getRequestBody();163s = read (is, str1.length());164165error = false;166for (int i=10; i< 30 * 1024; i++) {167byte c = (byte)is.read();168169if (c != (byte)i) {170error = true;171System.out.println ("error at position " + i);172}173}174if (!s.equals(str1) ) {175System.out.println ("received string : " + s);176exchange.sendResponseHeaders(500, 0);177} else if (error) {178System.out.println ("error");179exchange.sendResponseHeaders(500, 0);180} else {181exchange.sendResponseHeaders(200, 0);182}183break;184case "/test/test12": /* test12 */185printRequestURI(exchange);186is = exchange.getRequestBody();187188error = false;189for (int i=10; i< 30 * 1024; i++) {190byte c = (byte)is.read();191192if (c != (byte)i) {193error = true;194System.out.println ("error at position " + i);195}196}197if (error) {198System.out.println ("error");199exchange.sendResponseHeaders(500, 0);200} else {201exchange.sendResponseHeaders(200, 0);202}203break;204}205count ++;206exchange.close();207} catch (IOException e) {208e.printStackTrace();209}210}211212static void printRequestURI(HttpExchange exchange) {213URI uri = exchange.getRequestURI();214System.out.println("HttpServer: handle " + uri);215}216217218static String read (InputStream is, int len) {219try {220byte[] ba = new byte [len];221int c;222int l = 0;223while ((c= is.read(ba, l, ba.length-l)) != -1 && l<len) {224l += c;225}226return new String (ba, 0, l, "ISO8859-1");227} catch (Exception e) {228e.printStackTrace();229}230return null;231}232233static String read(InputStream is) {234try {235byte[] ba = new byte [8096];236int off = 0, c;237while ((c= is.read(ba, off, ba.length)) != -1) {238off += c;239}240return new String(ba, 0, off, "ISO8859-1");241} catch (Exception e) {242e.printStackTrace();243}244return null;245}246247static void write(OutputStream os, String str) {248try {249byte[] ba = str.getBytes("ISO8859-1");250os.write(ba);251} catch (Exception e) {252e.printStackTrace();253}254}255256static void readAndCompare(InputStream is, String cmp) throws IOException {257int c;258byte buf[] = new byte [1024];259int off = 0;260int len = 1024;261while ((c=is.read(buf, off, len)) != -1) {262off += c;263len -= c;264}265String s1 = new String(buf, 0, off, "ISO8859_1");266if (!cmp.equals(s1)) {267throw new IOException("strings not same");268}269}270271/* basic chunked test (runs twice) */272273static void test1 (String u) throws Exception {274URL url = new URL (u);275System.out.println ("client opening connection to: " + u);276HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();277urlc.setChunkedStreamingMode (20);278urlc.setDoOutput(true);279urlc.setRequestMethod ("POST");280OutputStream os = urlc.getOutputStream ();281os.write (str1.getBytes());282os.close();283InputStream is = urlc.getInputStream();284readAndCompare (is, str1);285is.close();286}287288/* basic fixed length test */289290static void test3 (String u) throws Exception {291URL url = new URL (u);292System.out.println ("client opening connection to: " + u);293HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();294urlc.setFixedLengthStreamingMode (str2.length());295urlc.setDoOutput(true);296urlc.setRequestMethod ("POST");297OutputStream os = urlc.getOutputStream ();298os.write (str2.getBytes());299os.close();300InputStream is = urlc.getInputStream();301readAndCompare (is, str2);302is.close();303}304305/* write too few bytes */306307static void test4 (String u) throws Exception {308URL url = new URL (u);309System.out.println ("client opening connection to: " + u);310HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();311urlc.setFixedLengthStreamingMode (str2.length()+1);312urlc.setDoOutput(true);313urlc.setRequestMethod ("POST");314OutputStream os = urlc.getOutputStream ();315os.write (str2.getBytes());316try {317os.close();318throw new Exception ("should have thrown IOException");319} catch (IOException e) {}320}321322/* write too many bytes */323324static void test5 (String u) throws Exception {325URL url = new URL (u);326System.out.println ("client opening connection to: " + u);327HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();328urlc.setFixedLengthStreamingMode (str2.length()-1);329urlc.setDoOutput(true);330urlc.setRequestMethod ("POST");331OutputStream os = urlc.getOutputStream ();332try {333os.write (str2.getBytes());334throw new Exception ("should have thrown IOException");335} catch (IOException e) {}336}337338/* check for HttpRetryException on redirection */339340static void test6 (String u) throws Exception {341URL url = new URL (u);342System.out.println ("client opening connection to: " + u);343HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();344urlc.setChunkedStreamingMode (20);345urlc.setDoOutput(true);346urlc.setRequestMethod ("POST");347OutputStream os = urlc.getOutputStream ();348os.write (str1.getBytes());349os.close();350try {351InputStream is = urlc.getInputStream();352throw new Exception ("should have gotten HttpRetryException");353} catch (HttpRetryException e) {354if (e.responseCode() != 307) {355throw new Exception ("Wrong response code " + e.responseCode());356}357if (!e.getLocation().equals ("http://foo.bar/")) {358throw new Exception ("Wrong location " + e.getLocation());359}360}361}362363/* next two tests send zero length posts */364365static void test7 (String u) throws Exception {366URL url = new URL (u);367System.out.println ("client opening connection to: " + u);368HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();369urlc.setChunkedStreamingMode (20);370urlc.setDoOutput(true);371urlc.setRequestMethod ("POST");372OutputStream os = urlc.getOutputStream ();373os.close();374int ret = urlc.getResponseCode();375if (ret != 200) {376throw new Exception ("Expected 200: got " + ret);377}378}379380static void test8 (String u) throws Exception {381URL url = new URL (u);382System.out.println ("client opening connection to: " + u);383HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();384urlc.setFixedLengthStreamingMode (0);385urlc.setDoOutput(true);386urlc.setRequestMethod ("POST");387OutputStream os = urlc.getOutputStream ();388os.close();389int ret = urlc.getResponseCode();390if (ret != 200) {391throw new Exception ("Expected 200: got " + ret);392}393}394395/* calling setChunkedStreamingMode with -1 should entail using396the default chunk size */397static void test9 (String u) throws Exception {398URL url = new URL (u);399System.out.println ("client opening connection to: " + u);400HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();401urlc.setChunkedStreamingMode (-1);402urlc.setDoOutput(true);403urlc.setRequestMethod ("POST");404OutputStream os = urlc.getOutputStream ();405os.write (str1.getBytes());406os.close();407InputStream is = urlc.getInputStream();408readAndCompare (is, str1);409is.close();410}411412static void test10 (String u) throws Exception {413URL url = new URL (u);414System.out.println ("client opening connection to: " + u);415HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();416urlc.setChunkedStreamingMode (4 * 1024);417urlc.setDoOutput(true);418urlc.setRequestMethod ("POST");419OutputStream os = urlc.getOutputStream ();420byte[] buf = new byte [200 * 1024];421for (int i=0; i< 200 * 1024; i++) {422buf[i] = (byte) i;423}424/* write a small bit first, and then the large buffer */425os.write (str1.getBytes());426os.write (buf, 10, buf.length - 10); /* skip 10 bytes to test offset */427os.close();428InputStream is = urlc.getInputStream();429is.close();430int ret = urlc.getResponseCode();431if (ret != 200) {432throw new Exception ("Expected 200: got " + ret);433}434}435436static void test11 (String u) throws Exception {437URL url = new URL (u);438System.out.println ("client opening connection to: " + u);439HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();440urlc.setChunkedStreamingMode (36 * 1024);441urlc.setDoOutput(true);442urlc.setRequestMethod ("POST");443OutputStream os = urlc.getOutputStream ();444byte[] buf = new byte [30 * 1024];445for (int i=0; i< 30 * 1024; i++) {446buf[i] = (byte) i;447}448/* write a small bit first, and then the large buffer */449os.write (str1.getBytes());450//os.write (buf, 10, buf.length - 10); /* skip 10 bytes to test offset */451os.write (buf, 10, (10 * 1024) - 10);452os.write (buf, (10 * 1024), (10 * 1024));453os.write (buf, (20 * 1024), (10 * 1024));454os.close();455InputStream is = urlc.getInputStream();456is.close();457int ret = urlc.getResponseCode();458if (ret != 200) {459throw new Exception ("Expected 200: got " + ret);460}461}462463static void test12 (String u) throws Exception {464URL url = new URL (u);465System.out.println ("client opening connection to: " + u);466HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();467urlc.setChunkedStreamingMode (36 * 1024);468urlc.setDoOutput(true);469urlc.setRequestMethod ("POST");470OutputStream os = urlc.getOutputStream ();471byte[] buf = new byte [30 * 1024];472for (int i=0; i< 30 * 1024; i++) {473buf[i] = (byte) i;474}475os.write (buf, 10, buf.length - 10); /* skip 10 bytes to test offset */476os.close();477InputStream is = urlc.getInputStream();478is.close();479int ret = urlc.getResponseCode();480if (ret != 200) {481throw new Exception ("Expected 200: got " + ret);482}483}484485486static com.sun.net.httpserver.HttpServer httpserver;487488public static void main (String[] args) throws Exception {489try {490httpserver = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0);491HttpContext ctx = httpserver.createContext("/test/", new Test() );492httpserver.start();493494int port = httpserver.getAddress().getPort();495496System.out.println ("Server started: listening on port: " + port);497test1("http://localhost:"+ port + "/test/test1");498test1("http://localhost:"+ port + "/test/test2");499test3("http://localhost:"+ port + "/test/test3");500test4("http://localhost:"+ port + "/test/test4");501test5("http://localhost:"+ port + "/test/test5");502test6("http://localhost:"+ port + "/test/test6");503test7("http://localhost:"+ port + "/test/test7");504test8("http://localhost:"+ port + "/test/test8");505test9("http://localhost:"+ port + "/test/test9");506test10("http://localhost:"+ port + "/test/test10");507test11("http://localhost:"+ port + "/test/test11");508test12("http://localhost:"+ port + "/test/test12");509} finally {510if (httpserver != null)511httpserver.stop(0);512}513}514515}516517518