From: lkcl Date: Thu, 15 Jul 2010 19:58:07 +0000 (+0100) Subject: remove debug info on debuglevel > 0 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=ea9344fcc6325519475e8a8c62aaf1fa7a0cf90c;p=multitaskhttpd.git remove debug info on debuglevel > 0 --- diff --git a/ProxyServer.py b/ProxyServer.py index 786e550..327a4d1 100644 --- a/ProxyServer.py +++ b/ProxyServer.py @@ -121,6 +121,7 @@ class ProxyServerRequestHandler(object): """ + debuglevel = 0 server_version = "SimpleHTTP/" + __version__ def on_query(self, client, reqtype, *args): @@ -128,7 +129,9 @@ class ProxyServerRequestHandler(object): self.client = client self.hr = args[0] - print "on_query", reqtype, repr(self.hr.headers), str(self.hr.headers) + if self.debuglevel > 0: + print "on_query", reqtype, repr(self.hr.headers), \ + str(self.hr.headers) session = self.client.session p = self.proxies.get(session, None) if not p: @@ -160,7 +163,8 @@ class ProxyServerRequestHandler(object): try: # send command req = "%s %s %s\n" % (reqtype, self.hr.path, "HTTP/1.1") - print "req", req + if self.debuglevel > 0: + print "req", req yield p.ss.write(req) conntype = self.hr.headers.get('Connection', "") @@ -171,7 +175,8 @@ class ProxyServerRequestHandler(object): # send headers hdrs = str(self.hr.headers) - print "hdrs", hdrs + if self.debuglevel > 0: + print "hdrs", hdrs yield p.ss.write(hdrs) yield p.ss.write('\r\n') @@ -180,11 +185,13 @@ class ProxyServerRequestHandler(object): max_chunk_size = 10*1024*1024 size_remaining = int(self.hr.headers["content-length"]) L = [] - print "size_remaining", size_remaining + if self.debuglevel > 0: + print "size_remaining", size_remaining while size_remaining: chunk_size = min(size_remaining, max_chunk_size) data = self.hr.rfile.read(chunk_size) - print "proxy rfile read", repr(data) + if self.debuglevel > 0: + print "proxy rfile read", repr(data) yield multitask.send(p.sock, data) size_remaining -= len(data) @@ -197,15 +204,17 @@ class ProxyServerRequestHandler(object): try: while 1: line = (yield p.ss.readline()) - print "reading from proxy", repr(line) + if self.debuglevel > 0: + print "reading from proxy", repr(line) res += line if line in ['\n', '\r\n']: break except StopIteration: - if httpd._debug: print "proxy read stopiter" + if self.debuglevel > 0: + print "proxy read stopiter" # TODO: close connection except: - if httpd._debug: + if self.debuglevel > 0: print 'proxy read error', \ (traceback and traceback.print_exc() or None) # TODO: close connection @@ -214,14 +223,16 @@ class ProxyServerRequestHandler(object): # Examine the headers and look for a Connection directive respheaders = mimetools.Message(f, 0) - print "response headers", str(respheaders) + if self.debuglevel > 0: + print "response headers", str(respheaders) remote = self.client.remote rcooks = httpd.process_cookies(respheaders, remote, "Set-Cookie", False) rcooks['session'] = self.hr.response_cookies['session'].value # nooo rcooks['session']['expires'] = \ self.hr.response_cookies['session']['expires'] self.hr.response_cookies = rcooks - print "rcooks", str(rcooks) + if self.debuglevel > 0: + print "rcooks", str(rcooks) # override connection: keep-alive hack #responseline = responseline.split(" ") @@ -248,7 +259,8 @@ class ProxyServerRequestHandler(object): self.hr.close_connection = 0 # write rest of data - print "writing to client body" + if self.debuglevel > 0: + print "writing to client body" yield self.client.writeMessage("\r\n") if respheaders.has_key('content-length'): @@ -257,7 +269,9 @@ class ProxyServerRequestHandler(object): while size_remaining: chunk_size = min(size_remaining, max_chunk_size) data = (yield p.ss.read(chunk_size)) - print "reading from proxy expecting", size_remaining, repr(data) + if self.debuglevel > 0: + print "reading from proxy expecting", \ + size_remaining, repr(data) yield self.client.writeMessage(data) size_remaining -= len(data) else: @@ -267,18 +281,21 @@ class ProxyServerRequestHandler(object): data = (yield p.ss.read(1024)) except httpd.ConnectionClosed: break - print "reading from proxy", repr(data) + if self.debuglevel > 0: + print "reading from proxy", repr(data) if data == '': break yield self.client.writeMessage(data) if not keepalive: #self.hr.close_connection: - print 'proxy wants client to close_connection' + if self.debuglevel > 0: + print 'proxy wants client to close_connection' try: yield self.client.connectionClosed() raise httpd.ConnectionClosed except httpd.ConnectionClosed: - print 'close_connection done' + if self.debuglevel > 0: + print 'close_connection done' pass p.serving = False @@ -288,8 +305,10 @@ class ProxyServerRequestHandler(object): # there's nothing there to talk to. self.client.removeConnection() self.proxies.pop(session) + except: - print traceback.print_exc() + if self.debuglevel > 0: + print traceback.print_exc() raise StopIteration diff --git a/httpd.py b/httpd.py index a2aadb3..a5f1dd7 100644 --- a/httpd.py +++ b/httpd.py @@ -104,7 +104,8 @@ class MultitaskHTTPRequestHandler(BaseHTTPRequestHandler): def process_cookies(headers, remote, cookie_key="Cookie", add_sess=True): ch = headers.getheaders(cookie_key) - print "messageReceived cookieheaders=", '; '.join(ch) + if _debug: + print "messageReceived cookieheaders=", '; '.join(ch) res = [] for c in ch: c = c.split(";") @@ -116,7 +117,8 @@ def process_cookies(headers, remote, cookie_key="Cookie", add_sess=True): has_sess = False response_cookies = SimpleCookie() for c in res: - print "found cookie", repr(c) + if _debug: + print "found cookie", repr(c) name, value = c.split("=") response_cookies[name] = value #response_cookies[name]['path'] = "/" @@ -304,7 +306,8 @@ class Protocol(object): def parseRequests(self): '''Parses complete messages until connection closed. Raises ConnectionLost exception.''' - print "parseRequests start", repr(self) + if _debug: + print "parseRequests start", repr(self) self.hr = MultitaskHTTPRequestHandler(self.stream, self.remote, None) self.hr.close_connection = 1 self.cookies = CookieJar() @@ -314,15 +317,17 @@ class Protocol(object): # prepare reading the request so that when it's handed # over to "standard" HTTPRequestHandler, the data's already # there. - print "parseRequests" + if _debug: + print "parseRequests" try: readok = (yield multitask.readable(self.stream.sock, 5000)) except select.error: print "select error: connection closed" raise ConnectionClosed - print "readok", readok - print + if _debug: + print "readok", readok + print raw_requestline = (yield self.stream.readline()) if _debug: print "parseRequests, line", raw_requestline if not raw_requestline: @@ -349,14 +354,17 @@ class Protocol(object): pos = 0 self.hr.rfile.truncate(0) self.hr.rfile.write(data) - print "parseRequests write after" + if _debug: + print "parseRequests write after" self.hr.rfile.seek(pos) - print "parseRequests seek after" + if _debug: + print "parseRequests seek after" if not self.hr.parse_request(): raise ConnectionClosed - print "parseRequests parse_req after" - print "parseRequest headers", repr(self.hr.headers), str(self.hr.headers) + if _debug: + print "parseRequests parse_req after" + print "parseRequest headers", repr(self.hr.headers), str(self.hr.headers) try: yield self.messageReceived(self.hr) except ConnectionClosed: @@ -379,10 +387,12 @@ class Protocol(object): if data is None: # just in case TCP socket is not closed, close it. try: - print "stream closing" - print self.stream + if _debug: + print "stream closing" + print self.stream yield self.stream.close() - print "stream closed" + if _debug: + print "stream closed" except: pass break @@ -692,17 +702,20 @@ class HTTPServer(object): yield client.rejectConnection(reason='Unsupported encoding ' + str(client.objectEncoding) + '. Please use NetConnection.defaultObjectEncoding=ObjectEncoding.AMF0') yield client.connectionClosed() else: - print "cookies", str(msg.response_cookies) + if _debug: + print "cookies", str(msg.response_cookies) session = msg.response_cookies['session'].value client.session = session name = msg.path - print "serverlistener", name + if _debug: + print "serverlistener", name if '*' not in self.apps and name not in self.apps: yield client.rejectConnection(reason='Application not found: ' + name) else: # create application instance as needed and add in our list if _debug: print 'name=', name, 'name in apps', str(name in self.apps) app = self.apps[name] if name in self.apps else self.apps['*'] # application class - print "clients", self.clients.keys() + if _debug: + print "clients", self.clients.keys() if session in self.clients: inst = self.clients[session][0] else: @@ -711,11 +724,13 @@ class HTTPServer(object): msg.server = inst # whew! just in time! try: methodname = "on%s" % msg.command - print methodname, dir(inst) + if _debug: + print methodname, dir(inst) method = getattr(inst, methodname, None) result = method(client, msg) close_connection = msg.close_connection - print "close connection", close_connection + if _debug: + print "close connection", close_connection except: if _debug: traceback.print_exc() yield client.rejectConnection(reason='Exception on %s' % methodname) @@ -738,7 +753,8 @@ class HTTPServer(object): print 'close_connection done' pass else: - print "result", result + if _debug: + print "result", result yield client.rejectConnection(reason='Rejected in onConnect') except StopIteration: raise except: diff --git a/proxyapp.py b/proxyapp.py index 0e6226c..636d1e8 100644 --- a/proxyapp.py +++ b/proxyapp.py @@ -35,7 +35,7 @@ class ProxyApp(ProxyServerRequestHandler, BaseApp): self.proxies = {} if __name__ == '__main__': - httpd.set_debug(True) + #httpd.set_debug(True) agent = HTTPServer() agent.apps = dict({'*': ProxyApp}) agent.start()