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
Fix "Twisted Web in 60 Seconds" examples on Python 3 #894
2bc9df6
bc4c128
1bc9f6e
0c4177f
5195b0e
f740c99
c4ddb8b
c44dfd0
6e50c2c
f43925e
7396141
006f480
ea1608e
d9cb395
ee89d0d
c03cda1
1c3097b
f3bd540
Diff settings
static.File decodes request paths as UTF-8 with a unicode FilePath.
If the underlying path is itself unicode, then the request path should be decoded as UTF-8 per RFC 2047: https://tools.ietf.org/html/rfc2047 To ensure compatibility with Python 3, all other paths should be native strings.
- Loading branch information...
| @@ -12,7 +12,6 @@ | ||
| import itertools | ||
| import mimetypes | ||
| import os | ||
| import sys | ||
| import time | ||
| import warnings | ||
| @@ -23,8 +22,8 @@ | ||
| from twisted.web import http | ||
| from twisted.web.util import redirectTo | ||
| from twisted.python.compat import (_PY3, StringType, intToBytes, nativeString, | ||
| networkString) | ||
| from twisted.python.compat import (_PY3, intToBytes, nativeString, | ||
| networkString, unicode) | ||
| from twisted.python.compat import escape | ||
| from twisted.python import components, filepath, log | ||
| @@ -246,7 +245,7 @@ def __init__(self, path, defaultType="text/html", ignoredExts=(), registry=None, | ||
| if ignoredExts in (0, 1) or allowExt: | ||
| warnings.warn("ignoredExts should receive a list, not a boolean") | ||
| if ignoredExts or allowExt: | ||
| self.ignoredExts = [b'*'] | ||
| self.ignoredExts = ['*'] | ||
| else: | ||
| self.ignoredExts = [] | ||
| else: | ||
| @@ -274,11 +273,33 @@ def directoryListing(self): | ||
| def getChild(self, path, request): | ||
| """ | ||
| If this L{File}'s path refers to a directory, return a L{File} | ||
| If this L{File}"s path refers to a directory, return a L{File} | ||
| referring to the file named C{path} in that directory. | ||
| If C{path} is the empty string, return a L{DirectoryLister} instead. | ||
| If C{path} is the empty string, return a L{DirectoryLister} | ||
| instead. | ||
| @param path: The current path segment. | ||
| @type path: L{bytes} | ||
| @param request: The incoming request. | ||
| @type request: An that provides L{twisted.web.iweb.IRequest}. | ||
| @return: A resource representing the requested file or | ||
| directory, or L{NoResource} if the path cannot be | ||
| accessed. | ||
| @rtype: An object that provides L{resource.IResource}. | ||
| """ | ||
| if isinstance(self.path, unicode): | ||
| try: | ||
| # Request calls urllib.unquote on each path segment, | ||
| # leaving us with raw bytes. | ||
| path = path.decode('utf-8') | ||
Show comment
Hide comment
rodrigc
Contributor
|
||
| except UnicodeDecodeError: | ||
| log.err(None, | ||
| "Could not decode path segment as utf-8: %r" % (path,)) | ||
| return self.childNotFound | ||
| self.restat(reraise=False) | ||
| if not self.isdir(): | ||
| @@ -300,8 +321,6 @@ def getChild(self, path, request): | ||
| return self.childNotFound | ||
| extension = fpath.splitext()[1] | ||
| if not isinstance(extension, StringType): | ||
| extension = extension.decode(sys.getfilesystemencoding()) | ||
| if platformType == "win32": | ||
| # don't want .RPY to be different than .rpy, since that would allow | ||
| # source disclosure. | ||
| @@ -334,7 +353,7 @@ def _parseRangeHeader(self, range): | ||
| @return: A list C{[(start, stop)]} of pairs of length at least one. | ||
| @raise ValueError: if the header is syntactically invalid or if the | ||
| Bytes-Unit is anything other than 'bytes'. | ||
| Bytes-Unit is anything other than "bytes'. | ||
| """ | ||
| try: | ||
| kind, value = range.split(b'=', 1) | ||
@markrwilliams This part of the patch is wrong, and breaks several builders.
unicodedoes not have a.decodemethod