Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trailing slash on the root dir leads to an IOException when calling URL.openstream() #276

Closed
alexgast opened this issue Jan 14, 2021 · 8 comments
Labels
Milestone

Comments

@alexgast
Copy link

Instantiating a STGroupDir(URL root, ...), where root contains a trailing slash (/), leads to an Java IOException when calling URL.openstream() in the method public CompiledST loadTemplateFile(String prefix, String unqualifiedFileName). The reason is a doubleslash // coming from the expression f = new URL(root+prefix+unqualifiedFileName). Maybe URI normalizing could fix the problem, i.e. f = f.toURI().normalize().toURL(). This situation occurs (for example) after updating Apache Felix Framework to version 7.0.0 because of https://issues.apache.org/jira/browse/FELIX-6294.

@StephanRichter
Copy link
Contributor

I can confirm this behaviour.

@parrt
Copy link
Member

parrt commented Nov 10, 2021

Wow. That's weird... thanks for the PR, @StephanRichter

@parrt parrt added the type:bug label Nov 10, 2021
@parrt parrt added this to the 4.3.2 milestone Nov 10, 2021
parrt added a commit that referenced this issue Nov 24, 2021
@Skoti
Copy link

Skoti commented Jan 12, 2022

Instantiating a STGroupDir(URL root, ...), where root contains a trailing slash (/),

It's not only for the URL constructor.

Even if STGroupDir is initialized with a path string that do not ends with / it will add a trailing slash to the root in here:

    public STGroupDir(String dirName, char delimiterStartChar, char delimiterStopChar) {
        super(delimiterStartChar, delimiterStopChar);
        this.groupDirName = dirName;
        File dir = new File(dirName);
        if ( dir.exists() && dir.isDirectory() ) {
            // we found the directory and it'll be file based
            try {
                root = dir.toURI().toURL();

so the root (root = dir.toURI().toURL();) ends up with a trailing slash and later on when it is used to load the template it leads to an exception as described above.

Seems like the PR will fix that case too but it is still not released.
Is there a chance for 4.3.2 to be released soon as a hot fix?

@parrt
Copy link
Member

parrt commented Feb 26, 2022

this is next on my list to look at....thanks!

@parrt
Copy link
Member

parrt commented Feb 27, 2022

this seems very strange to me. Why does toURL() adding trailing slash and are we trying to overcome a bug in the networking library? am I not using toURL() correctly?

@parrt
Copy link
Member

parrt commented Feb 27, 2022

this seems to pass:

@Test public void testSimpleGroupWithTrailingSlashOnDir() throws Exception {
    String dir = getRandomDir() + "/"; <---------------- add /
    writeFile(dir, "a.st", "a(x) ::= <<foo>>");
    STGroup group = new STGroupDir(dir);
    ST st = group.getInstanceOf("a");
    String expected = "foo";
    String result = st.render();
    assertEquals(expected, result);
}

maybe it has to be a URL like file:///... and not a file name?

@parrt
Copy link
Member

parrt commented Feb 27, 2022

I should also point out that without the /it also passes:

    @Test public void testSimpleGroup() throws Exception {
        String dir = getRandomDir();
        writeFile(dir, "a.st", "a(x) ::= <<foo>>");
        STGroup group = new STGroupDir(dir);
        ST st = group.getInstanceOf("a");
        String expected = "foo";
        String result = st.render();
        assertEquals(expected, result);
    }

@parrt
Copy link
Member

parrt commented Mar 11, 2022

@Skoti I've tried adding trailing slashes but I can't get it to fail. I definitely think this is related to the bug when there is a jar #293 But I'm having trouble replicating the exception. any simple test case would dramatically increase my ability to fix this. thanks!

@parrt parrt closed this as completed in 2e88669 Mar 11, 2022
parrt added a commit that referenced this issue Mar 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants