Ticket #5911: 5911-6.patch

File 5911-6.patch, 4.3 KB (added by Dario Vinella, 4 years ago)
  • twisted/web/http.py

    diff --git a/twisted/web/http.py b/twisted/web/http.py
    index 18dfec2..bda14a3 100644
    a b class Request: 
    10311031            else:
    10321032                self.transport.write(data)
    10331033
    1034     def addCookie(self, k, v, expires=None, domain=None, path=None, max_age=None, comment=None, secure=None):
     1034    def addCookie(self, k, v, expires=None, domain=None, path=None,
     1035                  max_age=None, comment=None, secure=None, httpOnly=False):
    10351036        """
    10361037        Set an outgoing HTTP cookie.
    10371038
    10381039        In general, you should consider using sessions instead of cookies, see
    10391040        L{twisted.web.server.Request.getSession} and the
    10401041        L{twisted.web.server.Session} class for details.
     1042
     1043        @param k: cookie name
     1044        @type k: L{bytes}
     1045
     1046        @param v: cookie value
     1047        @type v: L{bytes}
     1048
     1049        @param expires: cookie expire attribute value in
     1050        "Wdy, DD Mon YYYY HH:MM:SS GMT" format
     1051        @type expires: L{bytes}
     1052
     1053        @param domain: cookie domain
     1054        @type domain: L{bytes}
     1055
     1056        @param path: cookie path
     1057        @type path: L{bytes}
     1058
     1059        @param max_age: cookie expiration in seconds from reception
     1060        @type max_age: L{int}
     1061
     1062        @param comment: cookie comment
     1063        @type comment: L{bytes}
     1064
     1065        @param secure: direct browser to send the cookie on encrypted
     1066        connections only
     1067        @type secure: L{bool}
     1068
     1069        @param httpOnly: direct browser not to expose cookies through channels
     1070        other than HTTP (and HTTPS) requests
     1071        @type httpOnly: L{bool}
    10411072        """
    1042         cookie = '%s=%s' % (k, v)
     1073        cookie = k + b"=" + v
    10431074        if expires is not None:
    1044             cookie = cookie +"; Expires=%s" % expires
     1075            cookie = cookie + b"; Expires=" + expires
    10451076        if domain is not None:
    1046             cookie = cookie +"; Domain=%s" % domain
     1077            cookie = cookie + b"; Domain=" + domain
    10471078        if path is not None:
    1048             cookie = cookie +"; Path=%s" % path
     1079            cookie = cookie + b"; Path=" + path
    10491080        if max_age is not None:
    1050             cookie = cookie +"; Max-Age=%s" % max_age
     1081            cookie = cookie + b"; Max-Age=" + intToBytes(max_age)
    10511082        if comment is not None:
    1052             cookie = cookie +"; Comment=%s" % comment
     1083            cookie = cookie + b"; Comment=" + comment
    10531084        if secure:
    1054             cookie = cookie +"; Secure"
     1085            cookie = cookie + b"; Secure"
     1086        if httpOnly:
     1087            cookie = cookie + b"; HttpOnly"
    10551088        self.cookies.append(cookie)
    10561089
    10571090    def setResponseCode(self, code, message=None):
  • twisted/web/test/test_http.py

    diff --git a/twisted/web/test/test_http.py b/twisted/web/test/test_http.py
    index 0d6b5c3..12f9bf4 100644
    a b class RequestTests(unittest.TestCase, ResponseTestMixin): 
    15291529        self.assertEqual(req.responseHeaders.getRawHeaders(b"test"), [b"lemur"])
    15301530
    15311531
     1532    def test_addCookieWithMinimumArguments(self):
     1533        """
     1534        Add a Set-Cookie header with just name and value to the response.
     1535        """
     1536        req = http.Request(DummyChannel(), False)
     1537        req.addCookie(b"foo", b"bar")
     1538        self.assertEqual(req.cookies[0], b"foo=bar")
     1539
     1540
     1541    def test_addCookieWithAllArguments(self):
     1542        """
     1543        Add a Set-Cookie header with name and value and all the supported
     1544        options to the response.
     1545        """
     1546        req = http.Request(DummyChannel(), False)
     1547        req.addCookie(
     1548            b"foo", b"bar", expires=b"Fri, 31 Dec 9999 23:59:59 GMT",
     1549            domain=b".example.com", path=b"/", max_age=31536000,
     1550            comment=b"test", secure=True, httpOnly=True)
     1551        self.assertEqual(req.cookies[0],
     1552                         b"foo=bar; Expires=Fri, 31 Dec 9999 23:59:59 GMT; "
     1553                         b"Domain=.example.com; Path=/; Max-Age=31536000; "
     1554                         b"Comment=test; Secure; HttpOnly")
     1555
     1556
    15321557    def test_firstWrite(self):
    15331558        """
    15341559        For an HTTP 1.0 request, L{http.Request.write} sends an HTTP 1.0
  • new file twisted/web/topfiles/5911.feature

    diff --git a/twisted/web/topfiles/5911.feature b/twisted/web/topfiles/5911.feature
    new file mode 100644
    index 0000000..6560891
    - +  
     1twisted.web.http.Request.addCookie now supports httpOnly attribute.