11
11
from types import ListType, StringType
12
12
import os, re, sys, time, urllib
13
13
14
+
from django.utils._os import safe_join
15
+
14
16
__version__ = "0.1"
15
17
__all__ = ['WSGIServer','WSGIRequestHandler','demo_app']
16
18
@@ -599,11 +601,25 @@ def __init__(self, application, media_dir=None):
599
601
self.application = application
600
602
if not media_dir:
601
603
import django
602
-
self.media_dir = django.__path__[0] + '/contrib/admin/media'
604
+
self.media_dir = \
605
+
os.path.join(django.__path__[0], 'contrib', 'admin', 'media')
603
606
else:
604
607
self.media_dir = media_dir
605
608
self.media_url = settings.ADMIN_MEDIA_PREFIX
606
609
610
+
def file_path(self, url):
611
+
"""
612
+
Returns the path to the media file on disk for the given URL.
613
+
614
+
The passed URL is assumed to begin with ADMIN_MEDIA_PREFIX. If the
615
+
resultant file path is outside the media directory, then a ValueError
616
+
is raised.
617
+
"""
618
+
# Remove ADMIN_MEDIA_PREFIX.
619
+
relative_url = url[len(self.media_url):]
620
+
relative_path = urllib.url2pathname(relative_url)
621
+
return safe_join(self.media_dir, relative_path)
622
+
607
623
def __call__(self, environ, start_response):
608
624
import os.path
609
625
@@ -614,19 +630,25 @@ def __call__(self, environ, start_response):
614
630
return self.application(environ, start_response)
615
631
616
632
# Find the admin file and serve it up, if it exists and is readable.
617
-
relative_url = environ['PATH_INFO'][len(self.media_url):]
618
-
file_path = os.path.join(self.media_dir, relative_url)
633
+
try:
634
+
file_path = self.file_path(environ['PATH_INFO'])
635
+
except ValueError: # Resulting file path was not valid.
636
+
status = '404 NOT FOUND'
637
+
headers = {'Content-type': 'text/plain'}
638
+
output = ['Page not found: %s' % environ['PATH_INFO']]
639
+
start_response(status, headers.items())
640
+
return output
619
641
if not os.path.exists(file_path):
620
642
status = '404 NOT FOUND'
621
643
headers = {'Content-type': 'text/plain'}
622
-
output = ['Page not found: %s' % file_path]
644
+
output = ['Page not found: %s' % environ['PATH_INFO']]
623
645
else:
624
646
try:
625
647
fp = open(file_path, 'rb')
626
648
except IOError:
627
649
status = '401 UNAUTHORIZED'
628
650
headers = {'Content-type': 'text/plain'}
629
-
output = ['Permission denied: %s' % file_path]
651
+
output = ['Permission denied: %s' % environ['PATH_INFO']]
630
652
else:
631
653
status = '200 OK'
632
654
headers = {}
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4