1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
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
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
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
87
88 clone.cookiejar._cookies = deepcopy(self.cookiejar._cookies, memo)
89
90 return clone
91
92
94 '''
95 Add a cookie header to the request if needed
96
97 This is a simple stub for CookieJar add_cookie_header
98
99 @param request: the request to be manipulated
100 @type request: urllib2 compatible Request - L{HttxRequest}
101 '''
102 self.cookiejar.add_cookie_header(request)
103
104
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