Skip to content
This repository was archived by the owner on Jan 13, 2021. It is now read-only.

Commit a3393c5

Browse files
committed
Initial hack for HTTPie support.
1 parent 05cfc21 commit a3393c5

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

hyper/contrib.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ def build_response(self, request, resp):
7272
Builds a Requests' response object. This emulates most of the logic of
7373
the standard fuction but deals with the lack of the ``.headers``
7474
property on the HTTP20Response object.
75+
76+
Additionally, this function builds in a number of features that are
77+
purely for HTTPie. This is to allow maximum compatibility with what
78+
urllib3 does, so that HTTPie doesn't fall over when it uses us.
7579
"""
7680
response = Response()
7781

@@ -87,8 +91,45 @@ def build_response(self, request, resp):
8791
response.request = request
8892
response.connection = self
8993

90-
# One last horrible patch: Requests expects its raw responses to have a
94+
# First horrible patch: Requests expects its raw responses to have a
9195
# release_conn method, which I don't. We should monkeypatch a no-op on.
9296
resp.release_conn = lambda: None
9397

98+
# Next, add the things HTTPie needs. It needs the following things:
99+
#
100+
# - The `raw` object has a property called `_original_response` that is
101+
# a `httplib` response object.
102+
# - `raw._original_response` has three simple properties: `version`,
103+
# `status`, `reason`.
104+
# - `raw._original_response.version` has one of three values: `9`,
105+
# `10`, `11`.
106+
# - `raw._original_response.msg` exists.
107+
# - `raw._original_response.msg._headers` exists and is an iterable of
108+
# two-tuples.
109+
#
110+
# We fake this out. Most of this exists on our response object already,
111+
# and the rest can be faked.
112+
class FakeOriginalResponse(object):
113+
def __init__(self, headers):
114+
self._headers = headers
115+
116+
def get_all(self, name, default=None):
117+
values = []
118+
119+
for n, v in self._headers:
120+
if n == name.lower():
121+
values.append(v)
122+
123+
if not values:
124+
return default
125+
126+
return values
127+
128+
129+
response.raw._original_response = orig = FakeOriginalResponse(resp.getheaders())
130+
orig.version = 20
131+
orig.status = resp.status
132+
orig.reason = resp.reason
133+
orig.msg = FakeOriginalResponse(resp.getheaders())
134+
94135
return response

0 commit comments

Comments
 (0)