MaikJaek / flying-saucer

Automatically exported from code.google.com/p/flying-saucer
0 stars 0 forks source link

NaiveUserAgent does not resolve absolute URIs properly #117

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Migrated from http://java.net/jira/browse/XHTMLRENDERER-143
Reported by mabiss on 17.11.2006 and updated 20.07.2008.
Priority was Major.

Original description:
I was passing DOM Document object to an ITextRenderer that had an image tag with
a n src attribute containing a full http URL (i.e. http://localhost/myimage.jpg)
but the NaiveUserAgent was unable to resolve it. It would try to establish a
base URI when it did not need to.

I have changed NaiveUserAgent#resolveURI to fix this (see below). What i wanted
it to do:

1: Check if the URI is a proper file URI, give it back if true
2: Check if the URI is absolute (i.e. contains a scheme), give it back if true
3: Try to resolve the URI with a base
3.1: If a base URI exists use it
3.2: If a base URI does not exists try to establsh one (that logic existed in
the code)

My code style is probably incompatible when it comes to logging and exception
handling but you can see the logic in it:

public String resolveURI(String sUri) {
    String resolvedURI = null;
    if (sUri != null){
        try {
            // 1: Check if it's a file
            File f = (new File(sUri)).getAbsoluteFile();
            if(f.getParentFile().exists()){
                resolvedURI = f.toURL().toString();
            }

            // not a file, try to resolve without a base URI first
            URI unresolvedUri = new URI(sUri);
            // if absolute (i.e. has a URI scheme), ignore base
            if(unresolvedUri != null && unresolvedUri.isAbsolute()){
                resolvedURI = sUri;
            }
            // not absolute, neet to try resolving against a base
            else{
                // first try to set a base URL if null
                if (this.baseURL == null) {
                    try {
                        URL result = new URL(sUri);
                        setBaseURL(result.toExternalForm());
                    } catch (MalformedURLException e) {
                        try {
                            setBaseURL(new
File(".").toURI().toURL().toExternalForm());
                        } catch (Exception e1) {
                            XRLog.exception("The default NaiveUserAgent doesn't
know how to resolve the base URL for " + sUri);
                        }
                    }
                }
                URL result = new URL(new URL(this.baseURL), sUri);
                resolvedURI = result.toString();
            }
        }catch(IllegalArgumentException e) {
            throw e;
        }catch(MalformedURLException e) {
            throw new IllegalArgumentException("The default NaiveUserAgent
cannot resolve the URL " + sUri + " with base URL " + baseURL);
        } catch (URISyntaxException e) {
            throw new IllegalArgumentException("The default NaiveUserAgent
cannot resolve the URL " + sUri + " with base URL " + baseURL);
        }
    }
    return resolvedURI;
}

Original issue reported on code.google.com by pdoubl...@gmail.com on 16 Feb 2011 at 10:11

GoogleCodeExporter commented 8 years ago
Original comment by pdoubleya on 20.07.2008:
This looks like a better approach than what we do now; will defer to R9,
however, for lack of time. Unfortunately, this one fell through the cracks.

Original comment by pdoubl...@gmail.com on 16 Feb 2011 at 10:12