Path: blob/master/test/jdk/com/sun/net/httpserver/bugs/B6529200.java
41155 views
/*1* Copyright (c) 2007, 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 652920026* @run main/othervm B652920027* @run main/othervm -Djava.net.preferIPv6Addresses=true B652920028* @summary lightweight http server does not work with http1.0 clients29*/3031import com.sun.net.httpserver.*;3233import java.util.*;34import java.util.concurrent.*;35import java.io.*;36import java.net.*;37import java.security.*;38import java.security.cert.*;39import javax.net.ssl.*;4041public class B6529200 {4243public static void main (String[] args) throws Exception {44Handler handler = new Handler();45InetAddress loopback = InetAddress.getLoopbackAddress();46InetSocketAddress addr = new InetSocketAddress (loopback, 0);47HttpServer server = HttpServer.create (addr, 0);48HttpContext ctx = server.createContext ("/test", handler);4950ExecutorService executor = Executors.newCachedThreadPool();51server.setExecutor (executor);52server.start ();5354/* test 1: keep-alive */5556Socket sock = new Socket (loopback, server.getAddress().getPort());57OutputStream os = sock.getOutputStream();58System.out.println ("GET /test/foo HTTP/1.0\r\nConnection: keep-alive\r\n\r\n");59os.write ("GET /test/foo HTTP/1.0\r\nConnection: keep-alive\r\n\r\n".getBytes());60os.flush();61InputStream is = sock.getInputStream();62StringBuffer s = new StringBuffer();63boolean finished = false;6465sock.setSoTimeout (10 * 1000);66try {67while (!finished) {68char c = (char) is.read();69s.append (c);70finished = s.indexOf ("\r\n\r\nhello") != -1;71/* test will timeout otherwise */72}73} catch (SocketTimeoutException e) {74server.stop (2);75executor.shutdown ();76throw new RuntimeException ("Test failed in test1");77}7879System.out.println (new String (s));8081/* test 2: even though we request keep-alive, server must close82* because it is sending unknown content length response */8384System.out.println("GET /test/foo HTTP/1.0\r\nConnection: keep-alive\r\n\r\n");85os.write ("GET /test/foo HTTP/1.0\r\nConnection: keep-alive\r\n\r\n".getBytes());86os.flush();87int i=0,c;88byte [] buf = new byte [8*1024];89try {90while ((c=is.read()) != -1) {91buf[i++] = (byte)c;92}93} catch (SocketTimeoutException e) {94server.stop (2);95executor.shutdown ();96throw new RuntimeException ("Test failed in test2");97}9899String ss = new String (buf, "ISO-8859-1");100if (ss.indexOf ("\r\n\r\nhello world") == -1) {101server.stop (2);102executor.shutdown ();103throw new RuntimeException ("Test failed in test2: wrong string");104}105System.out.println (ss);106is.close ();107server.stop (2);108executor.shutdown();109}110111112static class Handler implements HttpHandler {113int invocation = 1;114public void handle (HttpExchange t)115throws IOException116{117InputStream is;118OutputStream os;119switch (invocation++) {120case 1:121is = t.getRequestBody();122while (is.read() != -1) ;123is.close();124t.sendResponseHeaders (200, "hello".length());125os = t.getResponseBody();126os.write ("hello".getBytes());127os.close();128break;129case 2:130is = t.getRequestBody();131while (is.read() != -1) ;132is.close();133t.sendResponseHeaders (200, 0);134os = t.getResponseBody();135os.write ("hello world".getBytes());136os.close();137break;138}139}140}141}142143144