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

Already on GitHub? Sign in to your account

save html with wrong guessed mimetype as folder with index.html #16

Closed
wants to merge 2 commits into
from

Conversation

Projects
None yet
2 participants

These patches rewrite the destination paths for html content with the wrong guessed mimetype to be stored as index.html. For example for a path /site/content/ the content wound be stored in /site/content/index.html.
The commit adds a new option FREEZER_REWRITE_HTML_AS_FOLDER (default False) that enables this behaviour.

This trick works well for web servers that accept paths with or without the trailing slash as being the equivalent. This is the case for github's static pages (for example http://equalsraf.github.com/ was generated from http://ruiabreu.org/). However this does not work for Freezer.run().

equalsraf added some commits May 11, 2012

@equalsraf equalsraf Rewrite path as folder in mismatched html mimetype
Some servers treat file paths with or without a slash as being the
same. In such cases we can cause html content to be properly identified
as html by saving it as in /original/path/index.html.
- Added new boolean config option FREEZER_REWRITE_HTML_AS_FOLDER
  with default value to False
- When this option is True and the guessed mimetype mismatches, if the
  response mimetype is text/html then '/index.html' is appended to
  the path name.
faf0c5e
@equalsraf equalsraf Add unit tests for FREEZER_REWRITE_HTML_AS_FOLDER 8570830
Collaborator

SimonSapin commented May 12, 2012

Thanks for the patch, but I’m not sure this is the right thing to do.

The two URLs (with and without the trailing slash) are really not the same, and the redirect can only happens when the server is sure that only one of them exists. I think that the better fix is to add the trailing slash in the URL rules in the original application. Is there a scenario where you can not do this?

Unfortunately my application relies on the trailing to work properly, and I can't change it to behave otherwise.
It is mapping the URL path to content on disk, and in that case there can't be a file and folder with the same name, so changing the path would essentially break the app.

But yes it essentially a quick hack to help when:

  • Your app distinguishes cases with or without the trailing slash and the path does not have an html extension(for the mime guess)
  • Your static content web server does not distinguish the trailing slash
Collaborator

SimonSapin commented May 12, 2012

How about setting the default MIME type to HTML?

In Frozen-Flask: app.config['FREEZER_DEFAULT_MIMETYPE'] = 'text/html'
In Apache: DefaultType text/html (adapt for other servers)

Your HTML would be saved without warnings to files without extensions, and you would not use trailing slash in your Flask app nor in your static server. Would this work for you? I think this is more "right" than confusing the two URLs.

FREZZER_DEFAULT_MIMETYPE is more or lesse the oposite of this. FREZZER_DEFAULT_MIMETYPE works when you can change the web server settings, FREEZER_REWRITE_HTML_AS_FOLDER would be for cases where you cannot change the server - wihch is my current problem because with github pages.

Collaborator

SimonSapin commented May 12, 2012

So you can not change the Flask app nor the server config?

Yes that is correct.

Collaborator

SimonSapin commented May 12, 2012

Unfortunately my application relies on the trailing to work properly, and I can't change it to behave otherwise.
It is mapping the URL path to content on disk, and in that case there can't be a file and folder with the same name, so changing the path would essentially break the app.

Instead of /foo/<path:filename> you can have /foo/<path:filename>/ as an URL rule. This way the trailing slash is in the URLs, but not in the filename variable that you map to file names.

(I’m insisting on trying to find an other solution because, as much as I appreciate you taking the time to make a patch (with tests!) I don’t like the idea of what this patch does.)

I understand, I wrote the code and even I am not too confortable with it :D. Mostly because this is too much of a corner case i.e. I am basically trying to fix a problem that should never happen.

I any case thanks for the time, I'll have to spend some time thinking this through, changing the paths will have some deeper consequences on this setup.

@equalsraf equalsraf closed this Dec 4, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment