Commits

Anonymous committed 5af2d1e

CherryProxy: moved and renamed private methods with an underscore

  • Participants
  • Parent commits dae3ace

Comments (0)

Files changed (1)

File CherryProxy/cherryproxy/CherryProxy.py

 #                      - added methods to send response without forwarding
 #                        request to server
 # 2011-09-30 v0.11 PL: - added methods to filter headers before reading body
+# 2011-11-15 v0.12 PL: - moved and renamed private methods with an underscore
 
 
 #------------------------------------------------------------------------------
 
 #--- CONSTANTS ----------------------------------------------------------------
 
-__version__ = '0.11'
+__version__ = '0.12'
 
 SERVER_NAME = 'CherryProxy/%s' % __version__
 
         self.req = threading.local()
         self.resp = threading.local()
         if debug:
-            self.debug = self.debug_enabled
+            self.debug = self._debug_enabled
             self.debug_mode = True
         else:
-            self.debug = self.debug_disabled
+            self.debug = self._debug_disabled
             self.debug_mode = False
         self.options = options
 
         self.log.info('CherryProxy stopped.')
 
 
-    def debug_enabled(self, string):
+    def filter_request_headers(self):
         """
-        debug method when debug mode is enabled
-        """
-        #print string
-        self.log.debug(string)
+        Method to be overridden:
+        Called to analyse/filter/modify the request received from the client,
+        before reading the full request with its body if there is one,
+        before it is sent to the server.
 
-    def debug_disabled(self, string):
-        """
-        debug method when debug mode is disabled (does nothing)
+        This method may call set_response() if the request needs to be blocked
+        before being sent to the server.
         """
         pass
 
 
-    def init_request_response(self):
+    def filter_request(self):
+        """
+        Method to be overridden:
+        Called to analyse/filter/modify the request received from the client,
+        after reading the full request with its body if there is one,
+        before it is sent to the server.
+
+        This method may call set_response() if the request needs to be blocked
+        before being sent to the server.
+        """
+        pass
+
+
+    def filter_response_headers(self):
+        """
+        Method to be overridden:
+        Called to analyse/filter/modify the response received from the server,
+        before reading the full response with its body if there is one,
+        before it is sent back to the client.
+
+        This method may call set_response() if the response needs to be blocked
+        (e.g. replaced by a simple response) before being sent to the client.
+        """
+        pass
+
+
+    def filter_response(self):
+        """
+        Method to be overridden:
+        Called to analyse/filter/modify the response received from the server,
+        after reading the full response with its body if there is one,
+        before it is sent back to the client.
+
+        This method may call set_response() if the response needs to be blocked
+        (e.g. replaced by a simple response) before being sent to the client.
+        """
+        pass
+
+
+    def set_response(self, status, reason=None, data=None, content_type='text/plain'):
+        """
+        set a HTTP response to be sent to the client instead of the one from
+        the server.
+
+        - status: int, HTTP status code (see RFC 2616)
+        - reason: str, optional text for the response line, standard text by default
+        - data: str, optional body for the response, default="status reason"
+        - content_type: str, content-type corresponding to data
+        """
+        self.resp.status = status
+        if reason is None:
+            # get standard text corresponding to status
+            reason = httplib.responses[status]
+        self.resp.reason = reason
+        if data is None:
+            data = "%d %s" % (status, reason)
+        self.resp.data = data
+        # reset all headers
+        self.resp.headers = []
+        self.resp.headers.append(('content-type', content_type))
+        #self.resp.headers.append(('content-length', str(len(data)))) # not required
+
+
+    def set_response_forbidden(self, status=403, reason='Forbidden',
+        data=None, content_type='text/plain'):
+        """
+        set a HTTP 403 Forbidden response to be sent to the client instead of
+        the one from the server.
+
+        - status: int, HTTP status code (see RFC 2616)
+        - reason: str, optional text for the response line, standard text by default
+        - data: str, optional body for the response, default="status reason"
+        - content_type: str, content-type corresponding to data
+        """
+        self.set_response(status, reason=reason, data=data,
+            content_type=content_type)
+
+
+    def _proxy_app(self, environ, start_response):
+        """
+        main method called when a request is received from a client
+        (WSGI application)
+        """
+        self._init_request_response()
+        # parse request headers:
+        self._parse_request(environ)
+        # filter request headers before reading the request body:
+        self.filter_request_headers()
+        # check if the response was set by filter_request_headers, else continue:
+        if self.resp.status is None:
+            # if request has data, read it:
+            self._read_request_body()
+            # filter request before sending it to server:
+            self.filter_request()
+        # check if the response was set by filter_request, else forward to server:
+        if self.resp.status is None:
+            # send request to server:
+            self._send_request()
+            # parse response headers:
+            self._parse_response()
+            # filter response headers before reading the response body:
+            self.filter_response_headers()
+        # check if the response was set by filter_response_headers, else continue:
+        # (here we need to check if resp.data is still None)
+        if self.resp.data is None:
+            # read the response body
+            self._read_response_body()
+            # filter response before sending it to client:
+            self.filter_response()
+        # For now we always close the connection, even if the client sends
+        # several requests in one connection:
+        # (not optimal performance-wise, but simpler to code)
+        if self.resp.httpconn is not None:
+            self.resp.httpconn.close()
+        # send response to client:
+        self._send_response(start_response)
+        # send response body:
+        return [self.resp.data]
+
+
+    def _init_request_response(self):
         """
         Initialize variables when a new request is received
         """
         self.resp.data = None
 
 
