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")
    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))
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")
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")
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")
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")
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")
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")
Exemple #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")
Exemple #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"
Exemple #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()
Exemple #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")
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")
Exemple #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"
Exemple #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))
Exemple #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()
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")
Exemple #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"
Exemple #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")
Exemple #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")
Exemple #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"
Exemple #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"
Exemple #23
0
def assert_readline(payload, size, expected):
    body = Body(BytesIO(payload))
    assert body.readline(size) == expected
def assert_readline(payload, size, expected):
    body = Body(StringIO(payload))
    t.eq(body.readline(size), expected)
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")
def test_readline_new_line_before_size():
    body = Body(StringIO("abc\ndef"))
    t.eq(body.readline(4), "abc\n")
    t.eq(body.readline(), "def")
def test_readline_new_line_after_size():
    body = Body(StringIO("abc\ndef"))
    t.eq(body.readline(2), "ab")
    t.eq(body.readline(), "c\n")
def test_readline_new_line_before_size():
    body = Body(StringIO("abc\ndef"))
    t.eq(body.readline(4), "abc\n")
    t.eq(body.readline(), "def")
Exemple #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))
def assert_readline(payload, size, expected):
    body = Body(StringIO(payload))
    t.eq(body.readline(size), expected)
Exemple #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")
Exemple #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
def test_readline_new_line_after_size():
    body = Body(StringIO("abc\ndef"))
    t.eq(body.readline(2), "ab")
    t.eq(body.readline(), "c\n")
 def set_body_reader(self):
     super(Request, self).set_body_reader()
     if isinstance(self.body.reader, EOFReader):
         self.body = Body(LengthReader(self.unreader, 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")