| 1 | # https://pyisapie.svn.sourceforge.net/svnroot/pyisapie/Tags/1.0.121/PyISAPIe/Python/Examples/Django/pyisapie.py
|
|---|
| 2 | # 91 2008-01-10 23:53:59 -0800 (Thu, 10 Jan 2008)
|
|---|
| 3 | # (C)2008 Phillip Sitbon <phillip@sitbon.net>
|
|---|
| 4 | #
|
|---|
| 5 | """Custom request handler for PyISAPIe.
|
|---|
| 6 | """
|
|---|
| 7 |
|
|---|
| 8 | from django.core.handlers.base import BaseHandler
|
|---|
| 9 | from django.core import signals
|
|---|
| 10 | from django.dispatch import dispatcher
|
|---|
| 11 | from django.utils import datastructures
|
|---|
| 12 | from django import http
|
|---|
| 13 | import os
|
|---|
| 14 |
|
|---|
| 15 | from Http import *
|
|---|
| 16 |
|
|---|
| 17 | class PyISAPIeRequest(http.HttpRequest):
|
|---|
| 18 | def __init__(This):
|
|---|
| 19 | This.method = Env.REQUEST_METHOD.upper()
|
|---|
| 20 | This.path = Env.URL
|
|---|
| 21 | # aren't list comprehensions fun?!
|
|---|
| 22 | This._headers_in = \
|
|---|
| 23 | dict((N.lower(), V) for N, V in [Item.split(': ',1) for Item in Env.ALL_RAW.split('\r\n') if Item])
|
|---|
| 24 |
|
|---|
| 25 | def get_full_path(This):
|
|---|
| 26 | Qs = Env.QUERY_STRING
|
|---|
| 27 | return '%s%s' % (This.path, Qs and ('?' + Qs) or '')
|
|---|
| 28 |
|
|---|
| 29 | def _load_post_and_files(This):
|
|---|
| 30 | "Populates This._post and This._files"
|
|---|
| 31 | Ctype = Env.HTTP_CONTENT_TYPE or ''
|
|---|
| 32 | if Ctype.startswith('multipart'):
|
|---|
| 33 | This._post, This._files = http.parse_file_upload(This._headers_in, This.raw_post_data)
|
|---|
| 34 | else:
|
|---|
| 35 | This._post, This._files = http.QueryDict(This.raw_post_data), datastructures.MultiValueDict()
|
|---|
| 36 |
|
|---|
| 37 | def _get_request(This):
|
|---|
| 38 | if not hasattr(This, '_request'):
|
|---|
| 39 | This._request = datastructures.MergeDict(This.POST, This.GET)
|
|---|
| 40 | return This._request
|
|---|
| 41 |
|
|---|
| 42 | def _get_get(This):
|
|---|
| 43 | if not hasattr(This, '_get'):
|
|---|
| 44 | This._get = http.QueryDict(Env.QUERY_STRING)
|
|---|
| 45 | return This._get
|
|---|
| 46 |
|
|---|
| 47 | def _set_get(This, get):
|
|---|
| 48 | This._get = get
|
|---|
| 49 |
|
|---|
| 50 | def _get_post(This):
|
|---|
| 51 | if not hasattr(This, '_post'):
|
|---|
| 52 | This._load_post_and_files()
|
|---|
| 53 | return This._post
|
|---|
| 54 |
|
|---|
| 55 | def _set_post(This, post):
|
|---|
| 56 | This._post = post
|
|---|
| 57 |
|
|---|
| 58 | def _get_cookies(This):
|
|---|
| 59 | if not hasattr(This, '_cookies'):
|
|---|
| 60 | This._cookies = http.parse_cookie(This._headers_in.get('cookie', ''))
|
|---|
| 61 | return This._cookies
|
|---|
| 62 |
|
|---|
| 63 | def _set_cookies(This, cookies):
|
|---|
| 64 | This._cookies = cookies
|
|---|
| 65 |
|
|---|
| 66 | def _get_files(This):
|
|---|
| 67 | if not hasattr(This, '_files'):
|
|---|
| 68 | This._load_post_and_files()
|
|---|
| 69 | return This._files
|
|---|
| 70 |
|
|---|
| 71 | def _get_meta(This):
|
|---|
| 72 | "Lazy loader that returns This.META dictionary"
|
|---|
| 73 | if not hasattr(This, '_meta'):
|
|---|
| 74 | This._meta = {
|
|---|
| 75 | 'AUTH_TYPE': Env.AUTH_TYPE,
|
|---|
| 76 | 'CONTENT_LENGTH': Env.CONTENT_LENGTH,
|
|---|
| 77 | 'CONTENT_TYPE': Env.CONTENT_TYPE,
|
|---|
| 78 | 'GATEWAY_INTERFACE': Env.GATEWAY_INTERFACE,
|
|---|
| 79 | 'PATH_INFO': Env.PATH_INFO,
|
|---|
| 80 | 'PATH_TRANSLATED': Env.PATH_TRANSLATED,
|
|---|
| 81 | 'QUERY_STRING': Env.QUERY_STRING,
|
|---|
| 82 | 'REMOTE_ADDR': Env.REMOTE_ADDR,
|
|---|
| 83 | 'REMOTE_HOST': Env.REMOTE_HOST,
|
|---|
| 84 | 'REMOTE_IDENT': None,
|
|---|
| 85 | 'REMOTE_USER': Env.REMOTE_USER,
|
|---|
| 86 | 'REQUEST_METHOD': Env.REQUEST_METHOD,
|
|---|
| 87 | 'SCRIPT_NAME': Env.SCRIPT_NAME,
|
|---|
| 88 | 'SERVER_NAME': Env.SERVER_NAME,
|
|---|
| 89 | 'SERVER_PORT': Env.SERVER_PORT,
|
|---|
| 90 | 'SERVER_PROTOCOL': Env.SERVER_PROTOCOL,
|
|---|
| 91 | 'SERVER_SOFTWARE': Env.SERVER_SOFTWARE
|
|---|
| 92 | }
|
|---|
| 93 | for key, value in This._headers_in.items():
|
|---|
| 94 | key = 'HTTP_' + key.upper().replace('-', '_')
|
|---|
| 95 | This._meta[key] = value
|
|---|
| 96 | return This._meta
|
|---|
| 97 |
|
|---|
| 98 | def _get_raw_post_data(This):
|
|---|
| 99 | try:
|
|---|
| 100 | return This._raw_post_data
|
|---|
| 101 | except AttributeError:
|
|---|
| 102 | This._raw_post_data = Read()
|
|---|
| 103 | return This._raw_post_data
|
|---|
| 104 |
|
|---|
| 105 | def _get_user(This):
|
|---|
| 106 | if not hasattr(This, '_user'):
|
|---|
| 107 | from django.models.auth import users
|
|---|
| 108 | try:
|
|---|
| 109 | user_id = This.session[users.SESSION_KEY]
|
|---|
| 110 | if not user_id:
|
|---|
| 111 | raise ValueError
|
|---|
| 112 | This._user = users.get_object(pk=user_id)
|
|---|
| 113 | except (AttributeError, KeyError, ValueError, users.UserDoesNotExist):
|
|---|
| 114 | from django.parts.auth import anonymoususers
|
|---|
| 115 | This._user = anonymoususers.AnonymousUser()
|
|---|
| 116 | return This._user
|
|---|
| 117 |
|
|---|
| 118 | def _set_user(This, user):
|
|---|
| 119 | This._user = user
|
|---|
| 120 |
|
|---|
| 121 | GET = property(_get_get, _set_get)
|
|---|
| 122 | POST = property(_get_post, _set_post)
|
|---|
| 123 | COOKIES = property(_get_cookies, _set_cookies)
|
|---|
| 124 | FILES = property(_get_files)
|
|---|
| 125 | META = property(_get_meta)
|
|---|
| 126 | REQUEST = property(_get_request)
|
|---|
| 127 | raw_post_data = property(_get_raw_post_data)
|
|---|
| 128 | user = property(_get_user, _set_user)
|
|---|
| 129 |
|
|---|
| 130 | class PyISAPIeHandler(BaseHandler):
|
|---|
| 131 | def __call__(This):
|
|---|
| 132 | from django.conf import settings
|
|---|
| 133 |
|
|---|
| 134 |
|
|---|
| 135 | if This._request_middleware is None:
|
|---|
| 136 | This.load_middleware()
|
|---|
| 137 |
|
|---|
| 138 | dispatcher.send(signal=signals.request_started)
|
|---|
| 139 |
|
|---|
| 140 | try:
|
|---|
| 141 | request = PyISAPIeRequest()
|
|---|
| 142 | response = This.get_response(request)
|
|---|
| 143 |
|
|---|
| 144 | # Apply response middleware
|
|---|
| 145 | for middleware_method in This._response_middleware:
|
|---|
| 146 | response = middleware_method(request, response)
|
|---|
| 147 |
|
|---|
| 148 | finally:
|
|---|
| 149 | dispatcher.send(signal=signals.request_finished)
|
|---|
| 150 |
|
|---|
| 151 |
|
|---|
| 152 | process_response(response)
|
|---|
| 153 |
|
|---|
| 154 | def process_response(http_response):
|
|---|
| 155 | from django.conf import settings
|
|---|
| 156 | for itm in http_response.items():
|
|---|
| 157 | Header("%s: %s" % itm)
|
|---|
| 158 | for c in http_response.cookies.values():
|
|---|
| 159 | Header('Set-Cookie: ' + c.output(header=''))
|
|---|
| 160 | Header(Status = http_response.status_code)
|
|---|
| 161 | for chunk in http_response:
|
|---|
| 162 | Write(chunk)
|
|---|
| 163 |
|
|---|