mercurial/hgweb/hgwebdir_mod.py
author Gregory Szorc <gregory.szorc@gmail.com>
Sat, 10 Mar 2018 11:46:52 -0800
changeset 36863 1a1972b1a1ff
parent 36857 da4e2f87167d
child 36865 3d60a22e27f5
permissions -rw-r--r--
hgweb: use our new request object for "style" parameter The "style" parameter is kind of wonky because it is explicitly set and has lookups in random locations. Let's port it to qsparams first because it isn't straightforward. There is subtle change in behavior. But I don't think it is worth calling out in a BC. Our multidict's __getitem__ returns the last set value for a key, not the first. So if the query string set a variable multiple times, before we would get the first value and now we would get the last value. It makes no sense to specify these things multiple times. And I think last write wins is more sensible than first write wins. Differential Revision: https://phab.mercurial-scm.org/D2779
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2391
d351a3be3371 Fixing up comment headers for split up code.
Eric Hopper <hopper@omnifarious.org>
parents: 2360
diff changeset
     1
# hgweb/hgwebdir_mod.py - Web interface for a directory of repositories.
131
c9d51742471c moving hgweb to mercurial subdir
jake@edge2.net
parents:
diff changeset
     2
#
238
3b92f8fe47ae hgweb.py: kill #! line, clean up copyright notice
mpm@selenic.com
parents: 222
diff changeset
     3
# Copyright 21 May 2005 - (c) 2005 Jake Edge <jake@edge2.net>
2859
345bac2bc4ec update copyrights.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2538
diff changeset
     4
# Copyright 2005, 2006 Matt Mackall <mpm@selenic.com>
131
c9d51742471c moving hgweb to mercurial subdir
jake@edge2.net
parents:
diff changeset
     5
#
8225
46293a0c7e9f updated license to be explicit about GPL version 2
Martin Geisler <mg@lazybytes.net>
parents: 8216
diff changeset
     6
# This software may be used and distributed according to the terms of the
10263
25e572394f5c Update license to GPLv2+
Matt Mackall <mpm@selenic.com>
parents: 9903
diff changeset
     7
# GNU General Public License version 2 or any later version.
131
c9d51742471c moving hgweb to mercurial subdir
jake@edge2.net
parents:
diff changeset
     8
27046
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 27045
diff changeset
     9
from __future__ import absolute_import
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 27045
diff changeset
    10
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 27045
diff changeset
    11
import os
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 27045
diff changeset
    12
import re
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 27045
diff changeset
    13
import time
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 27045
diff changeset
    14
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 27045
diff changeset
    15
from ..i18n import _
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 27045
diff changeset
    16
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 27045
diff changeset
    17
from .common import (
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 27045
diff changeset
    18
    ErrorResponse,
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 27045
diff changeset
    19
    HTTP_NOT_FOUND,
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 27045
diff changeset
    20
    HTTP_OK,
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 27045
diff changeset
    21
    HTTP_SERVER_ERROR,
30766
d7bf7d2bd5ab hgweb: support Content Security Policy
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30749
diff changeset
    22
    cspvalues,
27046
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 27045
diff changeset
    23
    get_contact,
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 27045
diff changeset
    24
    get_mtime,
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 27045
diff changeset
    25
    ismember,
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 27045
diff changeset
    26
    paritygen,
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 27045
diff changeset
    27
    staticfile,
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 27045
diff changeset
    28
)
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 27045
diff changeset
    29
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 27045
diff changeset
    30
from .. import (
34244
fe5202bef5ce configitems: register the 'web.refreshinterval' config
Boris Feld <boris.feld@octobus.net>
parents: 34243
diff changeset
    31
    configitems,
27046
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 27045
diff changeset
    32
    encoding,
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 27045
diff changeset
    33
    error,
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 27045
diff changeset
    34
    hg,
29787
80df04266a16 hgweb: profile HTTP requests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29786
diff changeset
    35
    profiling,
34358
8cbcee0b923d py3: remove use of str() in hgwebdir
Yuya Nishihara <yuya@tcha.org>
parents: 34257
diff changeset
    36
    pycompat,
27046
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 27045
diff changeset
    37
    scmutil,
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 27045
diff changeset
    38
    templater,
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 27045
diff changeset
    39
    ui as uimod,
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 27045
diff changeset
    40
    util,
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 27045
diff changeset
    41
)
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 27045
diff changeset
    42
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 27045
diff changeset
    43
from . import (
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 27045
diff changeset
    44
    hgweb_mod,
36804
b9b968e21f78 hgweb: rename req to wsgireq
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36607
diff changeset
    45
    request as requestmod,
27046
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 27045
diff changeset
    46
    webutil,
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 27045
diff changeset
    47
    wsgicgi,
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 27045
diff changeset
    48
)
36607
c6061cadb400 util: extract all date-related utils in utils/dateutil module
Boris Feld <boris.feld@octobus.net>
parents: 34703
diff changeset
    49
from ..utils import dateutil
138
c77a679e9cfa Revamped templated hgweb
mpm@selenic.com
parents: 137
diff changeset
    50
8215
227707c90548 hgweb: some cleanups in hgwebdir, remove double defaults
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8209
diff changeset
    51
def cleannames(items):
227707c90548 hgweb: some cleanups in hgwebdir, remove double defaults
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8209
diff changeset
    52
    return [(util.pconvert(name).strip('/'), path) for name, path in items]
1181
4f5001f5b4c3 Make sure the repository names don't have slashes at the at or else in some
Vincent Wagelaar <vincent@ricardis.tudelft.nl>
parents: 1180
diff changeset
    53
8529
a767998f0a78 hgweb: make hgwebdir handle dict/list paths the same as config paths
Jeremy Whitlock <jcscoobyrs@gmail.com>
parents: 8389
diff changeset
    54
def findrepos(paths):
9723
a235644a0b93 hgweb: use a tuple-list instead of dictionary for append-only store
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 9363
diff changeset
    55
    repos = []
8529
a767998f0a78 hgweb: make hgwebdir handle dict/list paths the same as config paths
Jeremy Whitlock <jcscoobyrs@gmail.com>
parents: 8389
diff changeset
    56
    for prefix, root in cleannames(paths):
a767998f0a78 hgweb: make hgwebdir handle dict/list paths the same as config paths
Jeremy Whitlock <jcscoobyrs@gmail.com>
parents: 8389
diff changeset
    57
        roothead, roottail = os.path.split(root)
17104
5a9acb0b2086 help: improve hgweb help
Mads Kiilerich <mads@kiilerich.com>
parents: 16754
diff changeset
    58
        # "foo = /bar/*" or "foo = /bar/**" lets every repo /bar/N in or below
5a9acb0b2086 help: improve hgweb help
Mads Kiilerich <mads@kiilerich.com>
parents: 16754
diff changeset
    59
        # /bar/ be served as as foo/N .
5a9acb0b2086 help: improve hgweb help
Mads Kiilerich <mads@kiilerich.com>
parents: 16754
diff changeset
    60
        # '*' will not search inside dirs with .hg (except .hg/patches),
5a9acb0b2086 help: improve hgweb help
Mads Kiilerich <mads@kiilerich.com>
parents: 16754
diff changeset
    61
        # '**' will search inside dirs with .hg (and thus also find subrepos).
8529
a767998f0a78 hgweb: make hgwebdir handle dict/list paths the same as config paths
Jeremy Whitlock <jcscoobyrs@gmail.com>
parents: 8389
diff changeset
    62
        try:
a767998f0a78 hgweb: make hgwebdir handle dict/list paths the same as config paths
Jeremy Whitlock <jcscoobyrs@gmail.com>
parents: 8389
diff changeset
    63
            recurse = {'*': False, '**': True}[roottail]
a767998f0a78 hgweb: make hgwebdir handle dict/list paths the same as config paths
Jeremy Whitlock <jcscoobyrs@gmail.com>
parents: 8389
diff changeset
    64
        except KeyError:
9723
a235644a0b93 hgweb: use a tuple-list instead of dictionary for append-only store
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 9363
diff changeset
    65
            repos.append((prefix, root))
8529
a767998f0a78 hgweb: make hgwebdir handle dict/list paths the same as config paths
Jeremy Whitlock <jcscoobyrs@gmail.com>
parents: 8389
diff changeset
    66
            continue
11677
8f8a7976f4bc hgwebdir: allow pure relative globs in paths
Mads Kiilerich <mads@kiilerich.com>
parents: 11649
diff changeset
    67
        roothead = os.path.normpath(os.path.abspath(roothead))
13975
938fbeacac84 move walkrepos from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13964
diff changeset
    68
        paths = scmutil.walkrepos(roothead, followsym=True, recurse=recurse)
13402
f947d9a4c45c hgweb: doctest of url creation from wildcard expansion
Mads Kiilerich <mads@kiilerich.com>
parents: 13214
diff changeset
    69
        repos.extend(urlrepos(prefix, roothead, paths))
9723
a235644a0b93 hgweb: use a tuple-list instead of dictionary for append-only store
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 9363
diff changeset
    70
    return repos
8529
a767998f0a78 hgweb: make hgwebdir handle dict/list paths the same as config paths
Jeremy Whitlock <jcscoobyrs@gmail.com>
parents: 8389
diff changeset
    71
13402
f947d9a4c45c hgweb: doctest of url creation from wildcard expansion
Mads Kiilerich <mads@kiilerich.com>
parents: 13214
diff changeset
    72
def urlrepos(prefix, roothead, paths):
f947d9a4c45c hgweb: doctest of url creation from wildcard expansion
Mads Kiilerich <mads@kiilerich.com>
parents: 13214
diff changeset
    73
    """yield url paths and filesystem paths from a list of repo paths
f947d9a4c45c hgweb: doctest of url creation from wildcard expansion
Mads Kiilerich <mads@kiilerich.com>
parents: 13214
diff changeset
    74
13538
d24e97fd52a9 test-doctest: handle unix/windows path discrepancies
Patrick Mezard <pmezard@gmail.com>
parents: 13436
diff changeset
    75
    >>> conv = lambda seq: [(v, util.pconvert(p)) for v,p in seq]
34131
0fa781320203 doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents: 34022
diff changeset
    76
    >>> conv(urlrepos(b'hg', b'/opt', [b'/opt/r', b'/opt/r/r', b'/opt']))
13403
8ed91088acbb hgweb: make paths wildcards expanding in a repo root match repo correctly
Mads Kiilerich <mads@kiilerich.com>
parents: 13402
diff changeset
    77
    [('hg/r', '/opt/r'), ('hg/r/r', '/opt/r/r'), ('hg', '/opt')]
34131
0fa781320203 doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents: 34022
diff changeset
    78
    >>> conv(urlrepos(b'', b'/opt', [b'/opt/r', b'/opt/r/r', b'/opt']))
13402
f947d9a4c45c hgweb: doctest of url creation from wildcard expansion
Mads Kiilerich <mads@kiilerich.com>
parents: 13214
diff changeset
    79
    [('r', '/opt/r'), ('r/r', '/opt/r/r'), ('', '/opt')]
f947d9a4c45c hgweb: doctest of url creation from wildcard expansion
Mads Kiilerich <mads@kiilerich.com>
parents: 13214
diff changeset
    80
    """
f947d9a4c45c hgweb: doctest of url creation from wildcard expansion
Mads Kiilerich <mads@kiilerich.com>
parents: 13214
diff changeset
    81
    for path in paths:
f947d9a4c45c hgweb: doctest of url creation from wildcard expansion
Mads Kiilerich <mads@kiilerich.com>
parents: 13214
diff changeset
    82
        path = os.path.normpath(path)
13403
8ed91088acbb hgweb: make paths wildcards expanding in a repo root match repo correctly
Mads Kiilerich <mads@kiilerich.com>
parents: 13402
diff changeset
    83
        yield (prefix + '/' +
8ed91088acbb hgweb: make paths wildcards expanding in a repo root match repo correctly
Mads Kiilerich <mads@kiilerich.com>
parents: 13402
diff changeset
    84
               util.pconvert(path[len(roothead):]).lstrip('/')).strip('/'), path
13402
f947d9a4c45c hgweb: doctest of url creation from wildcard expansion
Mads Kiilerich <mads@kiilerich.com>
parents: 13214
diff changeset
    85
15003
a31b8e03af28 hgweb: extract the path logic from updatereqenv and add doctests
Matt Mackall <mpm@selenic.com>
parents: 15002
diff changeset
    86
def geturlcgivars(baseurl, port):
a31b8e03af28 hgweb: extract the path logic from updatereqenv and add doctests
Matt Mackall <mpm@selenic.com>
parents: 15002
diff changeset
    87
    """
a31b8e03af28 hgweb: extract the path logic from updatereqenv and add doctests
Matt Mackall <mpm@selenic.com>
parents: 15002
diff changeset
    88
    Extract CGI variables from baseurl
a31b8e03af28 hgweb: extract the path logic from updatereqenv and add doctests
Matt Mackall <mpm@selenic.com>
parents: 15002
diff changeset
    89
34131
0fa781320203 doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents: 34022
diff changeset
    90
    >>> geturlcgivars(b"http://host.org/base", b"80")
15003
a31b8e03af28 hgweb: extract the path logic from updatereqenv and add doctests
Matt Mackall <mpm@selenic.com>
parents: 15002
diff changeset
    91
    ('host.org', '80', '/base')
34131
0fa781320203 doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents: 34022
diff changeset
    92
    >>> geturlcgivars(b"http://host.org:8000/base", b"80")
15003
a31b8e03af28 hgweb: extract the path logic from updatereqenv and add doctests
Matt Mackall <mpm@selenic.com>
parents: 15002
diff changeset
    93
    ('host.org', '8000', '/base')
34131
0fa781320203 doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents: 34022
diff changeset
    94
    >>> geturlcgivars(b'/base', 8000)
15003
a31b8e03af28 hgweb: extract the path logic from updatereqenv and add doctests
Matt Mackall <mpm@selenic.com>
parents: 15002
diff changeset
    95
    ('', '8000', '/base')
34131
0fa781320203 doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents: 34022
diff changeset
    96
    >>> geturlcgivars(b"base", b'8000')
15003
a31b8e03af28 hgweb: extract the path logic from updatereqenv and add doctests
Matt Mackall <mpm@selenic.com>
parents: 15002
diff changeset
    97
    ('', '8000', '/base')
34131
0fa781320203 doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents: 34022
diff changeset
    98
    >>> geturlcgivars(b"http://host", b'8000')
15003
a31b8e03af28 hgweb: extract the path logic from updatereqenv and add doctests
Matt Mackall <mpm@selenic.com>
parents: 15002
diff changeset
    99
    ('host', '8000', '/')
34131
0fa781320203 doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents: 34022
diff changeset
   100
    >>> geturlcgivars(b"http://host/", b'8000')
15003
a31b8e03af28 hgweb: extract the path logic from updatereqenv and add doctests
Matt Mackall <mpm@selenic.com>
parents: 15002
diff changeset
   101
    ('host', '8000', '/')
a31b8e03af28 hgweb: extract the path logic from updatereqenv and add doctests
Matt Mackall <mpm@selenic.com>
parents: 15002
diff changeset
   102
    """
a31b8e03af28 hgweb: extract the path logic from updatereqenv and add doctests
Matt Mackall <mpm@selenic.com>
parents: 15002
diff changeset
   103
    u = util.url(baseurl)
a31b8e03af28 hgweb: extract the path logic from updatereqenv and add doctests
Matt Mackall <mpm@selenic.com>
parents: 15002
diff changeset
   104
    name = u.host or ''
a31b8e03af28 hgweb: extract the path logic from updatereqenv and add doctests
Matt Mackall <mpm@selenic.com>
parents: 15002
diff changeset
   105
    if u.port:
a31b8e03af28 hgweb: extract the path logic from updatereqenv and add doctests
Matt Mackall <mpm@selenic.com>
parents: 15002
diff changeset
   106
        port = u.port
a31b8e03af28 hgweb: extract the path logic from updatereqenv and add doctests
Matt Mackall <mpm@selenic.com>
parents: 15002
diff changeset
   107
    path = u.path or ""
a31b8e03af28 hgweb: extract the path logic from updatereqenv and add doctests
Matt Mackall <mpm@selenic.com>
parents: 15002
diff changeset
   108
    if not path.startswith('/'):
a31b8e03af28 hgweb: extract the path logic from updatereqenv and add doctests
Matt Mackall <mpm@selenic.com>
parents: 15002
diff changeset
   109
        path = '/' + path
a31b8e03af28 hgweb: extract the path logic from updatereqenv and add doctests
Matt Mackall <mpm@selenic.com>
parents: 15002
diff changeset
   110
34358
8cbcee0b923d py3: remove use of str() in hgwebdir
Yuya Nishihara <yuya@tcha.org>
parents: 34257
diff changeset
   111
    return name, pycompat.bytestr(port), path
15003
a31b8e03af28 hgweb: extract the path logic from updatereqenv and add doctests
Matt Mackall <mpm@selenic.com>
parents: 15002
diff changeset
   112
1559
59b3639df0a9 Convert all classes to new-style classes by deriving them from object.
Eric Hopper <hopper@omnifarious.org>
parents: 1554
diff changeset
   113
class hgwebdir(object):
26132
9df8c729e2e7 hgweb: add some documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26072
diff changeset
   114
    """HTTP server for multiple repositories.
9df8c729e2e7 hgweb: add some documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26072
diff changeset
   115
9df8c729e2e7 hgweb: add some documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26072
diff changeset
   116
    Given a configuration, different repositories will be served depending
9df8c729e2e7 hgweb: add some documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26072
diff changeset
   117
    on the request path.
9df8c729e2e7 hgweb: add some documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26072
diff changeset
   118
9df8c729e2e7 hgweb: add some documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26072
diff changeset
   119
    Instances are typically used as WSGI applications.
9df8c729e2e7 hgweb: add some documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26072
diff changeset
   120
    """
8191
35604226d712 hgweb: kill parentui references
Matt Mackall <mpm@selenic.com>
parents: 8190
diff changeset
   121
    def __init__(self, conf, baseui=None):
8371
1bd0fdf4c1ec hgwebdir: refresh configuration periodically
Bryan O'Sullivan <bos@serpentine.com>
parents: 8360
diff changeset
   122
        self.conf = conf
1bd0fdf4c1ec hgwebdir: refresh configuration periodically
Bryan O'Sullivan <bos@serpentine.com>
parents: 8360
diff changeset
   123
        self.baseui = baseui
26072
06320fb11699 hgweb: make refresh interval configurable
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
   124
        self.ui = None
8371
1bd0fdf4c1ec hgwebdir: refresh configuration periodically
Bryan O'Sullivan <bos@serpentine.com>
parents: 8360
diff changeset
   125
        self.lastrefresh = 0
9903
5d748045c2ae Do not overwrite motd attribute of hgwebdir instances on refresh.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 9842
diff changeset
   126
        self.motd = None
8371
1bd0fdf4c1ec hgwebdir: refresh configuration periodically
Bryan O'Sullivan <bos@serpentine.com>
parents: 8360
diff changeset
   127
        self.refresh()
1181
4f5001f5b4c3 Make sure the repository names don't have slashes at the at or else in some
Vincent Wagelaar <vincent@ricardis.tudelft.nl>
parents: 1180
diff changeset
   128
8371
1bd0fdf4c1ec hgwebdir: refresh configuration periodically
Bryan O'Sullivan <bos@serpentine.com>
parents: 8360
diff changeset
   129
    def refresh(self):
26072
06320fb11699 hgweb: make refresh interval configurable
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
   130
        if self.ui:
34244
fe5202bef5ce configitems: register the 'web.refreshinterval' config
Boris Feld <boris.feld@octobus.net>
parents: 34243
diff changeset
   131
            refreshinterval = self.ui.configint('web', 'refreshinterval')
fe5202bef5ce configitems: register the 'web.refreshinterval' config
Boris Feld <boris.feld@octobus.net>
parents: 34243
diff changeset
   132
        else:
fe5202bef5ce configitems: register the 'web.refreshinterval' config
Boris Feld <boris.feld@octobus.net>
parents: 34243
diff changeset
   133
            item = configitems.coreitems['web']['refreshinterval']
fe5202bef5ce configitems: register the 'web.refreshinterval' config
Boris Feld <boris.feld@octobus.net>
parents: 34243
diff changeset
   134
            refreshinterval = item.default
26072
06320fb11699 hgweb: make refresh interval configurable
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
   135
06320fb11699 hgweb: make refresh interval configurable
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
   136
        # refreshinterval <= 0 means to always refresh.
06320fb11699 hgweb: make refresh interval configurable
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
   137
        if (refreshinterval > 0 and
06320fb11699 hgweb: make refresh interval configurable
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
   138
            self.lastrefresh + refreshinterval > time.time()):
8371
1bd0fdf4c1ec hgwebdir: refresh configuration periodically
Bryan O'Sullivan <bos@serpentine.com>
parents: 8360
diff changeset
   139
            return
1bd0fdf4c1ec hgwebdir: refresh configuration periodically
Bryan O'Sullivan <bos@serpentine.com>
parents: 8360
diff changeset
   140
1bd0fdf4c1ec hgwebdir: refresh configuration periodically
Bryan O'Sullivan <bos@serpentine.com>
parents: 8360
diff changeset
   141
        if self.baseui:
11239
99bc18d1ab0f hgweb: fix race in refreshing repo list (issue2188)
Matt Mackall <mpm@selenic.com>
parents: 10675
diff changeset
   142
            u = self.baseui.copy()
1143
4fffb3d84b7c Allow list of (virtual, real) or dictionary to be passed to hgwebdir.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1142
diff changeset
   143
        else:
30559
d83ca854fa21 ui: factor out ui.load() to create a ui without loading configs (API)
Yuya Nishihara <yuya@tcha.org>
parents: 29787
diff changeset
   144
            u = uimod.ui.load()
20790
49f2d5644f04 config: set a 'source' in most cases where config don't come from file but code
Mads Kiilerich <madski@unity3d.com>
parents: 20677
diff changeset
   145
            u.setconfig('ui', 'report_untrusted', 'off', 'hgwebdir')
49f2d5644f04 config: set a 'source' in most cases where config don't come from file but code
Mads Kiilerich <madski@unity3d.com>
parents: 20677
diff changeset
   146
            u.setconfig('ui', 'nontty', 'true', 'hgwebdir')
25488
89ce95f907bd hgewb: disable progress when serving (issue4582)
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25427
diff changeset
   147
            # displaying bundling progress bar while serving feels wrong and may
89ce95f907bd hgewb: disable progress when serving (issue4582)
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25427
diff changeset
   148
            # break some wsgi implementations.
89ce95f907bd hgewb: disable progress when serving (issue4582)
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25427
diff changeset
   149
            u.setconfig('progress', 'disable', 'true', 'hgweb')
8136
6b5522cb2ad2 ui: refactor option setting
Matt Mackall <mpm@selenic.com>
parents: 7966
diff changeset
   150
8529
a767998f0a78 hgweb: make hgwebdir handle dict/list paths the same as config paths
Jeremy Whitlock <jcscoobyrs@gmail.com>
parents: 8389
diff changeset
   151
        if not isinstance(self.conf, (dict, list, tuple)):
a767998f0a78 hgweb: make hgwebdir handle dict/list paths the same as config paths
Jeremy Whitlock <jcscoobyrs@gmail.com>
parents: 8389
diff changeset
   152
            map = {'paths': 'hgweb-paths'}
13214
5bcb6c9d16db hgweb: abort if config file isn't found
Matt Mackall <mpm@selenic.com>
parents: 13066
diff changeset
   153
            if not os.path.exists(self.conf):
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26421
diff changeset
   154
                raise error.Abort(_('config file %s not found!') % self.conf)
11239
99bc18d1ab0f hgweb: fix race in refreshing repo list (issue2188)
Matt Mackall <mpm@selenic.com>
parents: 10675
diff changeset
   155
            u.readconfig(self.conf, remap=map, trust=True)
13667
8cbb59124e67 hgweb: support multiple directories for the same path
timeless <timeless@gmail.com>
parents: 13538
diff changeset
   156
            paths = []
8cbb59124e67 hgweb: support multiple directories for the same path
timeless <timeless@gmail.com>
parents: 13538
diff changeset
   157
            for name, ignored in u.configitems('hgweb-paths'):
8cbb59124e67 hgweb: support multiple directories for the same path
timeless <timeless@gmail.com>
parents: 13538
diff changeset
   158
                for path in u.configlist('hgweb-paths', name):
8cbb59124e67 hgweb: support multiple directories for the same path
timeless <timeless@gmail.com>
parents: 13538
diff changeset
   159
                    paths.append((name, path))
8529
a767998f0a78 hgweb: make hgwebdir handle dict/list paths the same as config paths
Jeremy Whitlock <jcscoobyrs@gmail.com>
parents: 8389
diff changeset
   160
        elif isinstance(self.conf, (list, tuple)):
a767998f0a78 hgweb: make hgwebdir handle dict/list paths the same as config paths
Jeremy Whitlock <jcscoobyrs@gmail.com>
parents: 8389
diff changeset
   161
            paths = self.conf
a767998f0a78 hgweb: make hgwebdir handle dict/list paths the same as config paths
Jeremy Whitlock <jcscoobyrs@gmail.com>
parents: 8389
diff changeset
   162
        elif isinstance(self.conf, dict):
a767998f0a78 hgweb: make hgwebdir handle dict/list paths the same as config paths
Jeremy Whitlock <jcscoobyrs@gmail.com>
parents: 8389
diff changeset
   163
            paths = self.conf.items()
8345
dcebff8a25dd hgwebdir: read --webdir-conf as actual configuration to ui (issue1586)
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
   164
11239
99bc18d1ab0f hgweb: fix race in refreshing repo list (issue2188)
Matt Mackall <mpm@selenic.com>
parents: 10675
diff changeset
   165
        repos = findrepos(paths)
99bc18d1ab0f hgweb: fix race in refreshing repo list (issue2188)
Matt Mackall <mpm@selenic.com>
parents: 10675
diff changeset
   166
        for prefix, root in u.configitems('collections'):
99bc18d1ab0f hgweb: fix race in refreshing repo list (issue2188)
Matt Mackall <mpm@selenic.com>
parents: 10675
diff changeset
   167
            prefix = util.pconvert(prefix)
13975
938fbeacac84 move walkrepos from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents: 13964
diff changeset
   168
            for path in scmutil.walkrepos(root, followsym=True):
11239
99bc18d1ab0f hgweb: fix race in refreshing repo list (issue2188)
Matt Mackall <mpm@selenic.com>
parents: 10675
diff changeset
   169
                repo = os.path.normpath(path)
99bc18d1ab0f hgweb: fix race in refreshing repo list (issue2188)
Matt Mackall <mpm@selenic.com>
parents: 10675
diff changeset
   170
                name = util.pconvert(repo)
99bc18d1ab0f hgweb: fix race in refreshing repo list (issue2188)
Matt Mackall <mpm@selenic.com>
parents: 10675
diff changeset
   171
                if name.startswith(prefix):
99bc18d1ab0f hgweb: fix race in refreshing repo list (issue2188)
Matt Mackall <mpm@selenic.com>
parents: 10675
diff changeset
   172
                    name = name[len(prefix):]
99bc18d1ab0f hgweb: fix race in refreshing repo list (issue2188)
Matt Mackall <mpm@selenic.com>
parents: 10675
diff changeset
   173
                repos.append((name.lstrip('/'), repo))
99bc18d1ab0f hgweb: fix race in refreshing repo list (issue2188)
Matt Mackall <mpm@selenic.com>
parents: 10675
diff changeset
   174
99bc18d1ab0f hgweb: fix race in refreshing repo list (issue2188)
Matt Mackall <mpm@selenic.com>
parents: 10675
diff changeset
   175
        self.repos = repos
99bc18d1ab0f hgweb: fix race in refreshing repo list (issue2188)
Matt Mackall <mpm@selenic.com>
parents: 10675
diff changeset
   176
        self.ui = u
34239
344fd1fe237b configitems: register the 'web.encoding' config
Boris Feld <boris.feld@octobus.net>
parents: 34238
diff changeset
   177
        encoding.encoding = self.ui.config('web', 'encoding')
34246
db63872e10cc configitems: register the 'web.style' config
Boris Feld <boris.feld@octobus.net>
parents: 34245
diff changeset
   178
        self.style = self.ui.config('web', 'style')
34248
b59620c52eec hgwebdir: read 'web.template' untrusted
Boris Feld <boris.feld@octobus.net>
parents: 34247
diff changeset
   179
        self.templatepath = self.ui.config('web', 'templates', untrusted=False)
34245
945c9816ec1d configitems: register the 'web.stripes' config
Boris Feld <boris.feld@octobus.net>
parents: 34244
diff changeset
   180
        self.stripecount = self.ui.config('web', 'stripes')
8621
13613221caf1 hgweb: extract config values after reading webdir-config
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8537
diff changeset
   181
        if self.stripecount:
13613221caf1 hgweb: extract config values after reading webdir-config
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8537
diff changeset
   182
            self.stripecount = int(self.stripecount)
13613221caf1 hgweb: extract config values after reading webdir-config
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8537
diff changeset
   183
        self._baseurl = self.ui.config('web', 'baseurl')
34243
d24816dfdcff configitems: register the 'web.prefix' config
Boris Feld <boris.feld@octobus.net>
parents: 34239
diff changeset
   184
        prefix = self.ui.config('web', 'prefix')
18515
bf8bbbf4aa45 hgwebdir: use web.prefix when creating url breadcrumbs (issue3790)
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 18258
diff changeset
   185
        if prefix.startswith('/'):
bf8bbbf4aa45 hgwebdir: use web.prefix when creating url breadcrumbs (issue3790)
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 18258
diff changeset
   186
            prefix = prefix[1:]
bf8bbbf4aa45 hgwebdir: use web.prefix when creating url breadcrumbs (issue3790)
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 18258
diff changeset
   187
        if prefix.endswith('/'):
bf8bbbf4aa45 hgwebdir: use web.prefix when creating url breadcrumbs (issue3790)
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 18258
diff changeset
   188
            prefix = prefix[:-1]
bf8bbbf4aa45 hgwebdir: use web.prefix when creating url breadcrumbs (issue3790)
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 18258
diff changeset
   189
        self.prefix = prefix
8371
1bd0fdf4c1ec hgwebdir: refresh configuration periodically
Bryan O'Sullivan <bos@serpentine.com>
parents: 8360
diff changeset
   190
        self.lastrefresh = time.time()
941
4cf418c2a013 Add a multi-repository server
mpm@selenic.com
parents: 939
diff changeset
   191
2535
b8ccf6386db7 Arrange for old copies of CGI scripts to still work.
Eric Hopper <hopper@omnifarious.org>
parents: 2514
diff changeset
   192
    def run(self):
30636
f1c9fafcbf46 py3: replace os.environ with encoding.environ (part 3 of 5)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 30559
diff changeset
   193
        if not encoding.environ.get('GATEWAY_INTERFACE',
f1c9fafcbf46 py3: replace os.environ with encoding.environ (part 3 of 5)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 30559
diff changeset
   194
                                    '').startswith("CGI/1."):
8663
45f626a39def wrap string literals in error messages
Martin Geisler <mg@lazybytes.net>
parents: 8621
diff changeset
   195
            raise RuntimeError("This function is only intended to be "
45f626a39def wrap string literals in error messages
Martin Geisler <mg@lazybytes.net>
parents: 8621
diff changeset
   196
                               "called while running as a CGI script.")
5566
d74fc8dec2b4 Less indirection in the WSGI web interface. This simplifies some code, and makes it more compliant with WSGI.
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5561
diff changeset
   197
        wsgicgi.launch(self)
d74fc8dec2b4 Less indirection in the WSGI web interface. This simplifies some code, and makes it more compliant with WSGI.
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5561
diff changeset
   198
d74fc8dec2b4 Less indirection in the WSGI web interface. This simplifies some code, and makes it more compliant with WSGI.
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5561
diff changeset
   199
    def __call__(self, env, respond):
36804
b9b968e21f78 hgweb: rename req to wsgireq
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36607
diff changeset
   200
        wsgireq = requestmod.wsgirequest(env, respond)
b9b968e21f78 hgweb: rename req to wsgireq
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36607
diff changeset
   201
        return self.run_wsgi(wsgireq)
2535
b8ccf6386db7 Arrange for old copies of CGI scripts to still work.
Eric Hopper <hopper@omnifarious.org>
parents: 2514
diff changeset
   202
36804
b9b968e21f78 hgweb: rename req to wsgireq
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36607
diff changeset
   203
    def read_allowed(self, ui, wsgireq):
