This repository has been archived by the owner on Jan 13, 2021. It is now read-only.
/
util.py
69 lines (54 loc) · 1.5 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# -*- coding: utf-8 -*-
"""
hyper/common/util
~~~~~~~~~~~~~~~~~
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
def to_bytestring(element):
"""
Converts a single string to a bytestring, encoding via UTF-8 if needed.
"""
if isinstance(element, unicode):
return element.encode('utf-8')
elif isinstance(element, bytes):
return element
else:
raise ValueError("Non string type.")
def to_bytestring_tuple(*x):
"""
Converts the given strings to a bytestring if necessary, returning a
tuple. Uses ``to_bytestring``.
"""
return tuple(imap(to_bytestring, x))
def to_host_port_tuple(host_port_str, default_port=80):
"""
Converts the given string containing a host and possibly a port
to a tuple.
"""
uri = URIReference(
scheme=None,
authority=host_port_str,
path=None,
query=None,
fragment=None
)
host = uri.host.strip('[]')
if not uri.port:
port = default_port
else:
port = int(uri.port)
return (host, port)
def to_native_string(string, encoding='utf-8'):
if isinstance(string, str):
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"