[Inteproxy-commits] r17 - trunk

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Tue Jan 30 16:20:11 CET 2007


Author: bh
Date: 2007-01-30 16:20:10 +0100 (Tue, 30 Jan 2007)
New Revision: 17

Modified:
   trunk/ChangeLog
   trunk/InteProxy.py
Log:
Add a --debug-level option.

* InteProxy.py (InteProxyHTTPRequestHandler.debug_level): New
class variable indicating the debug level.
(InteProxyHTTPRequestHandler.handle_proxy_request)
(InteProxyHTTPRequestHandler.transfer_data): log debug information
(InteProxyHTTPRequestHandler.log_debug): New method to actually
log the debug message if debug_level > 0
(setup_urllib2): New parameter debuglevel which is passed through
to the various http handlers to activate their debug output.
(run_server): New command line option --debug-level to set the
debug level.


Modified: trunk/ChangeLog
===================================================================
--- trunk/ChangeLog	2007-01-26 16:52:45 UTC (rev 16)
+++ trunk/ChangeLog	2007-01-30 15:20:10 UTC (rev 17)
@@ -1,3 +1,18 @@
+2007-01-30  Bernhard Herzog  <bh at intevation.de>
+
+	Add a --debug-level option.
+
+	* InteProxy.py (InteProxyHTTPRequestHandler.debug_level): New
+	class variable indicating the debug level.
+	(InteProxyHTTPRequestHandler.handle_proxy_request)
+	(InteProxyHTTPRequestHandler.transfer_data): log debug information
+	(InteProxyHTTPRequestHandler.log_debug): New method to actually
+	log the debug message if debug_level > 0
+	(setup_urllib2): New parameter debuglevel which is passed through
+	to the various http handlers to activate their debug output.
+	(run_server): New command line option --debug-level to set the
+	debug level.
+
 2007-01-26  Bernhard Herzog  <bh at intevation.de>
 
 	* InteProxy.py (InteProxyHTTPRequestHandler.handle_proxy_request):

Modified: trunk/InteProxy.py
===================================================================
--- trunk/InteProxy.py	2007-01-26 16:52:45 UTC (rev 16)
+++ trunk/InteProxy.py	2007-01-30 15:20:10 UTC (rev 17)
@@ -74,6 +74,10 @@
     # connect to the server can shutdown the server.
     allow_shutdown = False
 
+    # Whether debug output should be logged.  Currently every value > 0
+    # just switches debugging on.
+    debug_level = 0
+
     def do_SHUTDOWN(self):
         """Shutdown the server if self.allow_shutdown is True (default False).
 
@@ -113,12 +117,18 @@
         wrong, so it is removed and a new one is generated pointing to
         the real remote host.
         """
-        netloc, url = self.convert_url(self.path)
+        self.log_debug("Got %s request for %s", method, self.path)
+        for header, value in self.headers.items():
+            self.log_debug("header from client: %s:%r", header, value)
 
         #
         # Create a http request for the real location
         #
-        request = urllib2.Request("https://%s/%s" % (netloc, url))
+        netloc, url = self.convert_url(self.path)
+        remote_url = "https://%s%s" % (netloc, url)
+        self.log_debug("Converted url: %r", remote_url)
+
+        request = urllib2.Request(remote_url)
         for header, value in self.headers.items():
             if header.lower() == "host":
                 # the host header will be set by httplib and it should
@@ -149,8 +159,18 @@
         length = int(self.headers.getheader("Content-Length", "0"))
         if length:
             # FIXME: check whether length bytes were read
-            request.add_data(self.rfile.read(length))
+            data = self.rfile.read(length)
+            self.log_debug("body of client request (%d bytes):\n%r",
+                           length, data)
+        else:
+            self.log_debug("client request has no body")
+            data = None
 
+        if data is not None:
+            request.add_data(data)
+
+        self.log_debug("request sent")
+
         #
         # Retrieve the url described by the request and pass everything
         # through to the client.
