summaryrefslogtreecommitdiffhomepage
path: root/test/python/variables/asgi.py
blob: dd1cca7241de2264676c5a0a2d5c0c883d8c8713 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
async def application(scope, receive, send):
    assert scope['type'] == 'http'

    body = b''
    while True:
        m = await receive()
        body += m.get('body', b'')
        if not m.get('more_body', False):
            break

    headers = scope.get('headers', [])

    def get_header(n):
        res = []
        for h in headers:
            if h[0] == n:
                res.append(h[1])
        return b', '.join(res)

    await send({
        'type': 'http.response.start',
        'status': 200,
        'headers': [
            (b'content-type', get_header(b'content-type')),
            (b'content-length', str(len(body)).encode()),
            (b'request-method', scope['method'].encode()),
            (b'request-uri', scope['path'].encode()),
            (b'http-host', get_header(b'host')),
            (b'http-version', scope['http_version'].encode()),
            (b'asgi-version', scope['asgi']['version'].encode()),
            (b'asgi-spec-version', scope['asgi']['spec_version'].encode()),
            (b'scheme', scope['scheme'].encode()),
            (b'custom-header', get_header(b'custom-header')),
        ]
    })

    await send({
        'type': 'http.response.body',
        'body': body,
    })