Skip to content

Commit

Permalink
Merge pull request #24 from pfeairheller/feat-port-optional
Browse files Browse the repository at this point in the history
Add parameter to http.clienting.Client (and Requester) named portOptional.
  • Loading branch information
SmithSamuelM authored Nov 27, 2022
2 parents 0ac5ddd + f1bbf86 commit 61bc8a2
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/hio/core/http/clienting.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ def __init__(self,
headers=None,
body=b'',
data=None,
fargs=None):
fargs=None,
portOptional=False):
"""
Initialize Instance
Expand All @@ -64,6 +65,8 @@ def __init__(self,
body = http request body
data = dict to jsonify as body if provided
fargs = dict to url form encode as body if provided
portOptional = True indicates to leave off port 80 for http or 443 for https in Host header to support
non-compliant server implementations.
"""
self.hostname, self.port = httping.normalizeHostPort(hostname, port, 80)
self.scheme = scheme
Expand All @@ -78,6 +81,7 @@ def __init__(self,
self.body = body or b''
self.data = data
self.fargs = fargs
self.portOptional = True if portOptional else False

self.lines = [] # keep around for testing
self.head = b"" # keep around for testing
Expand Down Expand Up @@ -212,7 +216,10 @@ def build(self):
if host.find(u':') >= 0:
host = u'[' + host + u']'

value = "{0}:{1}".format(host, port)
if self.portOptional and (self.scheme, port) in (("http", 80), ("https", 443)):
value = host
else:
value = "{0}:{1}".format(host, port)

try:
value = value.encode("ascii")
Expand Down Expand Up @@ -660,6 +667,7 @@ def __init__(self,
redirectable=True,
redirects=None,
responses=None,
portOptional=False,
**kwa):
"""
Initialization method for instance.
Expand Down Expand Up @@ -700,6 +708,8 @@ def __init__(self,
each redirect is dict
responses is deque of responses if any processed by respondent
each response is dict
portOptional = True indicates to leave off port 80 for http or
443 for https in Host header to support non-compliant server implementations.
**kwa are passed through to init .connector tcp.Client or tcp.ClientTLS
"""
Expand Down Expand Up @@ -798,7 +808,8 @@ def __init__(self,
fragment=fragment,
body=body,
data=data,
fargs=fargs)
fargs=fargs,
portOptional=portOptional)
else:
requester.reinit(hostname=self.connector.hostname,
port=self.connector.port,
Expand Down
51 changes: 51 additions & 0 deletions tests/core/http/test_clienting.py
Original file line number Diff line number Diff line change
Expand Up @@ -2309,6 +2309,57 @@ def test_query_quoting():
'&oauth_signature=KBD3DdNVZBjyOd0fqQ9X17ack%3D')


def test_client_port_options():
beta = clienting.Client(hostname="127.0.0.1",
port=4321,
scheme='https')
beta.requester.build()
assert beta.requester.lines[1] == b'Host: 127.0.0.1:4321'
beta = clienting.Client(hostname="127.0.0.1",
port=443,
scheme='https')
beta.requester.build()
assert beta.requester.lines[1] == b'Host: 127.0.0.1:443'
beta = clienting.Client(hostname="127.0.0.1",
port=443,
scheme='https',
portOptional=True)
beta.requester.build()
assert beta.requester.lines[1] == b'Host: 127.0.0.1'

beta = clienting.Client(hostname="127.0.0.1",
port=80,
scheme='http')
beta.requester.build()
assert beta.requester.lines[1] == b'Host: 127.0.0.1:80'
beta = clienting.Client(hostname="127.0.0.1",
port=80,
scheme='http',
portOptional=True)
beta.requester.build()
assert beta.requester.lines[1] == b'Host: 127.0.0.1'

beta = clienting.Client(hostname="127.0.0.1",
port=443,
scheme='http',
portOptional=True)
beta.requester.build()
assert beta.requester.lines[1] == b'Host: 127.0.0.1:443'

beta = clienting.Client(hostname="127.0.0.1",
port=5564,
scheme='https',
portOptional=True)
beta.requester.build()
assert beta.requester.lines[1] == b'Host: 127.0.0.1:5564'

beta = clienting.Client(hostname="127.0.0.1",
port=1234,
scheme='http',
portOptional=True)
beta.requester.build()
assert beta.requester.lines[1] == b'Host: 127.0.0.1:1234'


if __name__ == '__main__':
test_query_quoting()

0 comments on commit 61bc8a2

Please sign in to comment.