Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/http/KeepAliveStream/KeepAliveStreamCloseWithWrongContentLength.java
38868 views
/*1* Copyright (c) 2002, 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 453324326* @summary Closing a keep alive stream gives NullPointerException27* @run main/othervm/timeout=30 KeepAliveStreamCloseWithWrongContentLength28*/2930import java.net.*;31import java.io.*;3233public class KeepAliveStreamCloseWithWrongContentLength {3435static class XServer extends Thread {36ServerSocket srv;37Socket s;38InputStream is;39OutputStream os;4041XServer (ServerSocket s) {42srv = s;43}4445public void run() {46try {47s = srv.accept ();48// read HTTP request from client49InputStream is = s.getInputStream();50// read the first ten bytes51for (int i=0; i<10; i++) {52is.read();53}54OutputStreamWriter ow =55new OutputStreamWriter((os = s.getOutputStream()));56ow.write("HTTP/1.0 200 OK\n");5758// Note: The client expects 10 bytes.59ow.write("Content-Length: 10\n");60ow.write("Content-Type: text/html\n");6162// Note: If this line is missing, everything works fine.63ow.write("Connection: Keep-Alive\n");64ow.write("\n");6566// Note: The (buggy) server only sends 9 bytes.67ow.write("123456789");68ow.flush();69} catch (Exception e) {70} finally {71try {if (os != null) { os.close(); }} catch (IOException e) {}72}73}74}7576public static void main (String[] args) throws Exception {77ServerSocket serversocket = new ServerSocket (0);78try {79int port = serversocket.getLocalPort ();80XServer server = new XServer (serversocket);81server.start ();82URL url = new URL ("http://localhost:"+port);83HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();84InputStream is = urlc.getInputStream ();85int c = 0;86while (c != -1) {87try {88c=is.read();89} catch (IOException ioe) {90is.read ();91break;92}93}94is.close();95} catch (IOException e) {96return;97} catch (NullPointerException e) {98throw new RuntimeException (e);99} finally {100if (serversocket != null) serversocket.close();101}102}103}104105106