1 /* 2 * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /** 25 * @test 26 * @bug 6189206 27 * @run main/othervm -Dhttp.keepAlive=false CloseOptionHeader 28 * @summary HTTP client should set "Connection: close" header in request when keepalive is disabled 29 */ 30 31 import java.net.*; 32 import java.util.*; 33 import java.io.*; 34 import sun.net.www.MessageHeader; 35 36 37 public class CloseOptionHeader implements Runnable { 38 static ServerSocket ss; 39 static boolean hasCloseHeader = false; 40 41 /* 42 * "Our" http server 43 */ 44 public void run() { 45 try { 46 Socket s = ss.accept(); 47 48 /* check the request to find close connection option header */ 49 InputStream is = s.getInputStream (); 50 MessageHeader mh = new MessageHeader(is); 51 String connHeader = mh.findValue("Connection"); 52 if (connHeader != null && connHeader.equalsIgnoreCase("close")) { 53 hasCloseHeader = true; 54 } 55 56 PrintStream out = new PrintStream( 57 new BufferedOutputStream( 58 s.getOutputStream() )); 59 60 /* response 200 */ 61 out.print("HTTP/1.1 200 OK\r\n"); 62 out.print("Content-Type: text/html; charset=iso-8859-1\r\n"); 63 out.print("Content-Length: 0\r\n"); 64 out.print("Connection: close\r\n"); 65 out.print("\r\n"); 66 out.print("\r\n"); 67 68 out.flush(); 69 70 s.close(); 71 ss.close(); 72 } catch (Exception e) { 73 e.printStackTrace(); 74 } 75 } 76 77 public static void main(String args[]) throws Exception { 78 Thread tester = new Thread(new CloseOptionHeader()); 79 80 /* start the server */ 81 ss = new ServerSocket(0); 82 tester.start(); 83 84 /* connect to the server just started 85 * server then check the request to see whether 86 * there is a close connection option header in it 87 */ 88 URL url = new URL("http://localhost:" + ss.getLocalPort()); 89 HttpURLConnection huc = (HttpURLConnection)url.openConnection(); 90 huc.connect(); 91 huc.getResponseCode(); 92 huc.disconnect(); 93 94 tester.join(); 95 96 if (!hasCloseHeader) { 97 throw new RuntimeException("Test failed : should see 'close' connection header"); 98 } 99 } 100 101 }