Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8144008

Setting NO_PROXY on HTTP URL connections does not stop proxying

XMLWordPrintable

    • b125
    • Verified

      FULL PRODUCT VERSION :


      A DESCRIPTION OF THE PROBLEM :
      Currently there doesn't seem to be a way to get a proxy-less URL connection in JDK. This is odd as as the documentation clearly states otherwise.

      For example in the tech notes (1) it is stated that:


      --- QUOTE ---
      URLConnection conn2 = url2.openConnection(Proxy.NO_PROXY);

      Now, this guarantees you that this specific URL will be retrieved though a direct connection bypassing any other proxy settings, which can be convenient.
      --- END QUOTE ---


      and furthermore in the Javadocs about java.net.Proxy:

      --- QUOTE ---
      A proxy setting that represents a DIRECT connection, basically telling the protocol handler not to use any proxying. Used, for instance, to create sockets bypassing any other global proxy settings (like SOCKS)
      --- END QUOTE ---

      From the test cases attached it can be seen that in reality it is impossible to ever create a proxy-less connection. The main problem in the JDK is that even if Proxy.NO_PROXY is explicitly defined on the connection then the ProxySelector will still be evaluated for that connection.

      There should be a way in the JDK to create a *guaranteed* proxy-less URL connection. If not the docs are clearly wrong. In particular the technotes are very clear: the use of Proxy.NO_PROXY "guarantees" that there will be no proxy used on the connection.


      REFERENCES:

      (1) http://docs.oracle.com/javase/8/docs/technotes/guides/net/proxies.html
      (2) http://docs.oracle.com/javase/8/docs/api/java/net/Proxy.html#NO_PROXY





      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Below is some example code that will demonstrate the problem.

      --- CODE ---
      public static void main(String[] args) throws MalformedURLException, IOException {
              
              System.setProperty("socksProxyHost", "127.0.0.1");
              System.setProperty("socksProxyPort", "9999");
              
              URL url = new URL("http://www.google.com");
              URLConnection connection = url.openConnection(Proxy.NO_PROXY);
              connection.connect();
          }
      --- END CODE ---

      This code will produce an exception about not being able to connect to SOCKS proxy, although we've specifically asked *not* to use a proxy.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      There should be no exception about SOCKS proxy as we have specifically asked *not* to use a proxy.
      ACTUAL -
      Code produces a SocketException: "Can't connect to SOCKS proxy:Connection refused: connect".

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      public class TestNoProxy {
          
          public static void main(String[] args) throws MalformedURLException, IOException {
              
              //ProxySelector.setDefault(new MyProxySelector());
              
              System.setProperty("socksProxyHost", "127.0.0.1");
              System.setProperty("socksProxyPort", "9999");

              
              URL url = new URL("http://www.oracle.com");
              URLConnection connection = url.openConnection(Proxy.NO_PROXY);
              connection.connect();
          }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Haven't found a perfect workaround so far. Currently we've implemented our own ProxySelector which delegates to the JDK's DefaultProxySelector except for URI scheme = 'socket' in which case the Selector will always return no_proxy. We can do this hack because use of Proxy.NO_PROXY on an URLConnection comes through to the ProxySelector as an URI where the protocol is "socket". But the workaround will disable proxying also for any valid SOCKS request that may not have specified not to use a proxy. So we're disabling too much.

            vtewari Vyom Tewari
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved: