os.path.vnormpath

Ben Allfree benles at bldigital.com
Sun Nov 9 14:29:06 EST 2003


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" <__peter__ at web.de> wrote in message
news:bol2ob$1uv$03$1 at 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
>
>






More information about the Python-list mailing list