Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/net/httpserver/bugs/B6529200.java
38867 views
/*1* Copyright (c) 2007, 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 652920026* @run main/othervm B652920027* @summary lightweight http server does not work with http1.0 clients28*/2930import com.sun.net.httpserver.*;3132import java.util.*;33import java.util.concurrent.*;34import java.io.*;35import java.net.*;36import java.security.*;37import java.security.cert.*;38import javax.net.ssl.*;3940public class B6529200 {4142public static void main (String[] args) throws Exception {43Handler handler = new Handler();44InetSocketAddress addr = new InetSocketAddress (0);45HttpServer server = HttpServer.create (addr, 0);46HttpContext ctx = server.createContext ("/test", handler);4748ExecutorService executor = Executors.newCachedThreadPool();49server.setExecutor (executor);50server.start ();5152/* test 1: keep-alive */5354Socket sock = new Socket ("localhost", server.getAddress().getPort());55OutputStream os = sock.getOutputStream();56System.out.println ("GET /test/foo HTTP/1.0\r\nConnection: keep-alive\r\n\r\n");57os.write ("GET /test/foo HTTP/1.0\r\nConnection: keep-alive\r\n\r\n".getBytes());58os.flush();59InputStream is = sock.getInputStream();60StringBuffer s = new StringBuffer();61boolean finished = false;6263sock.setSoTimeout (10 * 1000);64try {65while (!finished) {66char c = (char) is.read();67s.append (c);68finished = s.indexOf ("\r\n\r\nhello") != -1;69/* test will timeout otherwise */70}71} catch (SocketTimeoutException e) {72server.stop (2);73executor.shutdown ();74throw new RuntimeException ("Test failed in test1");75}7677System.out.println (new String (s));7879/* test 2: even though we request keep-alive, server must close80* because it is sending unknown content length response */8182System.out.println("GET /test/foo HTTP/1.0\r\nConnection: keep-alive\r\n\r\n");83os.write ("GET /test/foo HTTP/1.0\r\nConnection: keep-alive\r\n\r\n".getBytes());84os.flush();85int i=0,c;86byte [] buf = new byte [8*1024];87try {88while ((c=is.read()) != -1) {89buf[i++] = (byte)c;90}91} catch (SocketTimeoutException e) {92server.stop (2);93executor.shutdown ();94throw new RuntimeException ("Test failed in test2");95}9697String ss = new String (buf, "ISO-8859-1");98if (ss.indexOf ("\r\n\r\nhello world") == -1) {99server.stop (2);100executor.shutdown ();101throw new RuntimeException ("Test failed in test2: wrong string");102}103System.out.println (ss);104is.close ();105server.stop (2);106executor.shutdown();107}108109110static class Handler implements HttpHandler {111int invocation = 1;112public void handle (HttpExchange t)113throws IOException114{115InputStream is;116OutputStream os;117switch (invocation++) {118case 1:119is = t.getRequestBody();120while (is.read() != -1) ;121is.close();122t.sendResponseHeaders (200, "hello".length());123os = t.getResponseBody();124os.write ("hello".getBytes());125os.close();126break;127case 2:128is = t.getRequestBody();129while (is.read() != -1) ;130is.close();131t.sendResponseHeaders (200, 0);132os = t.getResponseBody();133os.write ("hello world".getBytes());134os.close();135break;136}137}138}139}140141142