comparison mercurial/hgweb/hgwebdir_mod.py @ 25083:ef36536abea3

hgweb: use try/except/finally
author Matt Mackall <mpm@selenic.com>
date Fri, 15 May 2015 09:56:27 -0500
parents e48a5d3996c2
children 724d7982b790
comparison
equal deleted inserted replaced
25082:e30b66bb7d4d 25083:ef36536abea3
174 174
175 return False 175 return False
176 176
177 def run_wsgi(self, req): 177 def run_wsgi(self, req):
178 try: 178 try:
179 try: 179 self.refresh()
180 self.refresh() 180
181 181 virtual = req.env.get("PATH_INFO", "").strip('/')
182 virtual = req.env.get("PATH_INFO", "").strip('/') 182 tmpl = self.templater(req)
183 tmpl = self.templater(req) 183 ctype = tmpl('mimetype', encoding=encoding.encoding)
184 ctype = tmpl('mimetype', encoding=encoding.encoding) 184 ctype = templater.stringify(ctype)
185 ctype = templater.stringify(ctype) 185
186 186 # a static file
187 # a static file 187 if virtual.startswith('static/') or 'static' in req.form:
188 if virtual.startswith('static/') or 'static' in req.form: 188 if virtual.startswith('static/'):
189 if virtual.startswith('static/'): 189 fname = virtual[7:]
190 fname = virtual[7:] 190 else:
191 else: 191 fname = req.form['static'][0]
192 fname = req.form['static'][0] 192 static = self.ui.config("web", "static", None,
193 static = self.ui.config("web", "static", None, 193 untrusted=False)
194 untrusted=False) 194 if not static:
195 if not static: 195 tp = self.templatepath or templater.templatepaths()
196 tp = self.templatepath or templater.templatepaths() 196 if isinstance(tp, str):
197 if isinstance(tp, str): 197 tp = [tp]
198 tp = [tp] 198 static = [os.path.join(p, 'static') for p in tp]
199 static = [os.path.join(p, 'static') for p in tp] 199 staticfile(static, fname, req)
200 staticfile(static, fname, req) 200 return []
201 return [] 201
202 202 # top-level index
203 # top-level index 203 elif not virtual:
204 elif not virtual: 204 req.respond(HTTP_OK, ctype)
205 req.respond(HTTP_OK, ctype) 205 return self.makeindex(req, tmpl)
206 return self.makeindex(req, tmpl) 206
207 207 # nested indexes and hgwebs
208 # nested indexes and hgwebs 208
209 209 repos = dict(self.repos)
210 repos = dict(self.repos) 210 virtualrepo = virtual
211 virtualrepo = virtual 211 while virtualrepo:
212 while virtualrepo: 212 real = repos.get(virtualrepo)
213 real = repos.get(virtualrepo) 213 if real:
214 if real: 214 req.env['REPO_NAME'] = virtualrepo
215 req.env['REPO_NAME'] = virtualrepo 215 try:
216 try: 216 # ensure caller gets private copy of ui
217 # ensure caller gets private copy of ui 217 repo = hg.repository(self.ui.copy(), real)
218 repo = hg.repository(self.ui.copy(), real) 218 return hgweb(repo).run_wsgi(req)
219 return hgweb(repo).run_wsgi(req) 219 except IOError, inst:
220 except IOError, inst: 220 msg = inst.strerror
221 msg = inst.strerror 221 raise ErrorResponse(HTTP_SERVER_ERROR, msg)
222 raise ErrorResponse(HTTP_SERVER_ERROR, msg) 222 except error.RepoError, inst:
223 except error.RepoError, inst: 223 raise ErrorResponse(HTTP_SERVER_ERROR, str(inst))
224 raise ErrorResponse(HTTP_SERVER_ERROR, str(inst)) 224
225 225 up = virtualrepo.rfind('/')
226 up = virtualrepo.rfind('/') 226 if up < 0:
227 if up < 0: 227 break
228 break 228 virtualrepo = virtualrepo[:up]
229 virtualrepo = virtualrepo[:up] 229
230 230 # browse subdirectories
231 # browse subdirectories 231 subdir = virtual + '/'
232 subdir = virtual + '/' 232 if [r for r in repos if r.startswith(subdir)]:
233 if [r for r in repos if r.startswith(subdir)]: 233 req.respond(HTTP_OK, ctype)
234 req.respond(HTTP_OK, ctype) 234 return self.makeindex(req, tmpl, subdir)
235 return self.makeindex(req, tmpl, subdir) 235
236 236 # prefixes not found
237 # prefixes not found 237 req.respond(HTTP_NOT_FOUND, ctype)
238 req.respond(HTTP_NOT_FOUND, ctype) 238 return tmpl("notfound", repo=virtual)
239 return tmpl("notfound", repo=virtual) 239
240 240 except ErrorResponse, err:
241 except ErrorResponse, err: 241 req.respond(err, ctype)
242 req.respond(err, ctype) 242 return tmpl('error', error=err.message or '')
243 return tmpl('error', error=err.message or '')
244 finally: 243 finally:
245 tmpl = None 244 tmpl = None
246 245
247 def makeindex(self, req, tmpl, subdir=""): 246 def makeindex(self, req, tmpl, subdir=""):
248 247