Skip to content

Commit

Permalink
Simpler http envelope data extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
IlyaSukhanov committed Jun 24, 2021
1 parent f6bd84e commit 8600912
Showing 1 changed file with 11 additions and 19 deletions.
30 changes: 11 additions & 19 deletions httpie/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from typing import Iterable, Optional
from urllib.parse import urlsplit
import urllib3


class HTTPMessage:
Expand Down Expand Up @@ -50,31 +49,24 @@ def iter_body(self, chunk_size=1):
def iter_lines(self, chunk_size):
return ((line, b'\n') for line in self._orig.iter_lines(chunk_size))

def _headers_http_client_http_message(self):
""" Extract header information from http.client.HTTPMessage """
original = self._orig.raw._original_response

version = {
def _headers_extract_version(self):
try:
raw_version = self._orig.raw._original_response.version
except AttributeError:
raw_version = None
return {
9: '0.9',
10: '1.0',
11: '1.1',
20: '2',
}[original.version]

return version, original.status, original.reason, original.msg._headers

def _headers_requests_response(self):
""" Extract header information from Requests.models.Response """
version = '1.1'
return version, self._orig.status_code, self._orig.reason, self._orig.headers.items()
}.get(raw_version, '1.1')

@property
def headers(self):
if isinstance(self._orig.raw, urllib3.HTTPResponse):
header_extract_method = self._headers_http_client_http_message
else:
header_extract_method = self._headers_requests_response
version, status, reason, http_headers = header_extract_method()
version = self._headers_extract_version()
status = self._orig.status_code
reason = self._orig.reason
http_headers = self._orig.headers.items()

status_line = f'HTTP/{version} {status} {reason}'
headers = [status_line]
Expand Down

0 comments on commit 8600912

Please sign in to comment.