Package httxlib :: Module httxcookiejar
[hide private]
[frames] | no frames]

Source Code for Module httxlib.httxcookiejar

  1  #!/usr/bin/env python 
  2  # -*- coding: latin-1; py-indent-offset:4 -*- 
  3  ################################################################################ 
  4  #  
  5  # This file is part of HttxLib 
  6  # 
  7  # HttxLib is an HTTP(s) Python library suited multithreaded/multidomain 
  8  # applications 
  9  # 
 10  # Copyright (C) 2010-2011 Daniel Rodriguez (aka Daniel Rodriksson) 
 11  # Copyright (C) 2011 Sensible Odds Ltd 
 12  # 
 13  # You can learn more and contact the author at: 
 14  # 
 15  #    http://code.google.com/p/httxlib/ 
 16  # 
 17  # HttxLib is free software: you can redistribute it and/or modify 
 18  # it under the terms of the GNU General Public License as published by 
 19  # the Free Software Foundation, either version 3 of the License, or 
 20  # (at your option) any later version. 
 21  # 
 22  # HttxLib is distributed in the hope that it will be useful, 
 23  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 24  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 25  # GNU General Public License for more details. 
 26  # 
 27  # You should have received a copy of the GNU General Public License 
 28  # along with HttxLib. If not, see <http://www.gnu.org/licenses/>. 
 29  # 
 30  ################################################################################ 
 31  ''' 
 32  Module implementing a deepcopyable Cookiejar 
 33  (Cookiejar is already multithread safe) 
 34  ''' 
 35   
 36   
 37  from cookielib import CookieJar 
 38  from copy import deepcopy 
 39   
 40  from httxobject import HttxObject 
 41   
 42   
43 -class HttxCookieJar(HttxObject):
44 ''' 45 An CookieJar holder to enable deepcopy semantics with locking. 46 47 CookieJars already lock access to the internals, but cannot be 48 deepcopied, which prevents separation of cookiejars into different 49 domains as achieved with different HttxOptions 50 51 A light wrapper over CookieJar with a lock for deepcopy and access 52 to the internal variable _cookies is needed to achieve deepcopy 53 and therefore enable separation of domains 54 55 @ivar cookiejar: CookieJar object holding the cookies 56 @type cookiejar: urllib2 CookieJar 57 ''' 58
59 - def __init__(self):
60 ''' 61 Constructor. It delegates construction to the base class 62 L{HttxObject} and initializes the member variables 63 ''' 64 HttxObject.__init__(self) 65 self.cookiejar = CookieJar()
66 67
68 - def __deepcopy__(self, memo):
69 ''' 70 Deepcopy support. 71 72 The lock prevents access from any other part of the library to this 73 CookieJar, enabling a deepcopy of the private variable into the 74 private variable of the clone to enable separation of domains 75 for CookieJar objects 76 77 The existing RLock in the CookieJar objects forbids direct deepcopy 78 79 @param memo: standard __deepcopy__ parameter to avoid circular references 80 @type memo: dict 81 @return: a cloned object 82 @rtype: L{HttxCookieJar} 83 ''' 84 clone = self.__class__() 85 with self.lock: 86 # CookieJar has a threading.RLock, so we may not deepcopy it 87 # and it has no __deepcopy_ implemented 88 clone.cookiejar._cookies = deepcopy(self.cookiejar._cookies, memo) 89 90 return clone
91 92 103 104
105 - def extract_cookies(self, response, request):
106 ''' 107 Extract cookies from the response, using request as a basis to do so 108 109 This is a simple stub for CookieJar extract_cookies 110 111 @param response: the response containing the headers where cookies 112 may be present 113 @type response: urllib2 compatible Response - L{HttxResponse} 114 @param request: the request to be manipulated 115 @type request: urllib2 compatible Request - L{HttxRequest} 116 ''' 117 self.cookiejar.extract_cookies(response, request)
118