Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/protocol/http/ChunkedErrorStream.java
38867 views
/*1* Copyright (c) 2006, 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 6488669 6595324 699349026* @run main/othervm ChunkedErrorStream27* @summary Chunked ErrorStream tests28*/2930import java.net.*;31import java.io.*;32import com.sun.net.httpserver.*;3334/**35* Part 1: 648866936* 1) Http server that responds with an error code (>=400)37* and a chunked response body. It also indicates that38* the connection will be closed.39* 2) Client sends request to server and tries to40* getErrorStream(). Some data must be able to be read41* from the errorStream.42*43* Part 2: 659532444* 1) Http server that responds with an error code (>=400)45* and a chunked response body greater than46* sun.net.http.errorstream.bufferSize, 4K + 10 bytes.47* 2) Client sends request to server and tries to48* getErrorStream(). 4K + 10 bytes must be read from49* the errorStream.50*51* Part 3: 699349052* Reuse persistent connection from part 2, the error stream53* buffering will have set a reduced timeout on the socket and54* tried to reset it to the default, infinity. Client must not55* throw a timeout exception. If it does, it indicates that the56* default timeout was not reset correctly.57* If no timeout exception is thrown, it does not guarantee that58* the timeout was reset correctly, as there is a potential race59* between the sleeping server and the client thread. Typically,60* 1000 millis has been enought to reliable reproduce this problem61* since the error stream buffering sets the timeout to 60 millis.62*/6364public class ChunkedErrorStream65{66com.sun.net.httpserver.HttpServer httpServer;6768static {69// Enable ErrorStream buffering70System.getProperties().setProperty("sun.net.http.errorstream.enableBuffering", "true");7172// No need to set this as 4K is the default73// System.getProperties().setProperty("sun.net.http.errorstream.bufferSize", "4096");74}7576public static void main(String[] args) {77new ChunkedErrorStream();78}7980public ChunkedErrorStream() {81try {82startHttpServer();83doClient();84} catch (IOException ioe) {85ioe.printStackTrace();86} finally {87httpServer.stop(1);88}89}9091void doClient() {92for (int times=0; times<3; times++) {93HttpURLConnection uc = null;94try {95InetSocketAddress address = httpServer.getAddress();96String URLStr = "http://localhost:" + address.getPort() + "/test/";97if (times == 0) {98URLStr += "first";99} else {100URLStr += "second";101}102103System.out.println("Trying " + URLStr);104URL url = new URL(URLStr);105uc = (HttpURLConnection)url.openConnection();106uc.getInputStream();107108throw new RuntimeException("Failed: getInputStream should throw and IOException");109} catch (IOException e) {110if (e instanceof SocketTimeoutException) {111e.printStackTrace();112throw new RuntimeException("Failed: SocketTimeoutException should not happen");113}114115// This is what we expect to happen.116InputStream es = uc.getErrorStream();117byte[] ba = new byte[1024];118int count = 0, ret;119try {120while ((ret = es.read(ba)) != -1)121count += ret;122es.close();123} catch (IOException ioe) {124ioe.printStackTrace();125}126127if (count == 0)128throw new RuntimeException("Failed: ErrorStream returning 0 bytes");129130if (times >= 1 && count != (4096+10))131throw new RuntimeException("Failed: ErrorStream returning " + count +132" bytes. Expecting " + (4096+10));133134System.out.println("Read " + count + " bytes from the errorStream");135}136}137}138139/**140* Http Server141*/142void startHttpServer() throws IOException {143httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0);144145// create HttpServer context146httpServer.createContext("/test/first", new FirstHandler());147httpServer.createContext("/test/second", new SecondHandler());148149httpServer.start();150}151152class FirstHandler implements HttpHandler {153public void handle(HttpExchange t) throws IOException {154InputStream is = t.getRequestBody();155byte[] ba = new byte[1024];156while (is.read(ba) != -1);157is.close();158159Headers resHeaders = t.getResponseHeaders();160resHeaders.add("Connection", "close");161t.sendResponseHeaders(404, 0);162OutputStream os = t.getResponseBody();163164// actual data doesn't matter. Just send 2K worth.165byte b = 'a';166for (int i=0; i<2048; i++)167os.write(b);168169os.close();170t.close();171}172}173174static class SecondHandler implements HttpHandler {175/* count greater than 0, slow response */176static int count = 0;177178public void handle(HttpExchange t) throws IOException {179InputStream is = t.getRequestBody();180byte[] ba = new byte[1024];181while (is.read(ba) != -1);182is.close();183184if (count > 0) {185System.out.println("server sleeping...");186try { Thread.sleep(1000); } catch(InterruptedException e) {}187}188count++;189190t.sendResponseHeaders(404, 0);191OutputStream os = t.getResponseBody();192193// actual data doesn't matter. Just send more than 4K worth194byte b = 'a';195for (int i=0; i<(4096+10); i++)196os.write(b);197198os.close();199t.close();200}201}202}203204205