mirror of
https://github.com/python/cpython.git
synced 2025-07-19 09:15:34 +00:00
Sjoerd Mullender:
File names with "funny" characters get translated wrong by pathname2url (any variety). E.g. the (Unix) file "/ufs/sjoerd/#tmp" gets translated into "/ufs/sjoerd/#tmp" which, when interpreted as a URL is file "/ufs/sjoerd/" with fragment ID "tmp". Here's an easy fix. (An alternative fix would be to change the various implementations of pathname2url and url2pathname to include calls to quote and unquote. [The main problem is with the normal use of URLs: url = url2pathname(file) transmit url url, tag = splittag(url) urlopen(url) ] In addition, this patch fixes some uses of unquote: - the host part of URLs should be unquoted - the file path in the FTP URL should be unquoted before it is split into components. - because of the latter, I removed all unquoting from ftpwrapper, and moved it to the caller, but that is not essential
This commit is contained in:
parent
dc86a4e5bb
commit
33add0a95a
1 changed files with 16 additions and 6 deletions
|
@ -42,6 +42,13 @@ else:
|
||||||
def pathname2url(pathname):
|
def pathname2url(pathname):
|
||||||
return pathname
|
return pathname
|
||||||
|
|
||||||
|
_url2pathname = url2pathname
|
||||||
|
def url2pathname(url):
|
||||||
|
return _url2pathname(unquote(url))
|
||||||
|
_pathname2url = pathname2url
|
||||||
|
def pathname2url(p):
|
||||||
|
return quote(_pathname2url(p))
|
||||||
|
|
||||||
# This really consists of two pieces:
|
# This really consists of two pieces:
|
||||||
# (1) a class which handles opening of all sorts of URLs
|
# (1) a class which handles opening of all sorts of URLs
|
||||||
# (plus assorted utilities etc.)
|
# (plus assorted utilities etc.)
|
||||||
|
@ -228,6 +235,7 @@ class URLopener:
|
||||||
host, selector = splithost(url)
|
host, selector = splithost(url)
|
||||||
if host:
|
if host:
|
||||||
user_passwd, host = splituser(host)
|
user_passwd, host = splituser(host)
|
||||||
|
host = unquote(host)
|
||||||
realhost = host
|
realhost = host
|
||||||
else:
|
else:
|
||||||
host, selector = url
|
host, selector = url
|
||||||
|
@ -298,6 +306,7 @@ class URLopener:
|
||||||
import gopherlib
|
import gopherlib
|
||||||
host, selector = splithost(url)
|
host, selector = splithost(url)
|
||||||
if not host: raise IOError, ('gopher error', 'no host given')
|
if not host: raise IOError, ('gopher error', 'no host given')
|
||||||
|
host = unquote(host)
|
||||||
type, selector = splitgophertype(selector)
|
type, selector = splitgophertype(selector)
|
||||||
selector, query = splitquery(selector)
|
selector, query = splitquery(selector)
|
||||||
selector = unquote(selector)
|
selector = unquote(selector)
|
||||||
|
@ -329,7 +338,6 @@ class URLopener:
|
||||||
host, port = splitport(host)
|
host, port = splitport(host)
|
||||||
if not port and socket.gethostbyname(host) in (
|
if not port and socket.gethostbyname(host) in (
|
||||||
localhost(), thishost()):
|
localhost(), thishost()):
|
||||||
file = unquote(file)
|
|
||||||
return addinfourl(
|
return addinfourl(
|
||||||
open(url2pathname(file), 'rb'),
|
open(url2pathname(file), 'rb'),
|
||||||
headers, 'file:'+file)
|
headers, 'file:'+file)
|
||||||
|
@ -343,6 +351,9 @@ class URLopener:
|
||||||
user, host = splituser(host)
|
user, host = splituser(host)
|
||||||
if user: user, passwd = splitpasswd(user)
|
if user: user, passwd = splitpasswd(user)
|
||||||
else: passwd = None
|
else: passwd = None
|
||||||
|
host = unquote(host)
|
||||||
|
user = unquote(user or '')
|
||||||
|
passwd = unquote(passwd or '')
|
||||||
host = socket.gethostbyname(host)
|
host = socket.gethostbyname(host)
|
||||||
if not port:
|
if not port:
|
||||||
import ftplib
|
import ftplib
|
||||||
|
@ -350,6 +361,7 @@ class URLopener:
|
||||||
else:
|
else:
|
||||||
port = int(port)
|
port = int(port)
|
||||||
path, attrs = splitattr(path)
|
path, attrs = splitattr(path)
|
||||||
|
path = unquote(path)
|
||||||
dirs = string.splitfields(path, '/')
|
dirs = string.splitfields(path, '/')
|
||||||
dirs, file = dirs[:-1], dirs[-1]
|
dirs, file = dirs[:-1], dirs[-1]
|
||||||
if dirs and not dirs[0]: dirs = dirs[1:]
|
if dirs and not dirs[0]: dirs = dirs[1:]
|
||||||
|
@ -548,13 +560,11 @@ def noheaders():
|
||||||
# Class used by open_ftp() for cache of open FTP connections
|
# Class used by open_ftp() for cache of open FTP connections
|
||||||
class ftpwrapper:
|
class ftpwrapper:
|
||||||
def __init__(self, user, passwd, host, port, dirs):
|
def __init__(self, user, passwd, host, port, dirs):
|
||||||
self.user = unquote(user or '')
|
self.user = user
|
||||||
self.passwd = unquote(passwd or '')
|
self.passwd = passwd
|
||||||
self.host = host
|
self.host = host
|
||||||
self.port = port
|
self.port = port
|
||||||
self.dirs = []
|
self.dirs = dirs
|
||||||
for dir in dirs:
|
|
||||||
self.dirs.append(unquote(dir))
|
|
||||||
self.init()
|
self.init()
|
||||||
def init(self):
|
def init(self):
|
||||||
import ftplib
|
import ftplib
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue