0N/A/*
3261N/A * Copyright (c) 2004, 2010, 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
65N/A * @bug 5026745 6631048
0N/A * @run main/othervm/timeout=500 Test
0N/A * @summary Cannot flush output stream when writing to an HttpUrlConnection
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.net.*;
0N/Aimport com.sun.net.httpserver.*;
0N/A
0N/Apublic class Test implements HttpHandler {
0N/A
2612N/A static volatile int count = 0;
0N/A
0N/A static final String str1 = "Helloworld1234567890abcdefghijklmnopqrstuvwxyz"+
0N/A "1234567890abcdefkjsdlkjflkjsldkfjlsdkjflkj"+
0N/A "1434567890abcdefkjsdlkjflkjsldkfjlsdkjflkj";
0N/A
0N/A static final String str2 = "Helloworld1234567890abcdefghijklmnopqrstuvwxyz"+
0N/A "1234567890";
0N/A
0N/A public void handle(HttpExchange exchange) {
0N/A String reqbody;
0N/A try {
2612N/A switch (exchange.getRequestURI().toString()) {
2612N/A case "/test/test1": /* test1 -- keeps conn alive */
2612N/A case "/test/test2": /* test2 -- closes conn */
0N/A printRequestURI(exchange);
0N/A reqbody = read(exchange.getRequestBody());
0N/A if (!reqbody.equals(str1)) {
0N/A exchange.sendResponseHeaders(500, 0);
0N/A break;
0N/A }
0N/A
0N/A Headers headers = exchange.getRequestHeaders();
0N/A String chunk = headers.getFirst("Transfer-encoding");
0N/A
0N/A if (!"chunked".equals (chunk)) {
0N/A exchange.sendResponseHeaders(501, 0);
0N/A break;
0N/A }
0N/A
0N/A exchange.sendResponseHeaders(200, reqbody.length());
0N/A write(exchange.getResponseBody(), reqbody);
0N/A
0N/A if (count == 1) {
0N/A Headers resHeaders = exchange.getResponseHeaders() ;
0N/A resHeaders.set("Connection", "close");
0N/A }
0N/A break;
2612N/A case "/test/test3": /* test 3 */
0N/A printRequestURI(exchange);
0N/A reqbody = read(exchange.getRequestBody());
0N/A
0N/A if (!reqbody.equals(str2)) {
0N/A exchange.sendResponseHeaders(500, 0);
0N/A break;
0N/A }
0N/A headers = exchange.getRequestHeaders();
0N/A int clen = Integer.parseInt( headers.getFirst("Content-length"));
0N/A
0N/A if (clen != str2.length()) {
0N/A exchange.sendResponseHeaders(501, 0);
0N/A break;
0N/A }
0N/A Headers resHeaders = exchange.getResponseHeaders() ;
0N/A resHeaders.set("Connection", "close");
0N/A
0N/A exchange.sendResponseHeaders(200, reqbody.length());
0N/A write(exchange.getResponseBody(), reqbody);
0N/A break;
2612N/A case "/test/test4": /* test 4 */
2612N/A case "/test/test5": /* test 5 */
0N/A printRequestURI(exchange);
0N/A break;
2612N/A case "/test/test6": /* test 6 */
0N/A printRequestURI(exchange);
0N/A resHeaders = exchange.getResponseHeaders() ;
0N/A resHeaders.set("Location", "http://foo.bar/");
0N/A resHeaders.set("Connection", "close");
0N/A exchange.sendResponseHeaders(307, 0);
0N/A break;
2612N/A case "/test/test7": /* test 7 */
2612N/A case "/test/test8": /* test 8 */
0N/A printRequestURI(exchange);
0N/A reqbody = read(exchange.getRequestBody());
0N/A if (reqbody != null && !"".equals(reqbody)) {
0N/A exchange.sendResponseHeaders(501, 0);
0N/A break;
0N/A }
0N/A resHeaders = exchange.getResponseHeaders() ;
0N/A resHeaders.set("Connection", "close");
0N/A exchange.sendResponseHeaders(200, 0);
0N/A break;
2612N/A case "/test/test9": /* test 9 */
0N/A printRequestURI(exchange);
0N/A reqbody = read(exchange.getRequestBody());
0N/A if (!reqbody.equals(str1)) {
0N/A exchange.sendResponseHeaders(500, 0);
0N/A break;
0N/A }
0N/A
0N/A headers = exchange.getRequestHeaders();
0N/A chunk = headers.getFirst("Transfer-encoding");
0N/A if (!"chunked".equals(chunk)) {
0N/A exchange.sendResponseHeaders(501, 0);
0N/A break;
0N/A }
0N/A
0N/A exchange.sendResponseHeaders(200, reqbody.length());
0N/A write(exchange.getResponseBody(), reqbody);
0N/A break;
2612N/A case "/test/test10": /* test10 */
0N/A printRequestURI(exchange);
0N/A InputStream is = exchange.getRequestBody();
0N/A String s = read (is, str1.length());
0N/A
0N/A boolean error = false;
0N/A for (int i=10; i< 200 * 1024; i++) {
0N/A byte c = (byte)is.read();
0N/A
0N/A if (c != (byte)i) {
0N/A error = true;
0N/A System.out.println ("error at position " + i);
0N/A }
0N/A }
0N/A if (!s.equals(str1) ) {
0N/A System.out.println ("received string : " + s);
0N/A exchange.sendResponseHeaders(500, 0);
0N/A } else if (error) {
0N/A System.out.println ("error");
0N/A exchange.sendResponseHeaders(500, 0);
0N/A } else {
0N/A exchange.sendResponseHeaders(200, 0);
0N/A }
0N/A break;
2612N/A case "/test/test11": /* test11 */
65N/A printRequestURI(exchange);
65N/A is = exchange.getRequestBody();
65N/A s = read (is, str1.length());
65N/A
65N/A error = false;
65N/A for (int i=10; i< 30 * 1024; i++) {
65N/A byte c = (byte)is.read();
65N/A
65N/A if (c != (byte)i) {
65N/A error = true;
65N/A System.out.println ("error at position " + i);
65N/A }
65N/A }
65N/A if (!s.equals(str1) ) {
65N/A System.out.println ("received string : " + s);
65N/A exchange.sendResponseHeaders(500, 0);
65N/A } else if (error) {
65N/A System.out.println ("error");
65N/A exchange.sendResponseHeaders(500, 0);
65N/A } else {
65N/A exchange.sendResponseHeaders(200, 0);
65N/A }
65N/A break;
2612N/A case "/test/test12": /* test12 */
65N/A printRequestURI(exchange);
65N/A is = exchange.getRequestBody();
65N/A
65N/A error = false;
65N/A for (int i=10; i< 30 * 1024; i++) {
65N/A byte c = (byte)is.read();
65N/A
65N/A if (c != (byte)i) {
65N/A error = true;
65N/A System.out.println ("error at position " + i);
65N/A }
65N/A }
65N/A if (error) {
65N/A System.out.println ("error");
65N/A exchange.sendResponseHeaders(500, 0);
65N/A } else {
65N/A exchange.sendResponseHeaders(200, 0);
65N/A }
65N/A break;
0N/A }
2612N/A count ++;
0N/A exchange.close();
0N/A } catch (IOException e) {
0N/A e.printStackTrace();
0N/A }
0N/A }
0N/A
0N/A static void printRequestURI(HttpExchange exchange) {
0N/A URI uri = exchange.getRequestURI();
0N/A System.out.println("HttpServer: handle " + uri);
0N/A }
0N/A
0N/A
0N/A static String read (InputStream is, int len) {
0N/A try {
0N/A byte[] ba = new byte [len];
0N/A int c;
0N/A int l = 0;
0N/A while ((c= is.read(ba, l, ba.length-l)) != -1 && l<len) {
0N/A l += c;
0N/A }
0N/A return new String (ba, 0, l, "ISO8859-1");
0N/A } catch (Exception e) {
0N/A e.printStackTrace();
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A static String read(InputStream is) {
0N/A try {
0N/A byte[] ba = new byte [8096];
0N/A int off = 0, c;
0N/A while ((c= is.read(ba, off, ba.length)) != -1) {
0N/A off += c;
0N/A }
0N/A return new String(ba, 0, off, "ISO8859-1");
0N/A } catch (Exception e) {
0N/A e.printStackTrace();
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A static void write(OutputStream os, String str) {
0N/A try {
0N/A byte[] ba = str.getBytes("ISO8859-1");
0N/A os.write(ba);
0N/A } catch (Exception e) {
0N/A e.printStackTrace();
0N/A }
0N/A }
0N/A
0N/A static void readAndCompare(InputStream is, String cmp) throws IOException {
0N/A int c;
0N/A byte buf[] = new byte [1024];
0N/A int off = 0;
0N/A int len = 1024;
0N/A while ((c=is.read(buf, off, len)) != -1) {
0N/A off += c;
0N/A len -= c;
0N/A }
0N/A String s1 = new String(buf, 0, off, "ISO8859_1");
0N/A if (!cmp.equals(s1)) {
0N/A throw new IOException("strings not same");
0N/A }
0N/A }
0N/A
0N/A /* basic chunked test (runs twice) */
0N/A
0N/A static void test1 (String u) throws Exception {
0N/A URL url = new URL (u);
0N/A System.out.println ("client opening connection to: " + u);
0N/A HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
0N/A urlc.setChunkedStreamingMode (20);
0N/A urlc.setDoOutput(true);
0N/A urlc.setRequestMethod ("POST");
0N/A OutputStream os = urlc.getOutputStream ();
0N/A os.write (str1.getBytes());
0N/A os.close();
0N/A InputStream is = urlc.getInputStream();
0N/A readAndCompare (is, str1);
0N/A is.close();
0N/A }
0N/A
0N/A /* basic fixed length test */
0N/A
0N/A static void test3 (String u) throws Exception {
0N/A URL url = new URL (u);
0N/A System.out.println ("client opening connection to: " + u);
0N/A HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
0N/A urlc.setFixedLengthStreamingMode (str2.length());
0N/A urlc.setDoOutput(true);
0N/A urlc.setRequestMethod ("POST");
0N/A OutputStream os = urlc.getOutputStream ();
0N/A os.write (str2.getBytes());
0N/A os.close();
0N/A InputStream is = urlc.getInputStream();
0N/A readAndCompare (is, str2);
0N/A is.close();
0N/A }
0N/A
0N/A /* write too few bytes */
0N/A
0N/A static void test4 (String u) throws Exception {
0N/A URL url = new URL (u);
0N/A System.out.println ("client opening connection to: " + u);
0N/A HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
0N/A urlc.setFixedLengthStreamingMode (str2.length()+1);
0N/A urlc.setDoOutput(true);
0N/A urlc.setRequestMethod ("POST");
0N/A OutputStream os = urlc.getOutputStream ();
0N/A os.write (str2.getBytes());
0N/A try {
0N/A os.close();
0N/A throw new Exception ("should have thrown IOException");
0N/A } catch (IOException e) {}
0N/A }
0N/A
0N/A /* write too many bytes */
0N/A
0N/A static void test5 (String u) throws Exception {
0N/A URL url = new URL (u);
0N/A System.out.println ("client opening connection to: " + u);
0N/A HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
0N/A urlc.setFixedLengthStreamingMode (str2.length()-1);
0N/A urlc.setDoOutput(true);
0N/A urlc.setRequestMethod ("POST");
0N/A OutputStream os = urlc.getOutputStream ();
0N/A try {
0N/A os.write (str2.getBytes());
0N/A throw new Exception ("should have thrown IOException");
0N/A } catch (IOException e) {}
0N/A }
0N/A
0N/A /* check for HttpRetryException on redirection */
0N/A
0N/A static void test6 (String u) throws Exception {
0N/A URL url = new URL (u);
0N/A System.out.println ("client opening connection to: " + u);
0N/A HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
0N/A urlc.setChunkedStreamingMode (20);
0N/A urlc.setDoOutput(true);
0N/A urlc.setRequestMethod ("POST");
0N/A OutputStream os = urlc.getOutputStream ();
0N/A os.write (str1.getBytes());
0N/A os.close();
0N/A try {
0N/A InputStream is = urlc.getInputStream();
0N/A throw new Exception ("should have gotten HttpRetryException");
0N/A } catch (HttpRetryException e) {
0N/A if (e.responseCode() != 307) {
0N/A throw new Exception ("Wrong response code " + e.responseCode());
0N/A }
0N/A if (!e.getLocation().equals ("http://foo.bar/")) {
0N/A throw new Exception ("Wrong location " + e.getLocation());
0N/A }
0N/A }
0N/A }
0N/A
0N/A /* next two tests send zero length posts */
0N/A
0N/A static void test7 (String u) throws Exception {
0N/A URL url = new URL (u);
0N/A System.out.println ("client opening connection to: " + u);
0N/A HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
0N/A urlc.setChunkedStreamingMode (20);
0N/A urlc.setDoOutput(true);
0N/A urlc.setRequestMethod ("POST");
0N/A OutputStream os = urlc.getOutputStream ();
0N/A os.close();
0N/A int ret = urlc.getResponseCode();
0N/A if (ret != 200) {
0N/A throw new Exception ("Expected 200: got " + ret);
0N/A }
0N/A }
0N/A
0N/A static void test8 (String u) throws Exception {
0N/A URL url = new URL (u);
0N/A System.out.println ("client opening connection to: " + u);
0N/A HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
0N/A urlc.setFixedLengthStreamingMode (0);
0N/A urlc.setDoOutput(true);
0N/A urlc.setRequestMethod ("POST");
0N/A OutputStream os = urlc.getOutputStream ();
0N/A os.close();
0N/A int ret = urlc.getResponseCode();
0N/A if (ret != 200) {
0N/A throw new Exception ("Expected 200: got " + ret);
0N/A }
0N/A }
0N/A
0N/A /* calling setChunkedStreamingMode with -1 should entail using
0N/A the default chunk size */
0N/A static void test9 (String u) throws Exception {
0N/A URL url = new URL (u);
0N/A System.out.println ("client opening connection to: " + u);
0N/A HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
0N/A urlc.setChunkedStreamingMode (-1);
0N/A urlc.setDoOutput(true);
0N/A urlc.setRequestMethod ("POST");
0N/A OutputStream os = urlc.getOutputStream ();
0N/A os.write (str1.getBytes());
0N/A os.close();
0N/A InputStream is = urlc.getInputStream();
0N/A readAndCompare (is, str1);
0N/A is.close();
0N/A }
0N/A
0N/A static void test10 (String u) throws Exception {
0N/A URL url = new URL (u);
0N/A System.out.println ("client opening connection to: " + u);
0N/A HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
0N/A urlc.setChunkedStreamingMode (4 * 1024);
0N/A urlc.setDoOutput(true);
0N/A urlc.setRequestMethod ("POST");
0N/A OutputStream os = urlc.getOutputStream ();
0N/A byte[] buf = new byte [200 * 1024];
0N/A for (int i=0; i< 200 * 1024; i++) {
0N/A buf[i] = (byte) i;
0N/A }
0N/A /* write a small bit first, and then the large buffer */
0N/A os.write (str1.getBytes());
0N/A os.write (buf, 10, buf.length - 10); /* skip 10 bytes to test offset */
0N/A os.close();
0N/A InputStream is = urlc.getInputStream();
0N/A is.close();
0N/A int ret = urlc.getResponseCode();
0N/A if (ret != 200) {
0N/A throw new Exception ("Expected 200: got " + ret);
0N/A }
0N/A }
0N/A
65N/A static void test11 (String u) throws Exception {
65N/A URL url = new URL (u);
65N/A System.out.println ("client opening connection to: " + u);
65N/A HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
65N/A urlc.setChunkedStreamingMode (36 * 1024);
65N/A urlc.setDoOutput(true);
65N/A urlc.setRequestMethod ("POST");
65N/A OutputStream os = urlc.getOutputStream ();
65N/A byte[] buf = new byte [30 * 1024];
65N/A for (int i=0; i< 30 * 1024; i++) {
65N/A buf[i] = (byte) i;
65N/A }
65N/A /* write a small bit first, and then the large buffer */
65N/A os.write (str1.getBytes());
65N/A //os.write (buf, 10, buf.length - 10); /* skip 10 bytes to test offset */
65N/A os.write (buf, 10, (10 * 1024) - 10);
65N/A os.write (buf, (10 * 1024), (10 * 1024));
65N/A os.write (buf, (20 * 1024), (10 * 1024));
65N/A os.close();
65N/A InputStream is = urlc.getInputStream();
65N/A is.close();
65N/A int ret = urlc.getResponseCode();
65N/A if (ret != 200) {
65N/A throw new Exception ("Expected 200: got " + ret);
65N/A }
65N/A }
65N/A
65N/A static void test12 (String u) throws Exception {
65N/A URL url = new URL (u);
65N/A System.out.println ("client opening connection to: " + u);
65N/A HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
65N/A urlc.setChunkedStreamingMode (36 * 1024);
65N/A urlc.setDoOutput(true);
65N/A urlc.setRequestMethod ("POST");
65N/A OutputStream os = urlc.getOutputStream ();
65N/A byte[] buf = new byte [30 * 1024];
65N/A for (int i=0; i< 30 * 1024; i++) {
65N/A buf[i] = (byte) i;
65N/A }
65N/A os.write (buf, 10, buf.length - 10); /* skip 10 bytes to test offset */
65N/A os.close();
65N/A InputStream is = urlc.getInputStream();
65N/A is.close();
65N/A int ret = urlc.getResponseCode();
65N/A if (ret != 200) {
65N/A throw new Exception ("Expected 200: got " + ret);
65N/A }
65N/A }
65N/A
65N/A
0N/A static com.sun.net.httpserver.HttpServer httpserver;
0N/A
0N/A public static void main (String[] args) throws Exception {
0N/A try {
0N/A httpserver = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0);
0N/A HttpContext ctx = httpserver.createContext("/test/", new Test() );
0N/A httpserver.start();
0N/A
0N/A int port = httpserver.getAddress().getPort();
0N/A
0N/A System.out.println ("Server started: listening on port: " + port);
0N/A test1("http://localhost:"+ port + "/test/test1");
0N/A test1("http://localhost:"+ port + "/test/test2");
0N/A test3("http://localhost:"+ port + "/test/test3");
0N/A test4("http://localhost:"+ port + "/test/test4");
0N/A test5("http://localhost:"+ port + "/test/test5");
0N/A test6("http://localhost:"+ port + "/test/test6");
0N/A test7("http://localhost:"+ port + "/test/test7");
0N/A test8("http://localhost:"+ port + "/test/test8");
0N/A test9("http://localhost:"+ port + "/test/test9");
0N/A test10("http://localhost:"+ port + "/test/test10");
65N/A test11("http://localhost:"+ port + "/test/test11");
65N/A test12("http://localhost:"+ port + "/test/test12");
0N/A } finally {
0N/A if (httpserver != null)
0N/A httpserver.stop(0);
0N/A }
0N/A }
0N/A
0N/A}