Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: fixes exception due to unread bytes in stream #1897

Merged
merged 6 commits into from
Aug 27, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions sanic/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,12 +416,13 @@ async def body_append(self, body):
async def stream_append(self):
while self._body_chunks:
body = self._body_chunks.popleft()
if self.request.stream.is_full():
self.transport.pause_reading()
await self.request.stream.put(body)
self.transport.resume_reading()
else:
await self.request.stream.put(body)
if self.request:
if self.request.stream.is_full():
self.transport.pause_reading()
await self.request.stream.put(body)
self.transport.resume_reading()
else:
await self.request.stream.put(body)

def on_message_complete(self):
# Entire request (headers and whole body) is received.
Expand Down
16 changes: 16 additions & 0 deletions tests/test_request_stream.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import pytest
import asyncio

from sanic.blueprints import Blueprint
from sanic.exceptions import HeaderExpectationFailed
from sanic.request import StreamBuffer
from sanic.response import json, stream, text
from sanic.views import CompositionView, HTTPMethodView
from sanic.views import stream as stream_decorator
from sanic.server import HttpProtocol


data = "abc" * 1_000_000
Expand Down Expand Up @@ -336,6 +338,20 @@ async def post(request, id):
assert response.status == 405
assert "Method GET not allowed for URL /post/random_id" in response.text

@pytest.mark.asyncio
async def test_request_stream_unread(app):
"""ensure no error is raised when leaving unread bytes in byte-buffer"""

err = None
protocol = HttpProtocol(loop=asyncio.get_event_loop(), app=app)
try:
protocol.request = None
protocol._body_chunks.append('this is a test')
await protocol.stream_append()
except AttributeError as e:
err = e

assert err is None and not protocol._body_chunks
Tronic marked this conversation as resolved.
Show resolved Hide resolved

def test_request_stream_blueprint(app):
"""for self.is_request_stream = True"""
Expand Down