mercurial/hgweb/hgweb_mod.py
author Yuya Nishihara <yuya@tcha.org>
Sun, 26 Aug 2018 22:23:25 +0900
changeset 39487 17ca967e9fca
parent 38967 4167437a45dd
child 39793 b63dee7bd0d9
permissions -rw-r--r--
hgweb: map Abort to 403 error to report inaccessible path for example Abort is so common in our codebase. We could instead introduce a dedicated type for path auditing errors, but we'll probably have to catch error.Abort anyway. As you can see, an abort message may include a full path to the repository, which might be considered information leak. If that matters, we should hide the message and send it to the server log instead.
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: 2361
diff changeset
     1
# hgweb/hgweb_mod.py - Web interface for a repository.
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>
4635
63b9d2deed48 Updated copyright notices and add "and others" to "hg version"
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4539
diff changeset
     4
# Copyright 2005-2007 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: 8191
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: 9842
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
26220
a43328baa2ac hgweb: use separate repo instances per thread
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26219
diff changeset
    11
import contextlib
26162
268b39770c28 hgweb: extract web substitutions table generation to own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26161
diff changeset
    12
import os
27046
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 27045
diff changeset
    13
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 27045
diff changeset
    14
from .common import (
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 27045
diff changeset
    15
    ErrorResponse,
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 27045
diff changeset
    16
    HTTP_BAD_REQUEST,
30766
d7bf7d2bd5ab hgweb: support Content Security Policy
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30749
diff changeset
    17
    cspvalues,
27046
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 27045
diff changeset
    18
    permhooks,
36885
9675147aec06 hgweb: send errors using new response API
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36884
diff changeset
    19
    statusmessage,
27046
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 27045
diff changeset
    20
)
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 27045
diff changeset
    21
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 27045
diff changeset
    22
