Merge pull request #200 from 38elements/invalid-usage

Change HttpParserError process
This commit is contained in:
Eli Uriegas 2016-12-17 22:50:00 -06:00 committed by GitHub
commit ef9d8710f5
2 changed files with 24 additions and 4 deletions

View File

@ -14,7 +14,7 @@ except ImportError:
from .log import log from .log import log
from .request import Request from .request import Request
from .exceptions import RequestTimeout, PayloadTooLarge from .exceptions import RequestTimeout, PayloadTooLarge, InvalidUsage
class Signal: class Signal:
@ -105,9 +105,9 @@ class HttpProtocol(asyncio.Protocol):
# Parse request chunk or close connection # Parse request chunk or close connection
try: try:
self.parser.feed_data(data) self.parser.feed_data(data)
except HttpParserError as e: except HttpParserError:
self.bail_out( exception = InvalidUsage('Bad Request')
"Invalid request data, connection closed ({})".format(e)) self.write_error(exception)
def on_url(self, url): def on_url(self, url):
self.url = url self.url = url

20
tests/test_bad_request.py Normal file
View File

@ -0,0 +1,20 @@
import asyncio
from sanic import Sanic
def test_bad_request_response():
app = Sanic('test_bad_request_response')
lines = []
async def _request(sanic, loop):
connect = asyncio.open_connection('127.0.0.1', 42101)
reader, writer = await connect
writer.write(b'not http')
while True:
line = await reader.readline()
if not line:
break
lines.append(line)
app.stop()
app.run(host='127.0.0.1', port=42101, debug=False, after_start=_request)
assert lines[0] == b'HTTP/1.1 400 Bad Request\r\n'
assert lines[-1] == b'Error: Bad Request'