JDK-6216082 : Redirect problem with HttpsURLConnection using a proxy
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 5.0
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: windows_2000,windows_xp
  • CPU: x86
  • Submitted: 2005-01-11
  • Updated: 2011-02-16
  • Resolved: 2005-03-18
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other JDK 6
5.0u10Resolved 6 betaFixed
Related Reports
Duplicate :  
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.5.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-b64)
Java HotSpot(TM) Client VM (build 1.5.0-b64, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows 2000 [Version 5.00.2195]

EXTRA RELEVANT SYSTEM CONFIGURATION :
The client application runs on Windows 2000.

The proxy server (Squid 2.5-STABLE-3-2) runs on Fidora Linux Core 1.

The webserver (Apache/2.0.50 (Unix) mod_ssl/2 OpenSSL/0.9.6c PHP/4.3.8) runs on SuSE Linux 8.0.




A DESCRIPTION OF THE PROBLEM :
When using the HttpsURLConnection to retrieve an URL over a proxy the implementation generates an invalid HTTP header in the case of a redirect by the webserver to another URL.

In detail each of the first two lines of the HTTP header will contain a GET request. The first line contains the redirected URL an the second line contains the initial URL. It seems to be, that the second line should be the referrer header field.

This bug also exists in all Java 1.4 versions and of course in all Plug-Ins
of 1.4.x and 1.5.  The bug doesn't appear without proxy.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Setup a proxy server and  a ssl webserver.
On the webserver two files are needed:

1. /redirect.php:

<?php
// please correct the redirect URL for your test environment!
header('Location: https://amrum.devel.pago.de/index.php', TRUE, 302 );
?>

2: /index.php: this file can be any html document or php script.

Edit the source of HttpsClass and correct the proxy settings and initial request URL accordingly to your test environment.

Perhaps you need to register the ssl server certificate in the jre keystore.

Run the class HttpsTest. This will produce the failure.




EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The class HttpsTest should execute normally without any exception.
ACTUAL -
java.io.IOException: Server returned HTTP response code: 400 for URL: https://amrum.devel.pago.de/index.php
	at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1133)
	at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:234)
	at HttpsTest.main(HttpsTest.java:19)
Using proxy: true


ERROR MESSAGES/STACK TRACES THAT OCCUR :
java.io.IOException: Server returned HTTP response code: 400 for URL: https://amrum.devel.pago.de/index.php
	at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1133)
	at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:234)
	at HttpsTest.main(HttpsTest.java:19)
Using proxy: true

Rest of the information included in the attachment Log.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.net.HttpURLConnection;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;

public class HttpsTest
{
  public static void main( String[] args )
  {
    HttpURLConnection con = null;
    try
    {
      HttpsURLConnection.setDefaultAllowUserInteraction( true );
      HttpsURLConnection.setFollowRedirects( true );
      System.setProperty( "javax.net.debug", "all" );
      // please correct the proxy settings and request URL accordingly to your environment
      System.setProperty( "https.proxyHost", "nimdenbus" );
      System.setProperty( "https.proxyPort", "3128" );
      URL url = new URL( "https://amrum.devel.pago.de/redirect.php" );
      con = ( HttpURLConnection )url.openConnection();
      con.getInputStream();
    }
    catch( Exception e )
    {
      e.printStackTrace();
    }
    finally
    {
      if( con != null )
      {
        System.out.println( "Using proxy: " + con.usingProxy() );
      }
    }
    System.exit( 0 );
  }
}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Make the redirect on your own:
Disable following of redirect...

HttpsURLConnection.setFollowRedirects( false );

...read the Location HTTP header field, build the complete redirect url (if necessary) and perform the following request. That's it.
###@###.### 2005-1-11 13:25:44 GMT

Comments
EVALUATION Re-produce the bug. Tip: I have no such test facility, ie. https server, proxy. Just feed test code with https://www.sun.com/nettalk; it'll redirect the https request. The hard part of fix effort will be the test code. ###@###.### 2005-1-19 05:56:39 GMT Brad finds one bug in previous putback. The nature of the problem is: when an https client communicates with an https server through an https proxy, there are several situation under which more than one roundtrip occurs between the client and the proxy: 1) the https server redirects the client, so the client needs to talk to the proxy again; 2) the proxy instructs the client to authenticate itself, so the client need to talk to the proxy again. Our implementation relies on the fact that some header fields are remembered from previous talk with proxy. To fix this CR (6216082), which is caused by those cached header, I simply clear them. So break the proxy authenticate logic. This 'safe' fix is not so safe at all. Will submit another one. ###@###.### 2005-03-11 07:21:18 GMT
19-01-2005