serve-index icon indicating copy to clipboard operation
serve-index copied to clipboard

Ensure that links to directories end in a slash.

Open geekmug opened this issue 10 years ago • 10 comments

I use strict routing and missing slashes causes redirection.

geekmug avatar Jan 26 '15 08:01 geekmug

Thanks! Can you fix the tests and update all the types for have the trailing slashes instead of only the HTML view?

dougwilson avatar Jan 26 '15 13:01 dougwilson

Whoops, sorry I spaced on the test cases. Originally, I was reluctant to change the applicaiton/json and text/plain output because of a fear of ambiguity if there was a path separator at the end of the file name (since in the HTML output a path separator in the file name would be encode into "%2F"), but I think it's fair to say that would be a very broken directory (I don't think POSIX even allows that).

The other output types previously didn't call fs.stat for the directory contents, so I pulled that up into the main serveIndex function. As a side-effect of that, the html output now calls stat twice on each file in the directory. I wanted to change the "files" argument to be the { name: file, stat: stats[i] } objects used in the html function, but that breaks the public API. Unfortunately, the html export uses extra information from the stat to do the "details" view, so it's not trivial to eliminate. However, it's extremely likely the 2nd stat calls will be cached, so it probably will have no performance impact

geekmug avatar Jan 26 '15 17:01 geekmug

Gotcha. I'm' sure you can refactor this some more to make sure there is only one stat call per file for even the HTML view. We also cannot add the post slash before calling the view functions, unless you're OK with this waiting until the next major version.

dougwilson avatar Jan 26 '15 17:01 dougwilson

Err, sorry for the churn, caught a mistake stripping the slash off of ".." (which obviously doesn't make sense).

geekmug avatar Jan 26 '15 17:01 geekmug

Ah ok, I understand the viewpoint that adding the slash broke the public API too. I can push the stat calls down into the exports.json and exports.plain, and that makes it only a single stat call per file too. If you that works for you, I'll refactor it that way.

geekmug avatar Jan 26 '15 17:01 geekmug

If you that works for you, I'll refactor it that way.

Yes, please :) In fact, I wanted to do this a long time ago, but didn't want to add the stat calls into JSON and text. Since you actually want it, I'm OK with it, since there's an actual demand now :)

dougwilson avatar Jan 26 '15 17:01 dougwilson

Is it a problem to add arguments to the json and plain functions? The path isn't passed down to those two functions, and there is no way to recover that information from within those functions (because they don't know the root either). It would be nice if they could have the same arguments as the html one, but that currently takes (req, res, files, next, dir, showUp, icons, path, view, template, stylesheet). And, the part I need is path.

geekmug avatar Jan 26 '15 18:01 geekmug

Feel free to add new arguments on the end :)

dougwilson avatar Jan 26 '15 20:01 dougwilson

@geekmug , it seems to be working fine, except for one tiny detail:

  • when you have a .. directory that links to the root, the link gets transformed to // and, when you click on it, the browser opens about:blank page.
    Take a look at what i did on line 398 of my pull request, it fixed it, but it also has to be changed in the json and plain text templates. https://github.com/expressjs/serve-index/pull/31/files#diff-168726dbe96b3ce427e7fedce31bb0bcR398

andrepadez avatar May 18 '15 13:05 andrepadez

Also, this feature needs to be added as an option, at least until the next major release. Checkout my pull request's #31 conversation. Cheers

andrepadez avatar May 18 '15 13:05 andrepadez