473,624 Members | 2,252 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

os.path.vnormpa th

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(r oot)
p = copy.deepcopy(p ath)

if os.path.isabs(p ath):
return root + path
while os.path.commonp refix([root, os.path.normpat h(os.path.join( r,p))])
<> root:
r = os.path.join(r, "junk")
return os.path.normpat h(os.path.join( r,p))
if __name__ == "__main__":
print vnormpath("C:\\ foo\\baz",
"..\\..\\..\\.. \\foo\\baz\\..\ \..\\..\\frob\\ glop")
Jul 18 '05 #1
3 1767
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(r oot)
p = copy.deepcopy(p ath)
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(p ath):
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.commonp refix([root,
os.path.normpat h(os.path.join( r,p))])
<> root:
r = os.path.join(r, "junk")
return os.path.normpat h(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
Jul 18 '05 #2
Correct. vnormpath() should enforce the same rules web servers resolves a
virtual paths on the server side.

Example:

Virtual root - C:\foo\baz\wwwr oot
URL - <a href="..\grop.h tml">

The above should never resolve to anything higher than
C:\foo\baz\wwwr oot\grop.html regardless of its position in the web root
directory.

vnormpath("C:\\ foo\\baz\\wwwro ot", "..\\grop.html" ) ==
"C:\\foo\\baz\\ wwwroot\\grop.h tml"

Likewise (here's a catch):

Virtual root - C:\foo\baz\wwwr oot
URL - <a href="\..\..\ba z\wwwroot\grop. html">

Should resolve to C:\foo\baz\wwwr oot\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(r oot)
p = copy.deepcopy(p ath)


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(p ath):
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.commonp refix([root,
os.path.normpat h(os.path.join( r,p))])
<> root:
r = os.path.join(r, "junk")
return os.path.normpat h(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

Jul 18 '05 #3
"Ben Allfree" <be****@bldigit al.com> schreef:
Virtual root - C:\foo\baz\wwwr oot
URL - <a href="\..\..\ba z\wwwroot\grop. html">

Should resolve to C:\foo\baz\wwwr oot\baz\wwwroot \grop.html


According to RFC-2396 this should resolve to:

C:\foo\baz\wwwr oot\..\..\baz\w wwroot\grop.htm l

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
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

31
3898
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 in the os module got objectified properly (or at least with some semblance of OO propriety!) In the issues section:
9
3597
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
70
4069
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 Python standard library, and Reinhold Birkenfeld started a discussion on it in python-dev <http://mail.python.org/pipermail/python-dev/2005-June/054438.html>. The upshot of the discussion was that many python-dev'ers wanted path added to the...
34
3243
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: /python/nondist/sandbox/path/{path.py,test_path.py} One thing is still different, though: a Path instance won't compare to a regular string.
2
23375
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 using '/' as delimiters and get the last 'word'. The reason I'm not entirely happy with that method is that it is platform specific. I would prefer to use a built in method if possible. Cheers,
1
5845
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 the no funny business can go on in the passed "path" that would produce a path not in the root (i.e. "..\..\dir1"). Here is my first stab at it, but not sure if this is too much or not enouph to ensure this. Any thoughts are welcome. TIA. ///...
5
8612
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". Same file path containing special characters works fine in one machine, but doesn't work in other. I am using following API function to get short file path. Declare Auto Function GetShortPathName Lib "kernel32" (ByVal lpszLongPath As
8
11481
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 file. each control has a border with another style, and each border has a unique path inside of it. I need to dynamically add these content controls using c# at runtime and am having trouble referencing the styles and adding the path into the...
6
4047
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, realpath, abspath here = dirname(realpath(abspath(__file__.rstrip("c")))) In particular, this takes care of the case of symlinked, compiled scripts, which is fairly common at my workplace, and I imagine in many *nix evironments.
0
8681
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8629
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8341
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8488
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6112
isladogs
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4183
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1488
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.