##// END OF EJS Templates
hgweb: use try/except/finally
Matt Mackall -
r25083:ef36536a default
parent child Browse files
Show More
@@ -176,71 +176,70 b' class hgwebdir(object):'
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()
181
180
182 virtual = req.env.get("PATH_INFO", "").strip('/')
181 virtual = req.env.get("PATH_INFO", "").strip('/')
183 tmpl = self.templater(req)
182 tmpl = self.templater(req)
184 ctype = tmpl('mimetype', encoding=encoding.encoding)
183 ctype = tmpl('mimetype', encoding=encoding.encoding)
185 ctype = templater.stringify(ctype)
184 ctype = templater.stringify(ctype)
186
185
187 # a static file
186 # a static file
188 if virtual.startswith('static/') or 'static' in req.form:
187 if virtual.startswith('static/') or 'static' in req.form:
189 if virtual.startswith('static/'):
188 if virtual.startswith('static/'):
190 fname = virtual[7:]
189 fname = virtual[7:]
191 else:
190 else:
192 fname = req.form['static'][0]
191 fname = req.form['static'][0]
193 static = self.ui.config("web", "static", None,
192 static = self.ui.config("web", "static", None,
194 untrusted=False)
193 untrusted=False)
195 if not static:
194 if not static:
196 tp = self.templatepath or templater.templatepaths()
195 tp = self.templatepath or templater.templatepaths()
197 if isinstance(tp, str):
196 if isinstance(tp, str):
198 tp = [tp]
197 tp = [tp]
199 static = [os.path.join(p, 'static') for p in tp]
198 static = [os.path.join(p, 'static') for p in tp]
200 staticfile(static, fname, req)
199 staticfile(static, fname, req)
201 return []
200 return []
202
201
203 # top-level index
202 # top-level index
204 elif not virtual:
203 elif not virtual:
205 req.respond(HTTP_OK, ctype)
204 req.respond(HTTP_OK, ctype)
206 return self.makeindex(req, tmpl)
205 return self.makeindex(req, tmpl)
207
206
208 # nested indexes and hgwebs
207 # nested indexes and hgwebs
209
208
210 repos = dict(self.repos)
209 repos = dict(self.repos)
211 virtualrepo = virtual
210 virtualrepo = virtual
212 while virtualrepo:
211 while virtualrepo:
213 real = repos.get(virtualrepo)
212 real = repos.get(virtualrepo)
214 if real:
213 if real:
215 req.env['REPO_NAME'] = virtualrepo
214 req.env['REPO_NAME'] = virtualrepo
216 try:
215 try:
217 # ensure caller gets private copy of ui
216 # ensure caller gets private copy of ui
218 repo = hg.repository(self.ui.copy(), real)
217 repo = hg.repository(self.ui.copy(), real)
219 return hgweb(repo).run_wsgi(req)
218 return hgweb(repo).run_wsgi(req)
220 except IOError, inst:
219 except IOError, inst:
221 msg = inst.strerror
220 msg = inst.strerror
222 raise ErrorResponse(HTTP_SERVER_ERROR, msg)
221 raise ErrorResponse(HTTP_SERVER_ERROR, msg)
223 except error.RepoError, inst:
222 except error.RepoError, inst:
224 raise ErrorResponse(HTTP_SERVER_ERROR, str(inst))
223 raise ErrorResponse(HTTP_SERVER_ERROR, str(inst))
225
224
226 up = virtualrepo.rfind('/')
225 up = virtualrepo.rfind('/')
227 if up < 0:
226 if up < 0:
228 break
227 break
229 virtualrepo = virtualrepo[:up]
228 virtualrepo = virtualrepo[:up]
230
229
231 # browse subdirectories
230 # browse subdirectories
232 subdir = virtual + '/'
231 subdir = virtual + '/'
233 if [r for r in repos if r.startswith(subdir)]:
232 if [r for r in repos if r.startswith(subdir)]:
234 req.respond(HTTP_OK, ctype)
233 req.respond(HTTP_OK, ctype)
235 return self.makeindex(req, tmpl, subdir)
234 return self.makeindex(req, tmpl, subdir)
236
235
237 # prefixes not found
236 # prefixes not found
238 req.respond(HTTP_NOT_FOUND, ctype)
237 req.respond(HTTP_NOT_FOUND, ctype)
239 return tmpl("notfound", repo=virtual)
238 return tmpl("notfound", repo=virtual)
240
239
241 except ErrorResponse, err:
240 except ErrorResponse, err:
242 req.respond(err, ctype)
241 req.respond(err, ctype)
243 return tmpl('error', error=err.message or '')
242 return tmpl('error', error=err.message or '')
244 finally:
243 finally:
245 tmpl = None
244 tmpl = None
246
245
General Comments 0
You need to be logged in to leave comments. Login now