Browse Source

[core] Python 2.4 treats HTTP 201 as error

* Python 2.4 urllib2 has a bug. Adding our own processor to treat all 200s
  codes as success.
* Fixed 2.4 compat issue: urllib2.HTTPError doesn't have getcode(). Just code.
bc Wong 13 years ago
parent
commit
14871715ed
1 changed files with 16 additions and 2 deletions
  1. 16 2
      desktop/core/src/desktop/lib/rest/http_client.py

+ 16 - 2
desktop/core/src/desktop/lib/rest/http_client.py

@@ -36,7 +36,7 @@ class RestException(Exception):
     self._message = str(error)
     # See if there is a code or a message. (For urllib2.HTTPError.)
     try:
-      self._code = error.getcode()
+      self._code = error.code
       self._message = error.read()
     except AttributeError:
       pass
@@ -85,7 +85,9 @@ class HttpClient(object):
     cookiejar = cookielib.CookieJar()
 
     self._opener = urllib2.build_opener(
-      urllib2.HTTPCookieProcessor(cookiejar), authhandler)
+        HTTPErrorProcessor(),
+        urllib2.HTTPCookieProcessor(cookiejar),
+        authhandler)
 
 
   def set_basic_auth(self, username, password, realm):
@@ -159,6 +161,18 @@ class HttpClient(object):
     return iri_to_uri(res)
 
 
+class HTTPErrorProcessor(urllib2.HTTPErrorProcessor):
+  """
+  Python 2.4 only recognize 200 and 206 as success. It's broken. So we install
+  the following processor to catch the bug.
+  """
+  def http_response(self, request, response):
+    if 200 <= response.code < 300:
+      return response
+    return urllib2.HTTPErrorProcessor.http_response(self, request, response)
+
+  https_response = http_response
+
 #
 # Method copied from Django
 #