Path: blob/master/test/jdk/sun/net/www/protocol/http/CloseOptionHeader.java
41159 views
/*1* Copyright (c) 2004, 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 618920626* @modules java.base/sun.net.www27* @library /test/lib28* @run main/othervm -Dhttp.keepAlive=false CloseOptionHeader29* @summary HTTP client should set "Connection: close" header in request when keepalive is disabled30*/3132import java.net.*;33import java.util.*;34import java.io.*;35import sun.net.www.MessageHeader;36import jdk.test.lib.net.URIBuilder;3738public class CloseOptionHeader implements Runnable {39static ServerSocket ss;40static boolean hasCloseHeader = false;4142/*43* "Our" http server44*/45public void run() {46try {47Socket s = ss.accept();4849/* check the request to find close connection option header */50InputStream is = s.getInputStream ();51MessageHeader mh = new MessageHeader(is);52String connHeader = mh.findValue("Connection");53if (connHeader != null && connHeader.equalsIgnoreCase("close")) {54hasCloseHeader = true;55}5657PrintStream out = new PrintStream(58new BufferedOutputStream(59s.getOutputStream() ));6061/* response 200 */62out.print("HTTP/1.1 200 OK\r\n");63out.print("Content-Type: text/html; charset=iso-8859-1\r\n");64out.print("Content-Length: 0\r\n");65out.print("Connection: close\r\n");66out.print("\r\n");67out.print("\r\n");6869out.flush();7071s.close();72ss.close();73} catch (Exception e) {74e.printStackTrace();75}76}7778public static void main(String args[]) throws Exception {79Thread tester = new Thread(new CloseOptionHeader());8081/* start the server */82InetAddress loopback = InetAddress.getLoopbackAddress();83ss = new ServerSocket();84ss.bind(new InetSocketAddress(loopback, 0));85tester.start();8687/* connect to the server just started88* server then check the request to see whether89* there is a close connection option header in it90*/91URL url = URIBuilder.newBuilder()92.scheme("http")93.host(ss.getInetAddress())94.port(ss.getLocalPort())95.toURL();96HttpURLConnection huc = (HttpURLConnection)url.openConnection();97huc.connect();98huc.getResponseCode();99huc.disconnect();100101tester.join();102103if (!hasCloseHeader) {104throw new RuntimeException("Test failed : should see 'close' connection header");105}106}107108}109110111