from .. import (
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 27045
diff changeset
    23
    encoding,
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 27045
diff changeset
    24
    error,
36547
7937850a523d hgweb: make templater mostly compatible with log templates
Yuya Nishihara <yuya@tcha.org>
parents: 36311
diff changeset
    25
    formatter,
27046
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 27045
diff changeset
    26
    hg,
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 27045
diff changeset
    27
    hook,
29798
80df04266a16 hgweb: profile HTTP requests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28954
diff changeset
    28
    profiling,
34515
8afc25e7effc hgweb: extract function for loading style from request context
Augie Fackler <augie@google.com>
parents: 34252
diff changeset
    29
    pycompat,
37223
307ee8883975 hgweb: use registrar to declare "websub" template filter
Yuya Nishihara <yuya@tcha.org>
parents: 37096
diff changeset
    30
    registrar,
27046
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 27045
diff changeset
    31
    repoview,
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 27045
diff changeset
    32
    templatefilters,
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 27045
diff changeset
    33
    templater,
37509
cb7b275c0cd0 hgweb: wrap {pathdef} with mappinglist
Yuya Nishihara <yuya@tcha.org>
parents: 37225
diff changeset
    34
    templateutil,
27046
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 27045
diff changeset
    35
    ui as uimod,
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 27045
diff changeset
    36
    util,
35896
ef3a24a023ec wireprotoserver: rename hgweb.protocol to wireprotoserver (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 35060
diff changeset
    37
    wireprotoserver,
27046
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 27045
diff changeset
    38
)
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 27045
diff changeset
    39
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 27045
diff changeset
    40
from . import (
36812
b9b968e21f78 hgweb: rename req to wsgireq
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36810
diff changeset
    41
    request as requestmod,
27046
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 27045
diff changeset
    42
    webcommands,
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 27045
diff changeset
    43
    webutil,
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 27045
diff changeset
    44
    wsgicgi,
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 27045
diff changeset
    45
)
138
c77a679e9cfa Revamped templated hgweb
mpm@selenic.com
parents: 137
diff changeset
    46
34515
8afc25e7effc hgweb: extract function for loading style from request context
Augie Fackler <augie@google.com>
parents: 34252
diff changeset
    47
def getstyle(req, configfn, templatepath):
8afc25e7effc hgweb: extract function for loading style from request context
Augie Fackler <augie@google.com>
parents: 34252
diff changeset
    48
    styles = (
36869
1a1972b1a1ff hgweb: use our new request object for "style" parameter
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36867
diff changeset
    49
        req.qsparams.get('style', None),
34515
8afc25e7effc hgweb: extract function for loading style from request context
Augie Fackler <augie@google.com>
parents: 34252
diff changeset
    50
        configfn('web', 'style'),
8afc25e7effc hgweb: extract function for loading style from request context
Augie Fackler <augie@google.com>
parents: 34252
diff changeset
    51
        'paper',
8afc25e7effc hgweb: extract function for loading style from request context
Augie Fackler <augie@google.com>
parents: 34252
diff changeset
    52
    )
8afc25e7effc hgweb: extract function for loading style from request context
Augie Fackler <augie@google.com>
parents: 34252
diff changeset
    53
    return styles, templater.stylemap(styles, templatepath)
8afc25e7effc hgweb: extract function for loading style from request context
Augie Fackler <augie@google.com>
parents: 34252
diff changeset
    54
18515
bf8bbbf4aa45 hgwebdir: use web.prefix when creating url breadcrumbs (issue3790)
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 18429
diff changeset
    55
def makebreadcrumb(url, prefix=''):
18258
bebb05a7e249 hgweb: add a "URL breadcrumb" to the index and repository pages
Angel Ezquerra <angel.ezquerra at gmail.com>
parents: 16754
diff changeset
    56
    '''Return a 'URL breadcrumb' list
bebb05a7e249 hgweb: add a "URL breadcrumb" to the index and repository pages
Angel Ezquerra <angel.ezquerra at gmail.com>
parents: 16754
diff changeset
    57
bebb05a7e249 hgweb: add a "URL breadcrumb" to the index and repository pages
Angel Ezquerra <angel.ezquerra at gmail.com>
parents: 16754
diff changeset
    58
    A 'URL breadcrumb' is a list of URL-name pairs,
bebb05a7e249 hgweb: add a "URL breadcrumb" to the index and repository pages
Angel Ezquerra <angel.ezquerra at gmail.com>
parents: 16754
diff changeset
    59
    corresponding to each of the path items on a URL.
bebb05a7e249 hgweb: add a "URL breadcrumb" to the index and repository pages
Angel Ezquerra <angel.ezquerra at gmail.com>
parents: 16754
diff changeset
    60
    This can be used to create path navigation entries.
bebb05a7e249 hgweb: add a "URL breadcrumb" to the index and repository pages
Angel Ezquerra <angel.ezquerra at gmail.com>
parents: 16754
diff changeset
    61
    '''
bebb05a7e249 hgweb: add a "URL breadcrumb" to the index and repository pages
Angel Ezquerra <angel.ezquerra at gmail.com>
parents: 16754
diff changeset
    62
    if url.endswith('/'):
bebb05a7e249 hgweb: add a "URL breadcrumb" to the index and repository pages
Angel Ezquerra <angel.ezquerra at gmail.com>
parents: 16754
diff changeset
    63
        url = url[:-1]
18515
bf8bbbf4aa45 hgwebdir: use web.prefix when creating url breadcrumbs (issue3790)
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 18429
diff changeset
    64
    if prefix:
bf8bbbf4aa45 hgwebdir: use web.prefix when creating url breadcrumbs (issue3790)
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 18429
diff changeset
    65
        url = '/' + prefix + url
18258
bebb05a7e249 hgweb: add a "URL breadcrumb" to the index and repository pages
Angel Ezquerra <angel.ezquerra at gmail.com>
parents: 16754
diff changeset
    66
    relpath = url
bebb05a7e249 hgweb: add a "URL breadcrumb" to the index and repository pages
Angel Ezquerra <angel.ezquerra at gmail.com>
parents: 16754
diff changeset
    67
    if relpath.startswith('/'):
bebb05a7e249 hgweb: add a "URL breadcrumb" to the index and repository pages
Angel Ezquerra <angel.ezquerra at gmail.com>
parents: 16754
diff changeset
    68
        relpath = relpath[1:]
bebb05a7e249 hgweb: add a "URL breadcrumb" to the index and repository pages
Angel Ezquerra <angel.ezquerra at gmail.com>
parents: 16754
diff changeset
    69
bebb05a7e249 hgweb: add a "URL breadcrumb" to the index and repository pages
Angel Ezquerra <angel.ezquerra at gmail.com>
parents: 16754
diff changeset
    70
    breadcrumb = []
bebb05a7e249 hgweb: add a "URL breadcrumb" to the index and repository pages
Angel Ezquerra <angel.ezquerra at gmail.com>
parents: 16754
diff changeset
    71
    urlel = url
bebb05a7e249 hgweb: add a "URL breadcrumb" to the index and repository pages
Angel Ezquerra <angel.ezquerra at gmail.com>
parents: 16754
diff changeset
    72
    pathitems = [''] + relpath.split('/')
bebb05a7e249 hgweb: add a "URL breadcrumb" to the index and repository pages
Angel Ezquerra <angel.ezquerra at gmail.com>
parents: 16754
diff changeset
    73
    for pathel in reversed(pathitems):
bebb05a7e249 hgweb: add a "URL breadcrumb" to the index and repository pages
Angel Ezquerra <angel.ezquerra at gmail.com>
parents: 16754
diff changeset
    74
        if not pathel or not urlel:
bebb05a7e249 hgweb: add a "URL breadcrumb" to the index and repository pages
Angel Ezquerra <angel.ezquerra at gmail.com>
parents: 16754
diff changeset
    75
            break
bebb05a7e249 hgweb: add a "URL breadcrumb" to the index and repository pages
Angel Ezquerra <angel.ezquerra at gmail.com>
parents: 16754
diff changeset
    76
        breadcrumb.append({'url': urlel, 'name': pathel})
bebb05a7e249 hgweb: add a "URL breadcrumb" to the index and repository pages
Angel Ezquerra <angel.ezquerra at gmail.com>
parents: 16754
diff changeset
    77
        urlel = os.path.dirname(urlel)
37509
cb7b275c0cd0 hgweb: wrap {pathdef} with mappinglist
Yuya Nishihara <yuya@tcha.org>
parents: 37225
diff changeset
    78
    return templateutil.mappinglist(reversed(breadcrumb))
18258
bebb05a7e249 hgweb: add a "URL breadcrumb" to the index and repository pages
Angel Ezquerra <angel.ezquerra at gmail.com>
parents: 16754
diff changeset
    79
26134
e0a6908f066f hgweb: establish class for holding per request context
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26133
diff changeset
    80
class requestcontext(object):
e0a6908f066f hgweb: establish class for holding per request context
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26133
diff changeset
    81
    """Holds state/context for an individual request.
e0a6908f066f hgweb: establish class for holding per request context
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26133
diff changeset
    82
e0a6908f066f hgweb: establish class for holding per request context
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26133
diff changeset
    83
    Servers can be multi-threaded. Holding state on the WSGI application
e0a6908f066f hgweb: establish class for holding per request context
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26133
diff changeset
    84
    is prone to race conditions. Instances of this class exist to hold
e0a6908f066f hgweb: establish class for holding per request context
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26133
diff changeset
    85
    mutable and race-free state for requests.
e0a6908f066f hgweb: establish class for holding per request context
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26133
diff changeset
    86
    """
36876
1f42d621f090 hgweb: support using new response object for web commands
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36875
diff changeset
    87
    def __init__(self, app, repo, req, res):
26219
ae33fff17c1e hg: establish a cache for localrepository instances
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26218
diff changeset
    88
        self.repo = repo
26217
0d0a0837895d hgweb: remove proxy to hgweb instance
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26211
diff changeset
    89
        self.reponame = app.reponame
36876
1f42d621f090 hgweb: support using new response object for web commands
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36875
diff changeset
    90
        self.req = req
1f42d621f090 hgweb: support using new response object for web commands
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36875
diff changeset
    91
        self.res = res
26134
e0a6908f066f hgweb: establish class for holding per request context
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26133
diff changeset
    92
34590
95f4e5b1ec92 configitems: register the 'web.maxchanges' config
Boris Feld <boris.feld@octobus.net>
parents: 34589
diff changeset
    93
        self.maxchanges = self.configint('web', 'maxchanges')
34252
945c9816ec1d configitems: register the 'web.stripes' config
Boris Feld <boris.feld@octobus.net>
parents: 34246
diff changeset
    94
        self.stripecount = self.configint('web', 'stripes')
34588
0d9928a67254 configitems: register the 'web.maxshortchanges' config
Boris Feld <boris.feld@octobus.net>
parents: 34587
diff changeset
    95
        self.maxshortchanges = self.configint('web', 'maxshortchanges')
34589
883d06211973 configitems: register the 'web.maxfiles' config
Boris Feld <boris.feld@octobus.net>
parents: 34588
diff changeset
    96
        self.maxfiles = self.configint('web', 'maxfiles')
35060
da5d5ea7d696 config: rename allowpull to allow-pull
David Demelier <markand@malikania.fr>
parents: 34806
diff changeset
    97
        self.allowpull = self.configbool('web', 'allow-pull')
26135
edfb4d3b9672 hgweb: move some config options to requestcontext
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26134
diff changeset
    98
26163
84511b1d9724 hgweb: move templatepath to requestcontext
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26162
diff changeset
    99
        # we use untrusted=False to prevent a repo owner from using
84511b1d9724 hgweb: move templatepath to requestcontext
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26162
diff changeset
   100
        # web.templates in .hg/hgrc to get access to any file readable
84511b1d9724 hgweb: move templatepath to requestcontext
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26162
diff changeset
   101
        # by the user running the CGI script
26217
0d0a0837895d hgweb: remove proxy to hgweb instance
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26211
diff changeset
   102
        self.templatepath = self.config('web', 'templates', untrusted=False)
26163
84511b1d9724 hgweb: move templatepath to requestcontext
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26162
diff changeset
   103
26164
e037fd28c8bb hgweb: create websubtable on requestcontext
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26163
diff changeset
   104
        # This object is more expensive to build than simple config values.
e037fd28c8bb hgweb: create websubtable on requestcontext
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26163
diff changeset
   105
        # It is shared across requests. The app will replace the object
e037fd28c8bb hgweb: create websubtable on requestcontext
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26163
diff changeset
   106
        # if it is updated. Since this is a reference and nothing should
e037fd28c8bb hgweb: create websubtable on requestcontext
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26163
diff changeset
   107
        # modify the underlying object, it should be constant for the lifetime
e037fd28c8bb hgweb: create websubtable on requestcontext
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26163
diff changeset
   108
        # of the request.
26217
0d0a0837895d hgweb: remove proxy to hgweb instance
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26211
diff changeset
   109
        self.websubtable = app.websubtable
26164
e037fd28c8bb hgweb: create websubtable on requestcontext
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26163
diff changeset
   110
30766
d7bf7d2bd5ab hgweb: support Content Security Policy
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30749
diff changeset
   111
        self.csp, self.nonce = cspvalues(self.repo.ui)
d7bf7d2bd5ab hgweb: support Content Security Policy
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30749
diff changeset
   112
26135
edfb4d3b9672 hgweb: move some config options to requestcontext
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26134
diff changeset
   113
    # Trust the settings from the .hg/hgrc files by default.
33328
c8f212cb0c83 hgweb: use ui._unset to prevent a warning in configitems
David Demelier <demelier.david@gmail.com>
parents: 32808
diff changeset
   114
    def config(self, section, name, default=uimod._unset, untrusted=True):
26135
edfb4d3b9672 hgweb: move some config options to requestcontext
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26134
diff changeset
   115
        return self.repo.ui.config(section, name, default,
edfb4d3b9672 hgweb: move some config options to requestcontext
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26134
diff changeset
   116
                                   untrusted=untrusted)
edfb4d3b9672 hgweb: move some config options to requestcontext
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26134
diff changeset
   117
33328
c8f212cb0c83 hgweb: use ui._unset to prevent a warning in configitems
David Demelier <demelier.david@gmail.com>
parents: 32808
diff changeset
   118
    def configbool(self, section, name, default=uimod._unset, untrusted=True):
26135
edfb4d3b9672 hgweb: move some config options to requestcontext
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26134
diff changeset
   119
        return self.repo.ui.configbool(section, name, default,
edfb4d3b9672 hgweb: move some config options to requestcontext
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26134
diff changeset
   120
                                       untrusted=untrusted)
edfb4d3b9672 hgweb: move some config options to requestcontext
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26134
diff changeset
   121
33328
c8f212cb0c83 hgweb: use ui._unset to prevent a warning in configitems
David Demelier <demelier.david@gmail.com>
parents: 32808
diff changeset
   122
    def configint(self, section, name, default=uimod._unset, untrusted=True):
26135
edfb4d3b9672 hgweb: move some config options to requestcontext
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26134
diff changeset
   123
        return self.repo.ui.configint(section, name, default,
edfb4d3b9672 hgweb: move some config options to requestcontext
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26134
diff changeset
   124
                                      untrusted=untrusted)
edfb4d3b9672 hgweb: move some config options to requestcontext
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26134
diff changeset
   125
33328
c8f212cb0c83 hgweb: use ui._unset to prevent a warning in configitems
David Demelier <demelier.david@gmail.com>
parents: 32808
diff changeset
   126
    def configlist(self, section, name, default=uimod._unset, untrusted=True):
26135
edfb4d3b9672 hgweb: move some config options to requestcontext
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26134
diff changeset
   127
        return self.repo.ui.configlist(section, name, default,
edfb4d3b9672 hgweb: move some config options to requestcontext
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26134
diff changeset
   128
                                       untrusted=untrusted)
edfb4d3b9672 hgweb: move some config options to requestcontext
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26134
diff changeset
   129
26136
6defc74f3066 hgweb: move archive related attributes to requestcontext
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26135
diff changeset
   130
    def archivelist(self, nodeid):
37514
034a422aeaff hgweb: forward archivelist() of hgweb to webutil
Yuya Nishihara <yuya@tcha.org>
parents: 37512
diff changeset
   131
        return webutil.archivelist(self.repo.ui, nodeid)
18258
bebb05a7e249 hgweb: add a "URL breadcrumb" to the index and repository pages
Angel Ezquerra <angel.ezquerra at gmail.com>
parents: 16754
diff changeset
   132
36874
8ddb5c354906 hgweb: expose repo name on parsedrequest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36872
diff changeset
   133
    def templater(self, req):
26183
bf1b24785f13 hgweb: move templater instantiation to requestcontext
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26167
diff changeset
   134
        # determine scheme, port and server name
bf1b24785f13 hgweb: move templater instantiation to requestcontext
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26167
diff changeset
   135
        # this is needed to create absolute urls
34612
c2cb6be4212f configitems: register the 'web.logourl' config
Boris Feld <boris.feld@octobus.net>
parents: 34611
diff changeset
   136
        logourl = self.config('web', 'logourl')
34611
c879fc7bd71f configitems: register the 'web.logoimg' config
Boris Feld <boris.feld@octobus.net>
parents: 34605
diff changeset
   137
        logoimg = self.config('web', 'logoimg')
36810
7fc80c982656 hgweb: ensure all wsgi environment values are str
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36809
diff changeset
   138
        staticurl = (self.config('web', 'staticurl')
38745
258d90f69076 hgweb: strip trailing '/' in apppath before appending '/static/' (issue5943)
Cédric Krier <ced@b2ck.com>
parents: 38316
diff changeset
   139
                     or req.apppath.rstrip('/') + '/static/')
26183
bf1b24785f13 hgweb: move templater instantiation to requestcontext
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26167
diff changeset
   140
        if not staticurl.endswith('/'):
bf1b24785f13 hgweb: move templater instantiation to requestcontext
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26167
diff changeset
   141
            staticurl += '/'
bf1b24785f13 hgweb: move templater instantiation to requestcontext
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26167
diff changeset
   142
bf1b24785f13 hgweb: move templater instantiation to requestcontext
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26167
diff changeset
   143
        # figure out which style to use
bf1b24785f13 hgweb: move templater instantiation to requestcontext
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26167
diff changeset
   144
bf1b24785f13 hgweb: move templater instantiation to requestcontext
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26167
diff changeset
   145
        vars = {}
36874
8ddb5c354906 hgweb: expose repo name on parsedrequest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36872
diff changeset
   146
        styles, (style, mapfile) = getstyle(req, self.config,
34515
8afc25e7effc hgweb: extract function for loading style from request context
Augie Fackler <augie@google.com>
parents: 34252
diff changeset
   147
                                            self.templatepath)
26183
bf1b24785f13 hgweb: move templater instantiation to requestcontext
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26167
diff changeset
   148
        if style == styles[0]:
bf1b24785f13 hgweb: move templater instantiation to requestcontext
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26167
diff changeset
   149
            vars['style'] = style
bf1b24785f13 hgweb: move templater instantiation to requestcontext
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26167
diff changeset
   150
36813
ec46415ed826 hgweb: always use "?" when writing session vars
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36812
diff changeset
   151
        sessionvars = webutil.sessionvars(vars, '?')
26183
bf1b24785f13 hgweb: move templater instantiation to requestcontext
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26167
diff changeset
   152
bf1b24785f13 hgweb: move templater instantiation to requestcontext
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26167
diff changeset
   153
        if not self.reponame:
34586
c364f3f73634 configitems: register the 'web.name' config
Boris Feld <boris.feld@octobus.net>
parents: 34584
diff changeset
   154
            self.reponame = (self.config('web', 'name', '')
36874
8ddb5c354906 hgweb: expose repo name on parsedrequest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36872
diff changeset
   155
                             or req.reponame
8ddb5c354906 hgweb: expose repo name on parsedrequest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36872
diff changeset
   156
                             or req.apppath
8ddb5c354906 hgweb: expose repo name on parsedrequest
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36872
diff changeset
   157
                             or self.repo.root)
26183
bf1b24785f13 hgweb: move templater instantiation to requestcontext
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26167
diff changeset
   158
37223
307ee8883975 hgweb: use registrar to declare "websub" template filter
Yuya Nishihara <yuya@tcha.org>
parents: 37096
diff changeset
   159
        filters = {}
307ee8883975 hgweb: use registrar to declare "websub" template filter
Yuya Nishihara <yuya@tcha.org>
parents: 37096
diff changeset
   160
        templatefilter = registrar.templatefilter(filters)
37225
08e042f0a67c templatefilters: declare input type as bytes where appropriate
Yuya Nishihara <yuya@tcha.org>
parents: 37223
diff changeset
   161
        @templatefilter('websub', intype=bytes)
26183
bf1b24785f13 hgweb: move templater instantiation to requestcontext
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26167
diff changeset
   162
        def websubfilter(text):
27008
7f19f331ef59 hgweb: do not import templatefilters.revescape and websub as symbol
Yuya Nishihara <yuya@tcha.org>
parents: 27007
diff changeset
   163
            return templatefilters.websub(text, self.websubtable)
26183
bf1b24785f13 hgweb: move templater instantiation to requestcontext
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26167
diff changeset
   164
bf1b24785f13 hgweb: move templater instantiation to requestcontext
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26167
diff changeset
   165
        # create the templater
36547
7937850a523d hgweb: make templater mostly compatible with log templates
Yuya Nishihara <yuya@tcha.org>
parents: 36311
diff changeset
   166
        # TODO: export all keywords: defaults = templatekw.keywords.copy()
28954
f97a0bcfd7a1 templater: separate function to create templater from map file (API)
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
   167
        defaults = {
36816
0031e972ded2 hgweb: use the parsed application path directly
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36815
diff changeset
   168
            'url': req.apppath + '/',
28954
f97a0bcfd7a1 templater: separate function to create templater from map file (API)
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
   169
            'logourl': logourl,
f97a0bcfd7a1 templater: separate function to create templater from map file (API)
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
   170
            'logoimg': logoimg,
f97a0bcfd7a1 templater: separate function to create templater from map file (API)
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
   171
            'staticurl': staticurl,
36815
1e2194e0ef62 hgweb: use computed base URL from parsed request
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36814
diff changeset
   172
            'urlbase': req.advertisedbaseurl,
28954
f97a0bcfd7a1 templater: separate function to create templater from map file (API)
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
   173
            'repo': self.reponame,
f97a0bcfd7a1 templater: separate function to create templater from map file (API)
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
   174
            'encoding': encoding.encoding,
f97a0bcfd7a1 templater: separate function to create templater from map file (API)
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
   175
            'sessionvars': sessionvars,
36816
0031e972ded2 hgweb: use the parsed application path directly
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36815
diff changeset
   176
            'pathdef': makebreadcrumb(req.apppath),
28954
f97a0bcfd7a1 templater: separate function to create templater from map file (API)
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
   177
            'style': style,
30766
d7bf7d2bd5ab hgweb: support Content Security Policy
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30749
diff changeset
   178
            'nonce': self.nonce,
28954
f97a0bcfd7a1 templater: separate function to create templater from map file (API)
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
   179
        }
38967
4167437a45dd hgweb: use registrar to add "motd" template keyword
Yuya Nishihara <yuya@tcha.org>
parents: 38745
diff changeset
   180
        templatekeyword = registrar.templatekeyword(defaults)
4167437a45dd hgweb: use registrar to add "motd" template keyword
Yuya Nishihara <yuya@tcha.org>
parents: 38745
diff changeset
   181
        @templatekeyword('motd', requires=())
4167437a45dd hgweb: use registrar to add "motd" template keyword
Yuya Nishihara <yuya@tcha.org>
parents: 38745
diff changeset
   182
        def motd(context, mapping):
4167437a45dd hgweb: use registrar to add "motd" template keyword
Yuya Nishihara <yuya@tcha.org>
parents: 38745
diff changeset
   183
            yield self.config('web', 'motd')
4167437a45dd hgweb: use registrar to add "motd" template keyword
Yuya Nishihara <yuya@tcha.org>
parents: 38745
diff changeset
   184
36547
7937850a523d hgweb: make templater mostly compatible with log templates
Yuya Nishihara <yuya@tcha.org>
parents: 36311
diff changeset
   185
        tres = formatter.templateresources(self.repo.ui, self.repo)
28954
f97a0bcfd7a1 templater: separate function to create templater from map file (API)
Yuya Nishihara <yuya@tcha.org>
parents: 27046
diff changeset
   186
        tmpl = templater.templater.frommapfile(mapfile,
37223
307ee8883975 hgweb: use registrar to declare "websub" template filter
Yuya Nishihara <yuya@tcha.org>
parents: 37096
diff changeset
   187
                                               filters=filters,
36547
7937850a523d hgweb: make templater mostly compatible with log templates
Yuya Nishihara <yuya@tcha.org>
parents: 36311
diff changeset
   188
                                               defaults=defaults,
7937850a523d hgweb: make templater mostly compatible with log templates
Yuya Nishihara <yuya@tcha.org>
parents: 36311
diff changeset
   189
                                               resources=tres)
26183
bf1b24785f13 hgweb: move templater instantiation to requestcontext
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26167
diff changeset
   190
        return tmpl
bf1b24785f13 hgweb: move templater instantiation to requestcontext
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26167
diff changeset
   191
36889
061635d4221c hgweb: add a sendtemplate() helper function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36887
diff changeset
   192
    def sendtemplate(self, name, **kwargs):
061635d4221c hgweb: add a sendtemplate() helper function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36887
diff changeset
   193
        """Helper function to send a response generated from a template."""
37022
c97b936d8bb5 templater: use named function to expand template against mapping dict (API)
Yuya Nishihara <yuya@tcha.org>
parents: 36992
diff changeset
   194
        kwargs = pycompat.byteskwargs(kwargs)
c97b936d8bb5 templater: use named function to expand template against mapping dict (API)
Yuya Nishihara <yuya@tcha.org>
parents: 36992
diff changeset
   195
        self.res.setbodygen(self.tmpl.generate(name, kwargs))
36889
061635d4221c hgweb: add a sendtemplate() helper function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36887
diff changeset
   196
        return self.res.sendresponse()
26183
bf1b24785f13 hgweb: move templater instantiation to requestcontext
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26167
diff changeset
   197
1559
59b3639df0a9 Convert all classes to new-style classes by deriving them from object.
Eric Hopper <hopper@omnifarious.org>
parents: 1554
diff changeset
   198
class hgweb(object):
26132
9df8c729e2e7 hgweb: add some documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25777
diff changeset
   199
    """HTTP server for individual repositories.
9df8c729e2e7 hgweb: add some documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25777
diff changeset
   200
9df8c729e2e7 hgweb: add some documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25777
diff changeset
   201
    Instances of this class serve HTTP responses for a particular
9df8c729e2e7 hgweb: add some documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25777
diff changeset
   202
    repository.
9df8c729e2e7 hgweb: add some documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25777
diff changeset
   203
9df8c729e2e7 hgweb: add some documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25777
diff changeset
   204
    Instances are typically used as WSGI applications.
9df8c729e2e7 hgweb: add some documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25777
diff changeset
   205
9df8c729e2e7 hgweb: add some documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25777
diff changeset
   206
    Some servers are multi-threaded. On these servers, there may
9df8c729e2e7 hgweb: add some documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25777
diff changeset
   207
    be multiple active threads inside __call__.
9df8c729e2e7 hgweb: add some documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25777
diff changeset
   208
    """
10994
c12a57c1a67e hgweb: add baseui to hgweb entrypoint
Matt Mackall <mpm@selenic.com>
parents: 10905
diff changeset
   209
    def __init__(self, repo, name=None, baseui=None):
37746
b29f490eb904 hgweb_mod: inform hgweb class about paths actually being bytes
Augie Fackler <augie@google.com>
parents: 37514
diff changeset
   210
        if isinstance(repo, bytes):
10994
c12a57c1a67e hgweb: add baseui to hgweb entrypoint
Matt Mackall <mpm@selenic.com>
parents: 10905
diff changeset
   211
            if baseui:
c12a57c1a67e hgweb: add baseui to hgweb entrypoint
Matt Mackall <mpm@selenic.com>
parents: 10905
diff changeset
   212
                u = baseui.copy()
c12a57c1a67e hgweb: add baseui to hgweb entrypoint
Matt Mackall <mpm@selenic.com>
parents: 10905
diff changeset
   213
            else:
30564
d83ca854fa21 ui: factor out ui.load() to create a ui without loading configs (API)
Yuya Nishihara <yuya@tcha.org>
parents: 29798
diff changeset
   214
                u = uimod.ui.load()
20168
d4be314b2071 hgweb: avoid initialization race (issue3953)
Matt Mackall <mpm@selenic.com>
parents: 19906
diff changeset
   215
            r = hg.repository(u, repo)
987
bfe12654764d hgweb: change startup argument processing
mpm@selenic.com
parents: 986
diff changeset
   216
        else:
22087
af62f0280a76 hgweb: avoid config object race with hgwebdir (issue4326)
Matt Mackall <mpm@selenic.com>
parents: 21759
diff changeset
   217
            # we trust caller to give us a private copy
20168
d4be314b2071 hgweb: avoid initialization race (issue3953)
Matt Mackall <mpm@selenic.com>
parents: 19906
diff changeset
   218
            r = repo
133
fb84d3e71042 added template support for some hgweb output, also, template files for
jake@edge2.net
parents: 132
diff changeset
   219
20790
49f2d5644f04 config: set a 'source' in most cases where config don't come from file but code
Mads Kiilerich <madski@unity3d.com>
parents: 20253
diff changeset
   220
        r.ui.setconfig('ui', 'report_untrusted', 'off', 'hgweb')
49f2d5644f04 config: set a 'source' in most cases where config don't come from file but code
Mads Kiilerich <madski@unity3d.com>
parents: 20253
diff changeset
   221
        r.baseui.setconfig('ui', 'report_untrusted', 'off', 'hgweb')
49f2d5644f04 config: set a 'source' in most cases where config don't come from file but code
Mads Kiilerich <madski@unity3d.com>
parents: 20253
diff changeset
   222
        r.ui.setconfig('ui', 'nontty', 'true', 'hgweb')
49f2d5644f04 config: set a 'source' in most cases where config don't come from file but code
Mads Kiilerich <madski@unity3d.com>
parents: 20253
diff changeset
   223
        r.baseui.setconfig('ui', 'nontty', 'true', 'hgweb')
26294
1ffc61c4e32e hgweb: overwrite cwd to resolve file patterns relative to repo (issue4568)
Yuya Nishihara <yuya@tcha.org>
parents: 26247
diff changeset
   224
        # resolve file patterns relative to repo root
1ffc61c4e32e hgweb: overwrite cwd to resolve file patterns relative to repo (issue4568)
Yuya Nishihara <yuya@tcha.org>
parents: 26247
diff changeset
   225
        r.ui.setconfig('ui', 'forcecwd', r.root, 'hgweb')
1ffc61c4e32e hgweb: overwrite cwd to resolve file patterns relative to repo (issue4568)
Yuya Nishihara <yuya@tcha.org>
parents: 26247
diff changeset
   226
        r.baseui.setconfig('ui', 'forcecwd', r.root, 'hgweb')
37849
5b831053d9b6 hgweb: do not try to replace signal handlers while locking
Yuya Nishihara <yuya@tcha.org>
parents: 37827
diff changeset
   227
        # it's unlikely that we can replace signal handlers in WSGI server,
5b831053d9b6 hgweb: do not try to replace signal handlers while locking
Yuya Nishihara <yuya@tcha.org>
parents: 37827
diff changeset
   228
        # and mod_wsgi issues a big warning. a plain hgweb process (with no
5b831053d9b6 hgweb: do not try to replace signal handlers while locking
Yuya Nishihara <yuya@tcha.org>
parents: 37827
diff changeset
   229
        # threading) could replace signal handlers, but we don't bother
5b831053d9b6 hgweb: do not try to replace signal handlers while locking
Yuya Nishihara <yuya@tcha.org>
parents: 37827
diff changeset
   230
        # conditionally enabling it.
5b831053d9b6 hgweb: do not try to replace signal handlers while locking
Yuya Nishihara <yuya@tcha.org>
parents: 37827
diff changeset
   231
        r.ui.setconfig('ui', 'signal-safe-lock', 'false', 'hgweb')
5b831053d9b6 hgweb: do not try to replace signal handlers while locking
Yuya Nishihara <yuya@tcha.org>
parents: 37827
diff changeset
   232
        r.baseui.setconfig('ui', 'signal-safe-lock', 'false', 'hgweb')
25488
89ce95f907bd hgewb: disable progress when serving (issue4582)
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22578
diff changeset
   233
        # displaying bundling progress bar while serving feel wrong and may
89ce95f907bd hgewb: disable progress when serving (issue4582)
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22578
diff changeset
   234
        # break some wsgi implementation.
89ce95f907bd hgewb: disable progress when serving (issue4582)
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22578
diff changeset
   235
        r.ui.setconfig('progress', 'disable', 'true', 'hgweb')
89ce95f907bd hgewb: disable progress when serving (issue4582)
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 22578
diff changeset
   236
        r.baseui.setconfig('progress', 'disable', 'true', 'hgweb')
26220
a43328baa2ac hgweb: use separate repo instances per thread
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26219
diff changeset
   237
        self._repos = [hg.cachedlocalrepo(self._webifyrepo(r))]
a43328baa2ac hgweb: use separate repo instances per thread
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26219
diff changeset
   238
        self._lastrepo = self._repos[0]
5833
323b9c55b328 hook: redirect stdout to stderr for ssh and http servers
Matt Mackall <mpm@selenic.com>
parents: 5779
diff changeset
   239
        hook.redirect(True)
1172
3f30a5e7e15b Use path relative to document root as reponame if published via a web server.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1170
diff changeset
   240
        self.reponame = name
3555
881064004fd0 use untrusted settings in hgweb
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3499
diff changeset
   241
26218
7d45ec47c0af hgweb: create function to perform actions on new repo
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26217
diff changeset
   242
    def _webifyrepo(self, repo):
7d45ec47c0af hgweb: create function to perform actions on new repo
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26217
diff changeset
   243
        repo = getwebview(repo)
7d45ec47c0af hgweb: create function to perform actions on new repo
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26217
diff changeset
   244
        self.websubtable = webutil.getwebsubs(repo)
7d45ec47c0af hgweb: create function to perform actions on new repo
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26217
diff changeset
   245
        return repo
7d45ec47c0af hgweb: create function to perform actions on new repo
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26217
diff changeset
   246
26220
a43328baa2ac hgweb: use separate repo instances per thread
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26219
diff changeset
   247
    @contextlib.contextmanager
a43328baa2ac hgweb: use separate repo instances per thread
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26219
diff changeset
   248
    def _obtainrepo(self):
a43328baa2ac hgweb: use separate repo instances per thread
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26219
diff changeset
   249
        """Obtain a repo unique to the caller.
a43328baa2ac hgweb: use separate repo instances per thread
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26219
diff changeset
   250
a43328baa2ac hgweb: use separate repo instances per thread
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26219
diff changeset
   251
        Internally we maintain a stack of cachedlocalrepo instances
a43328baa2ac hgweb: use separate repo instances per thread
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26219
diff changeset
   252
        to be handed out. If one is available, we pop it and return it,
a43328baa2ac hgweb: use separate repo instances per thread
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26219
diff changeset
   253
        ensuring it is up to date in the process. If one is not available,
a43328baa2ac hgweb: use separate repo instances per thread
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26219
diff changeset
   254
        we clone the most recently used repo instance and return it.
26219
ae33fff17c1e hg: establish a cache for localrepository instances
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26218
diff changeset
   255
26220
a43328baa2ac hgweb: use separate repo instances per thread
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26219
diff changeset
   256
        It is currently possible for the stack to grow without bounds
a43328baa2ac hgweb: use separate repo instances per thread
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26219
diff changeset
   257
        if the server allows infinite threads. However, servers should
a43328baa2ac hgweb: use separate repo instances per thread
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26219
diff changeset
   258
        have a thread limit, thus establishing our limit.
a43328baa2ac hgweb: use separate repo instances per thread
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26219
diff changeset
   259
        """
a43328baa2ac hgweb: use separate repo instances per thread
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26219
diff changeset
   260
        if self._repos:
a43328baa2ac hgweb: use separate repo instances per thread
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26219
diff changeset
   261
            cached = self._repos.pop()
a43328baa2ac hgweb: use separate repo instances per thread
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26219
diff changeset
   262
            r, created = cached.fetch()
a43328baa2ac hgweb: use separate repo instances per thread
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26219
diff changeset
   263
        else:
a43328baa2ac hgweb: use separate repo instances per thread
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26219
diff changeset
   264
            cached = self._lastrepo.copy()
a43328baa2ac hgweb: use separate repo instances per thread
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26219
diff changeset
   265
            r, created = cached.fetch()
26240
2b1434e5eaa0 hg: always create new localrepository instance
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26226
diff changeset
   266
        if created:
2b1434e5eaa0 hg: always create new localrepository instance
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26226
diff changeset
   267
            r = self._webifyrepo(r)
26220
a43328baa2ac hgweb: use separate repo instances per thread
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26219
diff changeset
   268
a43328baa2ac hgweb: use separate repo instances per thread
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26219
diff changeset
   269
        self._lastrepo = cached
a43328baa2ac hgweb: use separate repo instances per thread
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26219
diff changeset
   270
        self.mtime = cached.mtime
a43328baa2ac hgweb: use separate repo instances per thread
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26219
diff changeset
   271
        try:
a43328baa2ac hgweb: use separate repo instances per thread
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26219
diff changeset
   272
            yield r
a43328baa2ac hgweb: use separate repo instances per thread
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26219
diff changeset
   273
        finally:
a43328baa2ac hgweb: use separate repo instances per thread
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26219
diff changeset
   274
            self._repos.append(cached)
258
268bcb5a072a hgweb: watch changelog for changes
mpm@selenic.com
parents: 241
diff changeset
   275
5591
08887121a652 split out hgweb commands into a separate file, move some code around
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5579
diff changeset
   276
    def run(self):
26132
9df8c729e2e7 hgweb: add some documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25777
diff changeset
   277
        """Start a server from CGI environment.
9df8c729e2e7 hgweb: add some documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25777
diff changeset
   278
9df8c729e2e7 hgweb: add some documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25777
diff changeset
   279
        Modern servers should be using WSGI and should avoid this
9df8c729e2e7 hgweb: add some documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25777
diff changeset
   280
        method, if possible.
9df8c729e2e7 hgweb: add some documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25777
diff changeset
   281
        """
30641
f1c9fafcbf46 py3: replace os.environ with encoding.environ (part 3 of 5)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 30564
diff changeset
   282
        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: 30564
diff changeset
   283
                                    '').startswith("CGI/1."):
