Ticket #5911: 5911-4.patch

File 5911-4.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 c9bd313..55f2884 100644
    a b class Request: 
    10221022            else:
    10231023                self.transport.write(data)
    10241024
    1025     def addCookie(self, k, v, expires=None, domain=None, path=None, max_age=None, comment=None, secure=None):
     1025    def addCookie(self, k, v, expires=None, domain=None, path=None,
     1026                  max_age=None, comment=None, secure=None, httpOnly=False):
    10261027        """
    10271028        Set an outgoing HTTP cookie.
    10281029
    10291030        In general, you should consider using sessions instead of cookies, see
    10301031        L{twisted.web.server.Request.getSession} and the
    10311032        L{twisted.web.server.Session} class for details.
     1033
     1034        @param k: cookie name
     1035        @type k: L{bytes}
     1036
     1037        @param v: cookie value
     1038        @type v: L{bytes}
     1039
     1040        @param expires: cookie expire attribute value in
     1041        "Wdy, DD Mon YYYY HH:MM:SS GMT" format
     1042        @type expires: L{bytes}
     1043
     1044        @param domain: cookie domain
     1045        @type domain: L{bytes}
     1046
     1047        @param path: cookie path
     1048        @type path: L{bytes}
     1049
     1050        @param max_age: cookie expiration in seconds from reception
     1051        @type max_age: L{int}
     1052
     1053        @param comment: cookie comment
     1054        @type comment: L{bytes}
     1055
     1056        @param secure: direct browser to send the cookie on encrypted
     1057        connections only
     1058        @type secure: L{bool}
     1059
     1060        @param httpOnly: direct browser not to expose cookies through channels
     1061        other than HTTP (and HTTPS) requests
     1062        @type httpOnly: L{bool}
    10321063        """
    10331064        cookie = '%s=%s' % (k, v)
    10341065        if expires is not None:
    1035             cookie = cookie +"; Expires=%s" % expires
     1066            cookie = cookie + "; Expires=%s" % (expires, )
    10361067        if domain is not None:
    1037             cookie = cookie +"; Domain=%s" % domain
     1068            cookie = cookie + "; Domain=%s" % (domain, )
    10381069        if path is not None:
    1039             cookie = cookie +"; Path=%s" % path
     1070            cookie = cookie + "; Path=%s" % (path, )
    10401071        if max_age is not None:
    1041             cookie = cookie +"; Max-Age=%s" % max_age
     1072            cookie = cookie + "; Max-Age=%s" % (max_age, )
    10421073        if comment is not None:
    1043             cookie = cookie +"; Comment=%s" % comment
     1074            cookie = cookie + "; Comment=%s" % (comment, )
    10441075        if secure:
    1045             cookie = cookie +"; Secure"
     1076            cookie = cookie + "; Secure"
     1077        if httpOnly:
     1078            cookie = cookie + "; HttpOnly"
    10461079        self.cookies.append(cookie)
    10471080
    10481081    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 81ab830..82810d0 100644
    a b class RequestTests(unittest.TestCase, ResponseTestMixin): 
    14631463        self.assertEqual(req.responseHeaders.getRawHeaders(b"test"), [b"lemur"])
    14641464
    14651465
     1466    def test_addCookieWithMinimumArguments(self):
     1467        """
     1468        Add a Set-Cookie header with just name and value to the response.
     1469        """
     1470        req = http.Request(DummyChannel(), False)
     1471        req.addCookie(b"foo", b"bar")
     1472        self.assertEqual(req.cookies[0], b"foo=bar")
     1473
     1474
     1475    def test_addCookieWithAllArguments(self):
     1476        """
     1477        Add a Set-Cookie header with name and value and all the supported
     1478        options to the response.
     1479        """
     1480        req = http.Request(DummyChannel(), False)
     1481        req.addCookie(
     1482            b"foo", b"bar", expires=b"Fri, 31 Dec 9999 23:59:59 GMT",
     1483            domain=b".example.com", path=b"/", max_age=31536000,
     1484            comment=b"test", secure=True, httpOnly=True)
     1485        self.assertEqual(req.cookies[0],
     1486                         b"foo=bar; Expires=Fri, 31 Dec 9999 23:59:59 GMT; "
     1487                         "Domain=.example.com; Path=/; Max-Age=31536000; "
     1488                         "Comment=test; Secure; HttpOnly")
     1489
     1490
    14661491    def test_firstWrite(self):
    14671492        """
    14681493        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.