xref: /unit/test/python/ctx_iter_atexit/wsgi.py (revision 1848:4bd548074e2c)
1602Szelenkov@nginx.comimport atexit
2602Szelenkov@nginx.com
3*1848Szelenkov@nginx.com
4602Szelenkov@nginx.comclass application:
5602Szelenkov@nginx.com    def __init__(self, environ, start_response):
6602Szelenkov@nginx.com        self.environ = environ
7602Szelenkov@nginx.com        self.start = start_response
8602Szelenkov@nginx.com
9602Szelenkov@nginx.com    def __iter__(self):
10602Szelenkov@nginx.com        atexit.register(self._atexit)
11602Szelenkov@nginx.com
12602Szelenkov@nginx.com        content_length = int(self.environ.get('CONTENT_LENGTH', 0))
13602Szelenkov@nginx.com        body = bytes(self.environ['wsgi.input'].read(content_length))
14602Szelenkov@nginx.com
15*1848Szelenkov@nginx.com        self.start(
16*1848Szelenkov@nginx.com            '200',
17*1848Szelenkov@nginx.com            [
18*1848Szelenkov@nginx.com                ('Content-Type', self.environ.get('CONTENT_TYPE')),
19*1848Szelenkov@nginx.com                ('Content-Length', str(len(body))),
20*1848Szelenkov@nginx.com            ],
21*1848Szelenkov@nginx.com        )
22602Szelenkov@nginx.com        yield body
23602Szelenkov@nginx.com
24602Szelenkov@nginx.com    def _atexit(self):
25*1848Szelenkov@nginx.com        self.start('200', [('Content-Length', '0')])
26