]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Improve custom reason code support.
authordano <oreilldf@gmail.com>
Sun, 11 May 2014 02:08:18 +0000 (22:08 -0400)
committerdano <oreilldf@gmail.com>
Sun, 11 May 2014 02:08:18 +0000 (22:08 -0400)
Adds support for custom reason messages to curl_httpclient.
Make custom reason code HTTPError.message if one is available.

tornado/curl_httpclient.py
tornado/httpclient.py

index 0df7a7eea9bfc552ede11c77330270b3995de60b..3fc171593bb28f831acc787d4255d083403a429f 100644 (file)
@@ -23,6 +23,7 @@ import logging
 import pycurl
 import threading
 import time
+import re
 
 from tornado import httputil
 from tornado import ioloop
@@ -268,6 +269,7 @@ class CurlAsyncHTTPClient(AsyncHTTPClient):
             info["callback"](HTTPResponse(
                 request=info["request"], code=code, headers=info["headers"],
                 buffer=buffer, effective_url=effective_url, error=error,
+                reason=info['headers'].get("reason", None),
                 request_time=time.time() - info["curl_start_time"],
                 time_info=time_info))
         except Exception:
@@ -470,7 +472,9 @@ def _curl_header_callback(headers, header_line):
     header_line = header_line.strip()
     if header_line.startswith("HTTP/"):
         headers.clear()
-        return
+        m = re.search("HTTP\/\S*\s*\d+\s*(.*?)\s*$", header_line)
+        if m:
+            header_line = "Reason: %s" % m.group(1)
     if not header_line:
         return
     headers.parse_line(header_line)
index 34716d53d8570c923dd3c625a3a46c1ac1c6a764..2a7965b40f6c520ddeab252c8e3769362f65fd32 100644 (file)
@@ -502,7 +502,8 @@ class HTTPResponse(object):
             self.effective_url = effective_url
         if error is None:
             if self.code < 200 or self.code >= 300:
-                self.error = HTTPError(self.code, response=self)
+                self.error = HTTPError(self.code, message=self.reason, 
+                                       response=self)
             else:
                 self.error = None
         else: