blob: 853e6608380e92da93581c2a39cb1460b93bb9c7 [file] [log] [blame]
Jim Fasarakis-Hilliardcb5297a2017-03-17 20:16:20 +02001"""Convert a NT pathname to a file URL and vice versa.
2
3This module only exists to provide OS-specific code
4for urllib.requests, thus do not use directly.
5"""
6# Testing is done through test_urllib.
Guido van Rossum746ea351996-06-26 19:47:56 +00007
8def url2pathname(url):
Georg Brandlc0b24732005-12-26 22:53:56 +00009 """OS-specific conversion from a relative URL of the 'file' scheme
10 to a file system path; not recommended for general use."""
11 # e.g.
Serhiy Storchaka458123b2015-10-24 17:39:36 +030012 # ///C|/foo/bar/spam.foo
13 # and
14 # ///C:/foo/bar/spam.foo
15 # become
16 # C:\foo\bar\spam.foo
Amaury Forgeot d'Arcc80902e2008-06-18 22:38:24 +000017 import string, urllib.parse
Georg Brandl07f159d2005-12-15 21:59:00 +000018 # Windows itself uses ":" even in URLs.
19 url = url.replace(':', '|')
Tim Peters2344fae2001-01-15 00:50:52 +000020 if not '|' in url:
21 # No drive specifier, just convert slashes
22 if url[:4] == '////':
23 # path is something like ////host/path/on/remote/host
24 # convert this to \\host\path\on\remote\host
25 # (notice halving of slashes at the start of the path)
26 url = url[2:]
Eric S. Raymondb08b2d32001-02-09 11:10:16 +000027 components = url.split('/')
Tim Peters2344fae2001-01-15 00:50:52 +000028 # make sure not to convert quoted slashes :-)
Amaury Forgeot d'Arcc80902e2008-06-18 22:38:24 +000029 return urllib.parse.unquote('\\'.join(components))
Eric S. Raymondb08b2d32001-02-09 11:10:16 +000030 comp = url.split('|')
Fred Drake27eebb82001-07-20 18:52:02 +000031 if len(comp) != 2 or comp[0][-1] not in string.ascii_letters:
Tim Peters2344fae2001-01-15 00:50:52 +000032 error = 'Bad URL: ' + url
Andrew Svetlovf7a17b42012-12-25 16:47:37 +020033 raise OSError(error)
Eric S. Raymondb08b2d32001-02-09 11:10:16 +000034 drive = comp[0][-1].upper()
35 components = comp[1].split('/')
Tim Peters2344fae2001-01-15 00:50:52 +000036 path = drive + ':'
Senthil Kumaran2d2ea1b2011-04-14 13:16:30 +080037 for comp in components:
Tim Peters2344fae2001-01-15 00:50:52 +000038 if comp:
Amaury Forgeot d'Arcc80902e2008-06-18 22:38:24 +000039 path = path + '\\' + urllib.parse.unquote(comp)
Senthil Kumaran2d2ea1b2011-04-14 13:16:30 +080040 # Issue #11474 - handing url such as |c/|
41 if path.endswith(':') and url.endswith('/'):
42 path += '\\'
Tim Peters2344fae2001-01-15 00:50:52 +000043 return path
Guido van Rossum746ea351996-06-26 19:47:56 +000044
45def pathname2url(p):
Georg Brandlc0b24732005-12-26 22:53:56 +000046 """OS-specific conversion from a file system path to a relative URL
47 of the 'file' scheme; not recommended for general use."""
48 # e.g.
Serhiy Storchaka458123b2015-10-24 17:39:36 +030049 # C:\foo\bar\spam.foo
Georg Brandlc0b24732005-12-26 22:53:56 +000050 # becomes
Serhiy Storchaka458123b2015-10-24 17:39:36 +030051 # ///C:/foo/bar/spam.foo
Amaury Forgeot d'Arcc80902e2008-06-18 22:38:24 +000052 import urllib.parse
Tim Peters2344fae2001-01-15 00:50:52 +000053 if not ':' in p:
54 # No drive specifier, just convert slashes and quote the name
55 if p[:2] == '\\\\':
56 # path is something like \\host\path\on\remote\host
57 # convert this to ////host/path/on/remote/host
58 # (notice doubling of slashes at the start of the path)
59 p = '\\\\' + p
Eric S. Raymondb08b2d32001-02-09 11:10:16 +000060 components = p.split('\\')
Amaury Forgeot d'Arcc80902e2008-06-18 22:38:24 +000061 return urllib.parse.quote('/'.join(components))
Eric S. Raymondb08b2d32001-02-09 11:10:16 +000062 comp = p.split(':')
Tim Peters2344fae2001-01-15 00:50:52 +000063 if len(comp) != 2 or len(comp[0]) > 1:
64 error = 'Bad path: ' + p
Andrew Svetlovf7a17b42012-12-25 16:47:37 +020065 raise OSError(error)
Guido van Rossum746ea351996-06-26 19:47:56 +000066
Amaury Forgeot d'Arcc80902e2008-06-18 22:38:24 +000067 drive = urllib.parse.quote(comp[0].upper())
Eric S. Raymondb08b2d32001-02-09 11:10:16 +000068 components = comp[1].split('\\')
Senthil Kumaran690ce9b2009-05-05 18:41:13 +000069 path = '///' + drive + ':'
Tim Peters2344fae2001-01-15 00:50:52 +000070 for comp in components:
71 if comp:
Amaury Forgeot d'Arcc80902e2008-06-18 22:38:24 +000072 path = path + '/' + urllib.parse.quote(comp)
Tim Peters2344fae2001-01-15 00:50:52 +000073 return path