This is the mail archive of the java-patches@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Patch: java.net - userInfo handling in URLs


Hi list,


I just commited the attached patch to merge userInfo support from GNU 
classpath into URLs


Michael


2004-04-22  Jeroen Frijters <jeroen@sumatra.nl>

	* java/net/URLStreamHandler.java
	(parseURL): Convert the file path to using '/' instead of native
	file separator.

2004-04-22  Guilhem Lavaux <guilhem@kaffe.org>

	* java/net/URL.java
	(userInfo): New field.
	(URL): Set authority to the right value.
	(setURL): Fixed authority and file initialization.
	* java/net/URLStreamHandler.java
	(parseURL): Take care of the query tag. Build authority.
	(toExternalForm): Fixed URL building using authority.

Index: java/net/URLConnection.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/net/URLConnection.java,v
retrieving revision 1.29
diff -u -r1.29 URLConnection.java
--- java/net/URLConnection.java	20 Apr 2004 13:05:10 -0000	1.29
+++ java/net/URLConnection.java	22 Apr 2004 07:01:54 -0000
@@ -108,7 +108,7 @@
    * This is the default value that will be used to determine whether or
    * not user interaction should be allowed.
    */
-  private static boolean defaultAllowUserInteraction = false;
+  private static boolean defaultAllowUserInteraction;
 
   /**
    * This is the default flag indicating whether or not to use caches to
@@ -126,7 +126,7 @@
    * Indicates whether or not a connection has been established to the
    * destination specified in the URL
    */
-  protected boolean connected = false;
+  protected boolean connected;
 
   /**
    * Indicates whether or not input can be read from this URL
@@ -136,7 +136,7 @@
   /**
    * Indicates whether or not output can be sent to this URL
    */
-  protected boolean doOutput = false;
+  protected boolean doOutput;
 
   /**
    * If this flag is set, the protocol is allowed to cache data whenever
@@ -157,7 +157,7 @@
    * modified more recently than the date set in this variable.  That date
    * should be specified as the number of seconds since 1/1/1970 GMT.
    */
