1
2 import logging
3
4 from urlparse import urlparse
5
6 from os.path import join, dirname, normpath
7
8 try:
9 from cPickle import UnpicklingError
10 except ImportError:
11 from pickle import UnpicklingError
12
13 from paste.httpheaders import \
14 (ACCEPT_LANGUAGE, SCRIPT_NAME, PATH_INFO, IF_MODIFIED_SINCE,
15 USER_AGENT, CONTENT_LENGTH, CONTENT_TYPE)
16
17 from babel.core import Locale, UnknownLocaleError
18
19 import muntjac
20
21 from muntjac.util import sys_path_install, defaultLocale
22
23 sys_path_install()
24
25
26 from WebKit.HTTPServlet import HTTPServlet
27
28
29 logger = logging.getLogger(__name__)
30
31
34
35
37
38 EndResponse = EndResponseException
39
40 - def __init__(self, contextRoot=None, contextPath=None, timeout=1800):
41
42
43 if contextRoot is not None:
44 self.contextRoot = contextRoot
45 else:
46 root = join(dirname(muntjac.__file__), 'public')
47 self.contextRoot = normpath(root)
48
49 self.contextPath = contextPath if contextPath is not None else ''
50
51 self._timeout = timeout
52
53
54
55
56
57 - def awake(self, transaction):
61
62
64
65
66 self.service(transaction.request(), transaction.response())
67
68
70 raise NotImplementedError
71
72
73 - def service(self, request, response):
74 raise NotImplementedError
75
76
77 - def getContextPath(self, request):
78
79 return self.contextPath
80
81
82 - def originalContextPath(self, request):
83
84 return self.getContextPath(request)
85
86
88
89 servletPath = SCRIPT_NAME(request.environ())
90
91
92
93
94
95
96
97
98
99 return servletPath
100
101
103 """
104 @param url: URL of the form scheme://netloc/path;parameters?query#frag
105 @return: the path part or the url
106 """
107 return urlparse(url)[2]
108
109
111
112 path = join(self.contextRoot, path.lstrip('/'))
113 stream = open(normpath(path), 'rb')
114 return stream
115
116
118
119 path = join(self.contextRoot, filename.lstrip('/'))
120 return path
121
122
124
125 return join(self.contextRoot, path.lstrip('/'))
126
127
128
130 return request.fields()
131
132
135
136
139
140
142 return request.serverDictionary().get(field)
143
144
146 return USER_AGENT(request.environ())
147
148
149 - def getContentLength(self, request):
150 return CONTENT_LENGTH(request.environ())
151
152
153 - def getContentType(self, request):
154 return CONTENT_TYPE(request.environ())
155
156
158 dh = IF_MODIFIED_SINCE(request.environ())
159 return int(dh) if dh else -1
160
161
163 portStr = request.environ().get('SERVER_PORT')
164 return int(portStr) if portStr is not None else None
165
166
168 """The request's URL from the protocol name up to the query string"""
169 return urlparse(request.uri())[2]
170
171
173 return PATH_INFO(request.environ())
174
175
177
178 tags = ACCEPT_LANGUAGE.parse(request.environ())
179 if tags:
180 try:
181 return Locale.parse(tags[0], sep='-')
182 except UnknownLocaleError, e:
183 try:
184 return Locale.parse(tags[0])
185 except UnknownLocaleError, e:
186 logger.error('Locale parsing error: %s' % e)
187 return defaultLocale()
188 else:
189 return defaultLocale()
190
191
193 return request.environ().get('SERVER_NAME', '')
194
195
197 """Check whether the request is a HTTPS connection."""
198 return request.environ().get('HTTPS', '').lower() == 'on'
199
200
203
204
205
208
209
212
213
214 - def write(self, response, value):
216
217
219 response.sendRedirect(url)
220
221
224
225
226
227 - def getSession(self, request, allowSessionCreation=True):
228 try:
229 if allowSessionCreation:
230 return request.session()
231 else:
232 if request.transaction().hasSession():
233 return request.session()
234 else:
235 return None
236 except EOFError, e:
237 logger.exception('Session retrieval error: %s' % str(e))
238 return None
239 except UnpicklingError, e:
240 logger.exception('Session retrieval error: %s' % str(e))
241 return None
242 except ValueError, e:
243 logger.exception('Session retrieval error: %s' % str(e))
244 return None
245
246
248 try:
249 request.session().invalidate()
250 except Exception, e:
251 logger.error('Session invalidation error: %s' % e)
252
253
255 try:
256 return request.sessionId()
257 except Exception, e:
258 logger.error('Session ID error: %s' % e)
259 return None
260
261
263 if session is not None:
264 return session.value(name, default)
265 else:
266 return default
267
268
272
273
275 if session is not None:
276 return session.value('timeout', self._timeout)
277 else:
278 return self._timeout
279
280
282 if session is not None:
283 return session.isNew()
284 else:
285 return True
286