aboutsummaryrefslogtreecommitdiffstats
path: root/libmproxy/console/contentview.py
blob: c6e43a27f9eec999a914065a4c2050ba0def781f (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import urwid
import common
from .. import utils, encoding, flow

VIEW_CUTOFF = 1024*100

VIEW_CONTENT_RAW = 0
VIEW_CONTENT_HEX = 1
VIEW_CONTENT_PRETTY = 2

CONTENT_VIEWS = {
    VIEW_CONTENT_RAW: "raw",
    VIEW_CONTENT_HEX: "hex",
    VIEW_CONTENT_PRETTY: "pretty"
}

VIEW_CONTENT_PRETTY_TYPE_AUTO = 0
VIEW_CONTENT_PRETTY_TYPE_JSON = 1
VIEW_CONTENT_PRETTY_TYPE_XML = 2
VIEW_CONTENT_PRETTY_TYPE_URLENCODED = 3

CONTENT_PRETTY_NAMES = {
    VIEW_CONTENT_PRETTY_TYPE_JSON: "JSON",
    VIEW_CONTENT_PRETTY_TYPE_XML: "XML",
    VIEW_CONTENT_PRETTY_TYPE_URLENCODED: "URL-encoded"
}

CONTENT_TYPES_MAP = {
    "text/html": VIEW_CONTENT_PRETTY_TYPE_XML,
    "application/json": VIEW_CONTENT_PRETTY_TYPE_JSON,
    "text/xml": VIEW_CONTENT_PRETTY_TYPE_XML,
    "multipart/form-data": VIEW_CONTENT_PRETTY_TYPE_URLENCODED
}

def trailer(clen, txt):
    rem = clen - VIEW_CUTOFF
    if rem > 0:
        txt.append(urwid.Text(""))
        txt.append(
            urwid.Text(
                [
                    ("highlight", "... %s of data not shown"%utils.pretty_size(rem))
                ]
            )
        )


def view_raw(hdrs, content):
    txt = []
    for i in utils.cleanBin(content[:VIEW_CUTOFF]).splitlines():
        txt.append(
            urwid.Text(("text", i))
        )
    trailer(len(content), txt)
    return "Raw", txt


def view_hex(hdrs, content):
    txt = []
    for offset, hexa, s in utils.hexdump(content[:VIEW_CUTOFF]):
        txt.append(urwid.Text([
            ("offset", offset),
            " ",
            ("text", hexa),
            "   ",
            ("text", s),
        ]))
    trailer(len(content), txt)
    return "HEX", txt


def view_xmlish(hdrs, content):
    txt = []
    for i in utils.pretty_xmlish(content[:VIEW_CUTOFF]):
        txt.append(
            urwid.Text(("text", i)),
        )
    trailer(len(content), txt)
    return "XML-like data", txt


def view_json(hdrs, content):
    lines = utils.pretty_json(content)
    if lines:
        txt = []
        sofar = 0
        for i in lines:
            sofar += len(i)
            txt.append(
                urwid.Text(("text", i)),
            )
            if sofar > VIEW_CUTOFF:
                break
        trailer(sum(len(i) for i in lines), txt)
        return "JSON", txt


# FIXME
def view_formdata(content, boundary):
    rx = re.compile(r'\bname="([^"]+)"')
    keys = []
    vals = []

    for i in content.split("--" + boundary):
        parts = i.splitlines()
        if len(parts) > 1 and parts[0][0:2] != "--":
            match = rx.search(parts[1])
            if match:
                keys.append(match.group(1) + ":")
                vals.append(utils.cleanBin(
                    "\n".join(parts[3+parts[2:].index(""):])
                ))
    r = [
        urwid.Text(("highlight", "Form data:\n")),
    ]
    r.extend(common.format_keyvals(
        zip(keys, vals),
        key = "header",
        val = "text"
    ))
    return r


def view_urlencoded(hdrs, content):
    lines = utils.urldecode(content)
    if lines:
        body = common.format_keyvals(
                    [(k+":", v) for (k, v) in lines],
                    key = "header",
                    val = "text"
               )
        return "URLEncoded form", body


PRETTY_FUNCTION_MAP = {
    VIEW_CONTENT_PRETTY_TYPE_XML: view_xmlish,
    VIEW_CONTENT_PRETTY_TYPE_JSON: view_json,
    VIEW_CONTENT_PRETTY_TYPE_URLENCODED: view_urlencoded
}

def get_view_func(viewmode, pretty_type, hdrs, content):
    """
        Returns a function object.
    """
    if viewmode == VIEW_CONTENT_HEX:
        return view_hex
    elif viewmode == VIEW_CONTENT_RAW:
        return view_raw
    else:
        if pretty_type == VIEW_CONTENT_PRETTY_TYPE_AUTO:
            ctype = hdrs.get("content-type")
            if ctype:
                ctype = ctype[0]
            ct = utils.parse_content_type(ctype) if ctype else None
            if ct:
                pretty_type = CONTENT_TYPES_MAP.get("%s/%s"%(ct[0], ct[1]))
            if not pretty_type and utils.isXML(content):
                pretty_type = VIEW_CONTENT_PRETTY_TYPE_XML
        return PRETTY_FUNCTION_MAP.get(pretty_type, view_raw)


def get_content_view(viewmode, pretty_type, hdrItems, content):
    """
        Returns a (msg, body) tuple.
    """
    msg = []

    hdrs = flow.ODictCaseless([list(i) for i in hdrItems])

    enc = hdrs.get("content-encoding")
    if enc and enc[0] != "identity":
        decoded = encoding.decode(enc[0], content)
        if decoded:
            content = decoded
            msg.append("[decoded %s]"%enc[0])

    if viewmode == VIEW_CONTENT_PRETTY and pretty_type != VIEW_CONTENT_PRETTY_TYPE_AUTO:
        msg.append("[forced to %s]"%(CONTENT_PRETTY_NAMES[pretty_type]))
    func = get_view_func(viewmode, pretty_type, hdrs, content)

    ret = func(hdrs, content)
    if not ret:
        ret = view_raw(hdrs, content)
    msg.append(ret[0])
    return " ".join(msg), ret[1]