Exemplo n.º 1
0
    def read(self, size=None):
        if size is not None and not isinstance(size, six.integer_types):
            raise TypeError("size parameter must be an int or long.")

        if size is not None:
            if size == 0:
                return b""
            if size < 0:
                size = None

        self.buf.seek(0, os.SEEK_END)

        if size is None and self.buf.tell():
            ret = self.buf.getvalue()
            self.buf = six.BytesIO()
            return ret
        if size is None:
            d = self.chunk()
            return d

        while self.buf.tell() < size:
            chunk = self.chunk()
            if not len(chunk):
                ret = self.buf.getvalue()
                self.buf = six.BytesIO()
                return ret
            self.buf.write(chunk)
        data = self.buf.getvalue()
        self.buf = six.BytesIO()
        self.buf.write(data[size:])
        return data[:size]
Exemplo n.º 2
0
    def read(self, size):
        if not isinstance(size, six.integer_types):
            raise TypeError("size must be an integral type")
        if size < 0:
            raise ValueError("Size must be positive.")
        if size == 0:
            return b""

        if self.finished:
            data = self.buf.getvalue()
            ret, rest = data[:size], data[size:]
            self.buf = six.BytesIO()
            self.buf.write(rest)
            return ret

        data = self.unreader.read()
        while data:
            self.buf.write(data)
            if self.buf.tell() > size:
                break
            data = self.unreader.read()

        if not data:
            self.finished = True

        data = self.buf.getvalue()
        ret, rest = data[:size], data[size:]
        self.buf = six.BytesIO()
        self.buf.write(rest)
        return ret
Exemplo n.º 3
0
    def parse_chunk_size(self, unreader, data=None):
        buf = six.BytesIO()
        if data is not None:
            buf.write(data)

        idx = buf.getvalue().find(b"\r\n")
        while idx < 0:
            self.get_data(unreader, buf)
            idx = buf.getvalue().find(b"\r\n")

        data = buf.getvalue()
        line, rest_chunk = data[:idx], data[idx + 2:]

        chunk_size = line.split(b";", 1)[0].strip()
        try:
            chunk_size = int(chunk_size, 16)
        except ValueError:
            raise InvalidChunkSize(chunk_size)

        if chunk_size == 0:
            try:
                self.parse_trailers(unreader, rest_chunk)
            except NoMoreData:
                pass
            return (0, None)
        return (chunk_size, rest_chunk)
Exemplo n.º 4
0
    def read(self, size=None):
        size = self.getsize(size)
        if size == 0:
            return b""

        if size < self.buf.tell():
            data = self.buf.getvalue()
            ret, rest = data[:size], data[size:]
            self.buf = six.BytesIO()
            self.buf.write(rest)
            return ret

        while size > self.buf.tell():
            data = self.reader.read(1024)
            if not len(data):
                break
            self.buf.write(data)

        data = self.buf.getvalue()
        ret, rest = data[:size], data[size:]
        self.buf = six.BytesIO()
        self.buf.write(rest)
        return ret
Exemplo n.º 5
0
    def parse_trailers(self, unreader, data):
        buf = six.BytesIO()
        buf.write(data)

        idx = buf.getvalue().find(b"\r\n\r\n")
        done = buf.getvalue()[:2] == b"\r\n"
        while idx < 0 and not done:
            self.get_data(unreader, buf)
            idx = buf.getvalue().find(b"\r\n\r\n")
            done = buf.getvalue()[:2] == b"\r\n"
        if done:
            unreader.unread(buf.getvalue()[2:])
            return b""
        self.req.trailers = self.req.parse_headers(buf.getvalue()[:idx])
        unreader.unread(buf.getvalue()[idx + 4:])
Exemplo n.º 6
0
    def readline(self, size=None):
        size = self.getsize(size)
        if size == 0:
            return b""

        line = self.buf.getvalue()
        self.buf = six.BytesIO()
        if len(line) < size:
            line += self.reader.read(size - len(line))
        extra_buf_data = line[size:]
        line = line[:size]

        idx = line.find(b"\n")
        if idx >= 0:
            ret = line[:idx + 1]
            self.buf.write(line[idx + 1:])
            self.buf.write(extra_buf_data)
            return ret
        self.buf.write(extra_buf_data)
        return line
Exemplo n.º 7
0
    def read(self, size):
        if not isinstance(size, six.integer_types):
            raise TypeError("size must be an integral type")
        if size < 0:
            raise ValueError("Size must be positive.")
        if size == 0:
            return b""

        if self.parser:
            while self.buf.tell() < size:
                try:
                    self.buf.write(six.next(self.parser))
                except StopIteration:
                    self.parser = None
                    break

        data = self.buf.getvalue()
        ret, rest = data[:size], data[size:]
        self.buf = six.BytesIO()
        self.buf.write(rest)
        return ret
Exemplo n.º 8
0
    def read(self, size):
        if not isinstance(size, six.integer_types):
            raise TypeError("size must be an integral type")

        size = min(self.length, size)
        if size < 0:
            raise ValueError("Size must be positive.")
        if size == 0:
            return b""

        buf = six.BytesIO()
        data = self.unreader.read()
        while data:
            buf.write(data)
            if buf.tell() >= size:
                break
            data = self.unreader.read()

        buf = buf.getvalue()
        ret, rest = buf[:size], buf[size:]
        self.unreader.unread(rest)
        self.length -= size
        return ret
Exemplo n.º 9
0
    def readline(self, size=None):
        size = self.getsize(size)
        if size == 0:
            return b""

        data = self.buf.getvalue()
        self.buf = six.BytesIO()

        ret = []
        while 1:
            idx = data.find(b"\n", 0, size)
            idx = idx + 1 if idx >= 0 else size if len(data) >= size else 0
            if idx:
                ret.append(data[:idx])
                self.buf.write(data[idx:])
                break

            ret.append(data)
            size -= len(data)
            data = self.reader.read(min(1024, size))
            if not data:
                break

        return b"".join(ret)
Exemplo n.º 10
0
 def __init__(self, reader):
     self.reader = reader
     self.buf = six.BytesIO()
Exemplo n.º 11
0
 def __init__(self, unreader):
     self.unreader = unreader
     self.buf = six.BytesIO()
     self.finished = False
Exemplo n.º 12
0
 def __init__(self, req, unreader):
     self.req = req
     self.parser = self.parse_chunked(unreader)
     self.buf = six.BytesIO()
Exemplo n.º 13
0
 def __init__(self):
     self.buf = six.BytesIO()