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

Commit

Permalink
adding HTTP version flags to connections and responses
Browse files Browse the repository at this point in the history
  • Loading branch information
nateprewitt committed Dec 1, 2016
1 parent cc033e1 commit 27c725d
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 4 deletions.
10 changes: 10 additions & 0 deletions hyper/common/util.py
Expand Up @@ -5,6 +5,8 @@
General utility functions for use with hyper.
"""
from enum import Enum

from hyper.compat import unicode, bytes, imap
from ..packages.rfc3986.uri import URIReference
from ..compat import is_py3
Expand Down Expand Up @@ -57,3 +59,11 @@ def to_native_string(string, encoding='utf-8'):
return string

return string.decode(encoding) if is_py3 else string.encode(encoding)


class HTTPVersion(Enum):
"""
Collection of all HTTP versions used in hyper.
"""
http11 = "HTTP/1.1"
http20 = "HTTP/2"
5 changes: 4 additions & 1 deletion hyper/http11/connection.py
Expand Up @@ -20,7 +20,7 @@
from ..common.bufsocket import BufferedSocket
from ..common.exceptions import TLSUpgrade, HTTPUpgrade
from ..common.headers import HTTPHeaderMap
from ..common.util import to_bytestring, to_host_port_tuple
from ..common.util import to_bytestring, to_host_port_tuple, HTTPVersion
from ..compat import bytes

# We prefer pycohttpparser to the pure-Python interpretation
Expand Down Expand Up @@ -63,6 +63,9 @@ def __init__(self, host, port=None, secure=None, ssl_context=None,
else:
self.host, self.port = host, port

# Version of the HTTP connection
self.version = HTTPVersion.http11

# Record whether we plan to secure the request. In future this should
# be extended to a security profile, but a bool will do for now.
# TODO: Actually do something with this!
Expand Down
4 changes: 4 additions & 0 deletions hyper/http11/response.py
Expand Up @@ -13,6 +13,7 @@
from ..common.decoder import DeflateDecoder
from ..common.exceptions import ChunkedDecodeError, InvalidResponseError
from ..common.exceptions import ConnectionResetError
from ..common.util import HTTPVersion

log = logging.getLogger(__name__)

Expand All @@ -34,6 +35,9 @@ def __init__(self, code, reason, headers, sock, connection=None):
#: once, and never assigned again.
self.headers = headers

#: HTTP Version of the response
self.version = HTTPVersion.http11

#: The response trailers. These are always intially ``None``.
self.trailers = None

Expand Down
7 changes: 6 additions & 1 deletion hyper/http20/connection.py
Expand Up @@ -14,7 +14,9 @@
from ..common.exceptions import ConnectionResetError
from ..common.bufsocket import BufferedSocket
from ..common.headers import HTTPHeaderMap
from ..common.util import to_host_port_tuple, to_native_string, to_bytestring
from ..common.util import (
to_host_port_tuple, to_native_string, to_bytestring, HTTPVersion
)
from ..compat import unicode, bytes
from .stream import Stream
from .response import HTTP20Response, HTTP20Push
Expand Down Expand Up @@ -102,6 +104,9 @@ def __init__(self, host, port=None, secure=None, window_manager=None,
else:
self.host, self.port = host, port

# HTTP Version of the connection
self.version = HTTPVersion.http20

if secure is not None:
self.secure = secure
elif self.port == 443:
Expand Down
4 changes: 4 additions & 0 deletions hyper/http20/response.py
Expand Up @@ -11,6 +11,7 @@

from ..common.decoder import DeflateDecoder
from ..common.headers import HTTPHeaderMap
from ..common.util import HTTPVersion

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -51,6 +52,9 @@ def __init__(self, headers, stream):
#: once, and never assigned again.
self.headers = headers

#: HTTP Version of the response
self.version = HTTPVersion.http20

# The response trailers. These are always intially ``None``.
self._trailers = None

Expand Down
3 changes: 2 additions & 1 deletion setup.py
Expand Up @@ -96,6 +96,7 @@ def run_tests(self):
# module at lower than 1.0, because it doesn't support CFFI v1.0 yet.
':platform_python_implementation == "PyPy" and python_full_version < "2.7.9"': [
'cryptography<1.0'
]
],
':python_version == "2.7" or python_version == "3.3"': ['enum34>=1.0.4, <2']
}
)
13 changes: 13 additions & 0 deletions test/test_http11.py
Expand Up @@ -19,6 +19,7 @@
from hyper.http11.response import HTTP11Response
from hyper.common.headers import HTTPHeaderMap
from hyper.common.exceptions import ChunkedDecodeError, ConnectionResetError
from hyper.common.util import HTTPVersion
from hyper.compat import bytes, zlib_compressobj


Expand Down Expand Up @@ -838,6 +839,18 @@ def test_closing_chunked_reads_dont_call_close_callback(self):
assert r._sock is None
assert connection.close.call_count == 1

def test_connection_version(self):
c = HTTP11Connection('httpbin.org')
assert c.version is HTTPVersion.http11

def test_response_version(self):
d = DummySocket()
headers = {
b'transfer-encoding': [b'chunked'], b'connection': [b'close']
}
r = HTTP11Response(200, 'OK', headers, d)
assert r.version is HTTPVersion.http11


class DummySocket(object):
def __init__(self):
Expand Down
10 changes: 9 additions & 1 deletion test/test_hyper.py
Expand Up @@ -16,7 +16,7 @@
combine_repeated_headers, split_repeated_headers, h2_safe_headers
)
from hyper.common.headers import HTTPHeaderMap
from hyper.common.util import to_bytestring
from hyper.common.util import to_bytestring, HTTPVersion
from hyper.compat import zlib_compressobj, is_py2
from hyper.contrib import HTTP20Adapter
import hyper.http20.errors as errors
Expand Down Expand Up @@ -82,6 +82,10 @@ def test_connections_can_parse_ipv6_hosts_and_ports(self):
assert c.proxy_host == 'ffff:aaaa::1'
assert c.proxy_port == 8443

def test_connection_version(self):
c = HTTP20Connection('www.google.com')
assert c.version is HTTPVersion.http20

def test_ping(self, frame_buffer):
def data_callback(chunk, **kwargs):
frame_buffer.add_data(chunk)
Expand Down Expand Up @@ -1097,6 +1101,10 @@ def test_read_compressed_frames(self):

assert received == b'this is test data'

def test_response_version(self):
r = HTTP20Response(HTTPHeaderMap([(':status', '200')]), None)
assert r.version is HTTPVersion.http20


class TestHTTP20Adapter(object):
def test_adapter_reuses_connections(self):
Expand Down

0 comments on commit 27c725d

Please sign in to comment.