author | Augie Fackler <augie@google.com> |
Sat, 04 May 2019 11:25:40 -0400 | |
changeset 43078 | 88eba7103660 |
parent 43077 | 687b865b95ad |
child 43089 | c59eb1560c44 |
permissions | -rw-r--r-- |
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 | 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 |
|
37747
2d5b5bcc3b9f
wsgicgi: un-do some prior porting work that is now wrong
Augie Fackler <augie@google.com>
parents:
37120
diff
changeset
|
13 |
import os |
2d5b5bcc3b9f
wsgicgi: un-do some prior porting work that is now wrong
Augie Fackler <augie@google.com>
parents:
37120
diff
changeset
|
14 |
|
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
37747
diff
changeset
|
15 |
from .. import pycompat |
27046
37fcfe52c68c
hgweb: use absolute_import
Yuya Nishihara <yuya@tcha.org>
parents:
18552
diff
changeset
|
16 |
|
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
37747
diff
changeset
|
17 |
from ..utils import procutil |
37119
d4a2e0d5d042
procutil: bulk-replace util.std* to point to new module
Yuya Nishihara <yuya@tcha.org>
parents:
34512
diff
changeset
|
18 |
|
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
37747
diff
changeset
|
19 |
from . import common |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
37747
diff
changeset
|
20 |
|
2506
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff
changeset
|
21 |
|
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff
changeset
|
22 |
def launch(application): |
37120
a8a902d7176e
procutil: bulk-replace function calls to point to new module
Yuya Nishihara <yuya@tcha.org>
parents:
37119
diff
changeset
|
23 |
procutil.setbinary(procutil.stdin) |
a8a902d7176e
procutil: bulk-replace function calls to point to new module
Yuya Nishihara <yuya@tcha.org>
parents:
37119
diff
changeset
|
24 |
procutil.setbinary(procutil.stdout) |
2506
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff
changeset
|
25 |
|
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
37747
diff
changeset
|
26 |
environ = dict(os.environ.iteritems()) # re-exports |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
27 |
environ.setdefault(r'PATH_INFO', b'') |
34512
482d6f6dba91
hgweb: when constructing or adding to a wsgi environ dict, use native strs
Augie Fackler <augie@google.com>
parents:
34486
diff
changeset
|
28 |
if environ.get(r'SERVER_SOFTWARE', r'').startswith(r'Microsoft-IIS'): |
17424
e7cfe3587ea4
fix trivial spelling errors
Mads Kiilerich <mads@kiilerich.com>
parents:
14956
diff
changeset
|
29 |
# IIS includes script_name in PATH_INFO |
34512
482d6f6dba91
hgweb: when constructing or adding to a wsgi environ dict, use native strs
Augie Fackler <augie@google.com>
parents:
34486
diff
changeset
|
30 |
scriptname = environ[r'SCRIPT_NAME'] |
482d6f6dba91
hgweb: when constructing or adding to a wsgi environ dict, use native strs
Augie Fackler <augie@google.com>
parents:
34486
diff
changeset
|
31 |
if environ[r'PATH_INFO'].startswith(scriptname): |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
37747
diff
changeset
|
32 |
environ[r'PATH_INFO'] = environ[r'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
|
33 |
|
37119
d4a2e0d5d042
procutil: bulk-replace util.std* to point to new module
Yuya Nishihara <yuya@tcha.org>
parents:
34512
diff
changeset
|
34 |
stdin = procutil.stdin |
34512
482d6f6dba91
hgweb: when constructing or adding to a wsgi environ dict, use native strs
Augie Fackler <augie@google.com>
parents:
34486
diff
changeset
|
35 |
if environ.get(r'HTTP_EXPECT', r'').lower() == r'100-continue': |
37119
d4a2e0d5d042
procutil: bulk-replace util.std* to point to new module
Yuya Nishihara <yuya@tcha.org>
parents:
34512
diff
changeset
|
36 |
stdin = common.continuereader(stdin, procutil.stdout.write) |
13570
617a87cb7eb2
hgweb: add support for 100-continue as recommended by PEP 333.
Augie Fackler <durin42@gmail.com>
parents:
10753
diff
changeset
|
37 |
|
34512
482d6f6dba91
hgweb: when constructing or adding to a wsgi environ dict, use native strs
Augie Fackler <augie@google.com>
parents:
34486
diff
changeset
|
38 |
environ[r'wsgi.input'] = stdin |
37119
d4a2e0d5d042
procutil: bulk-replace util.std* to point to new module
Yuya Nishihara <yuya@tcha.org>
parents:
34512
diff
changeset
|
39 |
environ[r'wsgi.errors'] = procutil.stderr |
34512
482d6f6dba91
hgweb: when constructing or adding to a wsgi environ dict, use native strs
Augie Fackler <augie@google.com>
parents:
34486
diff
changeset
|
40 |
environ[r'wsgi.version'] = (1, 0) |
482d6f6dba91
hgweb: when constructing or adding to a wsgi environ dict, use native strs
Augie Fackler <augie@google.com>
parents:
34486
diff
changeset
|
41 |
environ[r'wsgi.multithread'] = False |
482d6f6dba91
hgweb: when constructing or adding to a wsgi environ dict, use native strs
Augie Fackler <augie@google.com>
parents:
34486
diff
changeset
|
42 |
environ[r'wsgi.multiprocess'] = True |
482d6f6dba91
hgweb: when constructing or adding to a wsgi environ dict, use native strs
Augie Fackler <augie@google.com>
parents:
34486
diff
changeset
|
43 |
environ[r'wsgi.run_once'] = True |
2506
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff
changeset
|
44 |
|
34512
482d6f6dba91
hgweb: when constructing or adding to a wsgi environ dict, use native strs
Augie Fackler <augie@google.com>
parents:
34486
diff
changeset
|
45 |
if environ.get(r'HTTPS', r'off').lower() in (r'on', r'1', r'yes'): |
482d6f6dba91
hgweb: when constructing or adding to a wsgi environ dict, use native strs
Augie Fackler <augie@google.com>
parents:
34486
diff
changeset
|
46 |
environ[r'wsgi.url_scheme'] = r'https' |
2506
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff
changeset
|
47 |
else: |
34512
482d6f6dba91
hgweb: when constructing or adding to a wsgi environ dict, use native strs
Augie Fackler <augie@google.com>
parents:
34486
diff
changeset
|
48 |
environ[r'wsgi.url_scheme'] = r'http' |
2506
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff
changeset
|
49 |
|
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff
changeset
|
50 |
headers_set = [] |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff
changeset
|
51 |
headers_sent = [] |
37119
d4a2e0d5d042
procutil: bulk-replace util.std* to point to new module
Yuya Nishihara <yuya@tcha.org>
parents:
34512
diff
changeset
|
52 |
out = procutil.stdout |
2506
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff
changeset
|
53 |
|
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff
changeset
|
54 |
def write(data): |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff
changeset
|
55 |
if not headers_set: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
56 |
raise AssertionError(b"write() before start_response()") |
2506
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff
changeset
|
57 |
|
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff
changeset
|
58 |
elif not headers_sent: |
3673
eb0b4a2d70a9
white space and line break cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents:
2558
diff
changeset
|
59 |
# 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
|
60 |
status, response_headers = headers_sent[:] = headers_set |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
61 |
out.write(b'Status: %s\r\n' % pycompat.bytesurl(status)) |
37747
2d5b5bcc3b9f
wsgicgi: un-do some prior porting work that is now wrong
Augie Fackler <augie@google.com>
parents:
37120
diff
changeset
|
62 |
for hk, hv in response_headers: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
37747
diff
changeset
|
63 |
out.write( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
64 |
b'%s: %s\r\n' |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
37747
diff
changeset
|
65 |
% (pycompat.bytesurl(hk), pycompat.bytesurl(hv)) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
37747
diff
changeset
|
66 |
) |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
67 |
out.write(b'\r\n') |
2506
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff
changeset
|
68 |
|
2558
1120302009d7
hgweb: fix unbundle.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
2506
diff
changeset
|
69 |
out.write(data) |
1120302009d7
hgweb: fix unbundle.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
2506
diff
changeset
|
70 |
out.flush() |
2506
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff
changeset
|
71 |
|
3673
eb0b4a2d70a9
white space and line break cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents:
2558
diff
changeset
|
72 |
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
|
73 |
if exc_info: |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff
changeset
|
74 |
try: |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff
changeset
|
75 |
if headers_sent: |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff
changeset
|
76 |
# 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
|
77 |
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
|
78 |
finally: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
37747
diff
changeset
|
79 |
exc_info = None # avoid dangling circular ref |
2506
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff
changeset
|
80 |
elif headers_set: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
81 |
raise AssertionError(b"Headers already set!") |
2506
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff
changeset
|
82 |
|
3673
eb0b4a2d70a9
white space and line break cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents:
2558
diff
changeset
|
83 |
headers_set[:] = [status, response_headers] |
2506
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff
changeset
|
84 |
return write |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff
changeset
|
85 |
|
6922
1ec2d227a521
hgweb: fix WSGI iterators handling in CGI adapter (issue1254)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
5580
diff
changeset
|
86 |
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
|
87 |
try: |
a1cb8ca051c0
wsgicgi: call close() on iterable to avoid resource leaks
Konstantin Zemlyak <zart@zartsoft.ru>
parents:
10339
diff
changeset
|
88 |
for chunk in content: |
a1cb8ca051c0
wsgicgi: call close() on iterable to avoid resource leaks
Konstantin Zemlyak <zart@zartsoft.ru>
parents:
10339
diff
changeset
|
89 |
write(chunk) |
18552
e8efcc8ff5c0
hgweb.cgi: fix internal WSGI emulation (issue3804)
Mads Kiilerich <madski@unity3d.com>
parents:
17424
diff
changeset
|
90 |
if not headers_sent: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
91 |
write(b'') # 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
|
92 |
finally: |
34486
a57c938e7ac8
style: never use a space before a colon or comma
Alex Gaynor <agaynor@mozilla.com>
parents:
30636
diff
changeset
|
93 |
getattr(content, 'close', lambda: None)() |