[jcifs] jcifs bug

Michael B.Allen mballen at erols.com
Fri Mar 22 18:59:57 EST 2002


Bernie,

Thank you for provoking me to look at this. The jCIFS SMB URL Protocol
Handler was admittedly rather shady and the new URLStreamHandler and
changes in java.net.URL are pertainent to my #1 fix-it item which is SMB
URL parsing. I never fully understood how the URL Protocol Handler was
supposed to work (still not sure I do). You're this.toExternalForm( u )
bit seems to be pretty critical. Nice job.

Anyway, I have ammended your code to include proper handling of the
authinfo section (c.f. RFC 2396). I have also devised a possibly awfully
wrong Java < 1.2 version that sneaks the authinfo in as part of the
server component. It seems to work though. Both are inlined and attached:

jcifs/smb/Handler.java for Java 1.3

--8<--

/* jcifs smb client library in Java
 * Copyright (C) 2000  "Michael B. Allen" <mballen at erols.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package jcifs.smb;

import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
import java.io.IOException;

public class Handler extends URLStreamHandler {

	public URLConnection openConnection( URL u ) throws IOException {
		SmbFile f = new SmbFile( this.toExternalForm(u) );
		return new SmbURLConnection( u, f );
	}

	protected void parseURL( URL u, String spec, int start, int limit ) {
		SmbFile f;
		String authority;
		int port = -1;

		try {
			f = new SmbFile( spec, null, start, limit );

			if( f.authInfo.username != null ) {
				String userinfo = f.authInfo.username;
				if( f.authInfo.domain != null ) {
					userinfo = f.authInfo.domain + ';' + userinfo;
				}
				if( f.authInfo.password != null ) {
					userinfo += ':' + f.authInfo.password;
				}
				authority = userinfo + '@' + f.server;
			} else {
				authority = f.server;
			}
			if (f.port > 0 && f.port != 139) {
				port = f.port;
			}

			setURL( u, "smb", null, port, authority, null,
					'/' + f.share + f.canonicalPath, null, null );

		} catch( IOException ioe ) {
			Log.printStackTrace( "smb URLStreamHandler exception", ioe );
		}
	}
}

jcifs/smb/Handler.java for Java < 1.2

--8<--

/* jcifs smb client library in Java
 * Copyright (C) 2000  "Michael B. Allen" <mballen at erols.com>
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package jcifs.smb;

import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
import java.io.IOException;

public class Handler extends URLStreamHandler {

	public URLConnection openConnection( URL u ) throws IOException {
		SmbFile f = new SmbFile( this.toExternalForm(u) );
		return new SmbURLConnection( u, f );
	}

	protected void parseURL( URL u, String spec, int start, int limit ) {
		SmbFile f;
		String authority;
		int port = -1;

		try {
			f = new SmbFile( spec, null, start, limit );

			if( f.authInfo.username != null ) {
				String userinfo = f.authInfo.username;
				if( f.authInfo.domain != null ) {
					userinfo = f.authInfo.domain + ';' + userinfo;
				}
				if( f.authInfo.password != null ) {
					userinfo += ':' + f.authInfo.password;
				}
				authority = userinfo + '@' + f.server;
			} else {
				authority = f.server;
			}
			if (f.port > 0 && f.port != 139) {
				port = f.port;
			}

			setURL( u, "smb", authority, port,
					'/' + f.share + f.canonicalPath, null );

		} catch( IOException ioe ) {
			Log.printStackTrace( "smb URLStreamHandler exception", ioe );
		}
	}
}

I suppose I will release this as jcifs-0.6.3 but it would be nice to
hear back from you that one or both are working in your env.

Thanks,
Mike


On Thu, 21 Mar 2002 14:21:29 -0700
"Bernie Wieser" <bernie.wieser at calgary.qcdata.com> wrote:

> 
> Hi.  Oh my; was worried where the share was going to go.  Thank goodness
> that URLs allow for broader interpretation of the spec in 1.3.
> 
> Here is URLStreamHandler that allows serializable URLs
> 
> /* jcifs smb client library in Java
>  * Copyright (C) 2000  "Michael B. Allen" <mballen at erols.com>
>  *
>  * This library is free software; you can redistribute it and/or
>  * modify it under the terms of the GNU Lesser General Public
>  * License as published by the Free Software Foundation; either
>  * version 2.1 of the License, or (at your option) any later version.
>  *
>  * This library is distributed in the hope that it will be useful,
>  * but WITHOUT ANY WARRANTY; without even the implied warranty of
>  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
>  * Lesser General Public License for more details.
>  *
>  * You should have received a copy of the GNU Lesser General Public
>  * License along with this library; if not, write to the Free Software
>  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
>  */
> 
> package jcifs.smb;
> 
> import java.net.URL;
> import java.net.URLConnection;
> import java.net.URLStreamHandler;
> import java.io.IOException;
> 
> public class Handler extends URLStreamHandler {
> 
>     public URLConnection openConnection( URL u ) throws IOException {
>         SmbFile f = new SmbFile( this.toExternalForm(u) );
>         return new SmbURLConnection( u, f );
>     }
> 
>     protected void parseURL( URL u, String spec, int start, int limit ) {
> 
>         try {
>             SmbFile f;
>             f = new SmbFile( spec, null, start, limit );
>             // deprecated
>             //setURL( u, "smb", f.server, f.port, f.canonicalPath, null );
>             setURL(
>               u, // URL u,
>               "smb", // String protocol,
>               f.server, // String host,
>               f.port, // int port,
>               f.server+'/'+f.share, // String authority, (should include
> user:pass)
>               null, // String userInfo (user?),
>               f.canonicalPath, // String path,
>               null, // String query,
>               null // String ref
>               );
>         } catch( IOException ioe ) {
>             Log.printStackTrace( "smb URLStreamHandler exception", ioe );
>         }
>     }
>     protected String toExternalForm( URL u ) {
>       return "smb://"+u.getAuthority()+u.getPath();
>     }
> }
> 
> 
> -----Original Message-----
> From: Bernie Wieser [mailto:Bernie.Wieser at calgary.qcdata.com]
> Sent: Thursday, March 21, 2002 12:34 PM
> To: Michael B Allen
> Subject: [jcifs] jcifs bug
> 
> 
> 
> I've discovered another interesting bug that I'm in
> the process of fixing.
> 
> Our application, running inside of WebLogic, uses
> jcifs to move files about (not in a bean, don't worry.)
> 
> We store URLs in some beans we used, and observed
> a very interesting behavior.
> 
> for i = 1 to 10
> Object is loaded (with URL)
> URL retrieved, add to list
> Object is unloaded
> 
> When we ask the URL for its source in the list,
> each URL is the same, though different URL object.
> Tracing the code, I saw URL.toString call URL.toExtForm
> that calls the protocol handler toExtForm...
> 
> the jcifs toExtForm uses a cached SMBFile to generate
> the URL, assuming that parse was called at some point.
> because the object is being serialized/unserialized,
> the cached file is never being reset.
> 
> So - SMB URLs cannot be serialized.
> 
> I've completely removed this caching from the URLStreamHandler
> and will mail it to you for your interest.
> 
> B.
> 


-- 
May The Source be with you.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: Handler-1.3.java
Type: application/octet-stream
Size: 1993 bytes
Desc: not available
Url : http://lists.samba.org/archive/jcifs/attachments/20020322/3e0a507b/Handler-1.3.obj
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Handler-1.1.java
Type: application/octet-stream
Size: 2010 bytes
Desc: not available
Url : http://lists.samba.org/archive/jcifs/attachments/20020322/3e0a507b/Handler-1.1.obj


More information about the jcifs mailing list