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  * @modules java.base/sun.net.www
  28  * @run main/othervm -Dhttp.keepAlive=false CloseOptionHeader
  29  * @summary  HTTP client should set "Connection: close" header in request when keepalive is disabled
  30  */
  31 
  32 import java.net.*;
  33 import java.util.*;
  34 import java.io.*;
  35 import sun.net.www.MessageHeader;
  36 
  37 
  38 public class CloseOptionHeader implements Runnable {
  39     static ServerSocket ss;
  40     static boolean hasCloseHeader = false;
  41 
  42     /*
  43      * "Our" http server
  44      */
  45     public void run() {
  46         try {
  47             Socket s = ss.accept();
  48 
  49             /* check the request to find close connection option header */
  50             InputStream is = s.getInputStream ();
  51             MessageHeader mh = new MessageHeader(is);
  52             String connHeader = mh.findValue("Connection");
  53             if (connHeader != null && connHeader.equalsIgnoreCase("close")) {
  54                 hasCloseHeader = true;
  55             }
  56 
  57             PrintStream out = new PrintStream(
  58                                  new BufferedOutputStream(
  59                                     s.getOutputStream() ));
  60 
  61             /* response 200 */
  62             out.print("HTTP/1.1 200 OK\r\n");
  63             out.print("Content-Type: text/html; charset=iso-8859-1\r\n");
  64             out.print("Content-Length: 0\r\n");
  65             out.print("Connection: close\r\n");
  66             out.print("\r\n");
  67             out.print("\r\n");
  68 
  69             out.flush();
  70 
  71             s.close();
  72             ss.close();
  73         } catch (Exception e) {
  74             e.printStackTrace();
  75         }
  76     }
  77 
  78     public static void main(String args[]) throws Exception {
  79         Thread tester = new Thread(new CloseOptionHeader());
  80 
  81         /* start the server */
  82         ss = new ServerSocket(0);
  83         tester.start();
  84 
  85         /* connect to the server just started
  86          * server then check the request to see whether
  87          * there is a close connection option header in it
  88          */
  89         URL url = new URL("http://localhost:" + ss.getLocalPort());
  90         HttpURLConnection huc = (HttpURLConnection)url.openConnection();
  91         huc.connect();
  92         huc.getResponseCode();
  93         huc.disconnect();
  94 
  95         tester.join();
  96 
  97         if (!hasCloseHeader) {
  98             throw new RuntimeException("Test failed : should see 'close' connection header");
  99         }
 100     }
 101 
 102 }