mercurial/hgweb/hgwebdir_mod.py
changeset 5993 948a41e77902
parent 5976 9f1e6ab76069
child 6046 588ad9227b63
equal deleted inserted replaced
5992:30c40ba10963 5993:948a41e77902
     8 
     8 
     9 import os
     9 import os
    10 from mercurial.i18n import gettext as _
    10 from mercurial.i18n import gettext as _
    11 from mercurial import ui, hg, util, templater, templatefilters
    11 from mercurial import ui, hg, util, templater, templatefilters
    12 from common import ErrorResponse, get_mtime, staticfile, style_map, paritygen,\
    12 from common import ErrorResponse, get_mtime, staticfile, style_map, paritygen,\
    13                    get_contact
    13                    get_contact, HTTP_OK, HTTP_NOT_FOUND, HTTP_SERVER_ERROR
    14 from hgweb_mod import hgweb
    14 from hgweb_mod import hgweb
    15 from request import wsgirequest
    15 from request import wsgirequest
    16 
    16 
    17 # This is a stopgap
    17 # This is a stopgap
    18 class hgwebdir(object):
    18 class hgwebdir(object):
    74 
    74 
    75         try:
    75         try:
    76             try:
    76             try:
    77 
    77 
    78                 virtual = req.env.get("PATH_INFO", "").strip('/')
    78                 virtual = req.env.get("PATH_INFO", "").strip('/')
       
    79                 tmpl = self.templater(req)
       
    80                 ctype = tmpl('mimetype', encoding=util._encoding)
       
    81                 ctype = templater.stringify(ctype)
    79 
    82 
    80                 # a static file
    83                 # a static file
    81                 if virtual.startswith('static/') or 'static' in req.form:
    84                 if virtual.startswith('static/') or 'static' in req.form:
    82                     static = os.path.join(templater.templatepath(), 'static')
    85                     static = os.path.join(templater.templatepath(), 'static')
    83                     if virtual.startswith('static/'):
    86                     if virtual.startswith('static/'):
    87                     req.write(staticfile(static, fname, req))
    90                     req.write(staticfile(static, fname, req))
    88                     return
    91                     return
    89 
    92 
    90                 # top-level index
    93                 # top-level index
    91                 elif not virtual:
    94                 elif not virtual:
    92                     tmpl = self.templater(req)
    95                     req.respond(HTTP_OK, ctype)
    93                     req.write(self.makeindex(req, tmpl))
    96                     req.write(self.makeindex(req, tmpl))
    94                     return
    97                     return
    95 
    98 
    96                 # nested indexes and hgwebs
    99                 # nested indexes and hgwebs
       
   100                 
    97                 repos = dict(self.repos)
   101                 repos = dict(self.repos)
    98                 while virtual:
   102                 while virtual:
    99                     real = repos.get(virtual)
   103                     real = repos.get(virtual)
   100                     if real:
   104                     if real:
   101                         req.env['REPO_NAME'] = virtual
   105                         req.env['REPO_NAME'] = virtual
   102                         try:
   106                         try:
   103                             repo = hg.repository(self.parentui, real)
   107                             repo = hg.repository(self.parentui, real)
   104                             hgweb(repo).run_wsgi(req)
   108                             hgweb(repo).run_wsgi(req)
   105                             return
   109                             return
   106                         except IOError, inst:
   110                         except IOError, inst:
   107                             raise ErrorResponse(500, inst.strerror)
   111                             msg = inst.strerror
       
   112                             raise ErrorResponse(HTTP_SERVER_ERROR, msg)
   108                         except hg.RepoError, inst:
   113                         except hg.RepoError, inst:
   109                             raise ErrorResponse(500, str(inst))
   114                             raise ErrorResponse(HTTP_SERVER_ERROR, str(inst))
   110 
   115 
   111                     # browse subdirectories
   116                     # browse subdirectories
   112                     subdir = virtual + '/'
   117                     subdir = virtual + '/'
   113                     if [r for r in repos if r.startswith(subdir)]:
   118                     if [r for r in repos if r.startswith(subdir)]:
   114                         tmpl = self.templater(req)
   119                         req.respond(HTTP_OK, ctype)
   115                         req.write(self.makeindex(req, tmpl, subdir))
   120                         req.write(self.makeindex(req, tmpl, subdir))
   116                         return
   121                         return
   117 
   122 
   118                     up = virtual.rfind('/')
   123                     up = virtual.rfind('/')
   119                     if up < 0:
   124                     if up < 0:
   120                         break
   125                         break
   121                     virtual = virtual[:up]
   126                     virtual = virtual[:up]
   122 
   127 
   123                 # prefixes not found
   128                 # prefixes not found
   124                 tmpl = self.templater(req)
   129                 req.respond(HTTP_NOT_FOUND, ctype)
   125                 req.respond(404, tmpl("notfound", repo=virtual))
   130                 req.write(tmpl("notfound", repo=virtual))
   126 
   131 
   127             except ErrorResponse, err:
   132             except ErrorResponse, err:
   128                 tmpl = self.templater(req)
   133                 req.respond(err.code, ctype)
   129                 req.respond(err.code, tmpl('error', error=err.message or ''))
   134                 req.write(tmpl('error', error=err.message or ''))
   130         finally:
   135         finally:
   131             tmpl = None
   136             tmpl = None
   132 
   137 
   133     def makeindex(self, req, tmpl, subdir=""):
   138     def makeindex(self, req, tmpl, subdir=""):
   134 
   139 
   232                     **dict(sort))
   237                     **dict(sort))
   233 
   238 
   234     def templater(self, req):
   239     def templater(self, req):
   235 
   240 
   236         def header(**map):
   241         def header(**map):
   237             ctype = tmpl('mimetype', encoding=util._encoding)
       
   238             req.httphdr(templater.stringify(ctype))
       
   239             yield tmpl('header', encoding=util._encoding, **map)
   242             yield tmpl('header', encoding=util._encoding, **map)
   240 
   243 
   241         def footer(**map):
   244         def footer(**map):
   242             yield tmpl("footer", **map)
   245             yield tmpl("footer", **map)
   243 
   246