Path: blob/master/test/jdk/sun/net/www/http/KeepAliveStream/KeepAliveStreamCloseWithWrongContentLength.java
41155 views
/*1* Copyright (c) 2002, 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 453324326* @summary Closing a keep alive stream gives NullPointerException27* @library /test/lib28* @run main/othervm/timeout=30 KeepAliveStreamCloseWithWrongContentLength29*/3031import java.net.*;32import java.io.*;33import jdk.test.lib.net.URIBuilder;3435public class KeepAliveStreamCloseWithWrongContentLength {3637static class XServer extends Thread {38ServerSocket srv;39Socket s;40InputStream is;41OutputStream os;4243XServer (ServerSocket s) {44srv = s;45}4647public void run() {48try {49s = srv.accept ();50// read HTTP request from client51InputStream is = s.getInputStream();52// read the first ten bytes53for (int i=0; i<10; i++) {54is.read();55}56OutputStreamWriter ow =57new OutputStreamWriter((os = s.getOutputStream()));58ow.write("HTTP/1.0 200 OK\n");5960// Note: The client expects 10 bytes.61ow.write("Content-Length: 10\n");62ow.write("Content-Type: text/html\n");6364// Note: If this line is missing, everything works fine.65ow.write("Connection: Keep-Alive\n");66ow.write("\n");6768// Note: The (buggy) server only sends 9 bytes.69ow.write("123456789");70ow.flush();71} catch (Exception e) {72} finally {73try {if (os != null) { os.close(); }} catch (IOException e) {}74}75}76}7778public static void main (String[] args) throws Exception {79final InetAddress loopback = InetAddress.getLoopbackAddress();80final ServerSocket serversocket = new ServerSocket();81serversocket.bind(new InetSocketAddress(loopback, 0));8283try {84int port = serversocket.getLocalPort ();85XServer server = new XServer (serversocket);86server.start ();87URL url = URIBuilder.newBuilder()88.scheme("http")89.loopback()90.port(port)91.toURL();92HttpURLConnection urlc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);93InputStream is = urlc.getInputStream ();94int c = 0;95while (c != -1) {96try {97c=is.read();98} catch (IOException ioe) {99is.read ();100break;101}102}103is.close();104} catch (IOException e) {105return;106} catch (NullPointerException e) {107throw new RuntimeException (e);108} finally {109serversocket.close();110}111}112}113114115