mercurial/hgweb/wsgicgi.py
author Danek Duvall <danek.duvall@oracle.com>
Fri, 27 May 2016 15:20:03 -0700
branchstable
changeset 29257 a9764ab80e11
parent 27046 37fcfe52c68c
child 30473 39d13b8c101d
permissions -rw-r--r--
tests-subrepo-git: emit a different "pwned" message based on the test Having a single "pwned" message which may or may not be emitted during the tests for CVE-2016-3068 leads to extra confusion. Allow each test to emit a more detailed message based on what the expectations are. In both cases, we expect a version of git which has had the vulnerability plugged, as well as a version of mercurial which also knows about GIT_ALLOW_PROTOCOL. For the first test, we make sure GIT_ALLOW_PROTOCOL is unset, meaning that the ext-protocol subrepo should be ignored; if it isn't, there's either a problem with mercurial or the installed copy of git. For the second test, we explicitly allow ext-protocol subrepos, which means that the subrepo will be accessed and a message emitted confirming that this was, in fact, our intention.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2506
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
     1
# hgweb/wsgicgi.py - CGI->WSGI translator
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
     2
#
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
     3
# Copyright 2006 Eric Hopper <hopper@omnifarious.org>
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
     4
#
8225
46293a0c7e9f updated license to be explicit about GPL version 2
Martin Geisler <mg@lazybytes.net>
parents: 7622
diff changeset
     5
# 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: 10201
diff changeset
     6
# GNU General Public License version 2 or any later version.
2506
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
     7
#
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
     8
# This was originally copied from the public domain code at
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
     9
# http://www.python.org/dev/peps/pep-0333/#the-server-gateway-side
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
    10
27046
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 18552
diff changeset
    11
from __future__ import absolute_import
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 18552
diff changeset
    12
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 18552
diff changeset
    13
import os
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 18552
diff changeset
    14
import sys
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 18552
diff changeset
    15
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 18552
diff changeset
    16
from .. import (
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 18552
diff changeset
    17
    util,
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 18552
diff changeset
    18
)
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 18552
diff changeset
    19
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 18552
diff changeset
    20
from . import (
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 18552
diff changeset
    21
    common,
37fcfe52c68c hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents: 18552
diff changeset
    22
)
2506
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
    23
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
    24
def launch(application):
14233
659f34b833b9 rename util.set_binary to setbinary
Adrian Buehlmann <adrian@cadifra.com>
parents: 13570
diff changeset
    25
    util.setbinary(sys.stdin)
659f34b833b9 rename util.set_binary to setbinary
Adrian Buehlmann <adrian@cadifra.com>
parents: 13570
diff changeset
    26
    util.setbinary(sys.stdout)
2506
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
    27
7622
4dd7b28003d2 use dict.iteritems() rather than dict.items()
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7406
diff changeset
    28
    environ = dict(os.environ.iteritems())
5580
f429e0e067a8 Fix style nit and add some comments to tests.
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5579
diff changeset
    29
    environ.setdefault('PATH_INFO', '')
10201
d2847c4523e8 hgweb: improve IIS PATH_INFO fix ee8af8a4d905 (issue1580)
Patrick Mezard <pmezard@gmail.com>
parents: 8225
diff changeset
    30
    if environ.get('SERVER_SOFTWARE', '').startswith('Microsoft-IIS'):
17424
e7cfe3587ea4 fix trivial spelling errors
Mads Kiilerich <mads@kiilerich.com>
parents: 14956
diff changeset
    31
        # IIS includes script_name in PATH_INFO
10201
d2847c4523e8 hgweb: improve IIS PATH_INFO fix ee8af8a4d905 (issue1580)
Patrick Mezard <pmezard@gmail.com>
parents: 8225
diff changeset
    32
        scriptname = environ['SCRIPT_NAME']
d2847c4523e8 hgweb: improve IIS PATH_INFO fix ee8af8a4d905 (issue1580)
Patrick Mezard <pmezard@gmail.com>
parents: 8225
diff changeset
    33
        if environ['PATH_INFO'].startswith(scriptname):
d2847c4523e8 hgweb: improve IIS PATH_INFO fix ee8af8a4d905 (issue1580)
Patrick Mezard <pmezard@gmail.com>
parents: 8225
diff changeset
    34
            environ['PATH_INFO'] = environ['PATH_INFO'][len(scriptname):]
7406
ee8af8a4d905 hgweb: support broken IIS 5 behavior with .cgi in PATH_INFO
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7008
diff changeset
    35
13570
617a87cb7eb2 hgweb: add support for 100-continue as recommended by PEP 333.
Augie Fackler <durin42@gmail.com>
parents: 10753
diff changeset
    36
    stdin = sys.stdin
617a87cb7eb2 hgweb: add support for 100-continue as recommended by PEP 333.
Augie Fackler <durin42@gmail.com>
parents: 10753
diff changeset
    37
    if environ.get('HTTP_EXPECT', '').lower() == '100-continue':
617a87cb7eb2 hgweb: add support for 100-continue as recommended by PEP 333.
Augie Fackler <durin42@gmail.com>
parents: 10753
diff changeset
    38
        stdin = common.continuereader(stdin, sys.stdout.write)