7336
2dc868712dcc hgweb: support for deny_read/allow_read options
Mark Edgington <edgimar@gmail.com>
parents: 7225
diff changeset
   204
        """Check allow_read and deny_read config options of a repo's ui object
2dc868712dcc hgweb: support for deny_read/allow_read options
Mark Edgington <edgimar@gmail.com>
parents: 7225
diff changeset
   205
        to determine user permissions.  By default, with neither option set (or
2dc868712dcc hgweb: support for deny_read/allow_read options
Mark Edgington <edgimar@gmail.com>
parents: 7225
diff changeset
   206
        both empty), allow all users to read the repo.  There are two ways a
2dc868712dcc hgweb: support for deny_read/allow_read options
Mark Edgington <edgimar@gmail.com>
parents: 7225
diff changeset
   207
        user can be denied read access:  (1) deny_read is not empty, and the
2dc868712dcc hgweb: support for deny_read/allow_read options
Mark Edgington <edgimar@gmail.com>
parents: 7225
diff changeset
   208
        user is unauthenticated or deny_read contains user (or *), and (2)
2dc868712dcc hgweb: support for deny_read/allow_read options
Mark Edgington <edgimar@gmail.com>
parents: 7225
diff changeset
   209
        allow_read is not empty and the user is not in allow_read.  Return True
2dc868712dcc hgweb: support for deny_read/allow_read options
Mark Edgington <edgimar@gmail.com>
parents: 7225
diff changeset
   210
        if user is allowed to read the repo, else return False."""
2dc868712dcc hgweb: support for deny_read/allow_read options
Mark Edgington <edgimar@gmail.com>
parents: 7225
diff changeset
   211
36804
b9b968e21f78 hgweb: rename req to wsgireq
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36607
diff changeset
   212
        user = wsgireq.env.get('REMOTE_USER')
7336
2dc868712dcc hgweb: support for deny_read/allow_read options
Mark Edgington <edgimar@gmail.com>
parents: 7225
diff changeset
   213
7575
3ccaefc84f45 hgweb: fix long line lengths introduced in 2dc868712dcc
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7560
diff changeset
   214
        deny_read = ui.configlist('web', 'deny_read', untrusted=True)
19032
7d31f2e42a8a hgweb: refactor checks for granting and revoking user permissions
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 18645
diff changeset
   215
        if deny_read and (not user or ismember(ui, user, deny_read)):
7336
2dc868712dcc hgweb: support for deny_read/allow_read options
Mark Edgington <edgimar@gmail.com>
parents: 7225
diff changeset
   216
            return False
2dc868712dcc hgweb: support for deny_read/allow_read options
Mark Edgington <edgimar@gmail.com>
parents: 7225
diff changeset
   217
7575
3ccaefc84f45 hgweb: fix long line lengths introduced in 2dc868712dcc
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7560
diff changeset
   218
        allow_read = ui.configlist('web', 'allow_read', untrusted=True)
7336
2dc868712dcc hgweb: support for deny_read/allow_read options
Mark Edgington <edgimar@gmail.com>
parents: 7225
diff changeset
   219
        # by default, allow reading if no allow_read option has been set
19032
7d31f2e42a8a hgweb: refactor checks for granting and revoking user permissions
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 18645
diff changeset
   220
        if (not allow_read) or ismember(ui, user, allow_read):
7336
2dc868712dcc hgweb: support for deny_read/allow_read options
Mark Edgington <edgimar@gmail.com>
parents: 7225
diff changeset
   221
            return True
2dc868712dcc hgweb: support for deny_read/allow_read options
Mark Edgington <edgimar@gmail.com>
parents: 7225
diff changeset
   222
2dc868712dcc hgweb: support for deny_read/allow_read options
Mark Edgington <edgimar@gmail.com>
parents: 7225
diff changeset
   223
        return False
2dc868712dcc hgweb: support for deny_read/allow_read options
Mark Edgington <edgimar@gmail.com>
parents: 7225
diff changeset
   224
36804
b9b968e21f78 hgweb: rename req to wsgireq
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36607
diff changeset
   225
    def run_wsgi(self, wsgireq):
32788
eede022fc142 profile: drop maybeprofile
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 32004
diff changeset
   226
        profile = self.ui.configbool('profiling', 'enabled')
eede022fc142 profile: drop maybeprofile
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 32004
diff changeset
   227
        with profiling.profile(self.ui, enabled=profile):
36804
b9b968e21f78 hgweb: rename req to wsgireq
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36607
diff changeset
   228
            for r in self._runwsgi(wsgireq):
29787
80df04266a16 hgweb: profile HTTP requests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29786
diff changeset
   229
                yield r
29786
fc2442492606 hgweb: abstract call to hgwebdir wsgi function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 29471
diff changeset
   230
36804
b9b968e21f78 hgweb: rename req to wsgireq
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36607
diff changeset
   231
    def _runwsgi(self, wsgireq):
5601
8279cb841467 hgwebdir: split out makeindex function, facilitate test failure diagnosis
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5585
diff changeset
   232
        try:
25083
ef36536abea3 hgweb: use try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 22634
diff changeset
   233
            self.refresh()
5603
74f65f44a9aa hgwebdir: refactor inner loop
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5602
diff changeset
   234
30766
d7bf7d2bd5ab hgweb: support Content Security Policy
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30749
diff changeset
   235
            csp, nonce = cspvalues(self.ui)
d7bf7d2bd5ab hgweb: support Content Security Policy
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30749
diff changeset
   236
            if csp:
36804
b9b968e21f78 hgweb: rename req to wsgireq
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36607
diff changeset
   237
                wsgireq.headers.append(('Content-Security-Policy', csp))
30766
d7bf7d2bd5ab hgweb: support Content Security Policy
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30749
diff changeset
   238
36804
b9b968e21f78 hgweb: rename req to wsgireq
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36607
diff changeset
   239
            virtual = wsgireq.env.get("PATH_INFO", "").strip('/')
b9b968e21f78 hgweb: rename req to wsgireq
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36607
diff changeset
   240
            tmpl = self.templater(wsgireq, nonce)
25083
ef36536abea3 hgweb: use try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 22634
diff changeset
   241
            ctype = tmpl('mimetype', encoding=encoding.encoding)
ef36536abea3 hgweb: use try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 22634
diff changeset
   242
            ctype = templater.stringify(ctype)
5760
0145f9afb0e7 Removed tabs and trailing whitespace in python files
Thomas Arendsen Hein <thomas@intevation.de>
parents: 5603
diff changeset
   243
25083
ef36536abea3 hgweb: use try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 22634
diff changeset
   244
            # a static file
36804
b9b968e21f78 hgweb: rename req to wsgireq
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36607
diff changeset
   245
            if virtual.startswith('static/') or 'static' in wsgireq.form:
25083
ef36536abea3 hgweb: use try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 22634
diff changeset
   246
                if virtual.startswith('static/'):
ef36536abea3 hgweb: use try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 22634
diff changeset
   247
                    fname = virtual[7:]
ef36536abea3 hgweb: use try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 22634
diff changeset
   248
                else:
36804
b9b968e21f78 hgweb: rename req to wsgireq
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36607
diff changeset
   249
                    fname = wsgireq.form['static'][0]
25083
ef36536abea3 hgweb: use try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 22634
diff changeset
   250
                static = self.ui.config("web", "static", None,
ef36536abea3 hgweb: use try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 22634
diff changeset
   251
                                        untrusted=False)
ef36536abea3 hgweb: use try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 22634
diff changeset
   252
                if not static:
ef36536abea3 hgweb: use try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 22634
diff changeset
   253
                    tp = self.templatepath or templater.templatepaths()
ef36536abea3 hgweb: use try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 22634
diff changeset
   254
                    if isinstance(tp, str):
ef36536abea3 hgweb: use try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 22634
diff changeset
   255
                        tp = [tp]
ef36536abea3 hgweb: use try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 22634
diff changeset
   256
                    static = [os.path.join(p, 'static') for p in tp]
36804
b9b968e21f78 hgweb: rename req to wsgireq
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36607
diff changeset
   257
                staticfile(static, fname, wsgireq)
25083
ef36536abea3 hgweb: use try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 22634
diff changeset
   258
                return []
5603
74f65f44a9aa hgwebdir: refactor inner loop
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5602
diff changeset
   259
25083
ef36536abea3 hgweb: use try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 22634
diff changeset
   260
            # top-level index
31482
da7d19324b1e hgwebdir: add support for explicit index files
Matt Harbison <matt_harbison@yahoo.com>
parents: 30766
diff changeset
   261
da7d19324b1e hgwebdir: add support for explicit index files
Matt Harbison <matt_harbison@yahoo.com>
parents: 30766
diff changeset
   262
            repos = dict(self.repos)
da7d19324b1e hgwebdir: add support for explicit index files
Matt Harbison <matt_harbison@yahoo.com>
parents: 30766
diff changeset
   263
32004
bd3cb917761a hgwebdir: allow a repository to be hosted at "/"
Matt Harbison <matt_harbison@yahoo.com>
parents: 31482
diff changeset
   264
            if (not virtual or virtual == 'index') and virtual not in repos:
36804
b9b968e21f78 hgweb: rename req to wsgireq
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36607
diff changeset
   265
                wsgireq.respond(HTTP_OK, ctype)
b9b968e21f78 hgweb: rename req to wsgireq
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36607
diff changeset
   266
                return self.makeindex(wsgireq, tmpl)
5601
8279cb841467 hgwebdir: split out makeindex function, facilitate test failure diagnosis
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5585
diff changeset
   267
25083
ef36536abea3 hgweb: use try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 22634
diff changeset
   268
            # nested indexes and hgwebs
6210
942287cb1f57 Removed trailing spaces from everything except test output
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6046
diff changeset
   269
31482
da7d19324b1e hgwebdir: add support for explicit index files
Matt Harbison <matt_harbison@yahoo.com>
parents: 30766
diff changeset
   270
            if virtual.endswith('/index') and virtual not in repos:
da7d19324b1e hgwebdir: add support for explicit index files
Matt Harbison <matt_harbison@yahoo.com>
parents: 30766
diff changeset
   271
                subdir = virtual[:-len('index')]
da7d19324b1e hgwebdir: add support for explicit index files
Matt Harbison <matt_harbison@yahoo.com>
parents: 30766
diff changeset
   272
                if any(r.startswith(subdir) for r in repos):
36804
b9b968e21f78 hgweb: rename req to wsgireq
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36607
diff changeset
   273
                    wsgireq.respond(HTTP_OK, ctype)
b9b968e21f78 hgweb: rename req to wsgireq
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36607
diff changeset
   274
                    return self.makeindex(wsgireq, tmpl, subdir)
31482
da7d19324b1e hgwebdir: add support for explicit index files
Matt Harbison <matt_harbison@yahoo.com>
parents: 30766
diff changeset
   275
32004
bd3cb917761a hgwebdir: allow a repository to be hosted at "/"
Matt Harbison <matt_harbison@yahoo.com>
parents: 31482
diff changeset
   276
            def _virtualdirs():
bd3cb917761a hgwebdir: allow a repository to be hosted at "/"
Matt Harbison <matt_harbison@yahoo.com>
parents: 31482
diff changeset
   277
                # Check the full virtual path, each parent, and the root ('')
bd3cb917761a hgwebdir: allow a repository to be hosted at "/"
Matt Harbison <matt_harbison@yahoo.com>
parents: 31482
diff changeset
   278
                if virtual != '':
bd3cb917761a hgwebdir: allow a repository to be hosted at "/"
Matt Harbison <matt_harbison@yahoo.com>
parents: 31482
diff changeset
   279
                    yield virtual
bd3cb917761a hgwebdir: allow a repository to be hosted at "/"
Matt Harbison <matt_harbison@yahoo.com>
parents: 31482
diff changeset
   280
bd3cb917761a hgwebdir: allow a repository to be hosted at "/"
Matt Harbison <matt_harbison@yahoo.com>
parents: 31482
diff changeset
   281
                    for p in util.finddirs(virtual):
bd3cb917761a hgwebdir: allow a repository to be hosted at "/"
Matt Harbison <matt_harbison@yahoo.com>
parents: 31482
diff changeset
   282
                        yield p
bd3cb917761a hgwebdir: allow a repository to be hosted at "/"
Matt Harbison <matt_harbison@yahoo.com>
parents: 31482
diff changeset
   283
bd3cb917761a hgwebdir: allow a repository to be hosted at "/"
Matt Harbison <matt_harbison@yahoo.com>
parents: 31482
diff changeset
   284
                yield ''
bd3cb917761a hgwebdir: allow a repository to be hosted at "/"
Matt Harbison <matt_harbison@yahoo.com>
parents: 31482
diff changeset
   285
bd3cb917761a hgwebdir: allow a repository to be hosted at "/"
Matt Harbison <matt_harbison@yahoo.com>
parents: 31482
diff changeset
   286
            for virtualrepo in _virtualdirs():
25083
ef36536abea3 hgweb: use try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 22634
diff changeset
   287
                real = repos.get(virtualrepo)
ef36536abea3 hgweb: use try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 22634
diff changeset
   288
                if real:
36804
b9b968e21f78 hgweb: rename req to wsgireq
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36607
diff changeset
   289
                    wsgireq.env['REPO_NAME'] = virtualrepo
36856
1f7d9024674c hgweb: make parsedrequest part of wsgirequest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36805
diff changeset
   290
                    # We have to re-parse because of updated environment
1f7d9024674c hgweb: make parsedrequest part of wsgirequest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36805
diff changeset
   291
                    # variable.
1f7d9024674c hgweb: make parsedrequest part of wsgirequest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36805
diff changeset
   292
                    # TODO this is kind of hacky and we should have a better
1f7d9024674c hgweb: make parsedrequest part of wsgirequest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36805
diff changeset
   293
                    # way of doing this than with REPO_NAME side-effects.
36857
da4e2f87167d hgweb: expose input stream on parsed WSGI request object
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36856
diff changeset
   294
                    wsgireq.req = requestmod.parserequestfromenv(
da4e2f87167d hgweb: expose input stream on parsed WSGI request object
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36856
diff changeset
   295
                        wsgireq.env, wsgireq.req.bodyfh)
25083
ef36536abea3 hgweb: use try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 22634
diff changeset
   296
                    try:
ef36536abea3 hgweb: use try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 22634
diff changeset
   297
                        # ensure caller gets private copy of ui
ef36536abea3 hgweb: use try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 22634
diff changeset
   298
                        repo = hg.repository(self.ui.copy(), real)
36804
b9b968e21f78 hgweb: rename req to wsgireq
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36607
diff changeset
   299
                        return hgweb_mod.hgweb(repo).run_wsgi(wsgireq)
25660
328739ea70c3 global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25488
diff changeset
   300
                    except IOError as inst:
34022
d5b2beca16c0 python3: wrap all uses of <exception>.strerror with strtolocal
Augie Fackler <raf@durin42.com>
parents: 33328
diff changeset
   301
                        msg = encoding.strtolocal(inst.strerror)
25083
ef36536abea3 hgweb: use try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 22634
diff changeset
   302
                        raise ErrorResponse(HTTP_SERVER_ERROR, msg)
25660
328739ea70c3 global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25488
diff changeset
   303
                    except error.RepoError as inst:
34358
8cbcee0b923d py3: remove use of str() in hgwebdir
Yuya Nishihara <yuya@tcha.org>
parents: 34257
diff changeset
   304
                        raise ErrorResponse(HTTP_SERVER_ERROR, bytes(inst))
5601
8279cb841467 hgwebdir: split out makeindex function, facilitate test failure diagnosis
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5585
diff changeset
   305
25083
ef36536abea3 hgweb: use try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 22634
diff changeset
   306
            # browse subdirectories
ef36536abea3 hgweb: use try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 22634
diff changeset
   307
            subdir = virtual + '/'
ef36536abea3 hgweb: use try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 22634
diff changeset
   308
            if [r for r in repos if r.startswith(subdir)]:
36804
b9b968e21f78 hgweb: rename req to wsgireq
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36607
diff changeset
   309
                wsgireq.respond(HTTP_OK, ctype)
b9b968e21f78 hgweb: rename req to wsgireq
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36607
diff changeset
   310
                return self.makeindex(wsgireq, tmpl, subdir)
5603
74f65f44a9aa hgwebdir: refactor inner loop
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5602
diff changeset
   311
25083
ef36536abea3 hgweb: use try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 22634
diff changeset
   312
            # prefixes not found
36804
b9b968e21f78 hgweb: rename req to wsgireq
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36607
diff changeset
   313
            wsgireq.respond(HTTP_NOT_FOUND, ctype)
25083
ef36536abea3 hgweb: use try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 22634
diff changeset
   314
            return tmpl("notfound", repo=virtual)
5760
0145f9afb0e7 Removed tabs and trailing whitespace in python files
Thomas Arendsen Hein <thomas@intevation.de>
parents: 5603
diff changeset
   315
25660
328739ea70c3 global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25488
diff changeset
   316
        except ErrorResponse as err:
36804
b9b968e21f78 hgweb: rename req to wsgireq
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36607
diff changeset
   317
            wsgireq.respond(err, ctype)
25083
ef36536abea3 hgweb: use try/except/finally
Matt Mackall <mpm@selenic.com>
parents: 22634
diff changeset
   318
            return tmpl('error', error=err.message or '')
5601
8279cb841467 hgwebdir: split out makeindex function, facilitate test failure diagnosis
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5585
diff changeset
   319
        finally:
8279cb841467 hgwebdir: split out makeindex function, facilitate test failure diagnosis
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5585
diff changeset
   320
            tmpl = None
8279cb841467 hgwebdir: split out makeindex function, facilitate test failure diagnosis
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5585
diff changeset
   321
36804
b9b968e21f78 hgweb: rename req to wsgireq
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36607
diff changeset
   322
    def makeindex(self, wsgireq, tmpl, subdir=""):
5601
8279cb841467 hgwebdir: split out makeindex function, facilitate test failure diagnosis
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5585
diff changeset
   323
2171
290534ee163c Add download links to hgwebdir index page for allowed archive types.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2170
diff changeset
   324
        def archivelist(ui, nodeid, url):
3556
c3043ebe40a0 use untrusted settings in hgwebdir
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3488
diff changeset
   325
            allowed = ui.configlist("web", "allow_archive", untrusted=True)
13436
b391c0c9be61 hgwebdir: reduce memory usage for index generation
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13403
diff changeset
   326
            archives = []
30749
e38e7ea21987 hgweb: use archivespecs for links on repo index page too
Anton Shestakov <av6@dwimlabs.net>
parents: 30636
diff changeset
   327
            for typ, spec in hgweb_mod.archivespecs.iteritems():
e38e7ea21987 hgweb: use archivespecs for links on repo index page too
Anton Shestakov <av6@dwimlabs.net>
parents: 30636
diff changeset
   328
                if typ in allowed or ui.configbool("web", "allow" + typ,
3556
c3043ebe40a0 use untrusted settings in hgwebdir
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3488
diff changeset
   329
                                                    untrusted=True):
34486
a57c938e7ac8 style: never use a space before a colon or comma
Alex Gaynor <agaynor@mozilla.com>
parents: 34358
diff changeset
   330
                    archives.append({"type": typ, "extension": spec[2],
13436
b391c0c9be61 hgwebdir: reduce memory usage for index generation
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13403
diff changeset
   331
                                     "node": nodeid, "url": url})
b391c0c9be61 hgwebdir: reduce memory usage for index generation
Wagner Bruna <wbruna@softwareexpress.com.br>
parents: 13403
diff changeset
   332
            return archives
2171
290534ee163c Add download links to hgwebdir index page for allowed archive types.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2170
diff changeset
   333
10600
033d2fdc3b9d hgweb: separate generation of entries and sorting (cleanup)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10394
diff changeset
   334
        def rawentries(subdir="", **map):
9363
8635b33eaade hgweb: add web.descend configuration variable
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 9184
diff changeset
   335
34237
131f8cd2c2b4 configitems: register the 'web.descend' config
Boris Feld <boris.feld@octobus.net>
parents: 34234
diff changeset
   336
            descend = self.ui.configbool('web', 'descend')
34234
f6d25ffc8b7f configitems: register the 'web.collapse' config
Boris Feld <boris.feld@octobus.net>
parents: 34226
diff changeset
   337
            collapse = self.ui.configbool('web', 'collapse')
16239
287f76b3f502 hgweb: support multi-level repository indexes by enabling descend and collapse
Paul Boddie <paul@boddie.org.uk>
parents: 15003
diff changeset
   338
            seenrepos = set()
287f76b3f502 hgweb: support multi-level repository indexes by enabling descend and collapse
Paul Boddie <paul@boddie.org.uk>
parents: 15003
diff changeset
   339
            seendirs = set()
1141
033c968d7c66 Use ConfigParser only in hgwebdir.__init__()
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1140
diff changeset
   340
            for name, path in self.repos:
9363
8635b33eaade hgweb: add web.descend configuration variable
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 9184
diff changeset
   341
4841
9b0ebb5e0f94 hgweb: let hgwebdir browse subdirectories
Brendan Cully <brendan@kublai.com>
parents: 4709
diff changeset
   342
                if not name.startswith(subdir):
9b0ebb5e0f94 hgweb: let hgwebdir browse subdirectories
Brendan Cully <brendan@kublai.com>
parents: 4709
diff changeset
   343
                    continue
4843
496ac05c6a31 hgwebdir: show only trailing part of path when browsing subdirectories
Brendan Cully <brendan@kublai.com>
parents: 4841
diff changeset
   344
                name = name[len(subdir):]
16239
287f76b3f502 hgweb: support multi-level repository indexes by enabling descend and collapse
Paul Boddie <paul@boddie.org.uk>
parents: 15003
diff changeset
   345
                directory = False
287f76b3f502 hgweb: support multi-level repository indexes by enabling descend and collapse
Paul Boddie <paul@boddie.org.uk>
parents: 15003
diff changeset
   346
287f76b3f502 hgweb: support multi-level repository indexes by enabling descend and collapse
Paul Boddie <paul@boddie.org.uk>
parents: 15003
diff changeset
   347
                if '/' in name:
287f76b3f502 hgweb: support multi-level repository indexes by enabling descend and collapse
Paul Boddie <paul@boddie.org.uk>
parents: 15003
diff changeset
   348
                    if not descend:
