7152856: TEST_BUG: sun/net/www/protocol/jar/B4957695.java failing on Windows
authorkhazra
Tue, 17 Apr 2012 11:59:12 -0700
changeset 12447 92676a77a667
parent 12446 69b93f3b9f85
child 12448 b95438b17098
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
jdk/test/sun/net/www/protocol/jar/B4957695.java
--- a/jdk/test/sun/net/www/protocol/jar/B4957695.java	Tue Apr 17 11:21:35 2012 -0700
+++ b/jdk/test/sun/net/www/protocol/jar/B4957695.java	Tue Apr 17 11:59:12 2012 -0700
@@ -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,8 +32,56 @@
 
 public class B4957695 {
 
-    static int count = 0;
-    static boolean error = false;
+    static Server server;
+
+    static class Server extends Thread {
+        final ServerSocket srv;
+        static final byte[] requestEnd = new byte[] {'\r', '\n', '\r', '\n'};
+
+        Server(ServerSocket s) {
+            srv = s;
+        }
+
+        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");
+
+                    // Note: The (buggy) server only sends length-10 bytes.
+                    ow.write(new String(b));
+                    ow.flush();
+                }
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+        }
+    }
 
     static void read (InputStream is) throws IOException {
         int c,len=0;
@@ -45,32 +91,13 @@
         System.out.println ("read " + len + " bytes");
     }
 
-    static class CallBack extends AbstractCallback {
-
-        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();
-            } catch (IOException e) {
-                e.printStackTrace();
-            }
-        }
-
-    };
-
-    static HttpServer server;
-
     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 @@
         } 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 () {