Written to normalize web server path names based on a virtual root. I
propose that something equivalent to this be added to os.path
-------------------------------------
import os.path
import copy
def vnormpath(root,path):
"""
Normalize a path based on a virtual root.
"""
r = copy.deepcopy(root)
p = copy.deepcopy(path)
if os.path.isabs(path):
return root + path
while os.path.commonprefix([root, os.path.normpath(os.path.join(r,p))])
<> root:
r = os.path.join(r,"junk")
return os.path.normpath(os.path.join(r,p))
if __name__ == "__main__":
print vnormpath("C:\\foo\\baz",
"..\\..\\..\\..\\foo\\baz\\..\\..\\..\\frob\\glop" ) 3 1760
Ben Allfree wrote: Written to normalize web server path names based on a virtual root. I propose that something equivalent to this be added to os.path
The purpose remains unclear to me. Maybe you could expand a little.
For now, I suppose you intend vnormpath() to always return subfolders of
root.
------------------------------------- import os.path import copy
def vnormpath(root,path): """ Normalize a path based on a virtual root. """ r = copy.deepcopy(root) p = copy.deepcopy(path)
Strings are "immutable", i. e. you can't change them once created. You
therefore need not (I'd rather say must not) copy them. if os.path.isabs(path): return root + path
This can give you funny paths on Windows like "C:/firstC:/second"; on Unix
you can get anywhere the supplier of *path* wants,
e. g. vnormpath("/home/peter", "/../ben") returns "/home/peter/../ben" which
is equivalent to "home/ben".
while os.path.commonprefix([root, os.path.normpath(os.path.join(r,p))]) <> root: r = os.path.join(r,"junk") return os.path.normpath(os.path.join(r,p))
I tried to break that, too, but must admit I didn't succed so far :-)
By the way, most pythonistas favour != over <>.
if __name__ == "__main__": print vnormpath("C:\\foo\\baz", "..\\..\\..\\..\\foo\\baz\\..\\..\\..\\frob\\glop" )
The above demo is probably a good start, but try to think of a few more
use/test cases. For example, should
vnormpath("/home/ben", "/home/ben/temp") return "/home/ben/home/ben/temp" or
rather "home/ben/temp"? Also, I would compare vnormpath()'s actual against
the expected result instead of just printing it out. For production quality
code, have a look at the unittest module.
Conclusion: For the moment, be satisfied if you can fix the bugs and your
function serves *your* needs well. If you want to share the results, put it
on a web site.
For your code to make it into the python library, you have to convince
people that *they* need it badly and I dare say that you may face some
strong opposition.
Peter
Correct. vnormpath() should enforce the same rules web servers resolves a
virtual paths on the server side.
Example:
Virtual root - C:\foo\baz\wwwroot
URL - <a href="..\grop.html">
The above should never resolve to anything higher than
C:\foo\baz\wwwroot\grop.html regardless of its position in the web root
directory.
vnormpath("C:\\foo\\baz\\wwwroot", "..\\grop.html") ==
"C:\\foo\\baz\\wwwroot\\grop.html"
Likewise (here's a catch):
Virtual root - C:\foo\baz\wwwroot
URL - <a href="\..\..\baz\wwwroot\grop.html">
Should resolve to C:\foo\baz\wwwroot\baz\wwwroot\grop.html
I hope that make sense. My code is a start at solving the issue.
"Peter Otten" <__*******@web.de> wrote in message
news:bo*************@news.t-online.com... Ben Allfree wrote:
Written to normalize web server path names based on a virtual root. I propose that something equivalent to this be added to os.path The purpose remains unclear to me. Maybe you could expand a little. For now, I suppose you intend vnormpath() to always return subfolders of root.
------------------------------------- import os.path import copy
def vnormpath(root,path): """ Normalize a path based on a virtual root. """ r = copy.deepcopy(root) p = copy.deepcopy(path)
Strings are "immutable", i. e. you can't change them once created. You therefore need not (I'd rather say must not) copy them.
if os.path.isabs(path): return root + path
This can give you funny paths on Windows like "C:/firstC:/second"; on Unix you can get anywhere the supplier of *path* wants, e. g. vnormpath("/home/peter", "/../ben") returns "/home/peter/../ben"
which is equivalent to "home/ben".
while os.path.commonprefix([root, os.path.normpath(os.path.join(r,p))]) <> root: r = os.path.join(r,"junk") return os.path.normpath(os.path.join(r,p)) I tried to break that, too, but must admit I didn't succed so far :-) By the way, most pythonistas favour != over <>.
if __name__ == "__main__": print vnormpath("C:\\foo\\baz", "..\\..\\..\\..\\foo\\baz\\..\\..\\..\\frob\\glop" )
The above demo is probably a good start, but try to think of a few more use/test cases. For example, should vnormpath("/home/ben", "/home/ben/temp") return "/home/ben/home/ben/temp"
or rather "home/ben/temp"? Also, I would compare vnormpath()'s actual against the expected result instead of just printing it out. For production
quality code, have a look at the unittest module.
Conclusion: For the moment, be satisfied if you can fix the bugs and your function serves *your* needs well. If you want to share the results, put
it on a web site. For your code to make it into the python library, you have to convince people that *they* need it badly and I dare say that you may face some strong opposition.
Peter
"Ben Allfree" <be****@bldigital.com> schreef: Virtual root - C:\foo\baz\wwwroot URL - <a href="\..\..\baz\wwwroot\grop.html">
Should resolve to C:\foo\baz\wwwroot\baz\wwwroot\grop.html
According to RFC-2396 this should resolve to:
C:\foo\baz\wwwroot\..\..\baz\wwwroot\grop.html
The ".." above are real path segments/directory names and don't have the
special "go up 1 level" meaning.
IMHO for web servers that map URL path segments to local paths this should
result in an HTTP error (when ".." is not allowed on the local filesystem).
They also mention that in practice your solution is in use by some parsers,
so people might not be too surprised by its functioning... ;-)
<http://www.cs.tut.fi/~jkorpela/rfc/2396/full.html#C>
--
JanC
"Be strict when sending and tolerant when receiving."
RFC 1958 - Architectural Principles of the Internet - section 3.9 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: John Roth |
last post by:
I'm adding a thread for comments on Gerrit Holl's pre-pep, which
can be found here:
http://tinyurl.com/2578q
Frankly, I like the idea. It's about time that all of the file
and directory stuff...
|
by: Bengt dePaulis |
last post by:
I have a local directory that I want to include in my sys.path
How to save it permanently?
Regards
/Bengt
|
by: Michael Hoffman |
last post by:
Many of you are familiar with Jason Orendorff's path module
<http://www.jorendorff.com/articles/python/path/>, which is frequently
recommended here on c.l.p. I submitted an RFE to add it to the...
|
by: Reinhold Birkenfeld |
last post by:
Hi,
the arguments in the previous thread were convincing enough, so I made the
Path class inherit from str/unicode again.
It still can be found in CVS:...
|
by: Rob Cowie |
last post by:
Hi,
Given a string representing the path to a file, what is the best way to
get at the filename? Does the OS module provide a function to parse the
path? or is it acceptable to split the string...
|
by: William Stacey [MVP] |
last post by:
I need a bullet proof way to combine a root and a relative path to form a FQ
rooted path (similar to a VDir in IIS). Path.Combine alone will not do the
job in all cases. I also need to be sure...
|
by: Sakharam Phapale |
last post by:
Hi All,
I am using an API function, which takes file path as an input.
When file path contains special characters (@,#,$,%,&,^, etc), API function
gives an error as "Unable to open input file"....
|
by: moondaddy |
last post by:
I'm posting code for a user control ( FunctionConnectorSelector) below which
has 3 content controls in it. each content control uses a style from a
resource dictionary merged into the app.xaml...
|
by: bukzor |
last post by:
I have to go into these convulsions to get the directory that the
script is in whenever I need to use relative paths. I was wondering if
you guys have a better way:
from os.path import dirname,...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |