From 235611ec2ff733600bcf400e2f790ed4cedd2de6 Mon Sep 17 00:00:00 2001 From: IceArmy Date: Sat, 3 Dec 2011 23:04:08 -0800 Subject: [PATCH] Do a little cleanup Improved CaseInsensitiveDict a little. Added some more messages for the method patch. Added a break in a for loop --- python/pyphantomjs/utils.py | 20 +++++++++++++------- python/pyphantomjs/webserver.py | 9 +++++++-- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/python/pyphantomjs/utils.py b/python/pyphantomjs/utils.py index 86ec2b74..792a6e30 100644 --- a/python/pyphantomjs/utils.py +++ b/python/pyphantomjs/utils.py @@ -50,7 +50,7 @@ def debug(debug_type): class CaseInsensitiveDict(dict): def __delitem__(self, key): for dictKey in self: - if self.lowerKey(key) == self.lowerKey(dictKey): + if self.sameKey(key, dictKey): super(CaseInsensitiveDict, self).__delitem__(dictKey) return @@ -58,28 +58,34 @@ class CaseInsensitiveDict(dict): def __contains__(self, key): for dictKey in self: - if self.lowerKey(key) == self.lowerKey(dictKey): + if self.sameKey(key, dictKey): return True return False def __getitem__(self, key): for dictKey, dictValue in self.items(): - if self.lowerKey(key) == self.lowerKey(dictKey): + if self.sameKey(key, dictKey): return dictValue raise KeyError(key) def __setitem__(self, key, value): for dictKey in self: - if self.lowerKey(key) == self.lowerKey(dictKey): + if self.sameKey(key, dictKey): super(CaseInsensitiveDict, self).__setitem__(dictKey, value) return + super(CaseInsensitiveDict, self).__setitem__(key, value) - def lowerKey(self, key): + def sameKey(self, key, dictKey): if hasattr(key, 'lower'): - return key.lower() - return key + key = key.lower() + if hasattr(dictKey, 'lower'): + dictKey = dictKey.lower() + + if key == dictKey: + return True + return False class MessageHandler(object): diff --git a/python/pyphantomjs/webserver.py b/python/pyphantomjs/webserver.py index 778cd7e4..19eaf3dc 100644 --- a/python/pyphantomjs/webserver.py +++ b/python/pyphantomjs/webserver.py @@ -101,9 +101,12 @@ class WebServerHandler(BaseHTTPRequestHandler): method error would be returned. Since we patched that out as well, the user can actually handle when a request is or isn't valid through the code. - * Headers are automatically handled; they are sent, - and end, after the request handling. * Status code is sent after the request handling. + * Headers are automatically handled; they get sent, + then the content-length gets sent if using HTTP/1.1, + and then they end. + * We automatically write the body after the end of the + headers (otherwise the entire response gets messed up) ''' try: self.raw_requestline = self.rfile.readline(65537) @@ -152,6 +155,7 @@ class WebServerHandler(BaseHTTPRequestHandler): response = WebServerResponse(self) for server in servers: + # verify which server this request is for if self.server == server.httpd: connectionType = Qt.BlockingQueuedConnection if QThread.currentThread() == server.thread(): @@ -160,6 +164,7 @@ class WebServerHandler(BaseHTTPRequestHandler): QMetaObject.invokeMethod(server, 'newRequest', connectionType, Q_ARG(WebServerRequest, request), Q_ARG(WebServerResponse, response)) + break def log_message(self, format, *args): qDebug("%s - - %s" % (self.address_string(),