MAJOR=4
MINOR=67
DEVNAME=ttyS3
  8   ¹ˆl–ß=¿J
hŠm@Šãn\i¥1hß�þZuÑ¬ã #c�Ò3üÿÙ„Â}ïØ×ÃÕ ?÷     ¹# hgweb/hgweb_mod.py - Web interface for a repository.
#
# Copyright 21 May 2005 - (c) 2005 Jake Edge <jake@edge2.net>
# Copyright 2005-2007 Olivia Mackall <olivia@selenic.com>
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.


import contextlib
import os

from .common import (
    ErrorResponse,
    HTTP_BAD_REQUEST,
    cspvalues,
    permhooks,
    statusmessage,
)
from ..pycompat import getattr

from .. import (
    encoding,
    error,
    extensions,
    formatter,
    hg,
    hook,
    profiling,
    pycompat,
    registrar,
    repoview,
    templatefilters,
    templater,
    templateutil,
    ui as uimod,
    util,
    wireprotoserver,
)

from . import (
    request as requestmod,
    webcommands,
    webutil,
    wsgicgi,
)


def getstyle(req, configfn, templatepath):
    styles = (
        req.qsparams.get(b'style', None),
        configfn(b'web', b'style'),
        b'paper',
    )
    return styles, _stylemap(styles, templatepath)


def _stylemap(styles, path=None):
    """Return path to mapfile for a given style.

    Searches mapfile in the following locations:
    1. templatepath/style/map
    2. templatepath/map-style
    3. templatepath/map
    """

    for style in styles:
        # only plain name is allowed to honor template paths
        if (
            not style
            or style in (pycompat.oscurdir, pycompat.ospardir)
            or pycompat.ossep in style
            or pycompat.osaltsep
            and pycompat.osaltsep in style
        ):
            continue
        locations = (os.path.join(style, b'map'), b'map-' + style, b'map')

        for location in locations:
            mapfile, fp = templater.try_open_template(location, path)
            if mapfile:
                return style, mapfile, fp

    raise RuntimeError(b"No hgweb templates found in %r" % path)


def makebreadcrumb(url, prefix=b''):
    """Return a 'URL breadcrumb' list

    A 'URL breadcrumb' is a list of URL-name pairs,
    corresponding to each of the path items on a URL.
    This can be used to create path navigation entries.
    """
    if url.endswith(b'/'):
        url = url[:-1]
    if prefix:
        url = b'/' + prefix + url
    relpath = url
    if relpath.startswith(b'/'):
        relpath = relpath[1:]

    breadcrumb = []
    urlel = url
    pathitems = [b''] + relpath.split(b'/')
    for pathel in reversed(pathitems):
        if not pathel or not urlel:
            break
        breadcrumb.append({b'url': urlel, b'name': pathel})
        urlel = os.path.dirname(urlel)
    return templateutil.mappinglist(reversed(breadcrumb))


class requestcontext:
    """Holds state/context for an individual request.

    Servers can be multi-threaded. Holding state on the WSGI application
    is prone to race conditions. Instances of this class exist to hold
    mutable and race-free state for requests.
    """

    def __init__(self, app, repo, req, res):
        self.repo = repo
        self.reponame = app.reponame
        self.req = req
        self.res = res

        self.maxchanges = self.configint(b'web', b'maxchanges')
        self.stripecount = self.configint(b'web', b'stripes')
        self.maxshortchanges = self.configint(b'web', b'maxshortchanges')
        self.maxfiles = self.configint(b'web', b'maxfiles')
        self.allowpull = self.configbool(b'web', b'allow-pull')

        # we use untrusted=False to prevent a repo owner from using
        # web.templates in .hg/hgrc to get access to any file readable
        # by the user running the CGI script
        self.templatepath = self.config(b'web', b'templates', untrusted=False)

        # This object is more expensive to build than simple config values.
        # It is shared across requests. The app will replace the object
        # if it is updated. Since this is a reference and nothing should
        # modify the underlying object, it should be constant for the li