-    def parse_request(self, environ):
+    def _parse_request(self, environ):
         """
         parse a request received from a client
         """
         self.req.data = None
 
 
-    def filter_request_headers(self):
-        """
-        Method to be overridden:
-        Called to analyse/filter/modify the request received from the client,
-        before reading the full request with its body if there is one,
-        before it is sent to the server.
-
-        This method may call set_response() if the request needs to be blocked
-        before being sent to the server.
-        """
-        pass
-
-
-    def read_request_body(self):
+    def _read_request_body(self):
         """
         read the request body if available
         """
             self.debug('No request body.')
 
 
-    def filter_request(self):
-        """
-        Method to be overridden:
-        Called to analyse/filter/modify the request received from the client,
-        after reading the full request with its body if there is one,
-        before it is sent to the server.
-
-        This method may call set_response() if the request needs to be blocked
-        before being sent to the server.
-        """
-        pass
-
-
-    def send_request(self):
+    def _send_request(self):
         """
         forward a request received from a client to the server
         Get the response (but not the response body yet)
         self.debug('RESPONSE RECEIVED FROM SERVER: %s' % status)
 
 
-    def parse_response(self):
+    def _parse_response(self):
         """
         parse a request received from a client
         """
 
 
 
-    def filter_response_headers(self):
-        """
-        Method to be overridden:
-        Called to analyse/filter/modify the response received from the server,
-        before reading the full response with its body if there is one,
-        before it is sent back to the client.
-
-        This method may call set_response() if the response needs to be blocked
-        (e.g. replaced by a simple response) before being sent to the client.
-        """
-        pass
-
-
-    def read_response_body(self):
+    def _read_response_body(self):
         """
         read the response body and close the connection
         """
 ##        print repr(self.data)
 
 
-    def filter_response(self):
-        """
-        Method to be overridden:
-        Called to analyse/filter/modify the response received from the server,
-        after reading the full response with its body if there is one,
-        before it is sent back to the client.
-
-        This method may call set_response() if the response needs to be blocked
-        (e.g. replaced by a simple response) before being sent to the client.
-        """
-        pass
-
-
-    def set_response(self, status, reason=None, data=None, content_type='text/plain'):
-        """
-        set a HTTP response to be sent to the client instead of the one from
-        the server.
-
-        - status: int, HTTP status code (see RFC 2616)
-        - reason: str, optional text for the response line, standard text by default
-        - data: str, optional body for the response, default="status reason"
-        - content_type: str, content-type corresponding to data
-        """
-        self.resp.status = status
-        if reason is None:
-            # get standard text corresponding to status
-            reason = httplib.responses[status]
-        self.resp.reason = reason
-        if data is None:
-            data = "%d %s" % (status, reason)
-        self.resp.data = data
-        # reset all headers
-        self.resp.headers = []
-        self.resp.headers.append(('content-type', content_type))
-        #self.resp.headers.append(('content-length', str(len(data)))) # not required
-
-
-    def set_response_forbidden(self, status=403, reason='Forbidden',
-        data=None, content_type='text/plain'):
-        """
-        set a HTTP 403 Forbidden response to be sent to the client instead of
-        the one from the server.
-
-        - status: int, HTTP status code (see RFC 2616)
-        - reason: str, optional text for the response line, standard text by default
-        - data: str, optional body for the response, default="status reason"
-        - content_type: str, content-type corresponding to data
-        """
-        self.set_response(status, reason=reason, data=data,
-            content_type=content_type)
-
-
-    def send_response(self, start_response):
+    def _send_response(self, start_response):
         """
         send the response with headers (but no body yet)
         """
         start_response(status, self.resp.headers)
 
 
-    def proxy_app(self, environ, start_response):
+    def _debug_enabled(self, string):
         """
-        main method called when a request is received from a client
-        (WSGI application)
+        debug method when debug mode is enabled
         """
-        self.init_request_response()
-        # parse request headers:
-        self.parse_request(environ)
-        # filter request headers before reading the request body:
-        self.filter_request_headers()
-        # check if the response was set by filter_request_headers, else continue:
-        if self.resp.status is None:
-            # if request has data, read it:
-            self.read_request_body()
-            # filter request before sending it to server:
-            self.filter_request()
-        # check if the response was set by filter_request, else forward to server:
-        if self.resp.status is None:
-            # send request to server:
-            self.send_request()
-            # parse response headers:
-            self.parse_response()
-            # filter response headers before reading the response body:
-            self.filter_response_headers()
-        # check if the response was set by filter_response_headers, else continue:
-        # (here we need to check if resp.data is still None)
-        if self.resp.data is None:
-            # read the response body
-            self.read_response_body()
-            # filter response before sending it to client:
-            self.filter_response()
-        # For now we always close the connection, even if the client sends
-        # several requests in one connection:
-        # (not optimal performance-wise, but simpler to code)
-        if self.resp.httpconn is not None:
-            self.resp.httpconn.close()
-        # send response to client:
-        self.send_response(start_response)
-        # send response body:
-        return [self.resp.data]
+        #print string
+        self.log.debug(string)
+
+    def _debug_disabled(self, string):
+        """
+        debug method when debug mode is disabled (does nothing)
+        """
+        pass
 
 
 #=== MAIN =====================================================================