From 2b556bb83084c11f9844200e4ab82ae08c3f9399 Mon Sep 17 00:00:00 2001 From: benoitc Date: Sat, 27 Feb 2010 15:21:36 +0100 Subject: [PATCH] make it more compliant with the spec --- gunicorn/http/parser.py | 2 ++ gunicorn/http/request.py | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/gunicorn/http/parser.py b/gunicorn/http/parser.py index 0be6cc62..3f311d3d 100644 --- a/gunicorn/http/parser.py +++ b/gunicorn/http/parser.py @@ -12,6 +12,8 @@ class ParserError(Exception): class Parser(object): + _should_close = False + def __init__(self): self.status = "" self.headers = [] diff --git a/gunicorn/http/request.py b/gunicorn/http/request.py index 6d3b1da5..a8795f01 100644 --- a/gunicorn/http/request.py +++ b/gunicorn/http/request.py @@ -3,6 +3,7 @@ # This file is part of gunicorn released under the MIT license. # See the NOTICE for more information. +import ctypes import logging import os import re @@ -71,6 +72,29 @@ class Request(object): self.socket.send("100 Continue\n") if not self.parser.content_len and not self.parser.is_chunked: + if self.parser.should_close: + # According to the RFC : + # + # The presence of a message-body in a request is signaled by + # the inclusion of a Content-Length or Transfer-Encoding header + # field in the request's message-headers. A message-body MUST + # NOT be included in a request if the specification of the + # request method (section 5.1.1) does not allow sending an + # entity-body in requests. A server SHOULD read and forward a + # message-body on any request; if the request method does not + # include defined semantics for an entity-body, then the + # message-body SHOULD be ignored when handling the request. + # + # So we try to read here the body and just skip it + + l = sock.CHUNK_SIZE + while True: + b = ctypes.create_string_buffer(l) + try: + l = self.socket.recv_into(b, l) + except socket.error: + break + wsgi_input = StringIO.StringIO() else: wsgi_input = TeeInput(self.socket, self.parser, buf[i:])