mirror of https://github.com/django/django.git
Moved django.core.handlers.wsgi.AdminMediaHandler to django.core.servers.basehttp. Makes more sense to have it in there, because its only use is for the development server.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@491 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
43f3b984e8
commit
199aa88ffb
|
@ -144,48 +144,3 @@ class WSGIHandler(BaseHandler):
|
||||||
output = [response.get_content_as_string('utf-8')]
|
output = [response.get_content_as_string('utf-8')]
|
||||||
start_response(status, response_headers.items())
|
start_response(status, response_headers.items())
|
||||||
return output
|
return output
|
||||||
|
|
||||||
class AdminMediaHandler:
|
|
||||||
"""
|
|
||||||
WSGI middleware that intercepts calls to the admin media directory, as
|
|
||||||
defined by the ADMIN_MEDIA_PREFIX setting, and serves those images.
|
|
||||||
Use this ONLY LOCALLY, for development! This hasn't been tested for
|
|
||||||
security and is not super efficient.
|
|
||||||
"""
|
|
||||||
def __init__(self, application):
|
|
||||||
from django.conf import settings
|
|
||||||
import django
|
|
||||||
self.application = application
|
|
||||||
self.media_dir = django.__path__[0] + '/conf/admin_media'
|
|
||||||
self.media_url = settings.ADMIN_MEDIA_PREFIX
|
|
||||||
|
|
||||||
def __call__(self, environ, start_response):
|
|
||||||
import os.path
|
|
||||||
|
|
||||||
# Ignore requests that aren't under ADMIN_MEDIA_PREFIX. Also ignore
|
|
||||||
# all requests if ADMIN_MEDIA_PREFIX isn't a relative URL.
|
|
||||||
if self.media_url.startswith('http://') or self.media_url.startswith('https://') \
|
|
||||||
or not environ['PATH_INFO'].startswith(self.media_url):
|
|
||||||
return self.application(environ, start_response)
|
|
||||||
|
|
||||||
# Find the admin file and serve it up, if it exists and is readable.
|
|
||||||
relative_url = environ['PATH_INFO'][len(self.media_url):]
|
|
||||||
file_path = os.path.join(self.media_dir, relative_url)
|
|
||||||
if not os.path.exists(file_path):
|
|
||||||
status = '404 NOT FOUND'
|
|
||||||
headers = {'Content-type': 'text/plain'}
|
|
||||||
output = ['Page not found: %s' % file_path]
|
|
||||||
else:
|
|
||||||
try:
|
|
||||||
fp = open(file_path, 'r')
|
|
||||||
except IOError:
|
|
||||||
status = '401 UNAUTHORIZED'
|
|
||||||
headers = {'Content-type': 'text/plain'}
|
|
||||||
output = ['Permission denied: %s' % file_path]
|
|
||||||
else:
|
|
||||||
status = '200 OK'
|
|
||||||
headers = {}
|
|
||||||
output = [fp.read()]
|
|
||||||
fp.close()
|
|
||||||
start_response(status, headers.items())
|
|
||||||
return output
|
|
||||||
|
|
|
@ -485,8 +485,8 @@ inspectdb.args = "[dbname]"
|
||||||
|
|
||||||
def runserver(port):
|
def runserver(port):
|
||||||
"Starts a lightweight Web server for development."
|
"Starts a lightweight Web server for development."
|
||||||
from django.core.servers.basehttp import run, WSGIServerException
|
from django.core.servers.basehttp import run, AdminMediaHandler, WSGIServerException
|
||||||
from django.core.handlers.wsgi import AdminMediaHandler, WSGIHandler
|
from django.core.handlers.wsgi import WSGIHandler
|
||||||
if not port.isdigit():
|
if not port.isdigit():
|
||||||
sys.stderr.write("Error: %r is not a valid port number.\n" % port)
|
sys.stderr.write("Error: %r is not a valid port number.\n" % port)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
|
@ -591,6 +591,51 @@ class WSGIRequestHandler(BaseHTTPRequestHandler):
|
||||||
return
|
return
|
||||||
sys.stderr.write("[%s] %s\n" % (self.log_date_time_string(), format % args))
|
sys.stderr.write("[%s] %s\n" % (self.log_date_time_string(), format % args))
|
||||||
|
|
||||||
|
class AdminMediaHandler:
|
||||||
|
"""
|
||||||
|
WSGI middleware that intercepts calls to the admin media directory, as
|
||||||
|
defined by the ADMIN_MEDIA_PREFIX setting, and serves those images.
|
||||||
|
Use this ONLY LOCALLY, for development! This hasn't been tested for
|
||||||
|
security and is not super efficient.
|
||||||
|
"""
|
||||||
|
def __init__(self, application):
|
||||||
|
from django.conf import settings
|
||||||
|
import django
|
||||||
|
self.application = application
|
||||||
|
self.media_dir = django.__path__[0] + '/conf/admin_media'
|
||||||
|
self.media_url = settings.ADMIN_MEDIA_PREFIX
|
||||||
|
|
||||||
|
def __call__(self, environ, start_response):
|
||||||
|
import os.path
|
||||||
|
|
||||||
|
# Ignore requests that aren't under ADMIN_MEDIA_PREFIX. Also ignore
|
||||||
|
# all requests if ADMIN_MEDIA_PREFIX isn't a relative URL.
|
||||||
|
if self.media_url.startswith('http://') or self.media_url.startswith('https://') \
|
||||||
|
or not environ['PATH_INFO'].startswith(self.media_url):
|
||||||
|
return self.application(environ, start_response)
|
||||||
|
|
||||||
|
# Find the admin file and serve it up, if it exists and is readable.
|
||||||
|
relative_url = environ['PATH_INFO'][len(self.media_url):]
|
||||||
|
file_path = os.path.join(self.media_dir, relative_url)
|
||||||
|
if not os.path.exists(file_path):
|
||||||
|
status = '404 NOT FOUND'
|
||||||
|
headers = {'Content-type': 'text/plain'}
|
||||||
|
output = ['Page not found: %s' % file_path]
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
fp = open(file_path, 'r')
|
||||||
|
except IOError:
|
||||||
|
status = '401 UNAUTHORIZED'
|
||||||
|
headers = {'Content-type': 'text/plain'}
|
||||||
|
output = ['Permission denied: %s' % file_path]
|
||||||
|
else:
|
||||||
|
status = '200 OK'
|
||||||
|
headers = {}
|
||||||
|
output = [fp.read()]
|
||||||
|
fp.close()
|
||||||
|
start_response(status, headers.items())
|
||||||
|
return output
|
||||||
|
|
||||||
def run(port, wsgi_handler):
|
def run(port, wsgi_handler):
|
||||||
server_address = ('', port)
|
server_address = ('', port)
|
||||||
httpd = WSGIServer(server_address, WSGIRequestHandler)
|
httpd = WSGIServer(server_address, WSGIRequestHandler)
|
||||||
|
|
Loading…
Reference in New Issue