From 410404f45924b55b5c8a0061629c92a06017aed3 Mon Sep 17 00:00:00 2001 From: lkcl Date: Thu, 15 Jul 2010 21:10:12 +0100 Subject: [PATCH] more whitespace cleanup, remove more unneeded code --- httpd.py | 126 ++++++++++++++++++++++++------------------------------- 1 file changed, 55 insertions(+), 71 deletions(-) diff --git a/httpd.py b/httpd.py index e2dc6fe..bb9be64 100644 --- a/httpd.py +++ b/httpd.py @@ -385,21 +385,10 @@ class Client(Protocol): yield self.server.queue.put((self, msg)) # new connection def rejectConnection(self, reason=''): - '''Method to reject an incoming client.''' - response = Command() - response.id, response.name, response.type = 1, '_error', Message.RPC - response.setArg(dict(level='status', code='NetConnection.Connect.Rejected', - description=reason, details=None)) - self.writeMessage(response.toMessage()) - - def call(self, method, *args): - '''Call a (callback) method on the client.''' - cmd = Command() - cmd.id, cmd.name, cmd.type = self._nextCallId, method, (self.objectEncoding == 0.0 and Message.RPC or Message.RPC3) - cmd.args, cmd.cmdData = args, None - self._nextCallId += 1 - if _debug: print 'Client.call method=', method, 'args=', args, ' msg=', cmd.toMessage() - self.writeMessage(cmd.toMessage()) + '''Method to reject an incoming client. just close. + TODO: report back an HTTP error with "reason" in it. + ''' + self.removeConnection() class Server(object): @@ -500,65 +489,60 @@ class HTTPServer(object): del self.clients[session] continue - # if client.objectEncoding != 0 and client.objectEncoding != 3: - if client.objectEncoding != 0: - yield client.rejectConnection(reason='Unsupported encoding ' + str(client.objectEncoding) + '. Please use NetConnection.defaultObjectEncoding=ObjectEncoding.AMF0') - yield client.connectionClosed() - else: - if _debug: - print "cookies", str(msg.response_cookies) - session = msg.response_cookies['session'].value - client.session = session - name = msg.path + if _debug: + print "cookies", str(msg.response_cookies) + session = msg.response_cookies['session'].value + client.session = session + name = msg.path + 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 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 session in self.clients: + inst = self.clients[session][0] + else: + inst = app() + self.clients[session] = [inst]; inst._clients=self.clients[session] + msg.server = inst # whew! just in time! + try: + methodname = "on%s" % msg.command + if _debug: + print methodname, dir(inst) + method = getattr(inst, methodname, None) + result = method(client, msg) + close_connection = msg.close_connection if _debug: - print "clients", self.clients.keys() - if session in self.clients: - inst = self.clients[session][0] - else: - inst = app() - self.clients[session] = [inst]; inst._clients=self.clients[session] - msg.server = inst # whew! just in time! - try: - methodname = "on%s" % msg.command - if _debug: - print methodname, dir(inst) - method = getattr(inst, methodname, None) - result = method(client, msg) - close_connection = msg.close_connection - if _debug: - print "close connection", close_connection - except: - if _debug: traceback.print_exc() - yield client.rejectConnection(reason='Exception on %s' % methodname) - continue - if result is True or result is None: - if result is None: - msg.wfile.seek(0) - data = msg.wfile.read() - msg.wfile.seek(0) - msg.wfile.truncate() - yield client.writeMessage(data) - if close_connection: + print "close connection", close_connection + except: + if _debug: traceback.print_exc() + yield client.rejectConnection(reason='Exception on %s' % methodname) + continue + if result is True or result is None: + if result is None: + msg.wfile.seek(0) + data = msg.wfile.read() + msg.wfile.seek(0) + msg.wfile.truncate() + yield client.writeMessage(data) + if close_connection: + if _debug: + print 'close_connection requested' + try: + yield client.connectionClosed() + raise ConnectionClosed + except ConnectionClosed: if _debug: - print 'close_connection requested' - try: - yield client.connectionClosed() - raise ConnectionClosed - except ConnectionClosed: - if _debug: - print 'close_connection done' - pass - else: - if _debug: - print "result", result - yield client.rejectConnection(reason='Rejected in onConnect') + print 'close_connection done' + pass + else: + if _debug: + print "result", result + yield client.rejectConnection(reason='Rejected in onConnect') except StopIteration: raise except: if _debug: -- 2.30.2