8663
45f626a39def wrap string literals in error messages
Martin Geisler <mg@lazybytes.net>
parents: 8390
diff changeset
   284
            raise RuntimeError("This function is only intended to be "
45f626a39def wrap string literals in error messages
Martin Geisler <mg@lazybytes.net>
parents: 8390
diff changeset
   285
                               "called while running as a CGI script.")
5591
08887121a652 split out hgweb commands into a separate file, move some code around
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5579
diff changeset
   286
        wsgicgi.launch(self)
08887121a652 split out hgweb commands into a separate file, move some code around
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5579
diff changeset
   287
08887121a652 split out hgweb commands into a separate file, move some code around
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5579
diff changeset
   288
    def __call__(self, env, respond):
26132
9df8c729e2e7 hgweb: add some documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25777
diff changeset
   289
        """Run the WSGI application.
9df8c729e2e7 hgweb: add some documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25777
diff changeset
   290
9df8c729e2e7 hgweb: add some documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25777
diff changeset
   291
        This may be called by multiple threads.
9df8c729e2e7 hgweb: add some documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25777
diff changeset
   292
        """
36917
f0a851542a05 hgweb: remove wsgirequest (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36915
diff changeset
   293
        req = requestmod.parserequestfromenv(env)
f0a851542a05 hgweb: remove wsgirequest (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36915
diff changeset
   294
        res = requestmod.wsgiresponse(req, respond)
5591
08887121a652 split out hgweb commands into a separate file, move some code around
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5579
diff changeset
   295
36917
f0a851542a05 hgweb: remove wsgirequest (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36915
diff changeset
   296
        return self.run_wsgi(req, res)
f0a851542a05 hgweb: remove wsgirequest (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36915
diff changeset
   297
f0a851542a05 hgweb: remove wsgirequest (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36915
diff changeset
   298
    def run_wsgi(self, req, res):
26132
9df8c729e2e7 hgweb: add some documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25777
diff changeset
   299
        """Internal method to run the WSGI application.
5591
08887121a652 split out hgweb commands into a separate file, move some code around
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5579
diff changeset
   300
26132
9df8c729e2e7 hgweb: add some documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25777
diff changeset
   301
        This is typically only called by Mercurial. External consumers
9df8c729e2e7 hgweb: add some documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25777
diff changeset
   302
        should be using instances of this class as the WSGI application.
9df8c729e2e7 hgweb: add some documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25777
diff changeset
   303
        """
26220
a43328baa2ac hgweb: use separate repo instances per thread
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26219
diff changeset
   304
        with self._obtainrepo() as repo:
32808
eede022fc142 profile: drop maybeprofile
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 32004
diff changeset
   305
            profile = repo.ui.configbool('profiling', 'enabled')
eede022fc142 profile: drop maybeprofile
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 32004
diff changeset
   306
            with profiling.profile(repo.ui, enabled=profile):
36917
f0a851542a05 hgweb: remove wsgirequest (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36915
diff changeset
   307
                for r in self._runwsgi(req, res, repo):
29798
80df04266a16 hgweb: profile HTTP requests
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28954
diff changeset
   308
                    yield r
26220
a43328baa2ac hgweb: use separate repo instances per thread
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26219
diff changeset
   309
36917
f0a851542a05 hgweb: remove wsgirequest (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36915
diff changeset
   310
    def _runwsgi(self, req, res, repo):
36876
1f42d621f090 hgweb: support using new response object for web commands
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36875
diff changeset
   311
        rctx = requestcontext(self, repo, req, res)
5591
08887121a652 split out hgweb commands into a separate file, move some code around
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5579
diff changeset
   312
26160
952e0564b46e hgweb: move additional state setting outside of refresh
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26149
diff changeset
   313
        # This state is global across all threads.
34246
344fd1fe237b configitems: register the 'web.encoding' config
Boris Feld <boris.feld@octobus.net>
parents: 33328
diff changeset
   314
        encoding.encoding = rctx.config('web', 'encoding')
36915
84110a1d0f7d hgweb: store the raw WSGI environment dict
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36904
diff changeset
   315
        rctx.repo.ui.environ = req.rawenv
5591
08887121a652 split out hgweb commands into a separate file, move some code around
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5579
diff changeset
   316
30766
d7bf7d2bd5ab hgweb: support Content Security Policy
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30749
diff changeset
   317
        if rctx.csp:
d7bf7d2bd5ab hgweb: support Content Security Policy
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30749
diff changeset
   318
            # hgwebdir may have added CSP header. Since we generate our own,
d7bf7d2bd5ab hgweb: support Content Security Policy
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30749
diff changeset
   319
            # replace it.
36867
a88d68dc3ee8 hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36862
diff changeset
   320
            res.headers['Content-Security-Policy'] = rctx.csp
30766
d7bf7d2bd5ab hgweb: support Content Security Policy
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30749
diff changeset
   321
37049
1cfef5693203 wireproto: support /api/* URL space for exposing APIs
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37023
diff changeset
   322
        # /api/* is reserved for various API implementations. Dispatch
37096
db114320df7e hgweb: don't responsd to api requests unless feature is enabled
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37049
diff changeset
   323
        # accordingly. But URL paths can conflict with subrepos and virtual
db114320df7e hgweb: don't responsd to api requests unless feature is enabled
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37049
diff changeset
   324
        # repos in hgwebdir. So until we have a workaround for this, only
db114320df7e hgweb: don't responsd to api requests unless feature is enabled
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37049
diff changeset
   325
        # expose the URLs if the feature is enabled.
db114320df7e hgweb: don't responsd to api requests unless feature is enabled
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37049
diff changeset
   326
        apienabled = rctx.repo.ui.configbool('experimental', 'web.apiserver')
db114320df7e hgweb: don't responsd to api requests unless feature is enabled
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37049
diff changeset
   327
        if apienabled and req.dispatchparts and req.dispatchparts[0] == b'api':
37049
1cfef5693203 wireproto: support /api/* URL space for exposing APIs
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37023
diff changeset
   328
            wireprotoserver.handlewsgiapirequest(rctx, req, res,
1cfef5693203 wireproto: support /api/* URL space for exposing APIs
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37023
diff changeset
   329
                                                 self.check_perm)
1cfef5693203 wireproto: support /api/* URL space for exposing APIs
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37023
diff changeset
   330
            return res.sendresponse()
1cfef5693203 wireproto: support /api/* URL space for exposing APIs
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37023
diff changeset
   331
36867
a88d68dc3ee8 hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36862
diff changeset
   332
        handled = wireprotoserver.handlewsgirequest(
36883
02bea04b4c54 hgweb: transition permissions hooks to modern request type (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36882
diff changeset
   333
            rctx, req, res, self.check_perm)
36820
158d4ecc03c8 wireprotoserver: move all wire protocol handling logic out of hgweb
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36819
diff changeset
   334
        if handled:
36867
a88d68dc3ee8 hgweb: create dedicated type for WSGI responses
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36862
diff changeset
   335
            return res.sendresponse()
36820
158d4ecc03c8 wireprotoserver: move all wire protocol handling logic out of hgweb
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36819
diff changeset
   336
36904
d0b0fedbfb53 hgweb: change how dispatch path is reported
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36893
diff changeset
   337
        # Old implementations of hgweb supported dispatching the request via
d0b0fedbfb53 hgweb: change how dispatch path is reported
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36893
diff changeset
   338
        # the initial query string parameter instead of using PATH_INFO.
d0b0fedbfb53 hgweb: change how dispatch path is reported
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36893
diff changeset
   339
        # If PATH_INFO is present (signaled by ``req.dispatchpath`` having
d0b0fedbfb53 hgweb: change how dispatch path is reported
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36893
diff changeset
   340
        # a value), we use it. Otherwise fall back to the query string.
d0b0fedbfb53 hgweb: change how dispatch path is reported
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36893
diff changeset
   341
        if req.dispatchpath is not None:
36819
cfb9ef24968c hgweb: use parsed request to construct query parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36818
diff changeset
   342
            query = req.dispatchpath
5596
20b07b68a865 hgweb: get rid of some nested functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5595
diff changeset
   343
        else:
36819
cfb9ef24968c hgweb: use parsed request to construct query parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36818
diff changeset
   344
            query = req.querystring.partition('&')[0].partition(';')[0]
5591
08887121a652 split out hgweb commands into a separate file, move some code around
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5579
diff changeset
   345
5596
20b07b68a865 hgweb: get rid of some nested functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5595
diff changeset
   346
        # translate user-visible url structure to internal structure
20b07b68a865 hgweb: get rid of some nested functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5595
diff changeset
   347
36819
cfb9ef24968c hgweb: use parsed request to construct query parameters
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36818
diff changeset
   348
        args = query.split('/', 2)
36871
3d60a22e27f5 hgweb: perform all parameter lookup via qsparams
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36870
diff changeset
   349
        if 'cmd' not in req.qsparams and args and args[0]:
5591
08887121a652 split out hgweb commands into a separate file, move some code around
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5579
diff changeset
   350
            cmd = args.pop(0)
08887121a652 split out hgweb commands into a separate file, move some code around
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5579
diff changeset
   351
            style = cmd.rfind('-')
08887121a652 split out hgweb commands into a separate file, move some code around
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5579
diff changeset
   352
            if style != -1:
36869
1a1972b1a1ff hgweb: use our new request object for "style" parameter
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36867
diff changeset
   353
                req.qsparams['style'] = cmd[:style]
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10264
diff changeset
   354
                cmd = cmd[style + 1:]
5596
20b07b68a865 hgweb: get rid of some nested functions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5595
diff changeset
   355
5591
08887121a652 split out hgweb commands into a separate file, move some code around
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5579
diff changeset
   356
            # avoid accepting e.g. style parameter as command
14953
6ee6ecf1ee89 hgweb_mod: use safehasattr instead of hasattr
Augie Fackler <durin42@gmail.com>
parents: 14913
diff changeset
   357
            if util.safehasattr(webcommands, cmd):
36870
4e06e8336634 hgweb: set variables in qsparams
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36869
diff changeset
   358
                req.qsparams['cmd'] = cmd
5591
08887121a652 split out hgweb commands into a separate file, move some code around
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5579
diff changeset
   359
7287
6e9fe4ff9c54 hgweb: handle subdirectories within static directory
Brendan Cully <brendan@kublai.com>
parents: 7180
diff changeset
   360
            if cmd == 'static':
36870
4e06e8336634 hgweb: set variables in qsparams
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36869
diff changeset
   361
                req.qsparams['file'] = '/'.join(args)
7287
6e9fe4ff9c54 hgweb: handle subdirectories within static directory
Brendan Cully <brendan@kublai.com>
parents: 7180
diff changeset
   362
            else:
6e9fe4ff9c54 hgweb: handle subdirectories within static directory
Brendan Cully <brendan@kublai.com>
parents: 7180
diff changeset
   363
                if args and args[0]:
25777
1c2a8db33b8f hgweb: allow symbolic revisions with forward slashes in urls
Anton Shestakov <av6@dwimlabs.net>
parents: 25720
diff changeset
   364
                    node = args.pop(0).replace('%2F', '/')
36870
4e06e8336634 hgweb: set variables in qsparams
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36869
diff changeset
   365
                    req.qsparams['node'] = node
7287
6e9fe4ff9c54 hgweb: handle subdirectories within static directory
Brendan Cully <brendan@kublai.com>
parents: 7180
diff changeset
   366
                if args:
36870
4e06e8336634 hgweb: set variables in qsparams
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36869
diff changeset
   367
                    if 'file' in req.qsparams:
4e06e8336634 hgweb: set variables in qsparams
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36869
diff changeset
   368
                        del req.qsparams['file']
4e06e8336634 hgweb: set variables in qsparams
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36869
diff changeset
   369
                    for a in args:
4e06e8336634 hgweb: set variables in qsparams
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36869
diff changeset
   370
                        req.qsparams.add('file', a)
5591
08887121a652 split out hgweb commands into a separate file, move some code around
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5579
diff changeset
   371
36822
f9078c6caeb6 hgweb: parse and store HTTP request headers
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36820
diff changeset
   372
            ua = req.headers.get('User-Agent', '')
9731
0e080d519d1b hgweb: treat rev as raw-rev if user agent is hg
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8860
diff changeset
   373
            if cmd == 'rev' and 'mercurial' in ua:
36869
1a1972b1a1ff hgweb: use our new request object for "style" parameter
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36867
diff changeset
   374
                req.qsparams['style'] = 'raw'
9731
0e080d519d1b hgweb: treat rev as raw-rev if user agent is hg
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8860
diff changeset
   375
7287
6e9fe4ff9c54 hgweb: handle subdirectories within static directory
Brendan Cully <brendan@kublai.com>
parents: 7180
diff changeset
   376
            if cmd == 'archive':
36871
3d60a22e27f5 hgweb: perform all parameter lookup via qsparams
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36870
diff changeset
   377
                fn = req.qsparams['node']
37512
aac97d043e6d hgweb: drop archivespecs from requestcontext
Yuya Nishihara <yuya@tcha.org>
parents: 37511
diff changeset
   378
                for type_, spec in webutil.archivespecs.iteritems():
5591
08887121a652 split out hgweb commands into a separate file, move some code around
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5579
diff changeset
   379
                    ext = spec[2]
08887121a652 split out hgweb commands into a separate file, move some code around
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5579
diff changeset
   380
                    if fn.endswith(ext):
36871
3d60a22e27f5 hgweb: perform all parameter lookup via qsparams
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36870
diff changeset
   381
                        req.qsparams['node'] = fn[:-len(ext)]
36870
4e06e8336634 hgweb: set variables in qsparams
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36869
diff changeset
   382
                        req.qsparams['type'] = type_
36023
cdc93fe1da77 wireprotoserver: move protocol parsing and dispatch out of hgweb
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36021
diff changeset
   383
        else:
36871
3d60a22e27f5 hgweb: perform all parameter lookup via qsparams
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36870
diff changeset
   384
            cmd = req.qsparams.get('cmd', '')
5591
08887121a652 split out hgweb commands into a separate file, move some code around
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5579
diff changeset
   385
6149
b023915aa1bc hgweb: separate protocol calls from interface calls (issue996)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6142
diff changeset
   386
        # process the web interface request
5599
3de66c2a9734 hgweb: split out templater definition
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5598
diff changeset
   387
3de66c2a9734 hgweb: split out templater definition
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5598
diff changeset
   388
        try:
36889
061635d4221c hgweb: add a sendtemplate() helper function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36887
diff changeset
   389
            rctx.tmpl = rctx.templater(req)
36992
de117f579431 templater: factor out helper that renders named template as string
Yuya Nishihara <yuya@tcha.org>
parents: 36926
diff changeset
   390
            ctype = rctx.tmpl.render('mimetype',
de117f579431 templater: factor out helper that renders named template as string
Yuya Nishihara <yuya@tcha.org>
parents: 36926
diff changeset
   391
                                     {'encoding': encoding.encoding})
6149
b023915aa1bc hgweb: separate protocol calls from interface calls (issue996)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6142
diff changeset
   392
7562
b663b5563de7 hgweb: allow static content when deny_read denies access
Mark Edgington <edgimar@gmail.com>
parents: 7396
diff changeset
   393
            # check read permissions non-static content
b663b5563de7 hgweb: allow static content when deny_read denies access
Mark Edgington <edgimar@gmail.com>
parents: 7396
diff changeset
   394
            if cmd != 'static':
36883
02bea04b4c54 hgweb: transition permissions hooks to modern request type (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36882
diff changeset
   395
                self.check_perm(rctx, req, None)
7336
2dc868712dcc hgweb: support for deny_read/allow_read options
Mark Edgington <edgimar@gmail.com>
parents: 7311
diff changeset
   396
6149
b023915aa1bc hgweb: separate protocol calls from interface calls (issue996)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6142
diff changeset
   397
            if cmd == '':
37023
16bbb15406c9 hgweb: evaluate the "default" value as template
Yuya Nishihara <yuya@tcha.org>
parents: 37022
diff changeset
   398
                req.qsparams['cmd'] = rctx.tmpl.render('default', {})
36871
3d60a22e27f5 hgweb: perform all parameter lookup via qsparams
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36870
diff changeset
   399
                cmd = req.qsparams['cmd']
5890
a0e20a5eba3c hgweb: fast path for sending raw files
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5889
diff changeset
   400
30766
d7bf7d2bd5ab hgweb: support Content Security Policy
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30749
diff changeset
   401
            # Don't enable caching if using a CSP nonce because then it wouldn't
d7bf7d2bd5ab hgweb: support Content Security Policy
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30749
diff changeset
   402
            # be a nonce.
34605
625202a44d88 configitems: register the 'web.cache' config
Boris Feld <boris.feld@octobus.net>
parents: 34602
diff changeset
   403
            if rctx.configbool('web', 'cache') and not rctx.nonce:
36875
7ad6a275316f hgweb: inline caching() and port to modern mechanisms
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36874
diff changeset
   404
                tag = 'W/"%d"' % self.mtime
7ad6a275316f hgweb: inline caching() and port to modern mechanisms
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36874
diff changeset
   405
                if req.headers.get('If-None-Match') == tag:
36884
ccb70a77f746 hgweb: refactor 304 handling code
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36883
diff changeset
   406
                    res.status = '304 Not Modified'
37827
11ee9bf24791 hgweb: discard Content-Type header for 304 responses (issue5844)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37746
diff changeset
   407
                    # Content-Type may be defined globally. It isn't valid on a
11ee9bf24791 hgweb: discard Content-Type header for 304 responses (issue5844)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37746
diff changeset
   408
                    # 304, so discard it.
11ee9bf24791 hgweb: discard Content-Type header for 304 responses (issue5844)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37746
diff changeset
   409
                    try:
11ee9bf24791 hgweb: discard Content-Type header for 304 responses (issue5844)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37746
diff changeset
   410
                        del res.headers[b'Content-Type']
11ee9bf24791 hgweb: discard Content-Type header for 304 responses (issue5844)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37746
diff changeset
   411
                    except KeyError:
11ee9bf24791 hgweb: discard Content-Type header for 304 responses (issue5844)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37746
diff changeset
   412
                        pass
36884
ccb70a77f746 hgweb: refactor 304 handling code
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36883
diff changeset
   413
                    # Response body not allowed on 304.
ccb70a77f746 hgweb: refactor 304 handling code
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36883
diff changeset
   414
                    res.setbodybytes('')
ccb70a77f746 hgweb: refactor 304 handling code
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36883
diff changeset
   415
                    return res.sendresponse()
36875
7ad6a275316f hgweb: inline caching() and port to modern mechanisms
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36874
diff changeset
   416
7ad6a275316f hgweb: inline caching() and port to modern mechanisms
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36874
diff changeset
   417
                res.headers['ETag'] = tag
7ad6a275316f hgweb: inline caching() and port to modern mechanisms
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36874
diff changeset
   418
6149
b023915aa1bc hgweb: separate protocol calls from interface calls (issue996)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6142
diff changeset
   419
            if cmd not in webcommands.__all__:
6368
2c370f08c486 hgweb: better error messages
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6249
diff changeset
   420
                msg = 'no such method: %s' % cmd
6149
b023915aa1bc hgweb: separate protocol calls from interface calls (issue996)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6142
diff changeset
   421
                raise ErrorResponse(HTTP_BAD_REQUEST, msg)
b023915aa1bc hgweb: separate protocol calls from interface calls (issue996)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6142
diff changeset
   422
            else:
36876
1f42d621f090 hgweb: support using new response object for web commands
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36875
diff changeset
   423
                # Set some globals appropriate for web handlers. Commands can
1f42d621f090 hgweb: support using new response object for web commands
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36875
diff changeset
   424
                # override easily enough.
1f42d621f090 hgweb: support using new response object for web commands
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36875
diff changeset
   425
                res.status = '200 Script output follows'
1f42d621f090 hgweb: support using new response object for web commands
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36875
diff changeset
   426
                res.headers['Content-Type'] = ctype
36893
4daa22071d5d hgweb: stop passing req and tmpl into @webcommand functions (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36889
diff changeset
   427
                return getattr(webcommands, cmd)(rctx)
5600
9d900f7282e6 hgweb: explicitly pass around the templater
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5599
diff changeset
   428
25660
328739ea70c3 global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25565
diff changeset
   429
        except (error.LookupError, error.RepoLookupError) as err:
36291
af0a19d8812b py3: get bytes-repr of network errors portably
Augie Fackler <augie@google.com>
parents: 36025
diff changeset
   430
            msg = pycompat.bytestr(err)
18855
50c922c1b514 hgweb: show correct error message for i18n environment
Takumi IINO <trot.thunder@gmail.com>
parents: 18522
diff changeset
   431
            if (util.safehasattr(err, 'name') and
50c922c1b514 hgweb: show correct error message for i18n environment
Takumi IINO <trot.thunder@gmail.com>
parents: 18522
diff changeset
   432
                not isinstance(err,  error.ManifestLookupError)):
6368
2c370f08c486 hgweb: better error messages
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6249
diff changeset
   433
                msg = 'revision not found: %s' % err.name
36885
9675147aec06 hgweb: send errors using new response API
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36884
diff changeset
   434
9675147aec06 hgweb: send errors using new response API
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36884
diff changeset
   435
            res.status = '404 Not Found'
9675147aec06 hgweb: send errors using new response API
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36884
diff changeset
   436
            res.headers['Content-Type'] = ctype
36889
061635d4221c hgweb: add a sendtemplate() helper function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36887
diff changeset
   437
            return rctx.sendtemplate('error', error=msg)
36885
9675147aec06 hgweb: send errors using new response API
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36884
diff changeset
   438
        except (error.RepoError, error.RevlogError) as e:
9675147aec06 hgweb: send errors using new response API
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36884
diff changeset
   439
            res.status = '500 Internal Server Error'
9675147aec06 hgweb: send errors using new response API
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36884
diff changeset
   440
            res.headers['Content-Type'] = ctype
36889
061635d4221c hgweb: add a sendtemplate() helper function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36887
diff changeset
   441
            return rctx.sendtemplate('error', error=pycompat.bytestr(e))
39487
17ca967e9fca hgweb: map Abort to 403 error to report inaccessible path for example
Yuya Nishihara <yuya@tcha.org>
parents: 38967
diff changeset
   442
        except error.Abort as e:
17ca967e9fca hgweb: map Abort to 403 error to report inaccessible path for example
Yuya Nishihara <yuya@tcha.org>
parents: 38967
diff changeset
   443
            res.status = '403 Forbidden'
17ca967e9fca hgweb: map Abort to 403 error to report inaccessible path for example
Yuya Nishihara <yuya@tcha.org>
parents: 38967
diff changeset
   444
            res.headers['Content-Type'] = ctype
17ca967e9fca hgweb: map Abort to 403 error to report inaccessible path for example
Yuya Nishihara <yuya@tcha.org>
parents: 38967
diff changeset
   445
            return rctx.sendtemplate('error', error=pycompat.bytestr(e))
36885
9675147aec06 hgweb: send errors using new response API
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36884
diff changeset
   446
        except ErrorResponse as e:
38316
519b46a8f4d2 hgweb: propagate http headers from ErrorResponse for web interface commands
Sune Foldager <cryo@cyanite.org>
parents: 37849
diff changeset
   447
            for k, v in e.headers:
519b46a8f4d2 hgweb: propagate http headers from ErrorResponse for web interface commands
Sune Foldager <cryo@cyanite.org>
parents: 37849
diff changeset
   448
                res.headers[k] = v
36885
9675147aec06 hgweb: send errors using new response API
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36884
diff changeset
   449
            res.status = statusmessage(e.code, pycompat.bytestr(e))
9675147aec06 hgweb: send errors using new response API
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36884
diff changeset
   450
            res.headers['Content-Type'] = ctype
36889
061635d4221c hgweb: add a sendtemplate() helper function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36887
diff changeset
   451
            return rctx.sendtemplate('error', error=pycompat.bytestr(e))
5599
3de66c2a9734 hgweb: split out templater definition
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5598
diff changeset
   452
26134
e0a6908f066f hgweb: establish class for holding per request context
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26133
diff changeset
   453
    def check_perm(self, rctx, req, op):
22200
b27c3beaaf30 cleanup: avoid local vars shadowing imports
Mads Kiilerich <madski@unity3d.com>
parents: 22087
diff changeset
   454
        for permhook in permhooks:
26134
e0a6908f066f hgweb: establish class for holding per request context
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26133
diff changeset
   455
            permhook(rctx, req, op)
26208
c87566ac3c49 hgweb: extract _getview to own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26207
diff changeset
   456
c87566ac3c49 hgweb: extract _getview to own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26207
diff changeset
   457
def getwebview(repo):
c87566ac3c49 hgweb: extract _getview to own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26207
diff changeset
   458
    """The 'web.view' config controls changeset filter to hgweb. Possible
c87566ac3c49 hgweb: extract _getview to own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26207
diff changeset
   459
    values are ``served``, ``visible`` and ``all``. Default is ``served``.
c87566ac3c49 hgweb: extract _getview to own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26207
diff changeset
   460
    The ``served`` filter only shows changesets that can be pulled from the
c87566ac3c49 hgweb: extract _getview to own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26207
diff changeset
   461
    hgweb instance.  The``visible`` filter includes secret changesets but
c87566ac3c49 hgweb: extract _getview to own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26207
diff changeset
   462
    still excludes "hidden" one.
c87566ac3c49 hgweb: extract _getview to own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26207
diff changeset
   463
c87566ac3c49 hgweb: extract _getview to own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26207
diff changeset
   464
    See the repoview module for details.
c87566ac3c49 hgweb: extract _getview to own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26207
diff changeset
   465
c87566ac3c49 hgweb: extract _getview to own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26207
diff changeset
   466
    The option has been around undocumented since Mercurial 2.5, but no
c87566ac3c49 hgweb: extract _getview to own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26207
diff changeset
   467
    user ever asked about it. So we better keep it undocumented for now."""
34584
bf2389b1f15e configitems: register the 'web.view' config
Boris Feld <boris.feld@octobus.net>
parents: 34515
diff changeset
   468
    # experimental config: web.view
bf2389b1f15e configitems: register the 'web.view' config
Boris Feld <boris.feld@octobus.net>
parents: 34515
diff changeset
   469
    viewconfig = repo.ui.config('web', 'view', untrusted=True)
26208
c87566ac3c49 hgweb: extract _getview to own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26207
diff changeset
   470
    if viewconfig == 'all':
c87566ac3c49 hgweb: extract _getview to own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26207
diff changeset
   471
        return repo.unfiltered()
c87566ac3c49 hgweb: extract _getview to own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26207
diff changeset
   472
    elif viewconfig in repoview.filtertable:
c87566ac3c49 hgweb: extract _getview to own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26207
diff changeset
   473
        return repo.filtered(viewconfig)
c87566ac3c49 hgweb: extract _getview to own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26207
diff changeset
   474
    else:
c87566ac3c49 hgweb: extract _getview to own function
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26207
diff changeset
   475
        return repo.filtered('served')