1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 CXT_RESP_HEADERS = 'headers'
22 CXT_RESP_BODYFD = 'bodyFd'
23 CXT_CONNECTION = 'connection'
24
25
26 -class ResponseContext:
27
28 """ This class provides a response context for use by the proxy broker
29 and redirect components. This context provides a stackable set of
30 response, header, and connection sets which can be used to easily maintain
31 the proxy's response state in the event of redirects. """
32
33
34
36 self._contextStack = []
37 self.add()
38
39
40
42 """ Get the current response headers. """
43 return self._getCurrentContext()[CXT_RESP_HEADERS]
44
46 """ Set the current response headers. """
47 self._getCurrentContext()[CXT_RESP_HEADERS] = responseHeaders
48
49 - def getBodyFd(self):
50 """ Get the current response body file descriptor. """
51 return self._getCurrentContext()[CXT_RESP_BODYFD]
52
53 - def setBodyFd(self, responseBodyFd):
54 """ Set the current response body file descriptor. """
55 self._getCurrentContext()[CXT_RESP_BODYFD] = responseBodyFd
56
57 - def getConnection(self):
58 """ Get the current connection object. """
59 return self._getCurrentContext()[CXT_CONNECTION]
60
61 - def setConnection(self, connection):
62 """ Set the current connection object. """
63 self._getCurrentContext()[CXT_CONNECTION] = connection
64
66 """ Add a new context to the stack. The new context becomes the current
67 one.
68 """
69 self._contextStack.append(self._createContext())
70
72 """ Remove the current context. """
73 if not self._isEmpty():
74 self.close()
75 self._contextStack.pop()
76
78 """ Close the current context. """
79 context = self._getCurrentContext()
80 self._closeContext(context)
81
83 """ Close and remove all contexts. """
84 while self._contextStack:
85 self.remove()
86
88 """ String representation. """
89 return str(self._contextStack)
90
91
92
94 return len(self._contextStack) <= 0
95
96 @staticmethod
97 - def _closeContext(context):
98 if context:
99 if context[CXT_RESP_BODYFD]:
100 context[CXT_RESP_BODYFD].close()
101 if context[CXT_CONNECTION]:
102 context[CXT_CONNECTION].close()
103
105 return self._contextStack[-1]
106
107 @staticmethod
108 - def _createContext(responseHeaders=None,
109 responseBodyFd=None,
110 connection=None):
111 return {CXT_RESP_HEADERS: responseHeaders,
112 CXT_RESP_BODYFD: responseBodyFd,
113 CXT_CONNECTION: connection}
114
115
116
117
118
119
120 if __name__ == "__main__":
121 respContext = ResponseContext()
122 print "init | context = " + str(respContext)
123
124 respContext.remove()
125 print "remove | context = " + str(respContext)
126
127 respContext.add()
128 print "add | context = " + str(respContext)
129
130 respContext.remove()
131 print "remove | context = " + str(respContext)
132
133 respContext.add()
134 respContext.add()
135 print "addadd | context = " + str(respContext)
136
137 respContext.clear()
138 print "clear | context = " + str(respContext)
139
140 respContext.add()
141 print "add | context = " + str(respContext)
142