From e60ddf8b3058bed30f6cd203a734f622c1dae857 Mon Sep 17 00:00:00 2001 From: khazra Date: Tue, 17 Apr 2012 11:59:12 -0700 Subject: [PATCH] 7152856: TEST_BUG: sun/net/www/protocol/jar/B4957695.java failing on Windows Summary: Remove usage of HTTP Server at test/sun/net/www/httptest Reviewed-by: chegar, alanb --- test/sun/net/www/protocol/jar/B4957695.java | 83 ++++++++++++++------- 1 file changed, 54 insertions(+), 29 deletions(-) diff --git a/test/sun/net/www/protocol/jar/B4957695.java b/test/sun/net/www/protocol/jar/B4957695.java index da1ee0e51..0d25825f0 100644 --- a/test/sun/net/www/protocol/jar/B4957695.java +++ b/test/sun/net/www/protocol/jar/B4957695.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2006, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2012, Oracle and/or its affiliates. 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 @@ -24,8 +24,6 @@ /** * @test * @bug 4957695 - * @library ../../httptest/ - * @build HttpCallback HttpServer ClosedChannelList HttpTransaction AbstractCallback * @summary URLJarFile.retrieve does not delete tmpFile on IOException */ @@ -34,43 +32,72 @@ import java.net.*; public class B4957695 { - static int count = 0; - static boolean error = false; + static Server server; - static void read (InputStream is) throws IOException { - int c,len=0; - while ((c=is.read()) != -1) { - len += c; + static class Server extends Thread { + final ServerSocket srv; + static final byte[] requestEnd = new byte[] {'\r', '\n', '\r', '\n'}; + + Server(ServerSocket s) { + srv = s; } - System.out.println ("read " + len + " bytes"); - } - static class CallBack extends AbstractCallback { + void readOneRequest(InputStream is) throws IOException { + int requestEndCount = 0, r; + while ((r = is.read()) != -1) { + if (r == requestEnd[requestEndCount]) { + requestEndCount++; + if (requestEndCount == 4) { + break; + } + } else { + requestEndCount = 0; + } + } + } + + public void run() { + try (Socket s = srv.accept()) { + // read HTTP request from client + readOneRequest(s.getInputStream()); + try (OutputStreamWriter ow = + new OutputStreamWriter((s.getOutputStream()))) { + FileInputStream fin = new FileInputStream("foo1.jar"); + int length = fin.available(); + byte[] b = new byte[length-10]; + fin.read(b, 0, length-10); + ow.write("HTTP/1.0 200 OK\r\n"); + + // Note: The client expects length bytes. + ow.write("Content-Length: " + length + "\r\n"); + ow.write("Content-Type: text/html\r\n"); + ow.write("\r\n"); - public void request (HttpTransaction req, int count) { - try { - System.out.println ("Request received"); - req.setResponseEntityBody (new FileInputStream ("foo1.jar")); - System.out.println ("content length " + req.getResponseHeader ( - "Content-length" - )); - req.sendPartialResponse (200, "Ok"); - req.abortiveClose(); + // Note: The (buggy) server only sends length-10 bytes. + ow.write(new String(b)); + ow.flush(); + } } catch (IOException e) { e.printStackTrace(); } } + } - }; - - static HttpServer server; + static void read (InputStream is) throws IOException { + int c,len=0; + while ((c=is.read()) != -1) { + len += c; + } + System.out.println ("read " + len + " bytes"); + } public static void main (String[] args) throws Exception { String tmpdir = System.getProperty("java.io.tmpdir"); String[] list1 = listTmpFiles(tmpdir); - //server = new HttpServer (new CallBack(), 10, 1, 0); - server = new HttpServer (new CallBack(), 1, 5, 0); - int port = server.getLocalPort(); + ServerSocket serverSocket = new ServerSocket(0); + server = new Server(serverSocket); + server.start(); + int port = serverSocket.getLocalPort(); System.out.println ("Server: listening on port: " + port); URL url = new URL ("jar:http://localhost:"+port+"!/COPYRIGHT"); try { @@ -81,14 +108,12 @@ public class B4957695 { } catch (IOException e) { System.out.println ("Received IOException as expected"); } - server.terminate(); String[] list2 = listTmpFiles(tmpdir); if (!sameList (list1, list2)) { throw new RuntimeException ("some jar_cache files left behind"); } } - static String[] listTmpFiles (String d) { File dir = new File (d); return dir.list (new FilenameFilter () { -- GitLab