コード例 #1
0
def test_whole_sentences():
    parser = delimiting_parser.prepare(".")
    results = []
    next(parser)

    results += parse(parser, "first.second.")
    results += parse(parser, "third.forth.")

    assert results == ["first", "second", "third", "forth"]
コード例 #2
0
def test_multiple():
    parser = chunking_parser.prepare()
    results = []
    next(parser)

    results += parse(parser, "5\r")
    results += parse(parser, "\nabc")
    results += parse(parser, "de2\r\nXX")

    assert results == ["abcde", "XX"]
コード例 #3
0
def test_one_split():
    parser = chunking_parser.prepare()
    results = []
    next(parser)

    results += parse(parser, "5\r")
    results += parse(parser, "\nabc")
    results += parse(parser, "deXX")

    assert results == ["abcde"]
コード例 #4
0
def test_split():
    parser = delimiting_parser.prepare(".")
    results = []
    next(parser)

    results += parse(parser, "fi")
    results += parse(parser, "rs")
    results += parse(parser, "t.sec")
    results += parse(parser, "ond.")
    results += parse(parser, "third.forth.")

    assert results == ["first", "second", "third", "forth"]
コード例 #5
0
def test_two_responses_chunked_in_parts():
    msg = b"HTTP/1.1 200 OK\r\n" + \
          b"Content-Type: text/plain; charset=utf-8\r\n" + \
          b"Transfer-Encoding: chunked\r\n" + \
          b"\r\n" + \
          b"4\r\n" + \
          b"Wiki\r\n" + \
          b"5\r\n" + \
          b"pedia\r\n" + \
          b"E\r\n" + \
          b" in\r\n" + \
          b"\r\n" + \
          b"chunks.\r\n" + \
          b"0\r\n" + \
          b"\r\n"

    msg = msg * 2
    parser = intialize_parser(http_parser.get_http_request)

    msgs = chunks(msg, 15)
    parsed_messages = []

    for data in msgs:
        parsed_messages += parse(parser, data)

    assert len(parsed_messages) == 2
    for parsed_message in parsed_messages:
        assert parsed_message.headers[
            b'Content-Type'] == b"text/plain; charset=utf-8"
        assert parsed_message.body == b"Wikipedia in\r\n\r\nchunks."
コード例 #6
0
def test_two_requests_whole():
    msg = b"GET / HTTP/1.1\r\nHost: www.example.com\r\n\r\nGET / HTTP/1.1\r\nHost: www.example.com\r\n\r\n"

    parser = intialize_parser(http_parser.get_http_request)
    parsed_messages = list(parse(parser, msg))

    assert len(parsed_messages) == 2
    for parsed_message in parsed_messages:
        assert parsed_message.headers[b'Host'] == b"www.example.com"
        assert parsed_message.body == None
        assert parsed_message.method == b"GET"
        assert parsed_message.path == b"/"
コード例 #7
0
    def run(self):
        parser = intialize_parser(http_parser.get_http_request)
        while 1:
            try:
                try:
                    data = self.source.recv(1024)
                except ConnectionResetError:
                    data = None

                if self.tag == "request":
                    for msg in parse(parser, data):
                        self.communication.add_message(msg, self.tag)
                        # print(msg)
                        if msg.headers.get(b"Host"):
                            msg.headers[b"Host"] = self.host_header
                        self.send_request(msg)
                else:
                    for msg in parse(parser, data):
                        self.communication.add_message(msg, self.tag)
                        # print(msg)
                        if msg.headers.get(b"Transfer-Encoding",
                                           "") == b"chunked":
                            del msg.headers[b"Transfer-Encoding"]
                            msg.headers[b"Content-Length"] = str(len(
                                msg.body)).encode()
                        self.send_request(msg)

                if not data:
                    break
            except Exception as ex:
                print(ex)
                break

        log('%s terminating' % self)
        PipeThread.pipes.remove(self)
        log('%s pipes active' % len(PipeThread.pipes))

        self.sink.shutdown(socket.SHUT_WR)
コード例 #8
0
def test_one_response_no_length():
    msg = b"HTTP/1.1 200 OK\r\n" + \
          b"Content-Type: text/plain; charset=utf-8\r\n" + \
          b"\r\n" + \
          b"abcd\r\n"

    parser = intialize_parser(http_parser.get_http_request)
    parsed_messages = list(parse(parser, msg))

    assert len(parsed_messages) == 1
    for parsed_message in parsed_messages:
        assert parsed_message.headers[
            b'Content-Type'] == b"text/plain; charset=utf-8"
        assert parsed_message.body == b"abcd\r\n"
コード例 #9
0
def test_two_requests_in_pieces():
    msg = b"GET / HTTP/1.1\r\nHost: www.example.com\r\n\r\nGET / HTTP/1.1\r\nHost: www.example.com\r\n\r\n"
    msgs = chunks(msg, 15)

    parser = intialize_parser(http_parser.get_http_request)
    parsed_messages = []

    for data in msgs:
        parsed_messages += parse(parser, data)

    assert len(parsed_messages) == 2
    for parsed_message in parsed_messages:
        assert parsed_message.headers[b'Host'] == b"www.example.com"
        assert parsed_message.body == None
        assert parsed_message.method == b"GET"
        assert parsed_message.path == b"/"
コード例 #10
0
def test_two_responses_in_pieces():
    msg = b"HTTP/1.1 200 OK\r\n" + \
          b"Content-Type: text/plain; charset=utf-8\r\n" + \
          b"Content-Length: 6\r\n" + \
          b"\r\n" + \
          b"abcd\r\n"

    msg = msg * 3
    parser = intialize_parser(http_parser.get_http_request)
    msgs = chunks(msg, 15)
    parsed_messages = []

    for data in msgs:
        parsed_messages += parse(parser, data)

    assert len(parsed_messages) == 3
    for parsed_message in parsed_messages:
        assert parsed_message.headers[
            b'Content-Type'] == b"text/plain; charset=utf-8"
        assert parsed_message.body == b"abcd\r\n"
コード例 #11
0
def test_one_response_chunked_whole():
    msg = b"HTTP/1.1 200 OK\r\n" + \
          b"Content-Type: text/plain; charset=utf-8\r\n" + \
          b"Transfer-Encoding: chunked\r\n" + \
          b"\r\n" + \
          b"4\r\n" + \
          b"Wiki\r\n" + \
          b"5\r\n" + \
          b"pedia\r\n" + \
          b"E\r\n" + \
          b" in\r\n" + \
          b"\r\n" + \
          b"chunks.\r\n" + \
          b"0\r\n" + \
          b"\r\n"

    parser = intialize_parser(http_parser.get_http_request)
    parsed_messages = list(parse(parser, msg))
    assert len(parsed_messages) == 1
    for parsed_message in parsed_messages:
        assert parsed_message.headers[
            b'Content-Type'] == b"text/plain; charset=utf-8"
        assert parsed_message.body == b"Wikipedia in\r\n\r\nchunks."