-  protected long ifModifiedSince = 0L;
+  protected long ifModifiedSince;
 
   /**
    * This is the URL associated with this connection
@@ -165,8 +165,10 @@
   protected URL url;
 
   private static Hashtable handlers = new Hashtable();
-  private static SimpleDateFormat dateFormat1, dateFormat2, dateFormat3;
-  private static boolean dateformats_initialized = false;
+  private static SimpleDateFormat dateFormat1;
+  private static SimpleDateFormat dateFormat2;
+  private static SimpleDateFormat dateFormat3;
+  private static boolean dateformats_initialized;
 
   /**
    * Creates a URL connection to a given URL. A real connection is not made.
@@ -430,10 +432,10 @@
     String type = getContentType();
     ContentHandler ch = setContentHandler(type);
 
-    if (ch == null)
-      return getInputStream();
+    if (ch != null)
+      return ch.getContent(this);
 
-    return ch.getContent(this);
+    return getInputStream();
   }
 
   /**
Index: java/net/URLStreamHandler.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/net/URLStreamHandler.java,v
retrieving revision 1.27
diff -u -r1.27 URLStreamHandler.java
--- java/net/URLStreamHandler.java	20 Apr 2004 13:05:10 -0000	1.27
+++ java/net/URLStreamHandler.java	22 Apr 2004 07:01:54 -0000
@@ -126,6 +126,13 @@
     int port = url.getPort();
     String file = url.getFile();
     String ref = url.getRef();
+    String userInfo = url.getUserInfo();
+    String authority = url.getAuthority();
+    String query = null;
+    
+    // On Windows we need to change \ to / for file URLs
+    if (url.getProtocol().equals("file"))
+      spec = spec.replace(File.separatorChar, '/');
 
     if (spec.regionMatches(start, "//", 0, 2))
       {
@@ -141,14 +148,17 @@
 	else
 	  hostEnd = end;
 
-	host = spec.substring(start, hostEnd);
+	authority = host = spec.substring(start, hostEnd);
 
 	// We first need a genuine host name (with userinfo).
 	// So we check for '@': if it's present check the port in the
 	// section after '@' in the other case check it in the full string.
 	// P.S.: We don't care having '@' at the beginning of the string.
 	if ((at_host = host.indexOf('@')) >= 0)
-	  genuineHost = host.substring(at_host);
+	  {
+	    genuineHost = host.substring(at_host);
+	    userInfo = host.substring(0, at_host);
+	  }
 	else
 	  genuineHost = host;
 
@@ -193,18 +203,10 @@
     else if (start < end)
       {
 	// Context is available, but only override it if there is a new file.
-	char sepChar = '/';
-	int lastSlash = file.lastIndexOf(sepChar);
-	if (lastSlash < 0 && File.separatorChar != sepChar
-	    && url.getProtocol().equals("file"))
-	  {
-	    // On Windows, even '\' is allowed in a "file" URL.
-	    sepChar = File.separatorChar;
-	    lastSlash = file.lastIndexOf(sepChar);
-	  }
+	int lastSlash = file.lastIndexOf('/');
 
 	file =
-	  file.substring(0, lastSlash) + sepChar + spec.substring(start, end);
+	  file.substring(0, lastSlash) + '/' + spec.substring(start, end);
 
 	if (url.getProtocol().equals("file"))
 	  {
@@ -214,6 +216,7 @@
 	      {
 		boolean endsWithSlash = file.charAt(file.length() - 1) == '/';
 		file = new File(file).getCanonicalPath();
+		file = file.replace(File.separatorChar, '/');
 		if (endsWithSlash && file.charAt(file.length() - 1) != '/')
 		  file += '/';
 	      }
@@ -238,10 +241,21 @@
 	  }
       }
 
+    // We care about the query tag only if there is no reference at all.
+    if (ref == null)
+      {
+	  int queryTag = file.indexOf('?');
+	  if (queryTag != -1)
+	    {
+	      query = file.substring(queryTag + 1);
+	      file = file.substring(0, queryTag);
+	    }
+      }
+
     // XXX - Classpath used to call PlatformHelper.toCanonicalForm() on
     // the file part. It seems like overhead, but supposedly there is some
     // benefit in windows based systems (it also lowercased the string).
-    setURL(url, url.getProtocol(), host, port, file, ref);
+    setURL(url, url.getProtocol(), host, port, authority, userInfo, file, query, ref);
   }
 
   /*
@@ -492,42 +506,31 @@
     String file;
     String ref;
     String user;
+    String authority;
     int port;
 
     protocol = url.getProtocol();
-
-    // JDK 1.2 online doc infers that host could be null because it
-    // explicitly states that file cannot be null, but is silent on host.
-    host = url.getHost();
-    if (host == null)
-      host = "";
-
-    port = url.getPort();
+    authority = url.getAuthority();
+    if (authority == null)
+      authority = "";
+    
     file = url.getFile();
     ref = url.getRef();
-    user = url.getUserInfo();
 
     // Guess a reasonable size for the string buffer so we have to resize
     // at most once.
-    int size = protocol.length() + host.length() + file.length() + 24;
+    int size = protocol.length() + authority.length() + file.length() + 24;
     StringBuffer sb = new StringBuffer(size);
 
-    if (protocol.length() != 0)
+    if (protocol != null && protocol.length() > 0)
       {
 	sb.append(protocol);
 	sb.append(":");
       }
-
-    if (host.length() != 0)
+    
+    if (authority.length() != 0)
       {
-	sb.append("//");
-	if (user != null && ! "".equals(user))
-	  sb.append(user).append('@');
-	sb.append(host);
-
-	// Append port if port was in URL spec.
-	if (port >= 0)
-	  sb.append(':').append(port);
+	sb.append("//").append(authority);
       }
 
     sb.append(file);

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]