287f76b3f502 hgweb: support multi-level repository indexes by enabling descend and collapse
Paul Boddie <paul@boddie.org.uk>
parents: 15003
diff changeset
   349
                        continue
287f76b3f502 hgweb: support multi-level repository indexes by enabling descend and collapse
Paul Boddie <paul@boddie.org.uk>
parents: 15003
diff changeset
   350
287f76b3f502 hgweb: support multi-level repository indexes by enabling descend and collapse
Paul Boddie <paul@boddie.org.uk>
parents: 15003
diff changeset
   351
                    nameparts = name.split('/')
287f76b3f502 hgweb: support multi-level repository indexes by enabling descend and collapse
Paul Boddie <paul@boddie.org.uk>
parents: 15003
diff changeset
   352
                    rootname = nameparts[0]
287f76b3f502 hgweb: support multi-level repository indexes by enabling descend and collapse
Paul Boddie <paul@boddie.org.uk>
parents: 15003
diff changeset
   353
287f76b3f502 hgweb: support multi-level repository indexes by enabling descend and collapse
Paul Boddie <paul@boddie.org.uk>
parents: 15003
diff changeset
   354
                    if not collapse:
287f76b3f502 hgweb: support multi-level repository indexes by enabling descend and collapse
Paul Boddie <paul@boddie.org.uk>
parents: 15003
diff changeset
   355
                        pass
287f76b3f502 hgweb: support multi-level repository indexes by enabling descend and collapse
Paul Boddie <paul@boddie.org.uk>
parents: 15003
diff changeset
   356
                    elif rootname in seendirs:
287f76b3f502 hgweb: support multi-level repository indexes by enabling descend and collapse
Paul Boddie <paul@boddie.org.uk>
parents: 15003
diff changeset
   357
                        continue
287f76b3f502 hgweb: support multi-level repository indexes by enabling descend and collapse
Paul Boddie <paul@boddie.org.uk>
parents: 15003
diff changeset
   358
                    elif rootname in seenrepos:
287f76b3f502 hgweb: support multi-level repository indexes by enabling descend and collapse
Paul Boddie <paul@boddie.org.uk>
parents: 15003
diff changeset
   359
                        pass
287f76b3f502 hgweb: support multi-level repository indexes by enabling descend and collapse
Paul Boddie <paul@boddie.org.uk>
parents: 15003
diff changeset
   360
                    else:
287f76b3f502 hgweb: support multi-level repository indexes by enabling descend and collapse
Paul Boddie <paul@boddie.org.uk>
parents: 15003
diff changeset
   361
                        directory = True
287f76b3f502 hgweb: support multi-level repository indexes by enabling descend and collapse
Paul Boddie <paul@boddie.org.uk>
parents: 15003
diff changeset
   362
                        name = rootname
287f76b3f502 hgweb: support multi-level repository indexes by enabling descend and collapse
Paul Boddie <paul@boddie.org.uk>
parents: 15003
diff changeset
   363
287f76b3f502 hgweb: support multi-level repository indexes by enabling descend and collapse
Paul Boddie <paul@boddie.org.uk>
parents: 15003
diff changeset
   364
                        # redefine the path to refer to the directory
287f76b3f502 hgweb: support multi-level repository indexes by enabling descend and collapse
Paul Boddie <paul@boddie.org.uk>
parents: 15003
diff changeset
   365
                        discarded = '/'.join(nameparts[1:])
287f76b3f502 hgweb: support multi-level repository indexes by enabling descend and collapse
Paul Boddie <paul@boddie.org.uk>
parents: 15003
diff changeset
   366
287f76b3f502 hgweb: support multi-level repository indexes by enabling descend and collapse
Paul Boddie <paul@boddie.org.uk>
parents: 15003
diff changeset
   367
                        # remove name parts plus accompanying slash
287f76b3f502 hgweb: support multi-level repository indexes by enabling descend and collapse
Paul Boddie <paul@boddie.org.uk>
parents: 15003
diff changeset
   368
                        path = path[:-len(discarded) - 1]
287f76b3f502 hgweb: support multi-level repository indexes by enabling descend and collapse
Paul Boddie <paul@boddie.org.uk>
parents: 15003
diff changeset
   369
25426
5f3666da6910 hgwebdir: don't allow the hidden parent of a subrepo to show as a directory
Matt Harbison <mharbison@attotech.com>
parents: 25396
diff changeset
   370
                        try:
5f3666da6910 hgwebdir: don't allow the hidden parent of a subrepo to show as a directory
Matt Harbison <mharbison@attotech.com>
parents: 25396
diff changeset
   371
                            r = hg.repository(self.ui, path)
5f3666da6910 hgwebdir: don't allow the hidden parent of a subrepo to show as a directory
Matt Harbison <mharbison@attotech.com>
parents: 25396
diff changeset
   372
                            directory = False
5f3666da6910 hgwebdir: don't allow the hidden parent of a subrepo to show as a directory
Matt Harbison <mharbison@attotech.com>
parents: 25396
diff changeset
   373
                        except (IOError, error.RepoError):
5f3666da6910 hgwebdir: don't allow the hidden parent of a subrepo to show as a directory
Matt Harbison <mharbison@attotech.com>
parents: 25396
diff changeset
   374
                            pass
5f3666da6910 hgwebdir: don't allow the hidden parent of a subrepo to show as a directory
Matt Harbison <mharbison@attotech.com>
parents: 25396
diff changeset
   375
16239
287f76b3f502 hgweb: support multi-level repository indexes by enabling descend and collapse
Paul Boddie <paul@boddie.org.uk>
parents: 15003
diff changeset
   376
                parts = [name]
31482
da7d19324b1e hgwebdir: add support for explicit index files
Matt Harbison <matt_harbison@yahoo.com>
parents: 30766
diff changeset
   377
                parts.insert(0, '/' + subdir.rstrip('/'))
36804
b9b968e21f78 hgweb: rename req to wsgireq
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36607
diff changeset
   378
                if wsgireq.env['SCRIPT_NAME']:
b9b968e21f78 hgweb: rename req to wsgireq
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36607
diff changeset
   379
                    parts.insert(0, wsgireq.env['SCRIPT_NAME'])
16239
287f76b3f502 hgweb: support multi-level repository indexes by enabling descend and collapse
Paul Boddie <paul@boddie.org.uk>
parents: 15003
diff changeset
   380
                url = re.sub(r'/+', '/', '/'.join(parts) + '/')
287f76b3f502 hgweb: support multi-level repository indexes by enabling descend and collapse
Paul Boddie <paul@boddie.org.uk>
parents: 15003
diff changeset
   381
287f76b3f502 hgweb: support multi-level repository indexes by enabling descend and collapse
Paul Boddie <paul@boddie.org.uk>
parents: 15003
diff changeset
   382
                # show either a directory entry or a repository
287f76b3f502 hgweb: support multi-level repository indexes by enabling descend and collapse
Paul Boddie <paul@boddie.org.uk>
parents: 15003
diff changeset
   383
                if directory:
287f76b3f502 hgweb: support multi-level repository indexes by enabling descend and collapse
Paul Boddie <paul@boddie.org.uk>
parents: 15003
diff changeset
   384
                    # get the directory's time information
287f76b3f502 hgweb: support multi-level repository indexes by enabling descend and collapse
Paul Boddie <paul@boddie.org.uk>
parents: 15003
diff changeset
   385
                    try:
36607
c6061cadb400 util: extract all date-related utils in utils/dateutil module
Boris Feld <boris.feld@octobus.net>
parents: 34703
diff changeset
   386
                        d = (get_mtime(path), dateutil.makedate()[1])
16239
287f76b3f502 hgweb: support multi-level repository indexes by enabling descend and collapse
Paul Boddie <paul@boddie.org.uk>
parents: 15003
diff changeset
   387
                    except OSError:
287f76b3f502 hgweb: support multi-level repository indexes by enabling descend and collapse
Paul Boddie <paul@boddie.org.uk>
parents: 15003
diff changeset
   388
                        continue
287f76b3f502 hgweb: support multi-level repository indexes by enabling descend and collapse
Paul Boddie <paul@boddie.org.uk>
parents: 15003
diff changeset
   389
17838
d51364b318ea hgwebdir: make collapsed folders easier to distinguish from repositories
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 17104
diff changeset
   390
                    # add '/' to the name to make it obvious that
d51364b318ea hgwebdir: make collapsed folders easier to distinguish from repositories
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 17104
diff changeset
   391
                    # the entry is a directory, not a regular repository
20677
0e757575aef5 hgwebdir_mod: move from dict() construction to {} literals
Augie Fackler <raf@durin42.com>
parents: 20575
diff changeset
   392
                    row = {'contact': "",
0e757575aef5 hgwebdir_mod: move from dict() construction to {} literals
Augie Fackler <raf@durin42.com>
parents: 20575
diff changeset
   393
                           'contact_sort': "",
0e757575aef5 hgwebdir_mod: move from dict() construction to {} literals
Augie Fackler <raf@durin42.com>
parents: 20575
diff changeset
   394
                           'name': name + '/',
0e757575aef5 hgwebdir_mod: move from dict() construction to {} literals
Augie Fackler <raf@durin42.com>
parents: 20575
diff changeset
   395
                           'name_sort': name,
0e757575aef5 hgwebdir_mod: move from dict() construction to {} literals
Augie Fackler <raf@durin42.com>
parents: 20575
diff changeset
   396
                           'url': url,
0e757575aef5 hgwebdir_mod: move from dict() construction to {} literals
Augie Fackler <raf@durin42.com>
parents: 20575
diff changeset
   397
                           'description': "",
0e757575aef5 hgwebdir_mod: move from dict() construction to {} literals
Augie Fackler <raf@durin42.com>
parents: 20575
diff changeset
   398
                           'description_sort': "",
0e757575aef5 hgwebdir_mod: move from dict() construction to {} literals
Augie Fackler <raf@durin42.com>
parents: 20575
diff changeset
   399
                           'lastchange': d,
0e757575aef5 hgwebdir_mod: move from dict() construction to {} literals
Augie Fackler <raf@durin42.com>
parents: 20575
diff changeset
   400
                           'lastchange_sort': d[1]-d[0],
0e757575aef5 hgwebdir_mod: move from dict() construction to {} literals
Augie Fackler <raf@durin42.com>
parents: 20575
diff changeset
   401
                           'archives': [],
29471
c4fc33c477da hgweb: expose list of per-repo labels to templates
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28954
diff changeset
   402
                           'isdirectory': True,
c4fc33c477da hgweb: expose list of per-repo labels to templates
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28954
diff changeset
   403
                           'labels': [],
c4fc33c477da hgweb: expose list of per-repo labels to templates
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28954
diff changeset
   404
                           }
16239
287f76b3f502 hgweb: support multi-level repository indexes by enabling descend and collapse
Paul Boddie <paul@boddie.org.uk>
parents: 15003
diff changeset
   405
287f76b3f502 hgweb: support multi-level repository indexes by enabling descend and collapse
Paul Boddie <paul@boddie.org.uk>
parents: 15003
diff changeset
   406
                    seendirs.add(name)
