/* * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, * CA 95054 USA or visit www.sun.com if you need additional information or * have any questions. */ /** * @test * @bug 5026745 * @run main/othervm/timeout=500 Test * @summary Cannot flush output stream when writing to an HttpUrlConnection */ import java.io.*; import java.net.*; import com.sun.net.httpserver.*; public class Test implements HttpHandler { static int count = 0; static final String str1 = "Helloworld1234567890abcdefghijklmnopqrstuvwxyz"+ "1234567890abcdefkjsdlkjflkjsldkfjlsdkjflkj"+ "1434567890abcdefkjsdlkjflkjsldkfjlsdkjflkj"; static final String str2 = "Helloworld1234567890abcdefghijklmnopqrstuvwxyz"+ "1234567890"; public void handle(HttpExchange exchange) { String reqbody; try { switch (count) { case 0: /* test1 -- keeps conn alive */ case 1: /* test2 -- closes conn */ printRequestURI(exchange); reqbody = read(exchange.getRequestBody()); if (!reqbody.equals(str1)) { exchange.sendResponseHeaders(500, 0); break; } Headers headers = exchange.getRequestHeaders(); String chunk = headers.getFirst("Transfer-encoding"); if (!"chunked".equals (chunk)) { exchange.sendResponseHeaders(501, 0); break; } exchange.sendResponseHeaders(200, reqbody.length()); write(exchange.getResponseBody(), reqbody); if (count == 1) { Headers resHeaders = exchange.getResponseHeaders() ; resHeaders.set("Connection", "close"); } break; case 2: /* test 3 */ printRequestURI(exchange); reqbody = read(exchange.getRequestBody()); if (!reqbody.equals(str2)) { exchange.sendResponseHeaders(500, 0); break; } headers = exchange.getRequestHeaders(); int clen = Integer.parseInt( headers.getFirst("Content-length")); if (clen != str2.length()) { exchange.sendResponseHeaders(501, 0); break; } Headers resHeaders = exchange.getResponseHeaders() ; resHeaders.set("Connection", "close"); exchange.sendResponseHeaders(200, reqbody.length()); write(exchange.getResponseBody(), reqbody); break; case 3: /* test 4 */ case 4: /* test 5 */ printRequestURI(exchange); break; case 5: /* test 6 */ printRequestURI(exchange); resHeaders = exchange.getResponseHeaders() ; resHeaders.set("Location", "http://foo.bar/"); resHeaders.set("Connection", "close"); exchange.sendResponseHeaders(307, 0); break; case 6: /* test 7 */ case 7: /* test 8 */ printRequestURI(exchange); reqbody = read(exchange.getRequestBody()); if (reqbody != null && !"".equals(reqbody)) { exchange.sendResponseHeaders(501, 0); break; } resHeaders = exchange.getResponseHeaders() ; resHeaders.set("Connection", "close"); exchange.sendResponseHeaders(200, 0); break; case 8: /* test 9 */ printRequestURI(exchange); reqbody = read(exchange.getRequestBody()); if (!reqbody.equals(str1)) { exchange.sendResponseHeaders(500, 0); break; } headers = exchange.getRequestHeaders(); chunk = headers.getFirst("Transfer-encoding"); if (!"chunked".equals(chunk)) { exchange.sendResponseHeaders(501, 0); break; } exchange.sendResponseHeaders(200, reqbody.length()); write(exchange.getResponseBody(), reqbody); break; case 9: /* test10 */ printRequestURI(exchange); InputStream is = exchange.getRequestBody(); String s = read (is, str1.length()); boolean error = false; for (int i=10; i< 200 * 1024; i++) { byte c = (byte)is.read(); if (c != (byte)i) { error = true; System.out.println ("error at position " + i); } } if (!s.equals(str1) ) { System.out.println ("received string : " + s); exchange.sendResponseHeaders(500, 0); } else if (error) { System.out.println ("error"); exchange.sendResponseHeaders(500, 0); } else { exchange.sendResponseHeaders(200, 0); } break; } exchange.close(); count ++; } catch (IOException e) { e.printStackTrace(); } } static void printRequestURI(HttpExchange exchange) { URI uri = exchange.getRequestURI(); System.out.println("HttpServer: handle " + uri); } static String read (InputStream is, int len) { try { byte[] ba = new byte [len]; int c; int l = 0; while ((c= is.read(ba, l, ba.length-l)) != -1 && l