| 1 |
_id__ = '$Id$' |
| 2 |
__modulename__ = 'PathFilter' |
| 3 |
|
| 4 |
import os |
| 5 |
|
| 6 |
from harmonious import harm |
| 7 |
|
| 8 |
class PathFilter: |
| 9 |
|
| 10 |
def perform(self): |
| 11 |
|
| 12 |
self.__normalise() |
| 13 |
harm.request.status = self.__get_file_status() |
| 14 |
|
| 15 |
def __normalise(self): |
| 16 |
|
| 17 |
path_slash = False |
| 18 |
|
| 19 |
harm.request._unlock('abs_path') |
| 20 |
harm.request._unlock('path') |
| 21 |
|
| 22 |
# os.path.normcase will cut off trailing '/'s; we want to keep them. |
| 23 |
if harm.request.path[-1] == '/': |
| 24 |
|
| 25 |
path_slash = True |
| 26 |
|
| 27 |
harm.request.path = os.path.normcase(os.path.normpath(harm.request.path)) |
| 28 |
harm.request.abs_path = os.path.normcase(os.path.normpath(harm.request.abs_path)) |
| 29 |
|
| 30 |
if path_slash == True: |
| 31 |
|
| 32 |
if harm.request.path != '/': |
| 33 |
|
| 34 |
harm.request._unlock('path') |
| 35 |
harm.request.path = "%s/" % harm.request.path |
| 36 |
|
| 37 |
harm.request._unlock('abs_path') |
| 38 |
harm.request.abs_path = "%s/" % harm.request.abs_path |
| 39 |
|
| 40 |
if os.path.isdir(harm.request.abs_path): |
| 41 |
|
| 42 |
if harm.request.path[-1] != '/': |
| 43 |
|
| 44 |
harm.request._unlock('abs_path') |
| 45 |
harm.request._unlock('path') |
| 46 |
|
| 47 |
harm.request.path = "%s/" % harm.request.path |
| 48 |
harm.request.abs_path = "%s/" % harm.request.abs_path |
| 49 |
|
| 50 |
if len(harm.request.queries) > 0 or harm.request.fragment != '': |
| 51 |
|
| 52 |
harm.request._unlock('file') |
| 53 |
|
| 54 |
harm.request.file = 'index.py' |
| 55 |
else: |
| 56 |
|
| 57 |
harm.request._unlock('path') |
| 58 |
harm.request._unlock('abs_path') |
| 59 |
harm.request._unlock('file') |
| 60 |
|
| 61 |
harm.request.file = os.path.basename(harm.request.path) |
| 62 |
harm.request.abs_path = harm.request.abs_path.split(harm.request.file)[0] |
| 63 |
harm.request.path = harm.request.path.split(os.path.basename(harm.request.file))[0] |
| 64 |
|
| 65 |
def __get_file_status(self): |
| 66 |
|
| 67 |
if os.path.exists(os.path.join(harm.request.abs_path, harm.request.file)): |
| 68 |
|
| 69 |
if os.path.isdir(os.path.join(harm.request.abs_path, harm.request.file)): |
| 70 |
return 501 |
| 71 |
|
| 72 |
try: |
| 73 |
open(os.path.join(harm.request.abs_path, harm.request.file), 'r') |
| 74 |
except: |
| 75 |
return 403 |
| 76 |
|
| 77 |
return 200 |
| 78 |
|
| 79 |
else: |
| 80 |
|
| 81 |
return 404 |