Esempio n. 1
0
def test_readline_buffer_loaded_with_size():
    body = Body(BytesIO(b"abc\ndef"))
    body.read(1) # load internal buffer
    t.eq(body.readline(2), b"bc")
    t.eq(body.readline(2), b"\n")
    t.eq(body.readline(2), b"de")
    t.eq(body.readline(2), b"f")
Esempio n. 2
0
    def set_body_reader(self):
        chunked = False
        content_length = None
        for (name, value) in self.headers:
            if name == "CONTENT-LENGTH":
                if content_length is not None:
                    raise InvalidHeader("CONTENT-LENGTH", req=self)
                content_length = value
            elif name == "TRANSFER-ENCODING":
                if value.lower() == "chunked":
                    chunked = True
            elif name == "SEC-WEBSOCKET-KEY1":
                content_length = 8

        if chunked:
            self.body = Body(ChunkedReader(self, self.unreader))
        elif content_length is not None:
            try:
                content_length = int(content_length)
            except ValueError:
                raise InvalidHeader("CONTENT-LENGTH", req=self)

            if content_length < 0:
                raise InvalidHeader("CONTENT-LENGTH", req=self)

            self.body = Body(LengthReader(self.unreader, content_length))
        else:
            self.body = Body(EOFReader(self.unreader))
Esempio n. 3
0
def test_readline_no_new_line():
    body = Body(StringIO("abcdef"))
    t.eq(body.readline(), "abcdef")
    body = Body(StringIO("abcdef"))
    t.eq(body.readline(2), "ab")
    t.eq(body.readline(2), "cd")
    t.eq(body.readline(2), "ef")
Esempio n. 4
0
def test_readline_no_new_line():
    body = Body(BytesIO(b"abcdef"))
    t.eq(body.readline(), b"abcdef")
    body = Body(BytesIO(b"abcdef"))
    t.eq(body.readline(2), b"ab")
    t.eq(body.readline(2), b"cd")
    t.eq(body.readline(2), b"ef")
Esempio n. 5
0
def test_readline_buffer_loaded_with_size():
    body = Body(StringIO("abc\ndef"))
    body.read(1)  # load internal buffer
    t.eq(body.readline(2), "bc")
    t.eq(body.readline(2), "\n")
    t.eq(body.readline(2), "de")
    t.eq(body.readline(2), "f")
Esempio n. 6
0
def test_readline_buffer_loaded():
    reader = StringIO("abc\ndef")
    body = Body(reader)
    body.read(1)  # load internal buffer
    reader.write("g\nhi")
    reader.seek(7)
    t.eq(body.readline(), "bc\n")
    t.eq(body.readline(), "defg\n")
    t.eq(body.readline(), "hi")
Esempio n. 7
0
def test_readline_buffer_loaded():
    reader = BytesIO(b"abc\ndef")
    body = Body(reader)
    body.read(1) # load internal buffer
    reader.write(b"g\nhi")
    reader.seek(7)
    print(reader.getvalue())
    t.eq(body.readline(), b"bc\n")
    t.eq(body.readline(), b"defg\n")
    t.eq(body.readline(), b"hi")
Esempio n. 8
0
def test_readline_no_new_line():
    body = Body(StringIO("abcdef"))
    t.eq(body.readline(), "abcdef")
    body = Body(StringIO("abcdef"))
    t.eq(body.readline(2), "ab")
    t.eq(body.readline(2), "cd")
    t.eq(body.readline(2), "ef")
Esempio n. 9
0
def test_readline_no_new_line():
    body = Body(BytesIO(b"abcdef"))
    t.eq(body.readline(), b"abcdef")
    body = Body(BytesIO(b"abcdef"))
    t.eq(body.readline(2), b"ab")
    t.eq(body.readline(2), b"cd")
    t.eq(body.readline(2), b"ef")
Esempio n. 10
0
def test_readline_no_new_line():
    body = Body(BytesIO(b"abcdef"))
    assert body.readline() == b"abcdef"
    body = Body(BytesIO(b"abcdef"))
    assert body.readline(2) == b"ab"
    assert body.readline(2) == b"cd"
    assert body.readline(2) == b"ef"
Esempio n. 11
0
def test_non_filelike_obj_read_limit():
    from gunicorn.http.body import Body

    content = b"this will not really be a real fileobj"
    fileobj = BytesIO(content)
    body = Body(fileobj)
    ls = LimitingStream(body)

    assert ls.readable()
    assert ls.read(-1) == content
    assert len(content) == ls.tell()