287f76b3f502 hgweb: support multi-level repository indexes by enabling descend and collapse
Paul Boddie <paul@boddie.org.uk>
parents: 15003
diff changeset
   407
                    yield row
9363
8635b33eaade hgweb: add web.descend configuration variable
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 9184
diff changeset
   408
                    continue
4841
9b0ebb5e0f94 hgweb: let hgwebdir browse subdirectories
Brendan Cully <brendan@kublai.com>
parents: 4709
diff changeset
   409
8191
35604226d712 hgweb: kill parentui references
Matt Mackall <mpm@selenic.com>
parents: 8190
diff changeset
   410
                u = self.ui.copy()
1170
85555540a4e2 Make .hg/hgrc optional for repositories published by hgwebdir.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1165
diff changeset
   411
                try:
1473
7d66ce9895fa make readconfig take a filename instead of a file pointer as argument
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1445
diff changeset
   412
                    u.readconfig(os.path.join(path, '.hg', 'hgrc'))
25660
328739ea70c3 global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25488
diff changeset
   413
                except Exception as e:
6913
580d5e6bfc1f move % out of translatable strings
Martin Geisler <mg@daimi.au.dk>
parents: 6908
diff changeset
   414
                    u.warn(_('error reading %s/.hg/hgrc: %s\n') % (path, e))
5332
b0bfe087ad8a hgwebdir: ignore hgrc parse errors while building the index page
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5289
diff changeset
   415
                    continue
33328
c8f212cb0c83 hgweb: use ui._unset to prevent a warning in configitems
David Demelier <demelier.david@gmail.com>
parents: 32788
diff changeset
   416
                def get(section, name, default=uimod._unset):
3556
c3043ebe40a0 use untrusted settings in hgwebdir
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3488
diff changeset
   417
                    return u.config(section, name, default, untrusted=True)
941
4cf418c2a013 Add a multi-repository server
mpm@selenic.com
parents: 939
diff changeset
   418
4709
53eca35c3aeb Add option "hidden" to hgwebdir.
Markus F.X.J. Oberhumer <markus@oberhumer.com>
parents: 4462
diff changeset
   419
                if u.configbool("web", "hidden", untrusted=True):
53eca35c3aeb Add option "hidden" to hgwebdir.
Markus F.X.J. Oberhumer <markus@oberhumer.com>
parents: 4462
diff changeset
   420
                    continue
53eca35c3aeb Add option "hidden" to hgwebdir.
Markus F.X.J. Oberhumer <markus@oberhumer.com>
parents: 4462
diff changeset
   421
36804
b9b968e21f78 hgweb: rename req to wsgireq
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36607
diff changeset
   422
                if not self.read_allowed(u, wsgireq):
7336
2dc868712dcc hgweb: support for deny_read/allow_read options
Mark Edgington <edgimar@gmail.com>
parents: 7225
diff changeset
   423
                    continue
2dc868712dcc hgweb: support for deny_read/allow_read options
Mark Edgington <edgimar@gmail.com>
parents: 7225
diff changeset
   424
1348
b8c82bf3da21 hgwebdir: Fix date display
mpm@selenic.com
parents: 1333
diff changeset
   425
                # update time with local timezone
1524
0d47bb884330 hgweb: fix traceback by skipping invalid repo paths
TK Soh <teekaysoh@yahoo.com>
parents: 1511
diff changeset
   426
                try:
10078
97c75ad3b1a0 hgweb: Make get_mtime use repository to find store path.
Brendan Cully <brendan@kublai.com>
parents: 9903
diff changeset
   427
                    r = hg.repository(self.ui, path)
13796
6337149fc07c hgwebdir: handle IOErrors from localrepo while enumerating
timeless@gmail.com
parents: 13667
diff changeset
   428
                except IOError:
6337149fc07c hgwebdir: handle IOErrors from localrepo while enumerating
timeless@gmail.com
parents: 13667
diff changeset
   429
                    u.warn(_('error accessing repository at %s\n') % path)
6337149fc07c hgwebdir: handle IOErrors from localrepo while enumerating
timeless@gmail.com
parents: 13667
diff changeset
   430
                    continue
12038
9617803b1acb hgweb: handle exception of misconfigured path on index page
Yuya Nishihara <yuya@tcha.org>
parents: 11677
diff changeset
   431
                except error.RepoError:
9617803b1acb hgweb: handle exception of misconfigured path on index page
Yuya Nishihara <yuya@tcha.org>
parents: 11677
diff changeset
   432
                    u.warn(_('error accessing repository at %s\n') % path)
9617803b1acb hgweb: handle exception of misconfigured path on index page
Yuya Nishihara <yuya@tcha.org>
parents: 11677
diff changeset
   433
                    continue
9617803b1acb hgweb: handle exception of misconfigured path on index page
Yuya Nishihara <yuya@tcha.org>
parents: 11677
diff changeset
   434
                try:
36607
c6061cadb400 util: extract all date-related utils in utils/dateutil module
Boris Feld <boris.feld@octobus.net>
parents: 34703
diff changeset
   435
                    d = (get_mtime(r.spath), dateutil.makedate()[1])
1524
0d47bb884330 hgweb: fix traceback by skipping invalid repo paths
TK Soh <teekaysoh@yahoo.com>
parents: 1511
diff changeset
   436
                except OSError:
0d47bb884330 hgweb: fix traceback by skipping invalid repo paths
TK Soh <teekaysoh@yahoo.com>
parents: 1511
diff changeset
   437
                    continue
1348
b8c82bf3da21 hgwebdir: Fix date display
mpm@selenic.com
parents: 1333
diff changeset
   438
5779
e9f68860d5ed Don't let ui.username override web.contact (issue900)
Thomas Arendsen Hein <thomas@intevation.de>
parents: 5760
diff changeset
   439
                contact = get_contact(get)
34238
a6c18628dff1 configitems: register the 'web.description' config
Boris Feld <boris.feld@octobus.net>
parents: 34237
diff changeset
   440
                description = get("web", "description")
25396
78e8890cfb4b hgwebdir: avoid redundant repo and directory entries when 'web.name' is set
Matt Harbison <matt_harbison@yahoo.com>
parents: 22634
diff changeset
   441
                seenrepos.add(name)
2173
d1943df604c4 Make hgwebdir columns sortable.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2171
diff changeset
   442
                name = get("web", "name", name)
20677
0e757575aef5 hgwebdir_mod: move from dict() construction to {} literals
Augie Fackler <raf@durin42.com>
parents: 20575
diff changeset
   443
                row = {'contact': contact or "unknown",
0e757575aef5 hgwebdir_mod: move from dict() construction to {} literals
Augie Fackler <raf@durin42.com>
parents: 20575
diff changeset
   444
                       'contact_sort': contact.upper() or "unknown",
0e757575aef5 hgwebdir_mod: move from dict() construction to {} literals
Augie Fackler <raf@durin42.com>
parents: 20575
diff changeset
   445
                       'name': name,
0e757575aef5 hgwebdir_mod: move from dict() construction to {} literals
Augie Fackler <raf@durin42.com>
parents: 20575
diff changeset
   446
                       'name_sort': name,
0e757575aef5 hgwebdir_mod: move from dict() construction to {} literals
Augie Fackler <raf@durin42.com>
parents: 20575
diff changeset
   447
                       'url': url,
0e757575aef5 hgwebdir_mod: move from dict() construction to {} literals
Augie Fackler <raf@durin42.com>
parents: 20575
diff changeset
   448
                       'description': description or "unknown",
0e757575aef5 hgwebdir_mod: move from dict() construction to {} literals
Augie Fackler <raf@durin42.com>
parents: 20575
diff changeset
   449
                       'description_sort': description.upper() or "unknown",
0e757575aef5 hgwebdir_mod: move from dict() construction to {} literals
Augie Fackler <raf@durin42.com>
parents: 20575
diff changeset
   450
                       'lastchange': d,
0e757575aef5 hgwebdir_mod: move from dict() construction to {} literals
Augie Fackler <raf@durin42.com>
parents: 20575
diff changeset
   451
                       'lastchange_sort': d[1]-d[0],
0e757575aef5 hgwebdir_mod: move from dict() construction to {} literals
Augie Fackler <raf@durin42.com>
parents: 20575
diff changeset
   452
                       'archives': archivelist(u, "tip", url),
0e757575aef5 hgwebdir_mod: move from dict() construction to {} literals
Augie Fackler <raf@durin42.com>
parents: 20575
diff changeset
   453
                       'isdirectory': None,
29471
c4fc33c477da hgweb: expose list of per-repo labels to templates
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28954
diff changeset
   454
                       'labels': u.configlist('web', 'labels', untrusted=True),
20677
0e757575aef5 hgwebdir_mod: move from dict() construction to {} literals
Augie Fackler <raf@durin42.com>
parents: 20575
diff changeset
   455
                       }
16239
287f76b3f502 hgweb: support multi-level repository indexes by enabling descend and collapse
Paul Boddie <paul@boddie.org.uk>
parents: 15003
diff changeset
   456
10600
033d2fdc3b9d hgweb: separate generation of entries and sorting (cleanup)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10394
diff changeset
   457
                yield row
033d2fdc3b9d hgweb: separate generation of entries and sorting (cleanup)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10394
diff changeset
   458
033d2fdc3b9d hgweb: separate generation of entries and sorting (cleanup)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10394
diff changeset
   459
        sortdefault = None, False
033d2fdc3b9d hgweb: separate generation of entries and sorting (cleanup)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10394
diff changeset
   460
        def entries(sortcolumn="", descending=False, subdir="", **map):
033d2fdc3b9d hgweb: separate generation of entries and sorting (cleanup)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10394
diff changeset
   461
            rows = rawentries(subdir=subdir, **map)
033d2fdc3b9d hgweb: separate generation of entries and sorting (cleanup)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10394
diff changeset
   462
033d2fdc3b9d hgweb: separate generation of entries and sorting (cleanup)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10394
diff changeset
   463
            if sortcolumn and sortdefault != (sortcolumn, descending):
033d2fdc3b9d hgweb: separate generation of entries and sorting (cleanup)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10394
diff changeset
   464
                sortkey = '%s_sort' % sortcolumn
033d2fdc3b9d hgweb: separate generation of entries and sorting (cleanup)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10394
diff changeset
   465
                rows = sorted(rows, key=lambda x: x[sortkey],
033d2fdc3b9d hgweb: separate generation of entries and sorting (cleanup)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10394
diff changeset
   466
                              reverse=descending)
033d2fdc3b9d hgweb: separate generation of entries and sorting (cleanup)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10394
diff changeset
   467
            for row, parity in zip(rows, paritygen(self.stripecount)):
033d2fdc3b9d hgweb: separate generation of entries and sorting (cleanup)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10394
diff changeset
   468
                row['parity'] = parity
033d2fdc3b9d hgweb: separate generation of entries and sorting (cleanup)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10394
diff changeset
   469
                yield row
941
4cf418c2a013 Add a multi-repository server
mpm@selenic.com
parents: 939
diff changeset
   470
