Skip to content

feat: Determine URL protocol based on hostname#900

Closed
vchudnov-g wants to merge 4 commits intomainfrom
vchudnov-host-protocol
Closed

feat: Determine URL protocol based on hostname#900
vchudnov-g wants to merge 4 commits intomainfrom
vchudnov-host-protocol

Conversation

@vchudnov-g
Copy link
Contributor

This will make connections to localhost use HTTP. This is insecure but useful for testing. In a future change, we will allow setting transport options at client instantiation time.

@vchudnov-g vchudnov-g requested a review from a team as a code owner May 20, 2021 01:37
@google-cla google-cla bot added the cla: yes This human has signed the Contributor License Agreement. label May 20, 2021
@vchudnov-g vchudnov-g requested a review from software-dov May 20, 2021 01:38
@codecov
Copy link

codecov bot commented May 20, 2021

Codecov Report

Merging #900 (f6e383f) into master (7c185e8) will not change coverage.
The diff coverage is 100.00%.

❗ Current head f6e383f differs from pull request most recent head 0141d94. Consider uploading reports for the commit 0141d94 to get more accurate results
Impacted file tree graph

@@            Coverage Diff            @@
##            master      #900   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           26        27    +1     
  Lines         1608      1697   +89     
  Branches       328       347   +19     
=========================================
+ Hits          1608      1697   +89     
Impacted Files Coverage Δ
gapic/samplegen_utils/types.py 100.00% <ø> (ø)
gapic/samplegen_utils/utils.py 100.00% <ø> (ø)
gapic/utils/case.py 100.00% <ø> (ø)
gapic/utils/reserved_names.py 100.00% <ø> (ø)
gapic/generator/generator.py 100.00% <100.00%> (ø)
gapic/samplegen/samplegen.py 100.00% <100.00%> (ø)
gapic/schema/api.py 100.00% <100.00%> (ø)
gapic/schema/metadata.py 100.00% <100.00%> (ø)
gapic/schema/wrappers.py 100.00% <100.00%> (ø)
gapic/utils/__init__.py 100.00% <100.00%> (ø)
... and 3 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f14ceba...0141d94. Read the comment docs.

This will make connections to `localhost` use HTTP. This is insecure
but useful for testing. In a future change, we will allow setting
transport options at client instantiation time.
@vchudnov-g vchudnov-g force-pushed the vchudnov-host-protocol branch from 11308f8 to 7dc5ce3 Compare May 20, 2021 01:42
@vchudnov-g vchudnov-g changed the title feat: Determine URL protocol based on hostname. feat: Determine URL protocol based on hostname May 20, 2021
Copy link
Contributor

@busunkim96 busunkim96 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a unit test that exercises protocol_for?

Tracking issue:
https://github.com/googleapis/gapic-generator-python/issues/901
#}
def protocol_for(host: str) -> str:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Since It looks like this function shouldn't be called by end users)

Suggested change
def protocol_for(host: str) -> str:
def _protocol_for(host: str) -> str:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment on lines +255 to +259
LOCALHOST='localhost'
LOCALHOST_LEN=len(LOCALHOST)

if host.startswith(LOCALHOST) and (len(host)==LOCALHOST_LEN or host[LOCALHOST_LEN]==":"):
return 'http'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a slight preference for regex match here instead (especially since the docstring already seems to describe this in terms of regex :)) https://docs.python.org/3/library/re.html

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. It's more compact. The performance impact shouldn't be too bad, especially as I memoize the regex compilation.

- mark it private with leading underscore
- use regex for host matching
Copy link
Contributor Author

@vchudnov-g vchudnov-g left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made the code changes. Still need to implement the unit test. (Need to figure out how to run the tests locally first!)

Tracking issue:
https://github.com/googleapis/gapic-generator-python/issues/901
#}
def protocol_for(host: str) -> str:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment on lines +255 to +259
LOCALHOST='localhost'
LOCALHOST_LEN=len(LOCALHOST)

if host.startswith(LOCALHOST) and (len(host)==LOCALHOST_LEN or host[LOCALHOST_LEN]==":"):
return 'http'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. It's more compact. The performance impact shouldn't be too bad, especially as I memoize the regex compilation.

"""Determine the URL protocol to use for connecting to the given host.

This function returns 'http' for hosts that match
`^localhost(:.*)?$` and `https` for all others. This is intended
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have anything like this in the other languages? Overall this looks suspicious I would prefer to not have something like this. Determining protocol from host name does not seem reliable and also we are modifying production code for testing purposes. Also what if the host is specified as an IP (land specifically 127.0.0.1)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. This was discussed in an internal doc; I'll ping you the link. We're specifically looking for localhost because yes, this is a temporary special case to allow testing (see #901 for the long-term solution)

str: The protocol (`http` or `https`) to use for connecting to `host`.
"""
if not hasattr(_protocol_for, 'regex'):
_protocol_for.regex = re.compile("^localhost(:.*)?$")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this regex be executed for every call made? If yes, it will have some performance implications (not big probably, but here we sacrifice production code to support rare testing case, which is suboptimal).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The compilation of the regex will only happen once because I have it memoized via this attribute.

The matching will happen for every call for now, though we could refactor things so the protocol is only computed once per client and stored in an attribute. However, given that the performance hit is small, I'm inclined to think it's preferable to keep things simple.

@parthea
Copy link
Contributor

parthea commented Apr 9, 2022

I'm going to close this PR due to it being stale but please feel free to re-open and update it.

@parthea parthea closed this Apr 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla: yes This human has signed the Contributor License Agreement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants