United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-7095949 java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
JDK-7095949 : java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently

Details
Type:
Bug
Submit Date:
2011-09-28
Status:
Closed
Updated Date:
2012-06-18
Project Name:
JDK
Resolved Date:
2012-06-18
Component:
core-libs
OS:
windows_2003
Sub-Component:
java.net
CPU:
x86
Priority:
P3
Resolution:
Fixed
Affected Versions:
7u1
Fixed Versions:

Related Reports
Backport:
Backport:

Sub Tasks

Description
see comments

                                    

Comments
SUGGESTED FIX

diff -r 8d88e694441c test/java/net/URLConnection/RedirectLimit.java
--- a/test/java/net/URLConnection/RedirectLimit.java   
+++ b/test/java/net/URLConnection/RedirectLimit.java 
@@ -62,24 +62,37 @@ class RedirLimitServer extends Thread {
         port = ss.getLocalPort();
     }
 
+    static final byte[] requestEnd = new byte[] {'\r', '\n', '\r', '\n' };
+
+    void readCompleteRequest(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 {
             ss.setSoTimeout(TIMEOUT);
             for (int i=0; i<NUM_REDIRECTS; i++) {
                 try (Socket s = ss.accept()) {
                     s.setSoTimeout(TIMEOUT);
-                    InputStream is = s.getInputStream();
-                    OutputStream os = s.getOutputStream();
-                    is.read();
+                    readCompleteRequest(s.getInputStream());
                     String reply = reply1 + port + "/redirect" + i + reply2;
-                    os.write(reply.getBytes());
+                    s.getOutputStream().write(reply.getBytes());
                 }
             }
             try (Socket s = ss.accept()) {
-                InputStream is = s.getInputStream();
-                OutputStream os = s.getOutputStream();
-                is.read();
-                os.write(reply3.getBytes());
+                s.setSoTimeout(TIMEOUT);
+                readCompleteRequest(s.getInputStream());
+                s.getOutputStream().write(reply3.getBytes());
             }
         } catch (Exception e) {
             e.printStackTrace();

Similar changes for Redirect307Test.java
                                     
2011-10-04
EVALUATION

This is an issue with the testcase itself.

The server side of the test only reads one byte of the HTTP request and then proceeds to send its response. For the testcase purposes it doesn't need to know the full request since the response is canned anyway. Then the socket on the server side is closed and the loop continues awaiting another connection. If the server closes the socket before the client has read the response the server will send a TCP RST (reset the connection) since there is unread data in the socket's receive buffer. This will cause the client side to fail.

The solution is for the server to read the full HTTP request. This will avoid the above situation.
                                     
2011-10-04
EVALUATION

JDK8 Changeset:

Changeset: 24741fe639a8
Author:    chegar
Date:      2011-10-04 16:37 +0100
URL:       http://hg.openjdk.java.net/jdk8/tl/jdk/rev/24741fe639a8

7095949: java/net/URLConnection/RedirectLimit.java and Redirect307Test fail intermittently
Reviewed-by: alanb

! test/java/net/URLConnection/Redirect307Test.java
! test/java/net/URLConnection/RedirectLimit.java
                                     
2011-10-04



Hardware and Software, Engineered to Work Together