617a87cb7eb2 hgweb: add support for 100-continue as recommended by PEP 333.
Augie Fackler <durin42@gmail.com>
parents: 10753
diff changeset
    39
617a87cb7eb2 hgweb: add support for 100-continue as recommended by PEP 333.
Augie Fackler <durin42@gmail.com>
parents: 10753
diff changeset
    40
    environ['wsgi.input'] = stdin
3673
eb0b4a2d70a9 white space and line break cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2558
diff changeset
    41
    environ['wsgi.errors'] = sys.stderr
eb0b4a2d70a9 white space and line break cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2558
diff changeset
    42
    environ['wsgi.version'] = (1, 0)
eb0b4a2d70a9 white space and line break cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2558
diff changeset
    43
    environ['wsgi.multithread'] = False
2506
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
    44
    environ['wsgi.multiprocess'] = True
3673
eb0b4a2d70a9 white space and line break cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2558
diff changeset
    45
    environ['wsgi.run_once'] = True
2506
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
    46
10339
23e608f42f2c fix spaces/identation issues
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10263
diff changeset
    47
    if environ.get('HTTPS', 'off').lower() in ('on', '1', 'yes'):
2506
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
    48
        environ['wsgi.url_scheme'] = 'https'
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
    49
    else:
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
    50
        environ['wsgi.url_scheme'] = 'http'
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
    51
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
    52
    headers_set = []
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
    53
    headers_sent = []
2558
1120302009d7 hgweb: fix unbundle.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2506
diff changeset
    54
    out = sys.stdout
2506
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
    55
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
    56
    def write(data):
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
    57
        if not headers_set:
3673
eb0b4a2d70a9 white space and line break cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2558
diff changeset
    58
            raise AssertionError("write() before start_response()")
2506
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
    59
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
    60
        elif not headers_sent:
3673
eb0b4a2d70a9 white space and line break cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2558
diff changeset
    61
            # Before the first output, send the stored headers
eb0b4a2d70a9 white space and line break cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2558
diff changeset
    62
            status, response_headers = headers_sent[:] = headers_set
eb0b4a2d70a9 white space and line break cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2558
diff changeset
    63
            out.write('Status: %s\r\n' % status)
eb0b4a2d70a9 white space and line break cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2558
diff changeset
    64
            for header in response_headers:
eb0b4a2d70a9 white space and line break cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2558
diff changeset
    65
                out.write('%s: %s\r\n' % header)
eb0b4a2d70a9 white space and line break cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2558
diff changeset
    66
            out.write('\r\n')
2506
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
    67
2558
1120302009d7 hgweb: fix unbundle.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2506
diff changeset
    68
        out.write(data)
1120302009d7 hgweb: fix unbundle.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2506
diff changeset
    69
        out.flush()
2506
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
    70
3673
eb0b4a2d70a9 white space and line break cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2558
diff changeset
    71
    def start_response(status, response_headers, exc_info=None):
2506
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
    72
        if exc_info:
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
    73
            try:
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
    74
                if headers_sent:
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
    75
                    # Re-raise original exception if headers sent
7008
8fee8ff13d37 use Exception(args)-style raising consistently (py3k compatibility)
Peter Ruibal <peter.ruibal@intel.com>
parents: 6922
diff changeset
    76
                    raise exc_info[0](exc_info[1], exc_info[2])
2506
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
    77
            finally:
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
    78
                exc_info = None     # avoid dangling circular ref
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
    79
        elif headers_set:
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
    80
            raise AssertionError("Headers already set!")
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
    81
3673
eb0b4a2d70a9 white space and line break cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2558
diff changeset
    82
        headers_set[:] = [status, response_headers]
2506
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
    83
        return write
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
    84
6922
1ec2d227a521 hgweb: fix WSGI iterators handling in CGI adapter (issue1254)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 5580
diff changeset
    85
    content = application(environ, start_response)
10753
a1cb8ca051c0 wsgicgi: call close() on iterable to avoid resource leaks
Konstantin Zemlyak <zart@zartsoft.ru>
parents: 10339
diff changeset
    86
    try:
a1cb8ca051c0 wsgicgi: call close() on iterable to avoid resource leaks
Konstantin Zemlyak <zart@zartsoft.ru>
parents: 10339
diff changeset
    87
        for chunk in content:
a1cb8ca051c0 wsgicgi: call close() on iterable to avoid resource leaks
Konstantin Zemlyak <zart@zartsoft.ru>
parents: 10339
diff changeset
    88
            write(chunk)
18552
e8efcc8ff5c0 hgweb.cgi: fix internal WSGI emulation (issue3804)
Mads Kiilerich <madski@unity3d.com>
parents: 17424
diff changeset
    89
        if not headers_sent:
e8efcc8ff5c0 hgweb.cgi: fix internal WSGI emulation (issue3804)
Mads Kiilerich <madski@unity3d.com>
parents: 17424
diff changeset
    90
            write('')   # send headers now if body was empty
10753
a1cb8ca051c0 wsgicgi: call close() on iterable to avoid resource leaks
Konstantin Zemlyak <zart@zartsoft.ru>
parents: 10339
diff changeset
    91
    finally:
14956
1b3f5f603aef wsgicgi: use getattr instead of hasattr
Augie Fackler <durin42@gmail.com>
parents: 14233
diff changeset
    92
        getattr(content, 'close', lambda : None)()