aboutsummaryrefslogtreecommitdiffstats
path: root/netlib/http/http1/assemble.py
blob: e74732d2e35be4972a62fbeb09120de9816dc73a (plain)
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
from __future__ import absolute_import, print_function, division

import netlib.http.url
from netlib import exceptions


def assemble_request(request):
    if request.data.content is None:
        raise exceptions.HttpException("Cannot assemble flow with missing content")
    head = assemble_request_head(request)
    body = b"".join(assemble_body(request.data.headers, [request.data.content]))
    return head + body


def assemble_request_head(request):
    first_line = _assemble_request_line(request.data)
    headers = _assemble_request_headers(request.data)
    return b"%s\r\n%s\r\n" % (first_line, headers)


def assemble_response(response):
    if response.data.content is None:
        raise exceptions.HttpException("Cannot assemble flow with missing content")
    head = assemble_response_head(response)
    body = b"".join(assemble_body(response.data.headers, [response.data.content]))
    return head + body


def assemble_response_head(response):
    first_line = _assemble_response_line(response.data)
    headers = _assemble_response_headers(response.data)
    return b"%s\r\n%s\r\n" % (first_line, headers)


def assemble_body(headers, body_chunks):
    if "chunked" in headers.get("transfer-encoding", "").lower():
        for chunk in body_chunks:
            if chunk:
                yield b"%x\r\n%s\r\n" % (len(chunk), chunk)
        yield b"0\r\n\r\n"
    else:
        for chunk in body_chunks:
            yield chunk


def _assemble_request_line(request_data):
    """
    Args:
        request_data (netlib.http.request.RequestData)
    """
    form = request_data.first_line_format
    if form == "relative":
        return b"%s %s %s" % (
            request_data.method,
            request_data.path,
            request_data.http_version
        )
    elif form == "authority":
        return b"%s %s:%d %s" % (
            request_data.method,
            request_data.host,
            request_data.port,
            request_data.http_version
        )
    elif form == "absolute":
        return b"%s %s://%s:%d%s %s" % (
            request_data.method,
            request_data.scheme,
            request_data.host,
            request_data.port,
            request_data.path,
            request_data.http_version
        )
    else:
        raise RuntimeError("Invalid request form")


def _assemble_request_headers(request_data):
    """
    Args:
        request_data (netlib.http.request.RequestData)
    """
    headers = request_data.headers.copy()
    if "host" not in headers and request_data.scheme and request_data.host and request_data.port:
        headers["host"] = netlib.http.url.hostport(
            request_data.scheme,
            request_data.host,
            request_data.port
        )
    return bytes(headers)


def _assemble_response_line(response_data):
    return b"%s %d %s" % (
        response_data.http_version,
        response_data.status_code,
        response_data.reason,
    )


def _assemble_response_headers(response):
    return bytes(response.headers)