Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/protocol/http/CloseOptionHeader.java
38867 views
/*1* Copyright (c) 2004, 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* @run main/othervm -Dhttp.keepAlive=false CloseOptionHeader27* @summary HTTP client should set "Connection: close" header in request when keepalive is disabled28*/2930import java.net.*;31import java.util.*;32import java.io.*;33import sun.net.www.MessageHeader;343536public class CloseOptionHeader implements Runnable {37static ServerSocket ss;38static boolean hasCloseHeader = false;3940/*41* "Our" http server42*/43public void run() {44try {45Socket s = ss.accept();4647/* check the request to find close connection option header */48InputStream is = s.getInputStream ();49MessageHeader mh = new MessageHeader(is);50String connHeader = mh.findValue("Connection");51if (connHeader != null && connHeader.equalsIgnoreCase("close")) {52hasCloseHeader = true;53}5455PrintStream out = new PrintStream(56new BufferedOutputStream(57s.getOutputStream() ));5859/* response 200 */60out.print("HTTP/1.1 200 OK\r\n");61out.print("Content-Type: text/html; charset=iso-8859-1\r\n");62out.print("Content-Length: 0\r\n");63out.print("Connection: close\r\n");64out.print("\r\n");65out.print("\r\n");6667out.flush();6869s.close();70ss.close();71} catch (Exception e) {72e.printStackTrace();73}74}7576public static void main(String args[]) throws Exception {77Thread tester = new Thread(new CloseOptionHeader());7879/* start the server */80ss = new ServerSocket(0);81tester.start();8283/* connect to the server just started84* server then check the request to see whether85* there is a close connection option header in it86*/87URL url = new URL("http://localhost:" + ss.getLocalPort());88HttpURLConnection huc = (HttpURLConnection)url.openConnection();89huc.connect();90huc.getResponseCode();91huc.disconnect();9293tester.join();9495if (!hasCloseHeader) {96throw new RuntimeException("Test failed : should see 'close' connection header");97}98}99100}101102103