Esempio n. 12
0
def test_readline_buffer_loaded_with_size():
    body = Body(BytesIO(b"abc\ndef"))
    body.read(1)  # load internal buffer
    t.eq(body.readline(2), b"bc")
    t.eq(body.readline(2), b"\n")
    t.eq(body.readline(2), b"de")
    t.eq(body.readline(2), b"f")
Esempio n. 13
0
def test_readline_buffer_loaded_with_size():
    body = Body(StringIO("abc\ndef"))
    body.read(1) # load internal buffer
    t.eq(body.readline(2), "bc")
    t.eq(body.readline(2), "\n")
    t.eq(body.readline(2), "de")
    t.eq(body.readline(2), "f")
Esempio n. 14
0
def test_readline_buffer_loaded_with_size():
    body = Body(BytesIO(b"abc\ndef"))
    body.read(1)  # load internal buffer
    assert body.readline(2) == b"bc"
    assert body.readline(2) == b"\n"
    assert body.readline(2) == b"de"
    assert body.readline(2) == b"f"
Esempio n. 15
0
    def set_body_reader(self):
        chunked = False
        response_length = None
        for (name, value) in self.headers:
            if name == "CONTENT-LENGTH":
                try:
                    response_length = int(value)
                except ValueError:
                    response_length = None
            elif name == "TRANSFER-ENCODING":
                chunked = value.lower() == "chunked"
            elif name == "SEC-WEBSOCKET-KEY1":
                response_length = 8

            if response_length is not None or chunked:
                break

        if chunked:
            self.body = Body(ChunkedReader(self, self.unreader))
        elif response_length is not None:
            self.body = Body(LengthReader(self.unreader, response_length))
        else:
            self.body = Body(EOFReader(self.unreader))
Esempio n. 16
0
def test_non_filelike_obj_read():
    from gunicorn.http.body import Body

    content = b"this will not really be a real fileobj"
    fileobj = BytesIO(content)

    # Limited
    body1 = Body(fileobj)
    ls1 = LimitingStream(body1, 4)
    assert ls1.readable()

    resp1 = b""
    while True:
        buf = ls1.read(-1)
        if not buf:
            break
        resp1 += buf

    assert resp1 == content[:4]
    assert ls1.read(1) == b""
    assert 4 == ls1.tell()

    # Non limited
    fileobj.seek(0)
    body2 = Body(fileobj)
    ls2 = LimitingStream(body2)

    resp2 = b""
    while True:
        buf = ls2.read(-1)
        if not buf:
            break
        resp2 += buf

    assert resp2 == content
    assert ls2.read(1) == b""
    assert len(content) == ls2.tell()
Esempio n. 17
0
def test_readline_buffer_loaded():
    reader = StringIO("abc\ndef")
    body = Body(reader)
    body.read(1) # load internal buffer
    reader.write("g\nhi")
    reader.seek(7)
    t.eq(body.readline(), "bc\n")
    t.eq(body.readline(), "defg\n")
    t.eq(body.readline(), "hi")
Esempio n. 18
0
def test_readline_buffer_loaded():
    reader = BytesIO(b"abc\ndef")
    body = Body(reader)
    body.read(1) # load internal buffer
    reader.write(b"g\nhi")
    reader.seek(7)
    assert body.readline() == b"bc\n"
    assert body.readline() == b"defg\n"
    assert body.readline() == b"hi"
Esempio n. 19
0
def test_readline_buffer_loaded():
    reader = BytesIO(b"abc\ndef")
    body = Body(reader)
    body.read(1)  # load internal buffer
    reader.write(b"g\nhi")
    reader.seek(7)
    print(reader.getvalue())
    t.eq(body.readline(), b"bc\n")
    t.eq(body.readline(), b"defg\n")
    t.eq(body.readline(), b"hi")
Esempio n. 20
0
def test_readline_new_line_after_size():
    body = Body(BytesIO(b"abc\ndef"))
    t.eq(body.readline(2), b"ab")
    t.eq(body.readline(), b"c\n")
Esempio n. 21
0
def test_readline_new_line_after_size():
    body = Body(BytesIO(b"abc\ndef"))
    assert body.readline(2) == b"ab"
    assert body.readline() == b"c\n"
Esempio n. 22
0
def test_readline_new_line_before_size():
    body = Body(BytesIO(b"abc\ndef"))
    assert body.readline(4) == b"abc\n"
    assert body.readline() == b"def"
Esempio n. 23
0
def assert_readline(payload, size, expected):
    body = Body(BytesIO(payload))
    assert body.readline(size) == expected
Esempio n. 24
0
def assert_readline(payload, size, expected):
    body = Body(StringIO(payload))
    t.eq(body.readline(size), expected)
Esempio n. 25
0
def test_readline_new_line_after_size():
    body = Body(BytesIO(b"abc\ndef"))
    t.eq(body.readline(2), b"ab")
    t.eq(body.readline(), b"c\n")
Esempio n. 26
0
def test_readline_new_line_before_size():
    body = Body(StringIO("abc\ndef"))
    t.eq(body.readline(4), "abc\n")
    t.eq(body.readline(), "def")
Esempio n. 27
0
def test_readline_new_line_after_size():
    body = Body(StringIO("abc\ndef"))
    t.eq(body.readline(2), "ab")
    t.eq(body.readline(), "c\n")
Esempio n. 28
0
def test_readline_new_line_before_size():
    body = Body(StringIO("abc\ndef"))
    t.eq(body.readline(4), "abc\n")
    t.eq(body.readline(), "def")
Esempio n. 29
0
 def set_body_reader(self):
     super().set_body_reader()
     if isinstance(self.body.reader, EOFReader):
         # todo EOFReader 转换成 0 LengthReader why?
         self.body = Body(LengthReader(self.unreader, 0))
Esempio n. 30
0
def assert_readline(payload, size, expected):
    body = Body(StringIO(payload))
    t.eq(body.readline(size), expected)
Esempio n. 31
0
def test_readline_new_line_before_size():
    body = Body(BytesIO(b"abc\ndef"))
    t.eq(body.readline(4), b"abc\n")
    t.eq(body.readline(), b"def")
Esempio n. 32
0
    def logger(*args, **kwargs):
        qualified_name = func.__qualname__
        class_name = qualified_name.split(".")[0]
        func_name = qualified_name.split(".")[1]

        if func_name not in ("on_post", "on_put", "on_delete"):
            # do not log for other HTTP Methods
            func(*args, **kwargs)
            return
        req, resp = args
        headers = req.headers
        if headers is not None and 'USER-UUID' in headers.keys():
            user_uuid = headers['USER-UUID']
        else:
            # todo: deal with requests with NULL user_uuid
            print('user_logger: USER-UUID is NULL')
            # do not log for NULL user_uuid
            func(*args, **kwargs)
            return

        if func_name == "on_post":
            try:
                file_name = str(uuid.uuid4())
                with open(file_name, "wb") as fw:
                    reads = req.stream.read()
                    fw.write(reads)
                raw_json = reads.decode('utf-8')
                with open(file_name, "rb") as fr:
                    req.stream = Body(fr)
                    os.remove(file_name)
                    func(*args, **kwargs)
                    write_log(user_uuid=user_uuid, request_method='POST', resource_type=class_name,
                              resource_id=kwargs.get('id_'), request_body=raw_json)
            except Exception as e:
                if isinstance(e, falcon.HTTPError):
                    raise e
                else:
                    print('user_logger:' + str(e))
            return
        elif func_name == "on_put":
            try:
                file_name = str(uuid.uuid4())
                with open(file_name, "wb") as fw:
                    reads = req.stream.read()
                    fw.write(reads)
                raw_json = reads.decode('utf-8')
                with open(file_name, "rb") as fr:
                    req.stream = Body(fr)
                    os.remove(file_name)
                    func(*args, **kwargs)
                    write_log(user_uuid=user_uuid, request_method='PUT', resource_type=class_name,
                              resource_id=kwargs.get('id_'), request_body=raw_json)
            except Exception as e:
                if isinstance(e, falcon.HTTPError):
                    raise e
                else:
                    print('user_logger:' + str(e))

            return
        elif func_name == "on_delete":
            try:
                func(*args, **kwargs)
                write_log(user_uuid=user_uuid, request_method="DELETE", resource_type=class_name,
                          resource_id=kwargs.get('id_'), request_body=json.dumps(kwargs))
            except Exception as e:
                if isinstance(e, falcon.HTTPError):
                    raise e
                else:
                    print('user_logger:' + str(e))
            return
Esempio n. 33
0
def test_readline_new_line_after_size():
    body = Body(StringIO("abc\ndef"))
    t.eq(body.readline(2), "ab")
    t.eq(body.readline(), "c\n")
Esempio n. 34
0
 def set_body_reader(self):
     super(Request, self).set_body_reader()
     if isinstance(self.body.reader, EOFReader):
         self.body = Body(LengthReader(self.unreader, 0))
Esempio n. 35
0
def test_readline_new_line_before_size():
    body = Body(BytesIO(b"abc\ndef"))
    t.eq(body.readline(4), b"abc\n")
    t.eq(body.readline(), b"def")