Path: blob/master/test/jdk/sun/net/www/protocol/http/ChunkedErrorStream.java
41159 views
/*1* Copyright (c) 2006, 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 6488669 6595324 699349026* @library /test/lib27* @modules jdk.httpserver28* @run main/othervm ChunkedErrorStream29* @summary Chunked ErrorStream tests30*/3132import java.net.*;33import java.io.*;34import com.sun.net.httpserver.*;35import jdk.test.lib.net.URIBuilder;3637/**38* Part 1: 648866939* 1) Http server that responds with an error code (>=400)40* and a chunked response body. It also indicates that41* the connection will be closed.42* 2) Client sends request to server and tries to43* getErrorStream(). Some data must be able to be read44* from the errorStream.45*46* Part 2: 659532447* 1) Http server that responds with an error code (>=400)48* and a chunked response body greater than49* sun.net.http.errorstream.bufferSize, 4K + 10 bytes.50* 2) Client sends request to server and tries to51* getErrorStream(). 4K + 10 bytes must be read from52* the errorStream.53*54* Part 3: 699349055* Reuse persistent connection from part 2, the error stream56* buffering will have set a reduced timeout on the socket and57* tried to reset it to the default, infinity. Client must not58* throw a timeout exception. If it does, it indicates that the59* default timeout was not reset correctly.60* If no timeout exception is thrown, it does not guarantee that61* the timeout was reset correctly, as there is a potential race62* between the sleeping server and the client thread. Typically,63* 1000 millis has been enought to reliable reproduce this problem64* since the error stream buffering sets the timeout to 60 millis.65*/6667public class ChunkedErrorStream68{69com.sun.net.httpserver.HttpServer httpServer;7071static {72// Enable ErrorStream buffering73System.getProperties().setProperty("sun.net.http.errorstream.enableBuffering", "true");7475// No need to set this as 4K is the default76// System.getProperties().setProperty("sun.net.http.errorstream.bufferSize", "4096");77}7879public static void main(String[] args) {80new ChunkedErrorStream();81}8283public ChunkedErrorStream() {84try {85startHttpServer();86doClient();87} catch (IOException ioe) {88ioe.printStackTrace();89} finally {90httpServer.stop(1);91}92}9394void doClient() {95for (int times=0; times<3; times++) {96HttpURLConnection uc = null;97try {98String path = "/test/";99if (times == 0) {100path += "first";101} else {102path += "second";103}104105URL url = URIBuilder.newBuilder()106.scheme("http")107.host(httpServer.getAddress().getAddress())108.port(httpServer.getAddress().getPort())109.path(path)110.toURLUnchecked();111112System.out.println("Trying " + url);113uc = (HttpURLConnection)url.openConnection();114uc.getInputStream();115116throw new RuntimeException("Failed: getInputStream should throw and IOException");117} catch (IOException e) {118if (e instanceof SocketTimeoutException) {119e.printStackTrace();120throw new RuntimeException("Failed: SocketTimeoutException should not happen");121}122123// This is what we expect to happen.124InputStream es = uc.getErrorStream();125byte[] ba = new byte[1024];126int count = 0, ret;127try {128while ((ret = es.read(ba)) != -1)129count += ret;130es.close();131} catch (IOException ioe) {132ioe.printStackTrace();133}134135if (count == 0)136throw new RuntimeException("Failed: ErrorStream returning 0 bytes");137138if (times >= 1 && count != (4096+10))139throw new RuntimeException("Failed: ErrorStream returning " + count +140" bytes. Expecting " + (4096+10));141142System.out.println("Read " + count + " bytes from the errorStream");143}144}145}146147/**148* Http Server149*/150void startHttpServer() throws IOException {151InetAddress lba = InetAddress.getLoopbackAddress();152InetSocketAddress addr = new InetSocketAddress(lba, 0);153httpServer = com.sun.net.httpserver.HttpServer.create(addr, 0);154155// create HttpServer context156httpServer.createContext("/test/first", new FirstHandler());157httpServer.createContext("/test/second", new SecondHandler());158159httpServer.start();160}161162class FirstHandler implements HttpHandler {163public void handle(HttpExchange t) throws IOException {164InputStream is = t.getRequestBody();165byte[] ba = new byte[1024];166while (is.read(ba) != -1);167is.close();168169Headers resHeaders = t.getResponseHeaders();170resHeaders.add("Connection", "close");171t.sendResponseHeaders(404, 0);172OutputStream os = t.getResponseBody();173174// actual data doesn't matter. Just send 2K worth.175byte b = 'a';176for (int i=0; i<2048; i++)177os.write(b);178179os.close();180t.close();181}182}183184static class SecondHandler implements HttpHandler {185/* count greater than 0, slow response */186static int count = 0;187188public void handle(HttpExchange t) throws IOException {189InputStream is = t.getRequestBody();190byte[] ba = new byte[1024];191while (is.read(ba) != -1);192is.close();193194if (count > 0) {195System.out.println("server sleeping...");196try { Thread.sleep(1000); } catch(InterruptedException e) {}197}198count++;199200t.sendResponseHeaders(404, 0);201OutputStream os = t.getResponseBody();202203// actual data doesn't matter. Just send more than 4K worth204byte b = 'a';205for (int i=0; i<(4096+10); i++)206os.write(b);207208os.close();209t.close();210}211}212}213214215