From 12d0bd4dbb83fa9753145208cb0778ac8f9bb88d Mon Sep 17 00:00:00 2001 From: Pablo Hoffman Date: Tue, 20 Jan 2009 21:00:56 +0000 Subject: [PATCH] added Content-Length header population to Common downloader middleware --HG-- extra : convert_revision : svn%3Ab85faa78-f9eb-468e-a121-7cced6da292c%40754 --- scrapy/trunk/docs/ref/downloader-middleware.rst | 3 +++ scrapy/trunk/scrapy/contrib/downloadermiddleware/common.py | 2 ++ 2 files changed, 5 insertions(+) diff --git a/scrapy/trunk/docs/ref/downloader-middleware.rst b/scrapy/trunk/docs/ref/downloader-middleware.rst index c2285320c..8665e78be 100644 --- a/scrapy/trunk/docs/ref/downloader-middleware.rst +++ b/scrapy/trunk/docs/ref/downloader-middleware.rst @@ -34,6 +34,9 @@ thus it's recommended to leave it always enabled. Those tasks are: * If the request method is ``POST`` and the ``Content-Type`` header is not set, then set it to ``'application/x-www-form-urlencoded'``, the `default Form content type`_. + + * If the request contains a body and the ``Content-Length`` headers it not + set, then set it to the ``len(body)``. .. _default Form content type: http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.1 diff --git a/scrapy/trunk/scrapy/contrib/downloadermiddleware/common.py b/scrapy/trunk/scrapy/contrib/downloadermiddleware/common.py index 91d95496c..793ed85ed 100644 --- a/scrapy/trunk/scrapy/contrib/downloadermiddleware/common.py +++ b/scrapy/trunk/scrapy/contrib/downloadermiddleware/common.py @@ -17,4 +17,6 @@ class CommonMiddleware(object): request.headers.setdefault('Accept-Language', self.header_accept_language) if request.method == 'POST': request.headers.setdefault('Content-Type', 'application/x-www-form-urlencoded') + if request.body: + request.headers.setdefault('Content-Length', '%d' % len(request.body))