From 75933bae819a5b737db18c473eb8bc3297bf2f2c Mon Sep 17 00:00:00 2001 From: Konstantin Kapustin Date: Fri, 7 Sep 2012 17:06:06 +0400 Subject: [PATCH] Change base-classes for NoMoreData, ChunkMissingTerminator and InvalidChunkSize. If remote client send invalid data in request with "Transfer-Encoding:chunked" gunicorn can raised some exceptions (see http.body.ChunkedReader) as NoMoreData, ChunkMissingTerminator, InvalidChunkSize. User application shouldn't know about specific gunicorn exceptions and must catch standard IOError if want. Example: def app(env, start_response): body = env["wsgi.input"] chunk_size = 1024 while True: try: chunk = body.read(chunk_size) except IOError: .. correct action for error if not chunk: break .. do somethink with chunk --- gunicorn/http/errors.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/gunicorn/http/errors.py b/gunicorn/http/errors.py index 879d430b..f579e9d9 100644 --- a/gunicorn/http/errors.py +++ b/gunicorn/http/errors.py @@ -6,7 +6,7 @@ class ParseException(Exception): pass -class NoMoreData(ParseException): +class NoMoreData(IOError): def __init__(self, buf=None): self.buf = buf def __str__(self): @@ -49,14 +49,14 @@ class InvalidHeaderName(ParseException): def __str__(self): return "Invalid HTTP header name: %s" % self.hdr -class InvalidChunkSize(ParseException): +class InvalidChunkSize(IOError): def __init__(self, data): self.data = data def __str__(self): return "Invalid chunk size: %r" % self.data -class ChunkMissingTerminator(ParseException): +class ChunkMissingTerminator(IOError): def __init__(self, term): self.term = term