aboutsummaryrefslogtreecommitdiffstats
path: root/pathod/language/__init__.py
blob: 584e3f80371686a785d62e28fa46087f7b1cef27 (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
import itertools
import time

import pyparsing as pp

from . import http, http2, websockets, writer, exceptions

from .exceptions import RenderError, FileAccessDenied, ParseException
from .base import Settings

__all__ = [
    "RenderError", "FileAccessDenied", "ParseException",
    "Settings",
]


def expand(msg):
    times = getattr(msg, "times", None)
    if times:
        for j_ in range(int(times.value)):
            yield msg.strike_token("times")
    else:
        yield msg


def parse_pathod(s, use_http2=False):
    """
        May raise ParseException
    """
    try:
        s.encode("ascii")
    except UnicodeError:
        raise exceptions.ParseException("Spec must be valid ASCII.", 0, 0)
    try:
        if use_http2:
            expressions = [
                # http2.Frame.expr(),
                http2.Response.expr(),
            ]
        else:
            expressions = [
                websockets.WebsocketFrame.expr(),
                http.Response.expr(),
            ]
        reqs = pp.Or(expressions).parseString(s, parseAll=True)
    except pp.ParseException as v:
        raise exceptions.ParseException(v.msg, v.line, v.col)
    return itertools.chain(*[expand(i) for i in reqs])


def parse_pathoc(s, use_http2=False):
    try:
        s.encode("ascii")
    except UnicodeError:
        raise exceptions.ParseException("Spec must be valid ASCII.", 0, 0)
    try:
        if use_http2:
            expressions = [
                # http2.Frame.expr(),
                http2.Request.expr(),
            ]
        else:
            expressions = [
                websockets.WebsocketClientFrame.expr(),
                http.Request.expr(),
            ]
        reqs = pp.OneOrMore(pp.Or(expressions)).parseString(s, parseAll=True)
    except pp.ParseException as v:
        raise exceptions.ParseException(v.msg, v.line, v.col)
    return itertools.chain(*[expand(i) for i in reqs])


def parse_websocket_frame(s):
    """
        May raise ParseException
    """
    try:
        reqs = pp.OneOrMore(
            websockets.WebsocketFrame.expr()
        ).parseString(
            s,
            parseAll=True
        )
    except pp.ParseException as v:
        raise exceptions.ParseException(v.msg, v.line, v.col)
    return itertools.chain(*[expand(i) for i in reqs])


def serve(msg, fp, settings):
    """
        fp: The file pointer to write to.

        request_host: If this a request, this is the connecting host. If
        None, we assume it's a response. Used to decide what standard
        modifications to make if raw is not set.

        Calling this function may modify the object.
    """
    msg = msg.resolve(settings)
    started = time.time()

    vals = msg.values(settings)
    vals.reverse()

    actions = sorted(msg.actions[:])
    actions.reverse()
    actions = [i.intermediate(settings) for i in actions]

    disconnect = writer.write_values(fp, vals, actions[:])
    duration = time.time() - started
    ret = dict(
        disconnect=disconnect,
        started=started,
        duration=duration,
    )
    ret.update(msg.log(settings))
    return ret