Ticket #14770: django_ticket_14770.diff
File django_ticket_14770.diff, 9.8 KB (added by , 14 years ago) |
---|
-
django/http/__init__.py
37 37 class Http404(Exception): 38 38 pass 39 39 40 class HttpRequest(object): 40 41 class HttpBase(object): 42 """ 43 A common base class for HttpRequest and HttpResponse objects. 44 """ 45 46 def set_cookie(self, key, value='', max_age=None, expires=None, path='/', 47 domain=None, secure=False): 48 """ 49 Sets a cookie. 50 51 ``expires`` can be a string in the correct format or a 52 ``datetime.datetime`` object in UTC. If ``expires`` is a datetime 53 object then ``max_age`` will be calculated. 54 """ 55 # needed because WSGIRequest and ModPythonRequest don't call the 56 # __init__ of their superclass 57 if not hasattr(self, 'cookies'): 58 self.cookies = CompatCookie() 59 self.cookies[key] = value 60 if expires is not None: 61 if isinstance(expires, datetime.datetime): 62 delta = expires - expires.utcnow() 63 # Add one second so the date matches exactly (a fraction of 64 # time gets lost between converting to a timedelta and 65 # then the date string). 66 delta = delta + datetime.timedelta(seconds=1) 67 # Just set max_age - the max_age logic will set expires. 68 expires = None 69 max_age = max(0, delta.days * 86400 + delta.seconds) 70 else: 71 self.cookies[key]['expires'] = expires 72 if max_age is not None: 73 self.cookies[key]['max-age'] = max_age 74 # IE requires expires, so set it if hasn't been already. 75 if not expires: 76 self.cookies[key]['expires'] = cookie_date(time.time() + 77 max_age) 78 if path is not None: 79 self.cookies[key]['path'] = path 80 if domain is not None: 81 self.cookies[key]['domain'] = domain 82 if secure: 83 self.cookies[key]['secure'] = True 84 85 def delete_cookie(self, key, path='/', domain=None): 86 self.set_cookie(key, max_age=0, path=path, domain=domain, 87 expires='Thu, 01-Jan-1970 00:00:00 GMT') 88 89 90 class HttpRequest(HttpBase): 41 91 """A basic HTTP request.""" 42 92 43 93 # The encoding used in GET/POST dicts. None means use default setting. … … 49 99 self.path = '' 50 100 self.path_info = '' 51 101 self.method = None 102 self.cookies = CompatCookie() 52 103 53 104 def __repr__(self): 54 105 return '<HttpRequest\nGET:%s,\nPOST:%s,\nCOOKIES:%s,\nMETA:%s>' % \ … … 386 437 class BadHeaderError(ValueError): 387 438 pass 388 439 389 class HttpResponse( object):440 class HttpResponse(HttpBase): 390 441 """A basic HTTP response, with content and dictionary-accessed headers.""" 391 442 392 443 status_code = 200 … … 461 512 def get(self, header, alternate): 462 513 return self._headers.get(header.lower(), (None, alternate))[1] 463 514 464 def set_cookie(self, key, value='', max_age=None, expires=None, path='/',465 domain=None, secure=False):466 """467 Sets a cookie.468 469 ``expires`` can be a string in the correct format or a470 ``datetime.datetime`` object in UTC. If ``expires`` is a datetime471 object then ``max_age`` will be calculated.472 """473 self.cookies[key] = value474 if expires is not None:475 if isinstance(expires, datetime.datetime):476 delta = expires - expires.utcnow()477 # Add one second so the date matches exactly (a fraction of478 # time gets lost between converting to a timedelta and479 # then the date string).480 delta = delta + datetime.timedelta(seconds=1)481 # Just set max_age - the max_age logic will set expires.482 expires = None483 max_age = max(0, delta.days * 86400 + delta.seconds)484 else:485 self.cookies[key]['expires'] = expires486 if max_age is not None:487 self.cookies[key]['max-age'] = max_age488 # IE requires expires, so set it if hasn't been already.489 if not expires:490 self.cookies[key]['expires'] = cookie_date(time.time() +491 max_age)492 if path is not None:493 self.cookies[key]['path'] = path494 if domain is not None:495 self.cookies[key]['domain'] = domain496 if secure:497 self.cookies[key]['secure'] = True498 499 def delete_cookie(self, key, path='/', domain=None):500 self.set_cookie(key, max_age=0, path=path, domain=domain,501 expires='Thu, 01-Jan-1970 00:00:00 GMT')502 503 515 def _get_content(self): 504 516 if self.has_header('Content-Encoding'): 505 517 return ''.join(self._container) … … 604 616 return unicode(s, encoding, 'replace') 605 617 else: 606 618 return s 607 -
django/http/utils.py
82 82 83 83 return response 84 84 85 def set_request_cookies(request, response): 86 """ 87 Copy the cookies that were set via the request object to the 88 response object where they'll be sent to the browser. 89 """ 90 if hasattr(request, 'cookies'): 91 response.cookies.update(request.cookies) 92 93 return response 94 -
django/core/handlers/base.py
16 16 http.conditional_content_removal, 17 17 http.fix_IE_for_attach, 18 18 http.fix_IE_for_vary, 19 http.set_request_cookies, 19 20 ] 20 21 21 22 def __init__(self): -
tests/regressiontests/requests/views.py
1 from django.http import HttpResponse 2 3 4 def testing(request): 5 request.set_cookie('name', 'Inigo Montoya') 6 response = HttpResponse() 7 response.set_cookie('job', 'revenge') 8 return response 9 10 def test_who_wins(request): 11 request.set_cookie('name', 'Bender Bending Rodriquez') 12 response = HttpResponse() 13 response.set_cookie('name', 'Zoidberg') 14 return response 15 16 def delete_cookies(request): 17 request.delete_cookie('name') 18 response = HttpResponse() 19 response.delete_cookie('job') 20 return response -
tests/regressiontests/requests/tests.py
4 4 5 5 from django.core.handlers.modpython import ModPythonRequest 6 6 from django.core.handlers.wsgi import WSGIRequest, LimitedStream 7 from django.core.urlresolvers import reverse 7 8 from django.http import HttpRequest, HttpResponse, parse_cookie 9 from django.test import TestCase 8 10 from django.utils import unittest 9 11 from django.utils.http import cookie_date 10 12 … … 147 149 def test_read_by_lines(self): 148 150 request = WSGIRequest({'REQUEST_METHOD': 'POST', 'wsgi.input': StringIO('name=value')}) 149 151 self.assertEqual(list(request), ['name=value']) 152 153 154 class RequestCookiesTests(TestCase): 155 urls = "regressiontests.requests.urls" 156 157 def test_setting_cookie(self): 158 res = self.client.get(reverse('cookies_testing')) 159 self.assertEqual('Inigo Montoya', self.client.cookies['name'].value) 160 self.assertEqual('revenge', self.client.cookies['job'].value) 161 162 def test_delete_cookies(self): 163 self.test_setting_cookie() 164 self.client.get(reverse('cookies_delete')) 165 self.assertEqual('', self.client.cookies['name'].value) 166 self.assertEqual('', self.client.cookies['job'].value) 167 168 def test_who_wins(self): 169 self.client.get(reverse('cookies_who_wins')) 170 self.assertEqual('Bender Bending Rodriquez', self.client.cookies['name'].value) -
tests/regressiontests/requests/urls.py
1 from django.conf.urls.defaults import * 2 from django.http import HttpResponse 3 4 from views import * 5 6 7 urlpatterns = patterns('', 8 url(r'^testing/$', testing, name="cookies_testing"), 9 url(r'^delete_cookies/$', delete_cookies, name="cookies_delete"), 10 url(r'^test_who_wins/$', test_who_wins, name="cookies_who_wins"), 11 ) 12 No newline at end of file -
docs/ref/request-response.txt
299 299 for element in ET.iterparse(request): 300 300 process(element) 301 301 302 .. method:: HttpRequest.set_cookie(key, value='', max_age=None, expires=None, path='/', domain=None, secure=None) 302 303 304 .. versionadded:: 1.3 305 306 This method behaves identically to :meth:`HttpResponse.set_cookie()`. 307 308 .. note:: Cookies set via ``reqeust.set_cookie()`` will overwrite ones with 309 the same ``key`` which were set in the same request-response cycle via 310 ``response.set_cookie()``. 311 312 .. method:: HttpRequest.delete_cookie(key, path='/', domain=None) 313 314 .. versionadded:: 1.3 315 316 This method behaves identically to :meth:`HttpResponse.delete_cookie()`. 317 318 303 319 QueryDict objects 304 320 ----------------- 305 321