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

Bugfix/gzip #35

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Bugfix/gzip #35

wants to merge 3 commits into from

Conversation

jaboja
Copy link

@jaboja jaboja commented Mar 26, 2023

  • I've fixed the gzip responses, which seemed to get wrong when the chunked transfer encoding was also on. It may however still need some work, to make the gzip chunked, and not just avoid doing a chunked response when it's gzipped.
  • I've also fixed the MIME type for GET responses for directories, as the httpd/unix-directory makes no sense with a HTML listing of a directory.
  • And as an alternative to those listings, I've added support for index.html files. It requires defining the name(s) of the index file(s), however, and by default is disabled.

Copy link
Owner

@andrewleech andrewleech left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fantastic cleanup & consolidation in data_to_bytes_iterator() and send_body_encoded(). I've read through them and to me it does look like you've correctly captured the slightly different usages of this code nicely with a bunch of great simplifications along the way!

I don't have any way to really test this myself other than the unit tests run here in Actions - which fail but they fail in the same way as the master branch :-) so it's equivalent as far as that goes!

@@ -250,6 +205,8 @@ def _HEAD_GET(self, with_body=False):
content_type = 'text/html;charset=utf-8'
else:
content_type = dc.get_prop(uri, "DAV:", "getcontenttype")
if content_type == 'httpd/unix-directory':
content_type = 'text/html;charset=utf-8'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What inspired this change?
I'm not sure what the webdav spec suggests, however a google of webdav mime "httpd/unix-directory" gets a lot of hits; it seems to be pretty common?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a WebDAV response, but a standard HTTP GET/HEAD response returning the list of files as a HTML. Serving the HTML file as httpd/unix-directory makes no sense. It breaks the webbrowser pointed at that URL, while WebDAV clients would nevertheless use the WebDAV method PROPFIND and not the GET/HEAD.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok, that makes sense. I didn't actually realise the server would serve "regular" http requests as well as webdav ones.

Comment on lines +159 to +167
if os.path.isdir(path):
for filename in self.index_files:
new_path = os.path.join(path, filename)
if os.path.isfile(new_path):
path = new_path
break
else:
msg = self._get_listing(path)
return Resource(StringIO(msg), len(msg))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I gather this index file handing is quite non-standard for webdav servers?

Does this mean that if it's enabled and you try to view a folder that's got an index style file in it, you get served that file rather than the directory listing?

by default is disabled

Looking at the code, is an end user expected to edit this python file to add filenames to index_files = () above?
Or have to create a wrapper script that imports this, injects the names, then start the server programatically?
For this to be included I think it'd need at least some docs, if not a command line argument to enable?

Copy link
Author

@jaboja jaboja Mar 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. WebDAV allows that behavior:
    http://www.webdav.org/specs/rfc4918.html#rfc.section.9.4

  2. I intended it to be extended via inheritance, like this:

class CustomFilesystemHandler(FilesystemHandler):
    mimecheck = True
    index_files = ("index.html",)

However I see it would be better if I write some docs for that feature.

log.debug("Don't use iterator")

for buf in data:
yield buf if isinstance(buf, six.binary_type) else str(buf).encode('utf-8')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be safer as:

Suggested change
yield buf if isinstance(buf, six.binary_type) else str(buf).encode('utf-8')
yield buf if isinstance(buf, six.binary_type) else bytes(buf, 'utf-8')

Using str(buf) could cause an undesired output if buf happens to be something that doesn't implicitely convert to the str cleanly.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should just throw exception in such case? This should be str or bytes anyway.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well I don't think it's an expected error, throwing an exception is ugly useful if you're going to catch and deal with it somewhere.

Without tracing the code through I could see it potentially being a bytearray, or a future change might want to change it to one...

This was more of a best practices suggestion, if you want to convert the object to a bytes it's better to convert in one step, rather than convert to str first and then to bytes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants