How to use the uvicorn.protocols.http.RequestResponseState function in uvicorn

To help you get started, we’ve selected a few uvicorn examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github encode / uvicorn / uvicorn / protocols / http.py View on Github external
body,
                        b'\r\n'
                    ]
                    if not more_body:
                        content.append(b'0\r\n\r\n')
                    self.transport.write(b''.join(content))
                else:
                    self.transport.write(body)

            else:
                raise Exception("Unexpected 'http.response.body' message.")

            if more_body:
                self.state = RequestResponseState.SENDING_BODY
            else:
                self.state = RequestResponseState.CLOSED

        else:
            raise Exception('Unexpected message type "%s"' % message_type)

        if self.protocol.write_paused:
            await self.transport.drain()

        if self.state == RequestResponseState.CLOSED:
            self.protocol.on_response_complete(keep_alive=self.keep_alive)
github encode / uvicorn / uvicorn / protocols / http.py View on Github external
self.keep_alive = False
                content.extend([header_name, b': ', header_value, b'\r\n'])

            if self.content_length is None:
                self.state = RequestResponseState.FINALIZING_HEADERS
            else:
                content.append(b'\r\n')
                self.state = RequestResponseState.SENDING_BODY

            self.transport.write(b''.join(content))

        elif message_type == 'http.response.body':
            body = message.get('body', b'')
            more_body = message.get('more_body', False)

            if self.state == RequestResponseState.FINALIZING_HEADERS:
                if more_body:
                    content = [
                        b'transfer-encoding: chunked\r\n\r\n',
                        b'%x\r\n' % len(body),
                        body,
                        b'\r\n'
                    ]
                    self.chunked_encoding = True
                    self.transport.write(b''.join(content))
                else:
                    content = [
                        b'content-length: ',
                        str(len(body)).encode(),
                        b'\r\n\r\n',
                        body
                    ]
github encode / uvicorn / uvicorn / protocols / http.py View on Github external
DATE_HEADER,
            ]
            for header_name, header_value in headers:
                header = header_name.lower()
                if header == b'content-length':
                    self.content_length = int(header_value.decode())
                elif header == b'connection':
                    if header_value.lower() == b'close':
                        self.keep_alive = False
                content.extend([header_name, b': ', header_value, b'\r\n'])

            if self.content_length is None:
                self.state = RequestResponseState.FINALIZING_HEADERS
            else:
                content.append(b'\r\n')
                self.state = RequestResponseState.SENDING_BODY

            self.transport.write(b''.join(content))

        elif message_type == 'http.response.body':
            body = message.get('body', b'')
            more_body = message.get('more_body', False)

            if self.state == RequestResponseState.FINALIZING_HEADERS:
                if more_body:
                    content = [
                        b'transfer-encoding: chunked\r\n\r\n',
                        b'%x\r\n' % len(body),
                        body,
                        b'\r\n'
                    ]
                    self.chunked_encoding = True
github encode / uvicorn / uvicorn / protocols / http.py View on Github external
async def send(self, message):
        message_type = message['type']

        if message_type == 'http.response.start':
            if self.state != RequestResponseState.STARTED:
                raise Exception("Unexpected 'http.response.start' message.")

            status = message['status']
            headers = message.get('headers', [])

            content = [
                STATUS_LINE[status],
                SERVER_HEADER,
                DATE_HEADER,
            ]
            for header_name, header_value in headers:
                header = header_name.lower()
                if header == b'content-length':
                    self.content_length = int(header_value.decode())
                elif header == b'connection':
                    if header_value.lower() == b'close':