Path: blob/master/test/jdk/sun/net/www/http/ChunkedOutputStream/Test.java
41154 views
/*1* Copyright (c) 2004, 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 5026745 663104826* @modules jdk.httpserver27* @library /test/lib28* @run main/othervm/timeout=500 Test29* @summary Cannot flush output stream when writing to an HttpUrlConnection30*/3132import java.io.*;33import java.net.*;34import com.sun.net.httpserver.*;3536import jdk.test.lib.net.URIBuilder;3738public class Test implements HttpHandler {3940static volatile int count = 0;4142static final String str1 = "Helloworld1234567890abcdefghijklmnopqrstuvwxyz"+43"1234567890abcdefkjsdlkjflkjsldkfjlsdkjflkj"+44"1434567890abcdefkjsdlkjflkjsldkfjlsdkjflkj";4546static final String str2 = "Helloworld1234567890abcdefghijklmnopqrstuvwxyz"+47"1234567890";4849public void handle(HttpExchange exchange) {50String reqbody;51try {52switch (exchange.getRequestURI().toString()) {53case "/test/test1": /* test1 -- keeps conn alive */54case "/test/test2": /* test2 -- closes conn */55printRequestURI(exchange);56reqbody = read(exchange.getRequestBody());57if (!reqbody.equals(str1)) {58exchange.sendResponseHeaders(500, 0);59break;60}6162Headers headers = exchange.getRequestHeaders();63String chunk = headers.getFirst("Transfer-encoding");6465if (!"chunked".equals (chunk)) {66exchange.sendResponseHeaders(501, 0);67break;68}6970exchange.sendResponseHeaders(200, reqbody.length());71write(exchange.getResponseBody(), reqbody);7273if (count == 1) {74Headers resHeaders = exchange.getResponseHeaders() ;75resHeaders.set("Connection", "close");76}77break;78case "/test/test3": /* test 3 */79printRequestURI(exchange);80reqbody = read(exchange.getRequestBody());8182if (!reqbody.equals(str2)) {83exchange.sendResponseHeaders(500, 0);84break;85}86headers = exchange.getRequestHeaders();87int clen = Integer.parseInt( headers.getFirst("Content-length"));8889if (clen != str2.length()) {90exchange.sendResponseHeaders(501, 0);91break;92}93Headers resHeaders = exchange.getResponseHeaders() ;94resHeaders.set("Connection", "close");9596exchange.sendResponseHeaders(200, reqbody.length());97write(exchange.getResponseBody(), reqbody);98break;99case "/test/test4": /* test 4 */100case "/test/test5": /* test 5 */101printRequestURI(exchange);102break;103case "/test/test6": /* test 6 */104printRequestURI(exchange);105resHeaders = exchange.getResponseHeaders() ;106resHeaders.set("Location", "http://foo.bar/");107resHeaders.set("Connection", "close");108exchange.sendResponseHeaders(307, 0);109break;110case "/test/test7": /* test 7 */111case "/test/test8": /* test 8 */112printRequestURI(exchange);113reqbody = read(exchange.getRequestBody());114if (reqbody != null && !"".equals(reqbody)) {115exchange.sendResponseHeaders(501, 0);116break;117}118resHeaders = exchange.getResponseHeaders() ;119resHeaders.set("Connection", "close");120exchange.sendResponseHeaders(200, 0);121break;122case "/test/test9": /* test 9 */123printRequestURI(exchange);124reqbody = read(exchange.getRequestBody());125if (!reqbody.equals(str1)) {126exchange.sendResponseHeaders(500, 0);127break;128}129130headers = exchange.getRequestHeaders();131chunk = headers.getFirst("Transfer-encoding");132if (!"chunked".equals(chunk)) {133exchange.sendResponseHeaders(501, 0);134break;135}136137exchange.sendResponseHeaders(200, reqbody.length());138write(exchange.getResponseBody(), reqbody);139break;140case "/test/test10": /* test10 */141printRequestURI(exchange);142InputStream is = exchange.getRequestBody();143String s = read (is, str1.length());144145boolean error = false;146for (int i=10; i< 200 * 1024; i++) {147byte c = (byte)is.read();148149if (c != (byte)i) {150error = true;151System.out.println ("error at position " + i);152}153}154if (!s.equals(str1) ) {155System.out.println ("received string : " + s);156exchange.sendResponseHeaders(500, 0);157} else if (error) {158System.out.println ("error");159exchange.sendResponseHeaders(500, 0);160} else {161exchange.sendResponseHeaders(200, 0);162}163break;164case "/test/test11": /* test11 */165printRequestURI(exchange);166is = exchange.getRequestBody();167s = read (is, str1.length());168169error = false;170for (int i=10; i< 30 * 1024; i++) {171byte c = (byte)is.read();172173if (c != (byte)i) {174error = true;175System.out.println ("error at position " + i);176}177}178if (!s.equals(str1) ) {179System.out.println ("received string : " + s);180exchange.sendResponseHeaders(500, 0);181} else if (error) {182System.out.println ("error");183exchange.sendResponseHeaders(500, 0);184} else {185exchange.sendResponseHeaders(200, 0);186}187break;188case "/test/test12": /* test12 */189printRequestURI(exchange);190is = exchange.getRequestBody();191192error = false;193for (int i=10; i< 30 * 1024; i++) {194byte c = (byte)is.read();195196if (c != (byte)i) {197error = true;198System.out.println ("error at position " + i);199}200}201if (error) {202System.out.println ("error");203exchange.sendResponseHeaders(500, 0);204} else {205exchange.sendResponseHeaders(200, 0);206}207break;208}209count ++;210exchange.close();211} catch (IOException e) {212e.printStackTrace();213}214}215216static void printRequestURI(HttpExchange exchange) {217URI uri = exchange.getRequestURI();218System.out.println("HttpServer: handle " + uri);219}220221222static String read (InputStream is, int len) {223try {224byte[] ba = new byte [len];225int c;226int l = 0;227while ((c= is.read(ba, l, ba.length-l)) != -1 && l<len) {228l += c;229}230return new String (ba, 0, l, "ISO8859-1");231} catch (Exception e) {232e.printStackTrace();233}234return null;235}236237static String read(InputStream is) {238try {239byte[] ba = new byte [8096];240int off = 0, c;241while ((c= is.read(ba, off, ba.length)) != -1) {242off += c;243}244return new String(ba, 0, off, "ISO8859-1");245} catch (Exception e) {246e.printStackTrace();247}248return null;249}250251static void write(OutputStream os, String str) {252try {253byte[] ba = str.getBytes("ISO8859-1");254os.write(ba);255} catch (Exception e) {256e.printStackTrace();257}258}259260static void readAndCompare(InputStream is, String cmp) throws IOException {261int c;262byte buf[] = new byte [1024];263int off = 0;264int len = 1024;265while ((c=is.read(buf, off, len)) != -1) {266off += c;267len -= c;268}269String s1 = new String(buf, 0, off, "ISO8859_1");270if (!cmp.equals(s1)) {271throw new IOException("strings not same");272}273}274275/* basic chunked test (runs twice) */276277static void test1(URL url) throws Exception {278System.out.println("client opening connection to: " + url);279HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();280urlc.setChunkedStreamingMode (20);281urlc.setDoOutput(true);282urlc.setRequestMethod ("POST");283OutputStream os = urlc.getOutputStream ();284os.write (str1.getBytes());285os.close();286InputStream is = urlc.getInputStream();287readAndCompare (is, str1);288is.close();289}290291/* basic fixed length test */292293static void test3(URL url) throws Exception {294System.out.println("client opening connection to: " + url);295HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();296urlc.setFixedLengthStreamingMode (str2.length());297urlc.setDoOutput(true);298urlc.setRequestMethod ("POST");299OutputStream os = urlc.getOutputStream ();300os.write (str2.getBytes());301os.close();302InputStream is = urlc.getInputStream();303readAndCompare (is, str2);304is.close();305}306307/* write too few bytes */308309static void test4(URL url) throws Exception {310System.out.println("client opening connection to: " + url);311HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();312urlc.setFixedLengthStreamingMode (str2.length()+1);313urlc.setDoOutput(true);314urlc.setRequestMethod ("POST");315OutputStream os = urlc.getOutputStream ();316os.write (str2.getBytes());317try {318os.close();319throw new Exception ("should have thrown IOException");320} catch (IOException e) {}321}322323/* write too many bytes */324325static void test5(URL url) throws Exception {326System.out.println("client opening connection to: " + url);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(URL url) throws Exception {341System.out.println("client opening connection to: " + url);342HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();343urlc.setChunkedStreamingMode (20);344urlc.setDoOutput(true);345urlc.setRequestMethod ("POST");346OutputStream os = urlc.getOutputStream ();347os.write (str1.getBytes());348os.close();349try {350InputStream is = urlc.getInputStream();351throw new Exception ("should have gotten HttpRetryException");352} catch (HttpRetryException e) {353if (e.responseCode() != 307) {354throw new Exception ("Wrong response code " + e.responseCode());355}356if (!e.getLocation().equals ("http://foo.bar/")) {357throw new Exception ("Wrong location " + e.getLocation());358}359}360}361362/* next two tests send zero length posts */363364static void test7(URL url) throws Exception {365System.out.println("client opening connection to: " + url);366HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();367urlc.setChunkedStreamingMode (20);368urlc.setDoOutput(true);369urlc.setRequestMethod ("POST");370OutputStream os = urlc.getOutputStream ();371os.close();372int ret = urlc.getResponseCode();373if (ret != 200) {374throw new Exception ("Expected 200: got " + ret);375}376}377378static void test8(URL url) throws Exception {379System.out.println("client opening connection to: " + url);380HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();381urlc.setFixedLengthStreamingMode (0);382urlc.setDoOutput(true);383urlc.setRequestMethod ("POST");384OutputStream os = urlc.getOutputStream ();385os.close();386int ret = urlc.getResponseCode();387if (ret != 200) {388throw new Exception ("Expected 200: got " + ret);389}390}391392/* calling setChunkedStreamingMode with -1 should entail using393the default chunk size */394static void test9(URL url) throws Exception {395System.out.println("client opening connection to: " + url);396HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();397urlc.setChunkedStreamingMode (-1);398urlc.setDoOutput(true);399urlc.setRequestMethod ("POST");400OutputStream os = urlc.getOutputStream ();401os.write (str1.getBytes());402os.close();403InputStream is = urlc.getInputStream();404readAndCompare (is, str1);405is.close();406}407408static void test10(URL url) throws Exception {409System.out.println("client opening connection to: " + url);410HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();411urlc.setChunkedStreamingMode (4 * 1024);412urlc.setDoOutput(true);413urlc.setRequestMethod ("POST");414OutputStream os = urlc.getOutputStream ();415byte[] buf = new byte [200 * 1024];416for (int i=0; i< 200 * 1024; i++) {417buf[i] = (byte) i;418}419/* write a small bit first, and then the large buffer */420os.write (str1.getBytes());421os.write (buf, 10, buf.length - 10); /* skip 10 bytes to test offset */422os.close();423InputStream is = urlc.getInputStream();424is.close();425int ret = urlc.getResponseCode();426if (ret != 200) {427throw new Exception ("Expected 200: got " + ret);428}429}430431static void test11(URL url) throws Exception {432System.out.println("client opening connection to: " + url);433HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();434urlc.setChunkedStreamingMode (36 * 1024);435urlc.setDoOutput(true);436urlc.setRequestMethod ("POST");437OutputStream os = urlc.getOutputStream ();438byte[] buf = new byte [30 * 1024];439for (int i=0; i< 30 * 1024; i++) {440buf[i] = (byte) i;441}442/* write a small bit first, and then the large buffer */443os.write (str1.getBytes());444//os.write (buf, 10, buf.length - 10); /* skip 10 bytes to test offset */445os.write (buf, 10, (10 * 1024) - 10);446os.write (buf, (10 * 1024), (10 * 1024));447os.write (buf, (20 * 1024), (10 * 1024));448os.close();449InputStream is = urlc.getInputStream();450is.close();451int ret = urlc.getResponseCode();452if (ret != 200) {453throw new Exception ("Expected 200: got " + ret);454}455}456457static void test12(URL url) throws Exception {458System.out.println("client opening connection to: " + url);459HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();460urlc.setChunkedStreamingMode (36 * 1024);461urlc.setDoOutput(true);462urlc.setRequestMethod ("POST");463OutputStream os = urlc.getOutputStream ();464byte[] buf = new byte [30 * 1024];465for (int i=0; i< 30 * 1024; i++) {466buf[i] = (byte) i;467}468os.write (buf, 10, buf.length - 10); /* skip 10 bytes to test offset */469os.close();470InputStream is = urlc.getInputStream();471is.close();472int ret = urlc.getResponseCode();473if (ret != 200) {474throw new Exception ("Expected 200: got " + ret);475}476}477478479static HttpServer httpserver;480481private static URL buildTestURL(int port, String path)482throws MalformedURLException, URISyntaxException {483return URIBuilder.newBuilder()484.scheme("http")485.loopback()486.port(port)487.path(path)488.toURL();489}490491public static void main (String[] args) throws Exception {492try {493httpserver = HttpServer.create(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0);494httpserver.createContext("/test/", new Test());495httpserver.start();496497int port = httpserver.getAddress().getPort();498499System.out.println ("Server started: listening on port: " + port);500test1(buildTestURL(port, "/test/test1"));501test1(buildTestURL(port, "/test/test2"));502test3(buildTestURL(port, "/test/test3"));503test4(buildTestURL(port, "/test/test4"));504test5(buildTestURL(port, "/test/test5"));505test6(buildTestURL(port, "/test/test6"));506test7(buildTestURL(port, "/test/test7"));507test8(buildTestURL(port, "/test/test8"));508test9(buildTestURL(port, "/test/test9"));509test10(buildTestURL(port, "/test/test10"));510test11(buildTestURL(port, "/test/test11"));511test12(buildTestURL(port, "/test/test12"));512} finally {513if (httpserver != null)514httpserver.stop(0);515}516}517518}519520521