mercurial/hgweb/hgwebdir_mod.py
changeset 36900 219b23359f4c
parent 36897 d7fd203e36cc
child 36901 006165d4d7e2
equal deleted inserted replaced
36899:e67a2e05fa8a 36900:219b23359f4c
    31     configitems,
    31     configitems,
    32     encoding,
    32     encoding,
    33     error,
    33     error,
    34     hg,
    34     hg,
    35     profiling,
    35     profiling,
    36     pycompat,
       
    37     scmutil,
    36     scmutil,
    38     templater,
    37     templater,
    39     ui as uimod,
    38     ui as uimod,
    40     util,
    39     util,
    41 )
    40 )
    80     """
    79     """
    81     for path in paths:
    80     for path in paths:
    82         path = os.path.normpath(path)
    81         path = os.path.normpath(path)
    83         yield (prefix + '/' +
    82         yield (prefix + '/' +
    84                util.pconvert(path[len(roothead):]).lstrip('/')).strip('/'), path
    83                util.pconvert(path[len(roothead):]).lstrip('/')).strip('/'), path
    85 
       
    86 def geturlcgivars(baseurl, port):
       
    87     """
       
    88     Extract CGI variables from baseurl
       
    89 
       
    90     >>> geturlcgivars(b"http://host.org/base", b"80")
       
    91     ('host.org', '80', '/base')
       
    92     >>> geturlcgivars(b"http://host.org:8000/base", b"80")
       
    93     ('host.org', '8000', '/base')
       
    94     >>> geturlcgivars(b'/base', 8000)
       
    95     ('', '8000', '/base')
       
    96     >>> geturlcgivars(b"base", b'8000')
       
    97     ('', '8000', '/base')
       
    98     >>> geturlcgivars(b"http://host", b'8000')
       
    99     ('host', '8000', '/')
       
   100     >>> geturlcgivars(b"http://host/", b'8000')
       
   101     ('host', '8000', '/')
       
   102     """
       
   103     u = util.url(baseurl)
       
   104     name = u.host or ''
       
   105     if u.port:
       
   106         port = u.port
       
   107     path = u.path or ""
       
   108     if not path.startswith('/'):
       
   109         path = '/' + path
       
   110 
       
   111     return name, pycompat.bytestr(port), path
       
   112 
    84 
   113 def readallowed(ui, req):
    85 def readallowed(ui, req):
   114     """Check allow_read and deny_read config options of a repo's ui object
    86     """Check allow_read and deny_read config options of a repo's ui object
   115     to determine user permissions.  By default, with neither option set (or
    87     to determine user permissions.  By default, with neither option set (or
   116     both empty), allow all users to read the repo.  There are two ways a
    88     both empty), allow all users to read the repo.  There are two ways a
   357         self.style = self.ui.config('web', 'style')
   329         self.style = self.ui.config('web', 'style')
   358         self.templatepath = self.ui.config('web', 'templates', untrusted=False)
   330         self.templatepath = self.ui.config('web', 'templates', untrusted=False)
   359         self.stripecount = self.ui.config('web', 'stripes')
   331         self.stripecount = self.ui.config('web', 'stripes')
   360         if self.stripecount:
   332         if self.stripecount:
   361             self.stripecount = int(self.stripecount)
   333             self.stripecount = int(self.stripecount)
   362         self._baseurl = self.ui.config('web', 'baseurl')
       
   363         prefix = self.ui.config('web', 'prefix')
   334         prefix = self.ui.config('web', 'prefix')
   364         if prefix.startswith('/'):
   335         if prefix.startswith('/'):
   365             prefix = prefix[1:]
   336             prefix = prefix[1:]
   366         if prefix.endswith('/'):
   337         if prefix.endswith('/'):
   367             prefix = prefix[:-1]
   338             prefix = prefix[:-1]
   374             raise RuntimeError("This function is only intended to be "
   345             raise RuntimeError("This function is only intended to be "
   375                                "called while running as a CGI script.")
   346                                "called while running as a CGI script.")
   376         wsgicgi.launch(self)
   347         wsgicgi.launch(self)
   377 
   348 
   378     def __call__(self, env, respond):
   349     def __call__(self, env, respond):
   379         wsgireq = requestmod.wsgirequest(env, respond)
   350         baseurl = self.ui.config('web', 'baseurl')
       
   351         wsgireq = requestmod.wsgirequest(env, respond, altbaseurl=baseurl)
   380         return self.run_wsgi(wsgireq)
   352         return self.run_wsgi(wsgireq)
   381 
   353 
   382     def run_wsgi(self, wsgireq):
   354     def run_wsgi(self, wsgireq):
   383         profile = self.ui.configbool('profiling', 'enabled')
   355         profile = self.ui.configbool('profiling', 'enabled')
   384         with profiling.profile(self.ui, enabled=profile):
   356         with profiling.profile(self.ui, enabled=profile):
   453                 real = repos.get(virtualrepo)
   425                 real = repos.get(virtualrepo)
   454                 if real:
   426                 if real:
   455                     # Re-parse the WSGI environment to take into account our
   427                     # Re-parse the WSGI environment to take into account our
   456                     # repository path component.
   428                     # repository path component.
   457                     wsgireq.req = requestmod.parserequestfromenv(
   429                     wsgireq.req = requestmod.parserequestfromenv(
   458                         wsgireq.env, wsgireq.req.bodyfh, reponame=virtualrepo)
   430                         wsgireq.env, wsgireq.req.bodyfh, reponame=virtualrepo,
       
   431                         altbaseurl=self.ui.config('web', 'baseurl'))
   459                     try:
   432                     try:
   460                         # ensure caller gets private copy of ui
   433                         # ensure caller gets private copy of ui
   461                         repo = hg.repository(self.ui.copy(), real)
   434                         repo = hg.repository(self.ui.copy(), real)
   462                         return hgweb_mod.hgweb(repo).run_wsgi(wsgireq)
   435                         return hgweb_mod.hgweb(repo).run_wsgi(wsgireq)
   463                     except IOError as inst:
   436                     except IOError as inst:
   500                  "%s%s" % ((not descending and column == sortcolumn)
   473                  "%s%s" % ((not descending and column == sortcolumn)
   501                             and "-" or "", column))
   474                             and "-" or "", column))
   502                 for column in sortable]
   475                 for column in sortable]
   503 
   476 
   504         self.refresh()
   477         self.refresh()
   505         self.updatereqenv(wsgireq.env)
       
   506 
   478 
   507         entries = indexentries(self.ui, self.repos, wsgireq, req,
   479         entries = indexentries(self.ui, self.repos, wsgireq, req,
   508                                self.stripecount, sortcolumn=sortcolumn,
   480                                self.stripecount, sortcolumn=sortcolumn,
   509                                descending=descending, subdir=subdir)
   481                                descending=descending, subdir=subdir)
   510 
   482 
   521             else:
   493             else:
   522                 yield config('web', 'motd')
   494                 yield config('web', 'motd')
   523 
   495 
   524         def config(section, name, default=uimod._unset, untrusted=True):
   496         def config(section, name, default=uimod._unset, untrusted=True):
   525             return self.ui.config(section, name, default, untrusted)
   497             return self.ui.config(section, name, default, untrusted)
   526 
       
   527         self.updatereqenv(wsgireq.env)
       
   528 
   498 
   529         url = wsgireq.env.get('SCRIPT_NAME', '')
   499         url = wsgireq.env.get('SCRIPT_NAME', '')
   530         if not url.endswith('/'):
   500         if not url.endswith('/'):
   531             url += '/'
   501             url += '/'
   532 
   502 
   555             "style": style,
   525             "style": style,
   556             "nonce": nonce,
   526             "nonce": nonce,
   557         }
   527         }
   558         tmpl = templater.templater.frommapfile(mapfile, defaults=defaults)
   528         tmpl = templater.templater.frommapfile(mapfile, defaults=defaults)
   559         return tmpl
   529         return tmpl
   560 
       
   561     def updatereqenv(self, env):
       
   562         if self._baseurl is not None:
       
   563             name, port, path = geturlcgivars(self._baseurl, env['SERVER_PORT'])
       
   564             env['SERVER_NAME'] = name
       
   565             env['SERVER_PORT'] = port
       
   566             env['SCRIPT_NAME'] = path