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

Contents of /trunk/harmonious/_dispatcher.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 109 - (show annotations) (download) (as text)
Mon Aug 1 14:40:49 2005 UTC (9 years, 4 months ago) by port001
File MIME type: text/x-python
File size: 8510 byte(s)
Added a 'flush2disk' mode to the SessionStorage class which pickles session data to disk when shut down, and unpickles it on startup.

1 __id__ = '$Id$'
2 __modulename__ = 'dispatcher'
3
4 import os
5 import re
6 import sys
7 import time
8 import Cookie
9 import mimetypes
10 from types import ListType, TupleType, ClassType
11
12 from harmonious import server
13 from harmonious._module import load_module
14 from harmonious._pathfilter import PathFilter
15
16 class ImportError(Exception): pass
17 class ValueError(Exception): pass
18
19 class Dispatcher:
20
21 def __init__(self, harm):
22
23 self._harm = harm
24
25 def __send_response(self):
26
27 if self._harm.response.status == 0:
28
29 self._harm.request.send_response(self._harm.request.status)
30
31 else:
32
33 self._harm.request.send_response(self._harm.response.status)
34
35 if server.config.session['SessionEnable'] == True:
36
37 if self._harm.session.is_modified():
38
39 # Save all changes to our session and send the cookie
40 for key in self._harm.session.attrs.keys():
41
42 arg_dict = {key: self._harm.session.attrs[key]}
43 server._sessionStore.update_session(self._harm.session.get_id(), **arg_dict)
44
45 if self._harm.session.is_persistent() == True:
46
47 server._sessionStore.set_persistent(self._harm.session.get_id(), True)
48
49 else:
50
51 server._sessionStore.set_persistent(self._harm.session.get_id(), False)
52
53 session_cookie = Cookie.SimpleCookie()
54 session_cookie[server.config.session['CookieName']] = self._harm.session.get_id()
55 session_cookie[server.config.session['CookieName']]['path'] = '/'
56 # Uncomment for release
57 #session_cookie[server.config.session['CookieName']]['domain'] = self._harm.request.host
58
59 if self._harm.session.is_persistent() == False:
60
61 session_cookie[server.config.session['CookieName']]['max-age'] = server.config.session['Timeout'] * 60
62
63 self._harm.response.add_header('Set-Cookie', session_cookie[server.config.session['CookieName']].OutputString())
64
65 self._harm.response.add_header('Content-Length', len(self._harm.response.body))
66
67 if self._harm.request.request_version in ('HTTP/1.1', 'HTTP/1.0'):
68 if not self._harm.request.keep_alive:
69 self._harm.response.add_header('Connection', 'close')
70 else:
71 self._harm.response.add_header('Connection', 'keep-alive')
72 self._harm.response.add_header('Keep-Alive', "%s" % self._harm.request.keep_alive_value)
73
74 headers = self._harm.response.headers
75
76 for key in headers.keys():
77
78 print "Sending header: ", key, headers[key]
79 self._harm.request.send_header(key, headers[key])
80
81 self._harm.request.end_headers()
82 #print "Headers sent"
83
84 if self._harm.request.command != 'HEAD':
85 self._harm.request.wfile.write(self._harm.response.body)
86 #print "Body sent"
87
88 if not self._harm.request.keep_alive:
89 print "NOT KEEP ALIVE"
90 self._harm.request.close_request(self._harm.request.connection)
91
92 def __apply_filter(self, filter):
93
94 # Init filter instance
95 try:
96
97 this_filter = filter(self._harm.response.headers, self._harm.response.body)
98 except TypeError, e:
99
100 self._harm.request.send_error(500, close=True)
101 self._harm.request.close_request(self._harm.request.connection)
102 return False
103
104 # Call filter
105 try:
106 (self._harm.response.headers, self._harm.response.body) = this_filter.perform()
107 except:
108
109 self._harm.request.send_error(500, close=True)
110 self._harm.request.close_request(self._harm.request.connection)
111 return False
112
113 def __load_filters(self, filter_type):
114
115 if hasattr(server._domain_map[self._harm.request.host][self._harm.request.path][self._harm.request.file], filter_type):
116
117 filters = getattr(server._domain_map[self._harm.request.host][self._harm.request.path][self._harm.request.file], filter_type)
118
119 if type(filters) == ListType or type(filters) == TupleType:
120
121 if len(filters) > 0:
122
123 for filter in filters:
124
125 if type(filter) == ClassType:
126
127 if not self.__apply_filter(filter):
128 return False
129
130 else:
131
132 print >> sys.stderr, "Malformed domain module, preFilters of wrong type"
133 sys.exit(1)
134
135 def __serve_module(self):
136
137 # Init module instance
138 try:
139
140 instance = eval("server._domain_map[self._harm.request.host][self._harm.request.path][self._harm.request.file].%s(self._harm)" % \
141 self._harm.request.file[:-3].capitalize())
142
143 except:
144
145 self._harm.request.send_error(500, close=True)
146 self._harm.request.close_request(self._harm.request.connection)
147 return False
148
149 # Apply pre filters
150 if self.__load_filters('preFilters') == False:
151 return False
152
153 # Pickup the called page from either POST or GET
154 call_page = (self._harm.request.queries.get('page', False)
155 or self._harm.request.forms.getvalue('page', False))
156
157 if call_page and not hasattr(instance, call_page):
158 self._harm.request.send_error(404)
159 return
160
161 if not call_page:
162
163 # Find the default page
164 for method in dir(instance):
165
166 try:
167
168 default = getattr(getattr(instance, method), 'default')
169 except AttributeError:
170
171 default = False
172
173 if default == True:
174
175 call_page = method
176
177 if call_page:
178
179 try:
180
181 getattr(instance, call_page)()
182
183 except:
184
185 self._harm.request.send_error(500, close=True)
186 self._harm.request.close_request(self._harm.request.connection)
187 return False
188
189 # Apply post filters
190 if self.__load_filters('postFilters') == False:
191 return False
192
193 else:
194
195 self._harm.request.send_error(500, close=True)
196 self._harm.request.close_request(self._harm.request.connection)
197 return False
198
199 def __serve_static(self):
200
201 (mime_type, file_encoding) = mimetypes.guess_type(self._harm.request.file)
202
203 if mime_type != None:
204 self._harm.response.add_header('Content-Type', mime_type)
205
206 if file_encoding != None:
207 self._harm.response.add_header('Content-Encoding', file_encoding)
208
209
210 self._harm.response.body = open(os.path.join(self._harm.request.abs_path,
211 self._harm.request.file)).read()
212
213 def dispatch(self):
214
215 print "DISPATCH"
216
217 self._harm = PathFilter(self._harm).perform()
218
219 print "PATH:", self._harm.request.path
220 print "ABS_PATH:", self._harm.request.abs_path
221 print "FILE:", self._harm.request.file
222 print "PATH_STATUS:", self._harm.request.status
223
224 if self._harm.request.status == 404:
225
226 self._harm.request.send_error(404)
227 return
228
229 elif self._harm.request.status == 403:
230
231 self._harm.request.send_error(403)
232 return
233
234 elif self._harm.request.status == 501:
235
236 self._harm.request.send_error(501)
237 return
238
239 elif self._harm.request.status == 401:
240
241 # XXX: We need a better config parse before we can give a realm
242 self._harm.response.add_header('WWW-Authenticate', "Basic realm=\"Test\"")
243
244 elif self._harm.request.status == 400:
245
246 self._harm.request.send_error(400)
247 return
248
249 elif self._harm.request.status == 403:
250
251 self._harm.request.send_error(403)
252 return
253
254 elif self._harm.request.status == 200:
255
256 # Are we serving a domain module or a static file?
257 if self._harm.request.file.endswith('.py'):
258
259 if self.__serve_module() == False:
260 return
261
262 else:
263
264 self.__serve_static()
265
266 else:
267
268 self._harm.request.send_error(500, close=True)
269 self._harm.request.close_request(self._harm.request.connection)
270 return
271
272 self.__send_response()

Properties

Name Value
svn:keywords Id

  ViewVC Help
Powered by ViewVC 1.1.20