@@ -161,14 +181,18 @@
             # a HTTPError is a valid http response object, so we can
             # treat it like a normal response.
             response = err
+        self.log_debug("received response: %s: %r", response.code,
+                       response.msg)
         self.send_response(response.code, response.msg)
         for header, value in response.info().items():
+            self.log_debug("received header: %s:%r", header, value)
             self.send_header(header, value)
         self.end_headers()
 
         transfer_encoding = response.info().get("Transfer-encoding")
         self.transfer_data(response.read, self.wfile.write,
                            chunked = (transfer_encoding == "chunked"))
+        self.log_debug("request finished")
 
     def transfer_data(self, read, write, length=None, chunked=False):
         """Transfer data from one 'file' to another in chunks
@@ -180,6 +204,22 @@
         If the parameter chunked is true, the method uses the 'chunked'
         transfer encoding when writing the data.
         """
+
+        # wrap the read/write functions if debug logging is active so
+        # that the data read from the server and written to the client
+        # is logged.
+        if self.debug_level > 0:
+            orig_read = read
+            orig_write = write
+            def read(length):
+                data = orig_read(length)
+                self.log_debug("from server: %r", data)
+                return data
+            def write(data):
+                self.log_debug("to client: %r", data)
+                orig_write(data)
+
+        # Now transfer the data in blocks of max_chunk_size
         max_chunk_size = 4096
         while 1:
             if length is not None:
@@ -253,7 +293,13 @@
         traceback.print_exception(*(exc_info + (None, sys.stderr)))
         sys.stderr.flush()
 
+    def log_debug(self, template, *args):
+        if self.debug_level > 0:
+            message = "DEBUG: " + template % args
+            message = message.replace("%", "%%")
+            self.log_message(message)
 
+
 # same as the BaseHTTPRequestHandler method, but as a standalone function:
 def log_date_time_string():
     """Return the current time formatted for logging."""
@@ -322,7 +368,7 @@
             user, password = getpassword(title)
         return user, password
 
-def setup_urllib2():
+def setup_urllib2(debuglevel):
     auth_handler = urllib2.HTTPBasicAuthHandler(IntePasswordManager())
     if os.environ.has_key('https_proxy'):
         https_proxy = urlparse.urlsplit(os.getenv('https_proxy'))[1]
@@ -331,11 +377,15 @@
                          % (log_date_time_string(), https_proxy))
         opener = urllib2.build_opener(
             auth_handler,
-            proxyconnection.ConnectHTTPHandler(proxy=https_proxy),
-            proxyconnection.ConnectHTTPSHandler(proxy=https_proxy))
+            proxyconnection.ConnectHTTPHandler(proxy=https_proxy,
+                                               debuglevel=debuglevel),
+            proxyconnection.ConnectHTTPSHandler(proxy=https_proxy,
+                                                debuglevel=debuglevel))
     else:
-        httphandler = urllib2.HTTPHandler()
-        opener = urllib2.build_opener(auth_handler, httphandler)
+        httphandler = urllib2.HTTPHandler(debuglevel=debuglevel)
+        httpshandler = urllib2.HTTPSHandler(debuglevel=debuglevel)
+        opener = urllib2.build_opener(auth_handler, httphandler,
+                                      httpshandler)
     urllib2.install_opener(opener)
 
 
@@ -347,11 +397,13 @@
     parser.add_option("--logfile")
     parser.add_option("--allow-shutdown", action="store_true")
     parser.add_option("--port", type="int")
+    parser.add_option("--debug-level", type="int")
     opts, rest = parser.parse_args()
 
     HandlerClass.allow_shutdown = opts.allow_shutdown
+    HandlerClass.debug_level = opts.debug_level
 
-    setup_urllib2()
+    setup_urllib2(opts.debug_level)
 
     server_address = ('localhost', opts.port)
 



More information about the Inteproxy-commits mailing list