Ticket #6168: 6168_v2.diff

File 6168_v2.diff, 4.7 KB (added by borko, 3 years ago)
  • twisted/web/http_headers.py

    diff --git a/twisted/web/http_headers.py b/twisted/web/http_headers.py
    index f0790b9..4652af8 100644
    a b class Headers(object): 
    194194
    195195        @return: C{None}
    196196        """
     197
     198        if not all(isinstance(n, bytes) for n in name):
     199            raise TypeError("Header name %r must consist of bytes" % name)
     200
    197201        if not isinstance(values, list):
    198202            raise TypeError("Header entry %r should be list but found "
    199203                            "instance of %r instead" % (name, type(values)))
     204
     205        for val in values:
     206            if not all(isinstance(n, bytes) for n in val):
     207                raise TypeError("Header entry %r must consist of bytes" % val)
     208
    200209        self._rawHeaders[name.lower()] = values
    201210
    202211
  • twisted/web/test/test_http.py

    diff --git a/twisted/web/test/test_http.py b/twisted/web/test/test_http.py
    index f3f2dcc..21c3329 100644
    a b class PersistenceTestCase(unittest.TestCase): 
    306306    ptests = [
    307307        # (_prequest(connection=[b"Keep-Alive"]), b"HTTP/1.0", 1, {b'connection' : [b'Keep-Alive']}),
    308308        (_prequest(), b"HTTP/1.0", 0, {b'connection': None}),
    309         (_prequest(connection=[b"close"]), b"HTTP/1.1", 0,
     309        (_prequest(connection = [b"close"]), b"HTTP/1.1", 0,
    310310         {b'connection' : [b'close']}),
    311311        (_prequest(), b"HTTP/1.1", 1, {b'connection': None}),
    312312        (_prequest(), b"HTTP/0.9", 0, {b'connection': None}),
    class ParsingTestCase(unittest.TestCase): 
    631631        self.didRequest = False
    632632
    633633
    634     def runRequest(self, httpRequest, requestClass, success=1):
     634    def runRequest(self, httpRequest, requestClass, success = 1):
    635635        httpRequest = httpRequest.replace(b"\n", b"\r\n")
    636636        b = StringTransport()
    637637        a = http.HTTPChannel()
    Content-Transfer-Encoding: quoted-printable 
    903903abasdfg
    904904--AaB03x--
    905905'''
    906         self.runRequest(req, http.Request, success=False)
     906        self.runRequest(req, http.Request, success = False)
    907907    if _PY3:
    908908        testMissingContentDisposition.skip = (
    909909            "Cannot parse multipart/form-data on Python 3.  "
    Hello, 
    982982                # This request should fail, so this should never be called.
    983983                didRequest.append(True)
    984984
    985         channel = self.runRequest(httpRequest, MyRequest, success=False)
     985        channel = self.runRequest(httpRequest, MyRequest, success = False)
    986986        self.assertFalse(didRequest, "Request.process called")
    987987        self.assertEqual(
    988988            channel.transport.value(),
    class QueryArgumentsTestCase(unittest.TestCase): 
    997997            cgi.parse_qs(b"a=b&d=c;+=f"),
    998998            http.parse_qs(b"a=b&d=c;+=f"))
    999999        self.assertRaises(
    1000             ValueError, http.parse_qs, b"blah", strict_parsing=True)
     1000            ValueError, http.parse_qs, b"blah", strict_parsing = True)
    10011001        self.assertEqual(
    1002             cgi.parse_qs(b"a=&b=c", keep_blank_values=1),
    1003             http.parse_qs(b"a=&b=c", keep_blank_values=1))
     1002            cgi.parse_qs(b"a=&b=c", keep_blank_values = 1),
     1003            http.parse_qs(b"a=&b=c", keep_blank_values = 1))
    10041004        self.assertEqual(
    10051005            cgi.parse_qs(b"a=&b=c"),
    10061006            http.parse_qs(b"a=&b=c"))
    class RequestTests(unittest.TestCase, ResponseTestMixin): 
    13571357
    13581358        req.setResponseCode(200)
    13591359        req.clientproto = b"HTTP/1.0"
    1360         req.responseHeaders.setRawHeaders(b"test", [10])
     1360        req.responseHeaders.setRawHeaders(b"test", ["10"])
    13611361        req.write(b'Hello')
    13621362
    13631363        self.assertResponseEquals(
    class RequestTests(unittest.TestCase, ResponseTestMixin): 
    13671367              b"Hello")])
    13681368
    13691369        warnings = self.flushWarnings(
    1370             offendingFunctions=[self.test_nonByteHeaderValue])
    1371         self.assertEqual(1, len(warnings))
    1372         self.assertEqual(warnings[0]['category'], DeprecationWarning)
    1373         self.assertEqual(
    1374             warnings[0]['message'],
    1375             "Passing non-bytes header values is deprecated since "
    1376             "Twisted 12.3. Pass only bytes instead.")
     1370            offendingFunctions = [self.test_nonByteHeaderValue])
     1371        self.assertEqual(0, len(warnings))
     1372
     1373    def test_nonByteHeaderError(self):
     1374        """
     1375        L{http.Request.write} casts non-bytes header value to bytes
     1376        transparently.
     1377        """
     1378        req = http.Request(DummyChannel(), None)
    13771379
     1380        intlist = [int(1), int(2)]
     1381        err = self.assertRaises(TypeError, req.responseHeaders.setRawHeaders, intlist, ["10"])
     1382        self.assertEqual(str(err), "Header name " + intlist.__str__() + " must consist of bytes")
    13781383
    13791384    def test_firstWriteHTTP11Chunked(self):
    13801385        """