/[glsr]/trunk/harmonious/_dispatcher.py
Gentoo

Contents of /trunk/harmonious/_dispatcher.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 53 - (show annotations) (download) (as text)
Sun Jul 3 09:32:05 2005 UTC (9 years, 3 months ago) by port001
File MIME type: text/x-python
File size: 6142 byte(s)
Allow the HTTP response to be changed via harm.response.status

1 __id__ = '$Id$'
2 __modulename__ = 'Dispatcher'
3
4 import os
5 import re
6 import sys
7 import Cookie
8 import mimetypes
9 from types import ListType, TupleType, ClassType
10
11 from harmonious import harm
12 from harmonious._pathfilter import PathFilter
13
14 class ImportError(Exception): pass
15 class ValueError(Exception): pass
16
17 class Dispatcher:
18
19 def __init__(self, sessionStore):
20
21 self.__sessionStore = sessionStore
22
23 def __send_response(self):
24
25 if harm.response.status == 0:
26
27 harm.request._baseRequest.send_response(harm.request.status)
28
29 else:
30
31 harm.request._baseRequest.send_response(harm.response.status)
32
33 if harm.config.session['SessionEnable'] == True:
34
35 # Save all changes to our session and send the cookie
36 for key in harm.response.session.attrs.keys():
37
38 arg_dict = {key: harm.response.session.attrs[key]}
39 self.__sessionStore.update_session(harm.request.session.get_id(), **arg_dict)
40
41 if harm.response.session.get_persistent() == True:
42
43 self.__sessionStore.set_persistent(harm.response.session.get_id(), True)
44
45 else:
46
47 self.__sessionStore.set_persistent(harm.response.session.get_id(), False)
48
49 session_cookie = Cookie.SimpleCookie()
50 session_cookie[harm.config.session['CookieName']] = harm.response.session.get_id()
51 harm.response.add_header('Set-Cookie', session_cookie[harm.config.session['CookieName']].OutputString())
52
53 harm.response.add_header('Content-Length', len(harm.response.body))
54 harm.response.add_header('Connection', 'close') #XXX: We need to support persistent connections
55
56 headers = harm.response.headers
57
58 for key in headers.keys():
59
60 print "Sending header: ", key, headers[key]
61 harm.request._baseRequest.send_header(key, headers[key])
62
63 harm.request._baseRequest.end_headers()
64 print "Headers sent"
65
66 if harm.request.method != 'HEAD':
67 harm.request._baseRequest.wfile.write(harm.response.body)
68 print "Body sent"
69
70 def __apply_filter(self, filter):
71
72 # Init filter instance
73 try:
74
75 this_filter = filter(harm.response.headers, harm.response.body)
76 except TypeError, e:
77
78 harm.request._baseRequest.send_error(500)
79 return
80
81 # Call filter
82 try:
83 (harm.response.headers, harm.response.body) = this_filter.perform()
84 except:
85
86 harm.request._baseRequest.send_error(500)
87 return
88
89 def __load_filters(self, filter_type):
90
91 if hasattr(harm._domain_map[harm.request.host][harm.request.path][harm.request.file], filter_type):
92
93 filters = getattr(harm._domain_map[harm.request.host][harm.request.path][harm.request.file], filter_type)
94
95 if type(filters) == ListType or type(filters) == TupleType:
96
97 if len(filters) > 0:
98
99 for filter in filters:
100
101 if type(filter) == ClassType:
102
103 self.__apply_filter(filter)
104
105 else:
106
107 print >> sys.stderr, "Malformed domain module, preFilters of wrong type"
108 sys.exit(1)
109
110 def __serve_module(self):
111
112 # Init module instance
113 try:
114
115 instance = eval("harm._domain_map[harm.request.host][harm.request.path][harm.request.file].%s()" % harm.request.file[:-3].capitalize())
116
117 except AttributeError, e:
118
119 print >> sys.stderr, "Malformed domain module"
120 sys.exit(1)
121
122 # Apply pre filters
123 self.__load_filters('preFilters')
124
125 call_page = ''
126
127 if harm.request.queries.has_key('page'):
128
129 if hasattr(instance, harm.request.queries['page']):
130
131 call_page = harm.request.queries['page']
132
133 else:
134
135 harm.request._baseRequest.send_error(404)
136 return
137
138 else:
139
140 # Find the default page
141 for method in dir(instance):
142
143 try:
144
145 default = getattr(getattr(instance, method), 'default')
146 except AttributeError:
147
148 default = False
149
150 if default == True:
151
152 call_page = method
153
154 if call_page != '':
155
156 try:
157
158 getattr(instance, call_page)()
159 except IOError:
160
161 harm.request._baseRequest.send_error(500)
162 return
163
164 # Apply post filters
165 self.__load_filters('postFilters')
166
167 else:
168
169 harm.request._baseRequest.send_error(500)
170 return
171
172 def __serve_static(self):
173
174 (mime_type, file_encoding) = mimetypes.guess_type(harm.request.file)
175
176 if mime_type != None:
177 harm.response.add_header('Content-Type', mime_type)
178
179 if file_encoding != None:
180 harm.response.add_header('Content-Encoding', file_encoding)
181
182
183 harm.response.body = open(os.path.join(harm.request.abs_path,
184 harm.request.file)).read()
185
186 def dispatch(self):
187
188 print "DISPATCH"
189
190 PathFilter().perform()
191
192 print "PATH:", harm.request.path
193 print "ABS_PATH:", harm.request.abs_path
194 print "FILE:", harm.request.file
195 print "PATH_STATUS:", harm.request.status
196
197 if harm.request.status == 404:
198
199 harm.request._baseRequest.send_error(404)
200 return
201
202 elif harm.request.status == 403:
203
204 harm.request._baseRequest.send_error(403)
205 return
206
207 elif harm.request.status == 501:
208
209 harm.request._baseRequest.send_error(501)
210 return
211
212 elif harm.request.status == 200:
213
214 # Are we serving a domain module or a static file?
215 if harm.request.file.endswith('.py'):
216
217 self.__serve_module()
218
219 else:
220
221 self.__serve_static()
222
223 else:
224
225 harm.request._baseRequest.send_error(500)
226 return
227
228 self.__send_response()

Properties

Name Value
svn:keywords Id

  ViewVC Help
Powered by ViewVC 1.1.20