8371
1bd0fdf4c1ec hgwebdir: refresh configuration periodically
Bryan O'Sullivan <bos@serpentine.com>
parents: 8360
diff changeset
   471
        self.refresh()
5601
8279cb841467 hgwebdir: split out makeindex function, facilitate test failure diagnosis
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5585
diff changeset
   472
        sortable = ["name", "description", "contact", "lastchange"]
8346
b579823cc546 hgweb: get rid of inaccurate hgwebdir.repos_sorted, localize machinery
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8345
diff changeset
   473
        sortcolumn, descending = sortdefault
36804
b9b968e21f78 hgweb: rename req to wsgireq
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36607
diff changeset
   474
        if 'sort' in wsgireq.form:
b9b968e21f78 hgweb: rename req to wsgireq
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36607
diff changeset
   475
            sortcolumn = wsgireq.form['sort'][0]
5601
8279cb841467 hgwebdir: split out makeindex function, facilitate test failure diagnosis
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5585
diff changeset
   476
            descending = sortcolumn.startswith('-')
8279cb841467 hgwebdir: split out makeindex function, facilitate test failure diagnosis
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5585
diff changeset
   477
            if descending:
8279cb841467 hgwebdir: split out makeindex function, facilitate test failure diagnosis
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5585
diff changeset
   478
                sortcolumn = sortcolumn[1:]
8279cb841467 hgwebdir: split out makeindex function, facilitate test failure diagnosis
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5585
diff changeset
   479
            if sortcolumn not in sortable:
8279cb841467 hgwebdir: split out makeindex function, facilitate test failure diagnosis
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5585
diff changeset
   480
                sortcolumn = ""
4841
9b0ebb5e0f94 hgweb: let hgwebdir browse subdirectories
Brendan Cully <brendan@kublai.com>
parents: 4709
diff changeset
   481
5601
8279cb841467 hgwebdir: split out makeindex function, facilitate test failure diagnosis
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5585
diff changeset
   482
        sort = [("sort_%s" % column,
8279cb841467 hgwebdir: split out makeindex function, facilitate test failure diagnosis
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5585
diff changeset
   483
                 "%s%s" % ((not descending and column == sortcolumn)
8279cb841467 hgwebdir: split out makeindex function, facilitate test failure diagnosis
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5585
diff changeset
   484
                            and "-" or "", column))
8279cb841467 hgwebdir: split out makeindex function, facilitate test failure diagnosis
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5585
diff changeset
   485
                for column in sortable]
5928
3340aa5a64f7 hgweb: move HTTP content types out of header templates
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5915
diff changeset
   486
8371
1bd0fdf4c1ec hgwebdir: refresh configuration periodically
Bryan O'Sullivan <bos@serpentine.com>
parents: 8360
diff changeset
   487
        self.refresh()
36804
b9b968e21f78 hgweb: rename req to wsgireq
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36607
diff changeset
   488
        self.updatereqenv(wsgireq.env)
6221
2eb18c780287 Support web.baseurl in hgwebdir, overriding SCRIPT_NAME
Brendan Cully <brendan@kublai.com>
parents: 6217
diff changeset
   489
5965
abe373e16fe6 hgweb: forgot to centralize the req.write() calls in hgwebdir
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5928
diff changeset
   490
        return tmpl("index", entries=entries, subdir=subdir,
27043
ccdc95c6841e hgweb: do not import hgweb_mod.hgweb and .makebreadcrumb as symbol
Yuya Nishihara <yuya@tcha.org>
parents: 27007
diff changeset
   491
                    pathdef=hgweb_mod.makebreadcrumb('/' + subdir, self.prefix),
5965
abe373e16fe6 hgweb: forgot to centralize the req.write() calls in hgwebdir
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5928
diff changeset
   492
                    sortcolumn=sortcolumn, descending=descending,
abe373e16fe6 hgweb: forgot to centralize the req.write() calls in hgwebdir
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5928
diff changeset
   493
                    **dict(sort))
5602
d676d0f35bd8 hgwebdir: split out templater creation
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5601
diff changeset
   494
36804
b9b968e21f78 hgweb: rename req to wsgireq
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36607
diff changeset
   495
    def templater(self, wsgireq, nonce):
5602
d676d0f35bd8 hgwebdir: split out templater creation
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5601
diff changeset
   496
d676d0f35bd8 hgwebdir: split out templater creation
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5601
diff changeset
   497
        def motd(**map):
d676d0f35bd8 hgwebdir: split out templater creation
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5601
diff changeset
   498
            if self.motd is not None:
d676d0f35bd8 hgwebdir: split out templater creation
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5601
diff changeset
   499
                yield self.motd
d676d0f35bd8 hgwebdir: split out templater creation
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5601
diff changeset
   500
            else:
34587
b50c036494dc configitems: register the 'web.motd' config
Boris Feld <boris.feld@octobus.net>
parents: 34515
diff changeset
   501
                yield config('web', 'motd')
5602
d676d0f35bd8 hgwebdir: split out templater creation
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5601
diff changeset
   502
34226
cf08aaaea7f0 web: use '_unset' default value for proxy config method
Boris Feld <boris.feld@octobus.net>
parents: 34131
diff changeset
   503
        def config(section, name, default=uimod._unset, untrusted=True):
8191
35604226d712 hgweb: kill parentui references
Matt Mackall <mpm@selenic.com>
parents: 8190
diff changeset
   504
            return self.ui.config(section, name, default, untrusted)
5602
d676d0f35bd8 hgwebdir: split out templater creation
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5601
diff changeset
   505
36804
b9b968e21f78 hgweb: rename req to wsgireq
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36607
diff changeset
   506
        self.updatereqenv(wsgireq.env)
6221
2eb18c780287 Support web.baseurl in hgwebdir, overriding SCRIPT_NAME
Brendan Cully <brendan@kublai.com>
parents: 6217
diff changeset
   507
36804
b9b968e21f78 hgweb: rename req to wsgireq
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36607
diff changeset
   508
        url = wsgireq.env.get('SCRIPT_NAME', '')
5602
d676d0f35bd8 hgwebdir: split out templater creation
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5601
diff changeset
   509
        if not url.endswith('/'):
d676d0f35bd8 hgwebdir: split out templater creation
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5601
diff changeset
   510
            url += '/'
d676d0f35bd8 hgwebdir: split out templater creation
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5601
diff changeset
   511
8216
25266fe996b0 hgweb: use new sessionvars code in hgwebdir, too
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8215
diff changeset
   512
        vars = {}
36863
1a1972b1a1ff hgweb: use our new request object for "style" parameter
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36857
diff changeset
   513
        styles, (style, mapfile) = hgweb_mod.getstyle(wsgireq.req, config,
34515
8afc25e7effc hgweb: extract function for loading style from request context
Augie Fackler <augie@google.com>
parents: 34486
diff changeset
   514
                                                      self.templatepath)
9842
d3dbdca92458 hgweb: don't choke when an inexistent style is requested (issue1901)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 9724
diff changeset
   515
        if style == styles[0]:
d3dbdca92458 hgweb: don't choke when an inexistent style is requested (issue1901)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 9724
diff changeset
   516
            vars['style'] = style
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10264
diff changeset
   517
36805
ec46415ed826 hgweb: always use "?" when writing session vars
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36804
diff changeset
   518
        sessionvars = webutil.sessionvars(vars, r'?')
34612
c2cb6be4212f configitems: register the 'web.logourl' config
Boris Feld <boris.feld@octobus.net>
parents: 34611
diff changeset
   519
        logourl = config('web', 'logourl')
34611
c879fc7bd71f configitems: register the 'web.logoimg' config
Boris Feld <boris.feld@octobus.net>
parents: 34587
diff changeset
   520
        logoimg = config('web', 'logoimg')
5602
d676d0f35bd8 hgwebdir: split out templater creation
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5601
diff changeset
   521
        staticurl = config('web', 'staticurl') or url + 'static/'
d676d0f35bd8 hgwebdir: split out templater creation
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5601
diff changeset
   522
        if not staticurl.endswith('/'):
d676d0f35bd8 hgwebdir: split out templater creation
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5601
diff changeset
   523
            staticurl += '/'
d676d0f35bd8 hgwebdir: split out templater creation
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5601
diff changeset
   524
28954
f97a0bcfd7a1 templater: separate function to create templater from map file (API)
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
   525
        defaults = {
f97a0bcfd7a1 templater: separate function to create templater from map file (API)
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
   526
            "encoding": encoding.encoding,
f97a0bcfd7a1 templater: separate function to create templater from map file (API)
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
   527
            "motd": motd,
f97a0bcfd7a1 templater: separate function to create templater from map file (API)
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
   528
            "url": url,
f97a0bcfd7a1 templater: separate function to create templater from map file (API)
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
   529
            "logourl": logourl,
f97a0bcfd7a1 templater: separate function to create templater from map file (API)
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
   530
            "logoimg": logoimg,
f97a0bcfd7a1 templater: separate function to create templater from map file (API)
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
   531
            "staticurl": staticurl,
f97a0bcfd7a1 templater: separate function to create templater from map file (API)
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
   532
            "sessionvars": sessionvars,
f97a0bcfd7a1 templater: separate function to create templater from map file (API)
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
   533
            "style": style,
30766
d7bf7d2bd5ab hgweb: support Content Security Policy
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30749
diff changeset
   534
            "nonce": nonce,
28954
f97a0bcfd7a1 templater: separate function to create templater from map file (API)
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
   535
        }
f97a0bcfd7a1 templater: separate function to create templater from map file (API)
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
   536
        tmpl = templater.templater.frommapfile(mapfile, defaults=defaults)
5602
d676d0f35bd8 hgwebdir: split out templater creation
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5601
diff changeset
   537
        return tmpl
10673
9848b39a1472 hgweb: introduce helper method to update req.env
Yuya Nishihara <yuya@tcha.org>
parents: 10394
diff changeset
   538
9848b39a1472 hgweb: introduce helper method to update req.env
Yuya Nishihara <yuya@tcha.org>
parents: 10394
diff changeset
   539
    def updatereqenv(self, env):
9848b39a1472 hgweb: introduce helper method to update req.env
Yuya Nishihara <yuya@tcha.org>
parents: 10394
diff changeset
   540
        if self._baseurl is not None:
15003
a31b8e03af28 hgweb: extract the path logic from updatereqenv and add doctests
Matt Mackall <mpm@selenic.com>
parents: 15002
diff changeset
   541
            name, port, path = geturlcgivars(self._baseurl, env['SERVER_PORT'])
a31b8e03af28 hgweb: extract the path logic from updatereqenv and add doctests
Matt Mackall <mpm@selenic.com>
parents: 15002
diff changeset
   542
            env['SERVER_NAME'] = name
a31b8e03af28 hgweb: extract the path logic from updatereqenv and add doctests
Matt Mackall <mpm@selenic.com>
parents: 15002
diff changeset
   543
            env['SERVER_PORT'] = port
15001
dd74cd1e5d49 hgweb: handle 'baseurl' configurations with leading slash (issue2934)
wujek
parents: 14076
diff changeset
   544
            env['SCRIPT_NAME'] = path