Appease req.setHeader by passing bytes, not ints. 137/head
authorBrian Warner <warner@lothar.com>
Wed, 21 Jan 2015 18:31:31 +0000 (10:31 -0800)
committerBrian Warner <warner@lothar.com>
Wed, 21 Jan 2015 18:31:31 +0000 (10:31 -0800)
twisted.web.http.Request.setHeader() really wants a "bytes" object, but
we've been passing integers like len(body). Twisted-12.3 started to
complain about this (with a DeprecationWarning), but the warning is
usually silenced because py2.7 disables deprecations by default.

This fixes Tahoe's misbehavior, but others remain (in Nevow, at least).
I plan to set up some tooling to run tests with
PYTHONWARNINGS=default::DeprecationWarning and collect others. We won't
be able to fix the ones that occur outside of Tahoe, but at least we
should be able to fix our own.

refs ticket:2312

src/allmydata/web/common.py
src/allmydata/web/filenode.py

index af5f6d26a7adc41d7cc6527607449c2ae97eceb5..52fed6a04be4c0002a52acc7e546380b5abfe5d4 100644 (file)
@@ -206,7 +206,7 @@ def plural(sequence_or_length):
 def text_plain(text, ctx):
     req = IRequest(ctx)
     req.setHeader("content-type", "text/plain")
-    req.setHeader("content-length", len(text))
+    req.setHeader("content-length", b"%d" % len(text))
     return text
 
 class WebError(Exception):
@@ -315,7 +315,7 @@ class MyExceptionHandler(appserver.DefaultExceptionHandler):
         req.setHeader("content-type", "text/plain;charset=utf-8")
         if isinstance(text, unicode):
             text = text.encode("utf-8")
-        req.setHeader("content-length", str(len(text)))
+        req.setHeader("content-length", b"%d" % len(text))
         req.write(text)
         # TODO: consider putting the requested URL here
         req.finishRequest(False)
index ff65acc9f372cbdc68da684fde816483723ee070..7d61447a3a546287e126f8bab737f939aaf27c24 100644 (file)
@@ -452,7 +452,7 @@ class FileDownloader(rend.Page):
                     contentsize = last - first + 1
                     size = contentsize
 
-        req.setHeader("content-length", str(contentsize))
+        req.setHeader("content-length", b"%d" % contentsize)
         if req.method == "HEAD":
             return ""