0N/A/*
2362N/A * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/**
0N/A * @test
0N/A * @bug 6529200
0N/A * @run main/othervm B6529200
0N/A * @summary lightweight http server does not work with http1.0 clients
0N/A */
0N/A
0N/Aimport com.sun.net.httpserver.*;
0N/A
0N/Aimport java.util.*;
0N/Aimport java.util.concurrent.*;
0N/Aimport java.io.*;
0N/Aimport java.net.*;
0N/Aimport java.security.*;
0N/Aimport java.security.cert.*;
0N/Aimport javax.net.ssl.*;
0N/A
0N/Apublic class B6529200 {
0N/A
0N/A public static void main (String[] args) throws Exception {
0N/A Handler handler = new Handler();
0N/A InetSocketAddress addr = new InetSocketAddress (0);
0N/A HttpServer server = HttpServer.create (addr, 0);
0N/A HttpContext ctx = server.createContext ("/test", handler);
0N/A
0N/A ExecutorService executor = Executors.newCachedThreadPool();
0N/A server.setExecutor (executor);
0N/A server.start ();
0N/A
0N/A /* test 1: keep-alive */
0N/A
0N/A Socket sock = new Socket ("localhost", server.getAddress().getPort());
0N/A OutputStream os = sock.getOutputStream();
0N/A System.out.println ("GET /test/foo HTTP/1.0\r\nConnection: keep-alive\r\n\r\n");
0N/A os.write ("GET /test/foo HTTP/1.0\r\nConnection: keep-alive\r\n\r\n".getBytes());
0N/A os.flush();
0N/A InputStream is = sock.getInputStream();
0N/A StringBuffer s = new StringBuffer();
0N/A boolean finished = false;
0N/A
0N/A sock.setSoTimeout (10 * 1000);
0N/A try {
0N/A while (!finished) {
0N/A char c = (char) is.read();
0N/A s.append (c);
0N/A finished = s.indexOf ("\r\n\r\nhello") != -1;
0N/A /* test will timeout otherwise */
0N/A }
0N/A } catch (SocketTimeoutException e) {
0N/A server.stop (2);
0N/A executor.shutdown ();
0N/A throw new RuntimeException ("Test failed in test1");
0N/A }
0N/A
0N/A System.out.println (new String (s));
0N/A
0N/A /* test 2: even though we request keep-alive, server must close
0N/A * because it is sending unknown content length response */
0N/A
0N/A System.out.println("GET /test/foo HTTP/1.0\r\nConnection: keep-alive\r\n\r\n");
0N/A os.write ("GET /test/foo HTTP/1.0\r\nConnection: keep-alive\r\n\r\n".getBytes());
0N/A os.flush();
0N/A int i=0,c;
0N/A byte [] buf = new byte [8*1024];
0N/A try {
0N/A while ((c=is.read()) != -1) {
0N/A buf[i++] = (byte)c;
0N/A }
0N/A } catch (SocketTimeoutException e) {
0N/A server.stop (2);
0N/A executor.shutdown ();
0N/A throw new RuntimeException ("Test failed in test2");
0N/A }
0N/A
0N/A String ss = new String (buf, "ISO-8859-1");
0N/A if (ss.indexOf ("\r\n\r\nhello world") == -1) {
0N/A server.stop (2);
0N/A executor.shutdown ();
0N/A throw new RuntimeException ("Test failed in test2: wrong string");
0N/A }
0N/A System.out.println (ss);
0N/A is.close ();
0N/A server.stop (2);
0N/A executor.shutdown();
0N/A }
0N/A
0N/A
0N/A static class Handler implements HttpHandler {
0N/A int invocation = 1;
0N/A public void handle (HttpExchange t)
0N/A throws IOException
0N/A {
0N/A InputStream is;
0N/A OutputStream os;
0N/A switch (invocation++) {
0N/A case 1:
0N/A is = t.getRequestBody();
0N/A while (is.read() != -1) ;
0N/A is.close();
0N/A t.sendResponseHeaders (200, "hello".length());
0N/A os = t.getResponseBody();
0N/A os.write ("hello".getBytes());
0N/A os.close();
0N/A break;
0N/A case 2:
0N/A is = t.getRequestBody();
0N/A while (is.read() != -1) ;
0N/A is.close();
0N/A t.sendResponseHeaders (200, 0);
0N/A os = t.getResponseBody();
0N/A os.write ("hello world".getBytes());
0N/A os.close();
0N/A break;
0N/A }
0N/A }
0N/A }
0N/A}