Package proxy :: Module responseContext
[hide private]
[frames] | no frames]

Source Code for Module proxy.responseContext

  1  # 
  2  # Copyright (c) 2008--2017 Red Hat, Inc. 
  3  # 
  4  # This software is licensed to you under the GNU General Public License, 
  5  # version 2 (GPLv2). There is NO WARRANTY for this software, express or 
  6  # implied, including the implied warranties of MERCHANTABILITY or FITNESS 
  7  # FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 
  8  # along with this software; if not, see 
  9  # http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. 
 10  # 
 11  # Red Hat trademarks are not licensed under GPLv2. No permission is 
 12  # granted to use or replicate Red Hat trademarks that are incorporated 
 13  # in this software or its documentation. 
 14  # 
 15  # 
 16  # This module provides a response context for use by the proxy broker 
 17  # and redirect components.  This context provides a stackable set of 
 18  # response, header, and connection sets which can be used to easily maintain 
 19  # the proxy's response state in the event of redirects. 
 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 # Constructors and Destructors ############################################ 34
35 - def __init__(self):
36 self._contextStack = [] 37 self.add()
38 39 # Public Interface ######################################################## 40
41 - def getHeaders(self):
42 """ Get the current response headers. """ 43 return self._getCurrentContext()[CXT_RESP_HEADERS]
44
45 - def setHeaders(self, responseHeaders):
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
65 - def add(self):
66 """ Add a new context to the stack. The new context becomes the current 67 one. 68 """ 69 self._contextStack.append(self._createContext())
70
71 - def remove(self):
72 """ Remove the current context. """ 73 if not self._isEmpty(): 74 self.close() 75 self._contextStack.pop()
76
77 - def close(self):
78 """ Close the current context. """ 79 context = self._getCurrentContext() 80 self._closeContext(context)
81
82 - def clear(self):
83 """ Close and remove all contexts. """ 84 while self._contextStack: 85 self.remove()
86
87 - def __str__(self):
88 """ String representation. """ 89 return str(self._contextStack)
90 91 # Helper Methods ########################################################## 92
93 - def _isEmpty(self):
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
104 - def _getCurrentContext(self):
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 # Test Routine 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